Files
archived-orca/src/cli/plugin-dev-watch/plugin-dev-watch-shot.test.ts
T
Neil fa7c3f95fe feat(plugins): check custom editors and test them with mockEditorHost (#37293)
* feat(plugins): check custom editors statically and add mockEditorHost

orca plugin check now follows each custom editor entry (local script src,
inline module scripts, relative imports), transpiled and resolved as the
frame loader does, and fails at file:line:column on syntax errors, missing
files, refused imports, string eval/Function and files over their size
cap, with no running Orca. The check ships in the test hooks bundle.

orca/testing gains mockEditorHost: it runs the frame shell's real
window.orca (document API and session) over an in-memory text or bytes
file, with edits, replace, conflictNext, flush, setTheme and dispose.
Typings, docs and the skill guide cover both.

* feat(plugins): warn on editor eval and check editors in dev watch

editor_eval comes from a lexical scan that can misread rare code, so it is
now a warning that does not fail orca plugin check. dev watch --check runs
the editor check beside the panel check and reports it under editors.

* fix(plugins): check editors by the frame protocol's rules; mock blocks lost saves

The static editor check now reads files through the loader's contained
reader (symlinks out of the plugin are refused), runs only JavaScript
script types, honors the page's import map for native modules, classifies
files by the protocol's content types (JSON imports, extensionless HTML
entries), decodes escaped script URLs, and uses the protocol's caps: 10 MiB
entry, 1 MiB transpiled module, 16 MiB checked dependency.

mockEditorHost refuses an oversized edit before reading it and then rejects
flush() until a later edit lands, as Orca blocks the save.

* test(plugins): type mockEditorHost's frame API instead of reflecting on it

* fix(plugins): keep main's case-spelling check and hook typings through the rebase

The rebase onto main resolved plugin-artifact-validation.ts and
orca-testing.d.ts to B4's side; this puts back main's exact-path-spelling
check and hooks typings beside B4's contained reader and mockEditorHost
types, and regenerates the testing bundle.
2026-11-01 21:28:13 -08:00

