openpencil/tests/engine/mcp/webmcp-settings.test.ts
Danila Poyarkov aa86873dd7
test: typecheck the test suites and fix what that found (#896)
* 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
2026-10-05 12:42:38 +00:00

153 lines
5.3 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import { ref } from 'vue'
import type { WebMCP } from 'webmcp-types'
import { ALL_TOOLS, isToolExposed, type ToolDef } from '@open-pencil/core/tools'
import { getWebMCPTools, resolveWebMCPMode, type WebMCPMode } from '@/app/automation/webmcp/policy'
import { createWebMCPRuntimeService } from '@/app/automation/webmcp/service'
function createHost() {
const tools = new Map<string, WebMCP.ModelContextTool>()
return {
tools,
async registerTool(
tool: WebMCP.ModelContextTool,
options?: WebMCP.ModelContextRegisterToolOptions
) {
if (tools.has(tool.name)) throw new Error('Duplicate tool')
tools.set(tool.name, tool)
options?.signal?.addEventListener('abort', () => tools.delete(tool.name), { once: true })
}
}
}
async function ready(service: ReturnType<typeof createWebMCPRuntimeService>) {
for (let attempt = 0; attempt < 1000 && service.state.status === 'starting'; attempt++) {
await Promise.resolve()
}
expect(service.state.status).toBe('ready')
}
describe('WebMCP access preferences', () => {
test('default inclusion cannot override execution support or user access', () => {
const base = ALL_TOOLS[0]
if (!base) throw new Error('Missing tool fixture')
const read: ToolDef = {
...base,
name: 'read',
execution: { kind: 'sync', mutation: 'none' },
mutates: false,
exposure: {}
}
const edit: ToolDef = {
...read,
name: 'edit',
execution: { kind: 'sync', mutation: 'properties' },
mutates: true
}
const tools: ToolDef[] = [
read,
edit,
{ ...read, name: 'excluded', exposure: { webmcp: false } },
{
...edit,
name: 'structural',
execution: { kind: 'sync', mutation: 'document' },
exposure: { webmcp: true }
},
{ ...edit, name: 'async', execution: { kind: 'async', mutation: 'document' } }
]
expect(getWebMCPTools('off', tools)).toEqual([])
expect(getWebMCPTools('inspect', tools).map((tool) => tool.name)).toEqual(['read'])
expect(getWebMCPTools('edit', tools).map((tool) => tool.name)).toEqual(['read', 'edit'])
})
test('unrecognized stored access fails closed', () => {
for (const value of [undefined, null, true, {}, 'all', 'EDIT']) {
expect(resolveWebMCPMode(value)).toBe('off')
}
for (const mode of ['off', 'inspect', 'edit'] as const) {
expect(resolveWebMCPMode(mode)).toBe(mode)
}
expect(getWebMCPTools('off')).toEqual([])
expect(getWebMCPTools('inspect').length).toBeGreaterThan(0)
expect(getWebMCPTools('inspect').every((tool) => !tool.mutates)).toBe(true)
expect(getWebMCPTools('edit').some((tool) => tool.mutates)).toBe(true)
expect(getWebMCPTools('edit').every((tool) => isToolExposed(tool, 'webmcp'))).toBe(true)
})
test('reconfigures access and revokes retained callbacks immediately', async () => {
const context = createHost()
const service = createWebMCPRuntimeService()
const mode = ref<WebMCPMode>('off')
let executions = 0
const stop = service.start(context, mode, () => ({ execute: async () => ++executions }))
try {
expect(service.state.status).toBe('off')
expect(context.tools.size).toBe(0)
mode.value = 'edit'
await ready(service)
const retained = context.tools.get('set_fill')
if (!retained) throw new Error('Missing editing tool')
mode.value = 'inspect'
await expect(
retained.execute({ id: 'node', color: '#ffffff' }, { signal: new AbortController().signal })
).rejects.toThrow()
expect(executions).toBe(0)
await ready(service)
expect(context.tools.size).toBe(getWebMCPTools('inspect').length)
expect(service.state.toolCount).toBe(context.tools.size)
expect(context.tools.has('set_fill')).toBe(false)
mode.value = 'off'
expect(context.tools.size).toBe(0)
expect(service.state.status).toBe('off')
} finally {
stop()
}
})
test('serializes interrupted registrations and stops watching on disposal', async () => {
const context = createHost()
const mode = ref<WebMCPMode>('edit')
const service = createWebMCPRuntimeService()
const stop = service.start(context, mode, () => ({ execute: async () => null }))
await Promise.resolve()
mode.value = 'inspect'
mode.value = 'off'
mode.value = 'edit'
await ready(service)
expect(context.tools.size).toBe(getWebMCPTools('edit').length)
stop()
mode.value = 'inspect'
await Promise.resolve()
expect(context.tools.size).toBe(0)
expect(service.state.status).toBe('off')
})
test('reports unsupported browsers and registration errors', async () => {
const service = createWebMCPRuntimeService()
const mode = ref<WebMCPMode>('inspect')
service.start(undefined, mode, () => ({ execute: async () => null }))
expect(service.state.status).toBe('unsupported')
service.start(
{
registerTool: async () => {
throw new Error('Permission denied')
}
},
mode,
() => ({ execute: async () => null })
)
for (let attempt = 0; attempt < 100 && service.state.status === 'starting'; attempt++)
await Promise.resolve()
expect(service.state).toMatchObject({
status: 'error',
toolCount: 0,
error: 'Permission denied'
})
service.stop()
})
})