import {API_ERROR_CODES} from '@crumbs/core/schemas'
import {expect, test} from 'bun:test'

// the vocabulary's completeness claim, enforced: every HTTPException message
// thrown anywhere in the api must be a declared code, so the client's
// friendly-copy map can key off the full set. unexpected 500s are plain
// Errors (onError answers internal_error), never prose HTTPException messages

// dev-only fault injection — translated client-side, never user-facing
const EXCLUDED = new Set(['network_down'])

test('every thrown error code is in API_ERROR_CODES', async () => {
  const thrown = new Set<string>()
  for await (const path of new Bun.Glob('src/**/*.ts').scan()) {
    if (path.endsWith('.test.ts')) continue
    const text = await Bun.file(path).text()
    for (const match of text.matchAll(/message: '([^']*)'/g))
      thrown.add(match[1]!)
  }
  expect(thrown.size).toBeGreaterThan(0)
  const declared = new Set<string>(API_ERROR_CODES)
  const missing = [...thrown].filter(
    (code) => !declared.has(code) && !EXCLUDED.has(code),
  )
  expect(missing).toEqual([])
})