266 lines
9.3 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import type { PluginHostListEntry, PluginHostStatus } from '../../shared/plugins/plugin-list-entry'
import { RuntimeClientError, RuntimeRpcFailureError } from '../runtime/types'
import { formatPluginDevWatchEvent, type PluginDevWatchEvent } from './plugin-dev-watch-events'
import { runPluginDevWatch, type PluginDevWatchHost } from './plugin-dev-watch-loop'
import {
isLastingShotFailure,
pluginDevWatchShotPanel,
type PluginDevWatchShot,
type PluginDevWatchShotHost
} from './plugin-dev-watch-shot'
const KEY = 'acme.todos'
function entry(devChangeCount: number, status: PluginHostStatus = 'idle'): PluginHostListEntry {
return {
pluginKey: KEY,
consentFingerprint: 'sha256-x',
name: 'Todos',
version: '1.0.0',
publisher: 'acme',
status,
needsReconsent: false,
isDev: true,
devChangeCount,
official: false,
bundled: false,
capabilities: [],
panels: [
{ id: 'todos', title: 'Todos', tabKey: `plugin:${KEY}/todos` },
{ id: 'stats', title: 'Stats', tabKey: `plugin:${KEY}/stats` }
],
commands: [],
hasWorker: false,
restarts: 0
}
}
const SHOT: PluginDevWatchShot = {
panelId: 'todos',
path: '/work/shots/acme.todos-todos.png',
width: 360,
height: 700,
settled: true,
errors: []
}
type Take = PluginDevWatchShotHost['take']
function shotHost(take: Take, requested?: string) {
const open = vi.fn<PluginDevWatchShotHost['open']>(async () => {})
const host: PluginDevWatchShotHost = {
panelOf: (loaded) => pluginDevWatchShotPanel(loaded, requested),
open,
take: vi.fn(take)
}
return { host, open, take: host.take }
}
/** The load, then one status poll per entry in `saves` (each one save later), then Ctrl-C. */
async function watch(
host: Partial<Pick<PluginDevWatchHost, 'check' | 'shot' | 'devUnload'>>,
saves: PluginHostListEntry[] = [entry(1)]
): Promise<PluginDevWatchEvent[]> {
const events: PluginDevWatchEvent[] = []
const lists = [[entry(0)], ...saves.map((save) => [save])]
let interrupt = () => {}
let poll = 0
await runPluginDevWatch({
host: {
refreshTypes: async () => [],
devLoad: async () => ({
path: '/work/todos',
pluginKey: KEY,
added: true,
approval: 'approve-flag',
plugin: entry(0)
}),
list: async () => lists.shift() ?? [saves.at(-1) ?? entry(0)],
logs: async () => ({ pluginKey: KEY, lines: [] }),
approvalNeeded: async () => null,
devUnload: async () => {},
...host
},
emit: (event) => events.push(event),
wait: async () => {
poll += 1
if (poll === 2 * (saves.length + 1)) {
interrupt()
}
},
onInterrupt: (listener) => {
interrupt = listener
return () => {}
},
exitNow: () => {},
isConnectionLost: () => false
})
return events
}
const ofType = <T extends PluginDevWatchEvent['type']>(events: PluginDevWatchEvent[], type: T) =>
events.filter((event): event is Extract<PluginDevWatchEvent, { type: T }> => event.type === type)
describe('plugin dev watch --shot', () => {
it('opens the panel once, then shoots it after the load and each reload, after the check', async () => {
const order: string[] = []
const { host, open } = shotHost(async (pluginKey, panelId) => {
order.push(`shot ${pluginKey}/${panelId}`)
return SHOT
})
const events = await watch(
{
check: async () => {
order.push('check')
return { ok: true, invalid: [], panels: [], editors: [] }
},
shot: host
},
[entry(1), entry(2)]
)
expect(open).toHaveBeenCalledTimes(1)
expect(open).toHaveBeenCalledWith(KEY, 'todos')
expect(order).toEqual([
'check',
`shot ${KEY}/todos`,
'check',
`shot ${KEY}/todos`,
'check',
`shot ${KEY}/todos`
])
const shots = ofType(events, 'shot')
expect(shots).toHaveLength(3)
expect(events.at(-1)?.type).toBe('unloaded')
expect(formatPluginDevWatchEvent(shots[0]!)).toBe(
'shot /work/shots/acme.todos-todos.png (360x700)'
)
})
it('shoots --shot-panel and fails at start, unloading, on an unknown id', async () => {
const { host, open } = shotHost(async () => ({ ...SHOT, panelId: 'stats' }), 'stats')
await watch({ shot: host })
expect(open).toHaveBeenCalledWith(KEY, 'stats')
const devUnload = vi.fn(async () => {})
await expect(
watch({ shot: shotHost(async () => SHOT, 'nope').host, devUnload })
).rejects.toMatchObject({
code: 'invalid_argument',
message: '--shot-panel: acme.todos has no panel nope; its panels: todos, stats.'
})
expect(devUnload).toHaveBeenCalledTimes(1)
})
it('skips while away, says so once per time away, and resumes when back', async () => {
const outcomes: Awaited<ReturnType<Take>>[] = [
SHOT,
{ notShown: 'the right sidebar shows another tab' },
{ notShown: 'the right sidebar shows another tab' },
SHOT,
{ notShown: 'the right sidebar is closed' }
]
const { host, open } = shotHost(async () => outcomes.shift() ?? SHOT)
const events = await watch({ shot: host }, [entry(1), entry(2), entry(3), entry(4)])
expect(open).toHaveBeenCalledTimes(1)
expect(
events
.filter((event) => event.type === 'shot' || event.type === 'shot_skipped')
.map((event) => event.type)
).toEqual(['shot', 'shot_skipped', 'shot', 'shot_skipped'])
const skipped = ofType(events, 'shot_skipped')
expect(skipped[0]).toEqual({
type: 'shot_skipped',
pluginKey: KEY,
panelId: 'todos',
reason: 'the right sidebar shows another tab'
})
expect(formatPluginDevWatchEvent(skipped[1]!)).toBe(
'shot skipped: the right sidebar is closed, so todos is not on screen; open it again to resume'
)
})
it('skips a reload that left the plugin errored', async () => {
const { host, take } = shotHost(async () => SHOT)
await watch({ shot: host }, [entry(1, 'errored')])
expect(take).toHaveBeenCalledTimes(1)
})
it('turns itself off when the host can never capture', async () => {
const { host, take } = shotHost(async () => {
throw new RuntimeClientError('runtime_unsupported', 'too old')
})
host.open = async () => {
throw new RuntimeClientError('runtime_unsupported', 'too old')
}
const events = await watch({ shot: host }, [entry(1), entry(2)])
expect(take).not.toHaveBeenCalled()
expect(events).toContainEqual({
type: 'warning',
message: 'could not show todos for --shot, so --shot is off: too old'
})
})
it('keeps going after a one-off failure', async () => {
const outcomes: (() => Awaited<ReturnType<Take>>)[] = [
() => {
throw new Error('timeout')
},
() => ({ ...SHOT, settled: false, errors: ['boom'] })
]
const { host, take } = shotHost(async () => (outcomes.shift() ?? (() => SHOT))())
const events = await watch({ shot: host })
expect(take).toHaveBeenCalledTimes(2)
expect(events).toContainEqual({ type: 'warning', message: 'shot could not run: timeout' })
const last = ofType(events, 'shot').at(-1)
expect(last && formatPluginDevWatchEvent(last)).toBe(
'shot /work/shots/acme.todos-todos.png (360x700), not finished drawing\n error: boom'
)
})
it('carries pendingRequests into the shot event and notes it only when some are unanswered', async () => {
const outcomes = [
{ ...SHOT, pendingRequests: 2 },
{ ...SHOT, pendingRequests: 0 }
]
const { host } = shotHost(async () => outcomes.shift() ?? SHOT)
const shots = ofType(await watch({ shot: host }), 'shot')
expect(shots.map((shot) => shot.pendingRequests)).toEqual([2, 0])
expect(formatPluginDevWatchEvent(shots[0]!)).toBe(
'shot /work/shots/acme.todos-todos.png (360x700)\n note: 2 host call(s) were still unanswered; the panel may show its loading state'
)
expect(formatPluginDevWatchEvent(shots[1]!)).toBe(
'shot /work/shots/acme.todos-todos.png (360x700)'
)
})
})
describe('pluginDevWatchShotPanel', () => {
it('defaults to the first panel and checks a requested one', () => {
expect(pluginDevWatchShotPanel(entry(0), undefined)).toBe('todos')
expect(pluginDevWatchShotPanel(entry(0), 'stats')).toBe('stats')
expect(() => pluginDevWatchShotPanel(entry(0), 'x')).toThrow(/no panel x/)
expect(pluginDevWatchShotPanel({ ...entry(0), panels: [] }, undefined)).toBeNull()
// An entry the host could not list yet cannot be checked; the host refuses a wrong id later.
expect(pluginDevWatchShotPanel(null, 'stats')).toBe('stats')
})
})
describe('isLastingShotFailure', () => {
it('is true for an old host or a windowless one, not for passing trouble', () => {
const reply = (message: string) =>
new RuntimeRpcFailureError({
id: 'r',
ok: false,
error: { code: 'runtime_error', message },
_meta: { runtimeId: 'runtime_local' }
})
expect(isLastingShotFailure(new RuntimeClientError('runtime_unsupported', 'old'))).toBe(true)
expect(
isLastingShotFailure(reply('Showing plugin panels is only available in the Orca desktop app'))
).toBe(true)
expect(isLastingShotFailure(reply('no workspace'))).toBe(false)
expect(isLastingShotFailure(new Error('timeout'))).toBe(false)
})
})