* build: typecheck the test suites Tests were in no TypeScript program: no tsconfig included tests/** or packages/*/tests/**, and bun strips types without checking them, so a fixture could drop a required field and keep passing until something read it. @types/bun moves to the root because it was installed per package only, and #cli-tests/* joins the paths the root config already carries. * test: fix the type errors the test suites were hiding Typechecking the tests turned up 1123 errors. Most were ordinary strictness, but some were real: `NodeChange` bound to Figma's plugin typings rather than the Kiwi codec in thirteen .fig tests, materializeInstance was called with six arguments against five so the blobs and source children were dropped, CanvasKit pixels were written to a plain object that never reached WASM, and assertions were made through accessors that do not exist, so they asserted nothing. Fixtures that had quietly lost a required field now carry it, nullable results are narrowed through the existing expectDefined helper rather than assumed, and stand-ins for CanvasKit and the editor go through one named helper instead of an unexplained cast at each site. No test was deleted, skipped, or weakened, and no `any`, non-null assertion, or ts-expect-error was introduced. * docs: record what typechecking the tests established Pins the app program's global types with an assertion rather than a note, since an unpinned types list lets any root @types package decide which platform src/** is judged against. The two environment faults that look like code regressions — Vite's dependency pre-bundle outliving a package rebuild, and heavy .fig suites failing under load — go to the development docs, where an explanation belongs. * fix: align @types/bun and keep node types resolvable when extended The root manifest declared a newer @types/bun than every package, which check:monorepo rejects, and pinning the app program's types left them unresolvable from a config that extends this one out of tree. * fix: fail the test typecheck when the compiler itself fails The gate matched diagnostics by substring, so a compiler or config failure that named no test file printed a pass while having checked nothing. Diagnostics are now split by whether they name a file: an unscoped one is the run failing and stops the gate, a test file's is a finding, and a source file's stays out by design. Also drops the parameter planComponentConstruction never read, and makes the inner-shadow verification script exit non-zero when it renders no image instead of logging and succeeding. * chore: merge master into tests-typecheck
152 lines
5.1 KiB
TypeScript
152 lines
5.1 KiB
TypeScript
import { describe, expect, test } from 'bun:test'
|
|
|
|
import type { LanguageModel } from 'ai'
|
|
|
|
import { AI_PROVIDERS } from '@open-pencil/core/constants'
|
|
|
|
import { resolveLanguageModelID } from '@/app/ai/chat/model'
|
|
import { normalizeOpenRouterModel } from '@/app/ai/chat/provider-models'
|
|
import { createAnthropicCompatibleAdapter } from '@/app/ai/providers/compatible'
|
|
import { modelProviderAdapter } from '@/app/ai/providers/registry'
|
|
import type { ModelConfig } from '@/app/ai/providers/types'
|
|
import type { FetchFunction } from '@/app/http/types'
|
|
|
|
function languageModelInstance(model: LanguageModel): Exclude<LanguageModel, string> {
|
|
if (typeof model === 'string') throw new Error(`Expected a model instance, got ${model}`)
|
|
return model
|
|
}
|
|
|
|
describe('resolveLanguageModelID', () => {
|
|
test('uses the selected OpenRouter model when no custom model is configured', () => {
|
|
expect(
|
|
resolveLanguageModelID({
|
|
providerID: 'openrouter',
|
|
modelID: 'anthropic/claude-sonnet-4.6',
|
|
customModelID: ''
|
|
})
|
|
).toBe('anthropic/claude-sonnet-4.6')
|
|
})
|
|
|
|
test('uses a custom OpenRouter model ID when provided', () => {
|
|
expect(
|
|
resolveLanguageModelID({
|
|
providerID: 'openrouter',
|
|
modelID: 'anthropic/claude-sonnet-4.6',
|
|
customModelID: ' meta-llama/llama-3.3-70b-instruct '
|
|
})
|
|
).toBe('meta-llama/llama-3.3-70b-instruct')
|
|
})
|
|
})
|
|
|
|
describe('model provider registry', () => {
|
|
test('offers MiniMax-M3 as the default MiniMax model', () => {
|
|
const provider = AI_PROVIDERS.find(({ id }) => id === 'minimax')
|
|
expect(provider?.defaultModel).toBe('MiniMax-M3')
|
|
expect(provider?.models[0]).toMatchObject({ id: 'MiniMax-M3' })
|
|
})
|
|
|
|
test('sends MiniMax-M3 through the OpenAI-compatible chat endpoint', async () => {
|
|
let requestURL = ''
|
|
let requestBody = ''
|
|
const fetchSpy: FetchFunction = async (input, init) => {
|
|
requestURL = String(input)
|
|
requestBody = String(init?.body)
|
|
throw new Error('stop')
|
|
}
|
|
const config: ModelConfig = {
|
|
providerID: 'minimax',
|
|
apiKey: 'test-key',
|
|
modelID: 'MiniMax-M3',
|
|
customModelID: '',
|
|
customBaseURL: '',
|
|
customAPIType: 'completions'
|
|
}
|
|
|
|
const model = languageModelInstance(
|
|
modelProviderAdapter('minimax').create(config, { fetch: fetchSpy })
|
|
)
|
|
await Promise.resolve(
|
|
model.doGenerate({ prompt: [{ role: 'user', content: [{ type: 'text', text: 'hi' }] }] })
|
|
).catch(() => undefined)
|
|
|
|
expect(requestURL).toBe('https://api.minimax.io/v1/chat/completions')
|
|
expect(requestBody).toContain('"model":"MiniMax-M3"')
|
|
})
|
|
|
|
test('registers every direct provider without handling agent runtimes as models', () => {
|
|
for (const provider of AI_PROVIDERS.filter((entry) => entry.id !== 'harness:pi')) {
|
|
expect(modelProviderAdapter(provider.id).create).toBeFunction()
|
|
}
|
|
expect(() => modelProviderAdapter('harness:pi')).toThrow('Harness agents')
|
|
expect(() => modelProviderAdapter('acp:claude-code')).toThrow(
|
|
'ACP providers and Harness agents do not use direct API models'
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('anthropic compatible adapter', () => {
|
|
const config: ModelConfig = {
|
|
providerID: 'anthropic',
|
|
apiKey: 'test-key',
|
|
modelID: 'claude-sonnet-4.6',
|
|
customModelID: '',
|
|
customBaseURL: '',
|
|
customAPIType: 'completions'
|
|
}
|
|
|
|
async function capturedHeaders(): Promise<Record<string, string>> {
|
|
let captured: Record<string, string> = {}
|
|
const fetchSpy: FetchFunction = async (_input, init) => {
|
|
captured = Object.fromEntries(new Headers(init?.headers).entries())
|
|
throw new Error('stop')
|
|
}
|
|
|
|
const model = languageModelInstance(
|
|
createAnthropicCompatibleAdapter().create(config, { fetch: fetchSpy })
|
|
)
|
|
await Promise.resolve(
|
|
model.doGenerate({ prompt: [{ role: 'user', content: [{ type: 'text', text: 'hi' }] }] })
|
|
).catch(() => undefined)
|
|
|
|
return captured
|
|
}
|
|
|
|
test('opts into direct browser access so Anthropic returns CORS headers', async () => {
|
|
// Anthropic omits Access-Control-Allow-Origin unless this header is sent, which makes the
|
|
// web build fail with an opaque network error. See #436.
|
|
expect(await capturedHeaders()).toHaveProperty(
|
|
'anthropic-dangerous-direct-browser-access',
|
|
'true'
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('normalizeOpenRouterModel', () => {
|
|
test('keeps tool-capable OpenRouter models', () => {
|
|
expect(
|
|
normalizeOpenRouterModel({
|
|
id: 'meta-llama/llama-3.3-70b-instruct',
|
|
name: 'Llama 3.3 70B Instruct',
|
|
supported_parameters: ['tools'],
|
|
architecture: { input_modalities: ['text', 'image'] },
|
|
top_provider: { max_completion_tokens: 32_768 }
|
|
})
|
|
).toEqual({
|
|
id: 'meta-llama/llama-3.3-70b-instruct',
|
|
name: 'Llama 3.3 70B Instruct',
|
|
capabilities: ['tools', 'vision'],
|
|
recommendedMaxOutputTokens: 32_768
|
|
})
|
|
})
|
|
|
|
test('skips OpenRouter models without tool support', () => {
|
|
expect(
|
|
normalizeOpenRouterModel({
|
|
id: 'text-only/model',
|
|
name: 'Text Only',
|
|
supported_parameters: []
|
|
})
|
|
).toBeNull()
|
|
})
|
|
})
|