codeburn/tests/menubar-provider-alias-contract.test.ts
iamtoruk 034ae444d0 test(menubar): pin every ProviderFilter case to a real CLI provider id
ProviderFilter.cliArg is both the --provider argument and the id the
payload-consistency guard matches against providerDetails. A case whose cliArg
is not a registry name silently disables that guard for its tab: the lookup
finds no detail row, so no contradiction is ever detected. Nothing in either
language can see both sides, so the contract is checked from the TypeScript
side, where the registry is importable and the Swift source is readable.

Table-driven over all 30 enum cases. No mapping needed fixing: .kimiCode
already yields `kimicode`, which is the registry name (`kimi-code` is not a
provider the CLI knows), and every other case resolves too. Setting .kimiCode
to `kimi-code` makes this suite fail, which is the check working.
2026-09-02 07:55:54 -07:00

62 lines
2.3 KiB
TypeScript

import { readFileSync } from 'fs'
import { join } from 'path'
import { describe, expect, it } from 'vitest'
import { allProviderNames } from '../src/providers/index.js'
// The menubar's ProviderFilter.cliArg is what gets passed to `--provider`, and
// it is also the id the payload-consistency guard matches against
// providerDetails. A case whose cliArg is not a real registry name silently
// disables that guard for its tab: the lookup finds no detail row, so no
// contradiction is ever detected. Nothing in either language can see both
// sides, so the contract is checked here, where the registry is importable and
// the Swift source is readable.
const APP_STORE = join(process.cwd(), 'mac/Sources/CodeBurnMenubar/AppStore.swift')
function providerFilterSource(): string {
const source = readFileSync(APP_STORE, 'utf-8')
const start = source.indexOf('enum ProviderFilter')
expect(start).toBeGreaterThan(-1)
return source.slice(start)
}
/** Every `case foo = "Bar"` declared on the enum. */
function enumCases(source: string): string[] {
const body = source.slice(0, source.indexOf('var id: String'))
return [...body.matchAll(/^\s*case (\w+) = "/gm)].map(m => m[1]!)
}
/** The `case .foo: "bar"` table inside `var cliArg`. */
function cliArgTable(source: string): Record<string, string> {
const start = source.indexOf('var cliArg: String {')
expect(start).toBeGreaterThan(-1)
const body = source.slice(start, source.indexOf('\n }', start))
return Object.fromEntries(
[...body.matchAll(/case \.(\w+): "([^"]+)"/g)].map(m => [m[1]!, m[2]!]),
)
}
describe('menubar ProviderFilter to CLI provider registry', () => {
const source = providerFilterSource()
const cases = enumCases(source)
const cliArgs = cliArgTable(source)
const registry = new Set(allProviderNames())
it('declares a cliArg for every enum case', () => {
expect(cases.length).toBeGreaterThan(20)
expect(cases.filter(name => !(name in cliArgs))).toEqual([])
})
it.each(Object.entries(cliArgs).filter(([name]) => name !== 'all'))(
'maps .%s to a provider the CLI registry knows',
(_name, cliArg) => {
expect(registry.has(cliArg)).toBe(true)
},
)
it('keeps .all outside the registry, since it is the no-filter sentinel', () => {
expect(cliArgs['all']).toBe('all')
expect(registry.has('all')).toBe(false)
})
})