/**
 * A2 regression: Workspace enum crash
 * Bug: EventStore used 'created' and 'merging' which are not valid workspace statuses.
 * Valid: 'active' | 'merged' | 'abandoned' | 'cleaned'
 * Fix: 'created' → 'active'; 'merging' → metadata-only update (no status change).
 */

import { describe, it, expect } from 'bun:test'
import { readFileSync } from 'fs'
import { join } from 'path'

describe('A2: Workspace enum values', () => {
  const event_store_src = readFileSync(
    join(import.meta.dir, '..', '..', 'src', 'events', 'EventStore.ts'),
    'utf-8'
  )

  it('workspace.created projection uses status active, not created', () => {
    const created_block = event_store_src.match(/case 'workspace\.created'[^}]+}/s)
    expect(created_block).not.toBeNull()
    expect(created_block![0]).toContain("status: 'active'")
    expect(created_block![0]).not.toContain("status: 'created'")
  })

  it('workspace.merge.started does not set invalid merging status', () => {
    const merge_block = event_store_src.match(/case 'workspace\.merge\.started'[^}]+}/s)
    expect(merge_block).not.toBeNull()
    expect(merge_block![0]).not.toContain("status: 'merging'")
  })

  it('WorkspaceManager state type only allows valid values', () => {
    const ws_src = readFileSync(
      join(import.meta.dir, '..', '..', 'src', 'scheduler', 'WorkspaceManager.ts'),
      'utf-8'
    )
    const state_match = ws_src.match(/state:\s*'([^']+)'/g)
    if (state_match) {
      const valid = ['active', 'merged', 'abandoned', 'cleaned']
      for (const m of state_match) {
        const val = m.match(/'([^']+)'/)?.[1]
        if (val) expect(valid).toContain(val)
      }
    }
  })
})
