Files
orca/src/cli/plugin-authoring/plugin-test-node.ts
T
Neil 531658f1d1 Plugin test kit: orca/testing, orca plugin test and orca plugin check (#36969)
* feat(plugins): orca/testing kit, `orca plugin test` and `orca plugin check`

- `orca/testing` mounts a components panel in plain Node (node:test, vitest, bun) with the real
  runtime and Orca's own tree validator: find by text/role/label/testId, press/type/submit/toggle/
  select that resolve once the panel settles, tree() snapshots, errors (with stacks) and warnings,
  persisted state (with stored() for migrations), emit/setVisible/rerender, and unmount running
  setup cleanup. It re-exports node:test and strict assert so tests type-check without @types/node.
- mockHost refuses what Orca refuses with the same codes (surface, capability from the manifest,
  params, a gesture method without a press), checks configured results against the host API, and
  records calls.
- `orca plugin test [dir]` discovers *.test.ts(x) and runs them with node:test through Node module
  hooks that resolve `orca` like the guest's import map and transpile/resolve sources exactly as
  Orca's build-less loader, or with the plugin's own vitest via a generated config.
- `orca plugin check [dir]` validates, then renders each components panel once and reports what it
  threw (file:line), layouts Orca would refuse, and props it would drop.
- The kit, hooks and register entry ship minified beside orca-runtime.mjs and are unpacked from asar;
  typings land in .orca/types/orca-testing. The components template and hello-tsx get sample tests.
- ui.error carries an optional stack; a new runtime starts every atom from its initial value; the
  hydration fallback timer is cleared once hydrated.

* plugins: run plugin test/check on a system Node or Orca's own, and warn they are unsandboxed

* test(plugins): run the shipped example plugin tests from the unit suite
2026-11-01 02:42:12 -08:00

73 lines
2.5 KiB
TypeScript

import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { runProcess } from '@orca/process-host'
import type { ProcessResult, ProcessSpec } from '@orca/process-host/process-spec'
/** `module.register` (the `--import` hooks) arrived in Node 20.6; `node:test` is older. */
const MIN_NODE = [20, 6] as const
export type PluginTestNode = {
/** Absolute path of the executable to run. */
program: string
/** Extra environment: Orca's own runtime needs ELECTRON_RUN_AS_NODE to act as Node. */
env: Record<string, string>
source: 'system' | 'orca'
version: string
}
type Dependencies = {
run: (spec: ProcessSpec) => Promise<ProcessResult>
/** The runtime this CLI runs on: Electron as Node in a packaged app, or Node in development. */
execPath: string
version: string
electron: string | undefined
}
const defaults = (): Dependencies => ({
run: runProcess,
execPath: process.execPath,
version: process.version,
electron: process.versions.electron
})
function supports(version: string): boolean {
const [major = 0, minor = 0] = version.replace(/^v/, '').split('.').map(Number)
return major > MIN_NODE[0] || (major === MIN_NODE[0] && minor >= MIN_NODE[1])
}
/**
* The Node to run plugin tests with: the author's `node` when it is new enough, else Orca's own
* runtime, so tests run on a machine with no Node installed. The probe asks `node` for its own
* absolute path from an empty folder: on Windows a bare name resolves in the working directory
* first, and the plugin folder may hold a planted `node.exe`.
*/
export async function selectPluginTestNode(
deps: Dependencies = defaults()
): Promise<PluginTestNode> {
const empty = await mkdtemp(join(tmpdir(), 'orca-plugin-node-'))
const probe = await deps
.run({
program: 'node',
args: ['-p', 'process.version + " " + process.execPath'],
cwd: empty,
timeoutMs: 10_000,
maxOutputBytes: 4096
})
.catch(() => null)
.finally(() => rm(empty, { recursive: true, force: true }))
const answer = probe?.code === 0 ? probe.stdout.trim() : ''
const space = answer.indexOf(' ')
const version = space > 0 ? answer.slice(0, space) : ''
const program = space > 0 ? answer.slice(space + 1) : ''
if (version && program && supports(version)) {
return { program, env: {}, source: 'system', version }
}
return {
program: deps.execPath,
env: deps.electron ? { ELECTRON_RUN_AS_NODE: '1' } : {},
source: 'orca',
version: deps.version
}
}