Files
orca/src/cli/plugin-dev-watch/plugin-dev-watch-check.test.ts
T
Neil b17d408416 Plugin dev loop: TS workers, hot reload with state, orca plugin dev watch, authoring skill (#36982)
* feat(plugins): build-less TypeScript workers

A worker's main may be .ts/.mts. The plugin host installs module.registerHooks scoped to the
plugin folder: .ts files are transpiled with the frames' sucrase options (lines kept, inline
source map, source maps on), relative imports fall back to the frames' candidate files, and a
value import of the type-only 'orca/worker' fails with a clear error. Needs engines.orca
>=1.4.246.

* feat(plugins): components panels hot reload with their state

- In dev folders a save swaps only the hidden guest: keyed memory atoms are handed to the new
  guest (ui.retain → ui.hydrate memory), dropped when an atom's version changes.
- A save that fails to load keeps the last good render and shows an inline file:line:col
  error banner; the next good save clears it. A subtle Reloaded mark follows a reload.
- Transpile and import errors carry line and column; dev worker restarts keep storage.

* feat(plugins): orca plugin dev watch

One command for the inner loop: refreshes .orca/types, dev-loads the folder, follows its frame
and worker logs (plugins.getLogs gains an optional cursor), prints reloads (optional
devChangeCount on list entries) and status changes, and unloads on Ctrl-C. --json streams
NDJSON events for agents.

* feat(skills): orca-plugin-authoring bundled skill guide

A guide for agents to build Orca plugins end to end (decision table, build-less rules, the
dev watch loop, test/check, validate, release-entry, approvals, gotchas) with references
generated from the component catalog, host API table, event schemas, manifest schema and CLI
specs, checked for drift. Fixes the components template's Reset to set(count, 0).

* feat(plugins): orca plugin dev watch --check; test kit details in the authoring skill

- --check runs orca plugin check after the load and after each reload (one at a time; saves
  made meanwhile fold into one rerun) and prints each panel's render or errors inline, as a
  `check` event with --json. Local folders only, since it runs the plugin's code here.
- The orca-plugin-authoring guide's Test and check section now documents orca plugin test and
  check from #36969 exactly (JSON results, exit codes, test placement, the orca/testing kit and
  its common mistakes), and the loop uses dev watch --check.

* refactor(plugins): move the components renderer's timing and helper constants to their own module
2026-11-01 03:32:25 -08:00

139 lines
4.3 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import type { PluginHostListEntry } from '../../shared/plugins/plugin-list-entry'
import {
createPluginDevWatchCheckQueue,
type PluginDevWatchCheckResult
} from './plugin-dev-watch-check'
import { formatPluginDevWatchEvent, type PluginDevWatchEvent } from './plugin-dev-watch-events'
import { runPluginDevWatch, type PluginDevWatchHost } from './plugin-dev-watch-loop'
const KEY = 'acme.todos'
function entry(devChangeCount: number): PluginHostListEntry {
return {
pluginKey: KEY,
consentFingerprint: 'sha256-x',
name: 'Todos',
version: '1.0.0',
publisher: 'acme',
status: 'idle',
needsReconsent: false,
isDev: true,
devChangeCount,
official: false,
bundled: false,
capabilities: [],
panels: [],
commands: [],
hasWorker: false,
restarts: 0
}
}
const passed: PluginDevWatchCheckResult = {
ok: true,
invalid: [],
panels: [{ panel: 'todos', entry: 'src/panel.tsx', elements: 12, issues: [], warnings: [] }]
}
const failed: PluginDevWatchCheckResult = {
ok: false,
invalid: [],
panels: [
{
panel: 'todos',
entry: 'src/panel.tsx',
elements: 0,
issues: [{ kind: 'plugin', message: 'boom', location: 'src/panel.tsx:4:7' }],
warnings: []
}
]
}
/** Polls: 1 baseline, 2 status (unchanged), 3 logs, 4 status (one save), then Ctrl-C. */
async function watch(check: PluginDevWatchHost['check']): Promise<PluginDevWatchEvent[]> {
const events: PluginDevWatchEvent[] = []
const lists = [[entry(0)], [entry(1)]]
let interrupt = () => {}
let poll = 0
const host: PluginDevWatchHost = {
refreshTypes: async () => [],
devLoad: async () => ({
path: '/work/todos',
pluginKey: KEY,
added: true,
approval: 'approve-flag',
plugin: entry(0)
}),
list: async () => lists.shift() ?? [entry(1)],
logs: async () => ({ pluginKey: KEY, lines: [] }),
approvalNeeded: async () => null,
devUnload: async () => {},
check
}
await runPluginDevWatch({
host,
emit: (event) => events.push(event),
wait: async () => {
poll += 1
if (poll === 6) {
interrupt()
}
},
onInterrupt: (listener) => {
interrupt = listener
return () => {}
},
exitNow: () => {},
isConnectionLost: () => false
})
return events
}
describe('plugin dev watch --check', () => {
it('checks after the load and after each reload, before the unload line', async () => {
const check = vi.fn<NonNullable<PluginDevWatchHost['check']>>()
check.mockResolvedValueOnce(passed).mockResolvedValueOnce(failed)
const events = await watch(check)
expect(check).toHaveBeenCalledTimes(2)
const types = events.map((event) => event.type)
expect(types[0]).toBe('loaded')
expect(types.filter((type) => type === 'check')).toHaveLength(2)
expect(types.indexOf('check', types.indexOf('reloaded'))).toBeGreaterThan(-1)
expect(types.at(-1)).toBe('unloaded')
const last = events.findLast((event) => event.type === 'check')
expect(last && formatPluginDevWatchEvent(last)).toBe(
'✗ check failed for acme.todos\n todos (src/panel.tsx): failed\n src/panel.tsx:4:7: boom'
)
})
it('reports a check that cannot run as a warning and keeps watching', async () => {
const events = await watch(async () => {
throw new Error('no node')
})
expect(events).toContainEqual({ type: 'warning', message: 'check could not run: no node' })
expect(events.at(-1)?.type).toBe('unloaded')
})
it('does not check without --check', async () => {
const events = await watch(undefined)
expect(events.some((event) => event.type === 'check')).toBe(false)
})
})
describe('createPluginDevWatchCheckQueue', () => {
it('runs one check at a time and folds requests made meanwhile into one rerun', async () => {
const releases: (() => void)[] = []
const run = vi.fn(() => new Promise<void>((resolve) => releases.push(resolve)))
const queue = createPluginDevWatchCheckQueue(run)
queue.request()
queue.request()
queue.request()
expect(run).toHaveBeenCalledTimes(1)
releases.shift()?.()
await vi.waitFor(() => expect(run).toHaveBeenCalledTimes(2))
releases.shift()?.()
await queue.idle()
expect(run).toHaveBeenCalledTimes(2)
})
})