mirror of
https://github.com/stablyai/orca.git
synced 2026-11-18 11:54:46 +00:00
* 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.
181 lines
7.4 KiB
TypeScript
181 lines
7.4 KiB
TypeScript
import { readFileSync } from 'node:fs'
|
|
import path from 'node:path'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { PLUGIN_HOST_TYPINGS_FILE, renderPluginHostTypings } from './plugin-host-typings'
|
|
import {
|
|
authorTypingsDiagnostics,
|
|
authorTypingsProgram
|
|
} from './plugin-author-typings.test-fixture'
|
|
|
|
const REPO_ROOT = path.resolve(import.meta.dirname, '../../../..')
|
|
|
|
// One contract, declared once and read by the frame, the worker and the components panel.
|
|
const PROTOCOL = `
|
|
export type Todo = { id: string; title: string; done: boolean }
|
|
declare module 'orca' {
|
|
interface PluginRequests {
|
|
'todos.list': { input: { done?: boolean }; output: Todo[] }
|
|
'todos.add': { input: { title: string }; output: Todo }
|
|
}
|
|
interface PluginPanelEvents {
|
|
'todos.changed': { count: number }
|
|
}
|
|
}
|
|
`
|
|
|
|
const FRAME_SCRIPT = `
|
|
import type { JsonValue } from 'orca/host'
|
|
import type { Todo } from '../protocol'
|
|
|
|
const context = await orca.host.call('workspace.readContext')
|
|
const branch: string | undefined = context?.branch
|
|
const shown: { delivered: boolean } = await window.orca.host.call('notifications.show', { title: 'Hi' })
|
|
orca.on('theme', ({ colorScheme }) => colorScheme satisfies 'light' | 'dark')
|
|
orca.on('editor.flush', async () => {})
|
|
const doc = await orca.document?.get()
|
|
if (doc?.content.kind === 'text') {
|
|
await orca.document?.applyEdit({ baseVersion: doc.version, content: { kind: 'text', text: '' } })
|
|
}
|
|
await orca.files?.saveAs({ suggestedName: 'a.txt', data: new Uint8Array(1) })
|
|
// @ts-expect-error worker-only method
|
|
await orca.host.call('terminal.sendText', { terminalId: 't', text: 'x' })
|
|
const saved: JsonValue | null = await orca.storage.get('a')
|
|
const todos: Todo[] = await orca.plugin.request('todos.list', { done: false }, { timeoutMs: 5000 })
|
|
orca.plugin.on('todos.changed', ({ count }) => count satisfies number)
|
|
orca.on('workspace.created', ({ branch }) => branch satisfies string)
|
|
// @ts-expect-error declared requests type their input
|
|
await orca.plugin.request('todos.list', { done: 'no' })
|
|
// @ts-expect-error a declared request needs its input
|
|
await orca.plugin.request('todos.add')
|
|
const loose: JsonValue = await orca.plugin.request('undeclared.name')
|
|
// @ts-expect-error title is required
|
|
await orca.host.call('notifications.show', {})
|
|
// @ts-expect-error params are strict
|
|
await orca.host.call('workspace.readContext', { extra: true })
|
|
// @ts-expect-error unknown frame event
|
|
orca.on('worktree.created', () => {})
|
|
export { branch, shown, saved, todos, loose }
|
|
`
|
|
|
|
const WORKER_MAIN = `
|
|
import type { Activate, OrcaWorkerApi } from 'orca/worker'
|
|
import type { Todo } from './protocol'
|
|
const activate: Activate = (orca: OrcaWorkerApi) => {
|
|
const todos: Todo[] = []
|
|
orca.panels.handle('todos.list', async ({ done }, { panel, signal }) => {
|
|
signal.throwIfAborted()
|
|
orca.log(panel.id + panel.surface)
|
|
return todos.filter((todo) => done === undefined || todo.done === done)
|
|
})
|
|
orca.panels.handle('todos.add', ({ title }) => {
|
|
const todo = { id: String(todos.length), title, done: false }
|
|
todos.push(todo)
|
|
orca.panels.post('todos.changed', { count: todos.length })
|
|
return todo
|
|
})
|
|
// @ts-expect-error a handler answers with the declared output
|
|
orca.panels.handle('todos.add', () => ({ id: 1 }))
|
|
// @ts-expect-error posts carry the declared payload
|
|
orca.panels.post('todos.changed', { count: 'many' })
|
|
orca.settings.onDidChange(({ key }) => void orca.settings.get(key))
|
|
orca.commands.register('ping', async (_args, { signal }) => {
|
|
const stored = await orca.host.call('storage.get', { key: 'pings' }, { signal })
|
|
await orca.host.call('storage.set', { key: 'pings', value: [stored.value, { at: 1 }] })
|
|
return (await orca.host.call('storage.keys')).keys.length
|
|
})
|
|
orca.events.on('workspace.created', (payload) => orca.log(payload.path + payload.branch))
|
|
orca.events.on('agent.status.changed', ({ workspaceId, state, mainAgent }) => {
|
|
const done: boolean = state === 'done' && mainAgent?.outcome === 'success'
|
|
orca.log(String(workspaceId ?? done))
|
|
})
|
|
// @ts-expect-error frame-only method
|
|
void orca.host.call('shell.openExternal', { url: 'https://example.com' })
|
|
// @ts-expect-error unknown event
|
|
orca.events.on('worktree.created', () => {})
|
|
// @ts-expect-error storage values are JSON
|
|
void orca.host.call('storage.set', { key: 'k', value: () => 1 })
|
|
orca.hooks.on('terminal.send', (event, next) => {
|
|
if (event.text.includes('rm -rf /')) {
|
|
return { deny: 'blocked' }
|
|
}
|
|
return next({ ...event, text: event.text.trim() })
|
|
})
|
|
orca.hooks.on('workspace.create.after', async ({ path }) => orca.log(path))
|
|
// @ts-expect-error a deny carries its reason
|
|
orca.hooks.on('terminal.send', () => ({ deny: 1 }))
|
|
// @ts-expect-error unknown hook
|
|
orca.hooks.on('terminal.write', () => {})
|
|
}
|
|
export default activate
|
|
`
|
|
|
|
const COMPONENTS_PANEL = `
|
|
import { atom, Button, read, Stack, update, host, plugin, settings, on } from 'orca'
|
|
import type { Todo } from './protocol'
|
|
const count = atom(0)
|
|
const todos = atom<Todo[]>('todos', [], { persist: 'storage' })
|
|
void plugin.request('todos.add', { title: 'x' }).then((todo) => todo.title satisfies string)
|
|
plugin.on('todos.changed', ({ count }) => count satisfies number)
|
|
on('agent.status.changed', ({ state }) => state satisfies string)
|
|
void settings.get('apiUrl')
|
|
export default function Panel() {
|
|
return (
|
|
<Stack gap="md">
|
|
<Button onPress={() => update(count, (n) => n + 1)}>{read(count) + read(todos).length}</Button>
|
|
<Button onPress={() => void host.call('notifications.show', { title: 'x' })}>Notify</Button>
|
|
</Stack>
|
|
)
|
|
}
|
|
`
|
|
|
|
const PANEL_TEST = `
|
|
import { mockHost, mount, assert } from 'orca/testing'
|
|
import Panel from './panel'
|
|
import type { Todo } from './protocol'
|
|
const done: Todo = { id: '1', title: 'x', done: true }
|
|
const host = mockHost({
|
|
requests: { 'todos.list': () => [done], 'todos.add': ({ title }) => ({ ...done, title }) },
|
|
storage: { todos: [] },
|
|
settings: { apiUrl: 'https://example.com' }
|
|
})
|
|
const panel = await mount(Panel, { host })
|
|
await panel.post('todos.changed', { count: 1 })
|
|
await panel.emit('agent.status.changed', { workspaceId: null, paneKey: 'p', state: 'done', receivedAt: 1 })
|
|
// @ts-expect-error posts carry the declared payload
|
|
await panel.post('todos.changed', { count: 'one' })
|
|
assert.deepEqual(panel.persisted.storage, host.data().storage)
|
|
`
|
|
|
|
const EDITOR_TEST = `
|
|
import { assert, mockEditorHost } from 'orca/testing'
|
|
const host = mockEditorHost({ text: 'a' }, { colorScheme: 'light' })
|
|
const doc = await host.orca.document!.session({ debounceMs: 0 })
|
|
doc.change(doc.value + 'b')
|
|
host.conflictNext('outside')
|
|
await host.flush()
|
|
assert.equal(host.text, 'ab')
|
|
host.edits[0]?.refused satisfies string | undefined
|
|
host.dispose()
|
|
`
|
|
|
|
describe('author typings', () => {
|
|
it('checks in the host typings generated from the host API table', () => {
|
|
const checkedIn = readFileSync(path.join(REPO_ROOT, PLUGIN_HOST_TYPINGS_FILE), 'utf8')
|
|
// Run `pnpm generate:plugin-author-typings` when this fails.
|
|
expect(checkedIn).toBe(renderPluginHostTypings())
|
|
})
|
|
|
|
it('type-check a frame script, a worker main and a components panel', () => {
|
|
const program = authorTypingsProgram({
|
|
'protocol.ts': PROTOCOL,
|
|
'panel/app.ts': FRAME_SCRIPT,
|
|
'main.ts': WORKER_MAIN,
|
|
'src/panel.tsx': COMPONENTS_PANEL,
|
|
'src/protocol.ts': PROTOCOL,
|
|
'src/panel.test.ts': PANEL_TEST,
|
|
'editor/sync.test.ts': EDITOR_TEST
|
|
})
|
|
expect(authorTypingsDiagnostics(program)).toEqual([])
|
|
})
|
|
})
|