* 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
172 lines
5.1 KiB
TypeScript
172 lines
5.1 KiB
TypeScript
import { describe, expect, test } from 'bun:test'
|
|
|
|
import type { WebMCP } from 'webmcp-types'
|
|
|
|
import { registerWebMCPTools } from '@/app/automation/webmcp/registration'
|
|
|
|
function host() {
|
|
const tools = new Map<string, WebMCP.ModelContextTool>()
|
|
return {
|
|
tools,
|
|
async registerTool(
|
|
tool: WebMCP.ModelContextTool,
|
|
options?: WebMCP.ModelContextRegisterToolOptions
|
|
) {
|
|
tools.set(tool.name, tool)
|
|
options?.signal?.addEventListener('abort', () => tools.delete(tool.name), { once: true })
|
|
}
|
|
}
|
|
}
|
|
|
|
function requiredTool(tools: Map<string, WebMCP.ModelContextTool>, name: string) {
|
|
const tool = tools.get(name)
|
|
if (!tool) throw new Error(`Missing tool ${name}`)
|
|
return tool
|
|
}
|
|
|
|
const options = () => ({ signal: new AbortController().signal })
|
|
|
|
describe('WebMCP registration', () => {
|
|
test('unsupported browsers do nothing', async () => {
|
|
const registration = registerWebMCPTools(
|
|
undefined,
|
|
() => {
|
|
throw new Error('Must not resolve an editor')
|
|
},
|
|
'edit'
|
|
)
|
|
await registration.ready
|
|
registration.dispose()
|
|
})
|
|
|
|
test('registers only reviewed tools and unregisters on disposal', async () => {
|
|
const context = host()
|
|
const registration = registerWebMCPTools(context, () => ({ execute: async () => ({}) }), 'edit')
|
|
await registration.ready
|
|
expect(context.tools.has('get_node')).toBe(true)
|
|
for (const name of [
|
|
'eval',
|
|
'render',
|
|
'save_file',
|
|
'delete_node',
|
|
'create_variable',
|
|
'set_image_fill'
|
|
]) {
|
|
expect(context.tools.has(name)).toBe(false)
|
|
}
|
|
expect(context.tools.get('get_node')?.annotations?.readOnlyHint).toBe(true)
|
|
expect(context.tools.get('set_fill')?.annotations?.readOnlyHint).toBe(false)
|
|
for (const tool of context.tools.values()) {
|
|
expect(tool.annotations?.untrustedContentHint).toBe(true)
|
|
expect(tool.inputSchema).toMatchObject({ type: 'object' })
|
|
}
|
|
registration.dispose()
|
|
expect(context.tools.size).toBe(0)
|
|
})
|
|
|
|
test('validates before execution and captures the target for each call', async () => {
|
|
const context = host()
|
|
let active = 'first'
|
|
let calls = 0
|
|
const registration = registerWebMCPTools(
|
|
context,
|
|
() => {
|
|
const captured = active
|
|
return {
|
|
execute: async () => {
|
|
calls++
|
|
await Promise.resolve()
|
|
return { document: captured }
|
|
}
|
|
}
|
|
},
|
|
'edit'
|
|
)
|
|
await registration.ready
|
|
try {
|
|
const tool = requiredTool(context.tools, 'get_node')
|
|
await expect(tool.execute({}, options())).rejects.toThrow()
|
|
expect(calls).toBe(0)
|
|
const pending = tool.execute({ id: 'node' }, options())
|
|
active = 'second'
|
|
expect(await pending).toBe('{"document":"first"}')
|
|
expect(await tool.execute({ id: 'node' }, options())).toBe('{"document":"second"}')
|
|
} finally {
|
|
registration.dispose()
|
|
}
|
|
})
|
|
|
|
test('rejects cancelled calls and excessive results', async () => {
|
|
const context = host()
|
|
const registration = registerWebMCPTools(
|
|
context,
|
|
() => ({
|
|
execute: async () => 'x'.repeat(300_000)
|
|
}),
|
|
'edit'
|
|
)
|
|
await registration.ready
|
|
try {
|
|
const tool = requiredTool(context.tools, 'get_node')
|
|
const cancelled = new AbortController()
|
|
cancelled.abort()
|
|
await expect(tool.execute({ id: 'node' }, { signal: cancelled.signal })).rejects.toThrow()
|
|
await expect(tool.execute({ id: 'node' }, options())).rejects.toThrow('Result too large')
|
|
} finally {
|
|
registration.dispose()
|
|
}
|
|
})
|
|
|
|
test('reports committed edits even when their result exceeds the size limit', async () => {
|
|
const context = host()
|
|
let content = ''
|
|
const registration = registerWebMCPTools(
|
|
context,
|
|
() => ({
|
|
execute: async (_def, args) => {
|
|
content = String(args.text)
|
|
return { id: args.id, text: content }
|
|
}
|
|
}),
|
|
'edit'
|
|
)
|
|
await registration.ready
|
|
try {
|
|
const text = 'x'.repeat(300_000)
|
|
const result = await requiredTool(context.tools, 'set_text').execute(
|
|
{ id: 'node', text },
|
|
options()
|
|
)
|
|
expect(content).toBe(text)
|
|
expect(result).toBe(
|
|
JSON.stringify({
|
|
ok: true,
|
|
resultOmitted: true,
|
|
message: 'Edit committed. Result exceeds the size limit; inspect a smaller selection.'
|
|
})
|
|
)
|
|
} finally {
|
|
registration.dispose()
|
|
}
|
|
})
|
|
|
|
test('cleans partial registration after browser rejection', async () => {
|
|
const context = host()
|
|
const registration = registerWebMCPTools(
|
|
{
|
|
async registerTool(
|
|
tool: WebMCP.ModelContextTool,
|
|
options?: WebMCP.ModelContextRegisterToolOptions
|
|
) {
|
|
if (context.tools.size === 2) throw new Error('Registration denied')
|
|
await context.registerTool(tool, options)
|
|
}
|
|
},
|
|
() => ({ execute: async () => ({}) }),
|
|
'edit'
|
|
)
|
|
await expect(registration.ready).rejects.toThrow('Registration denied')
|
|
expect(context.tools.size).toBe(0)
|
|
})
|
|
})
|