Plugins can add pages to the sidebar, VM tabs, Settings and dialogs. Their pages can now use host forms and navigation, VM and settings access, authorized file I/O, task progress, controlled HTTP requests, events and private storage. Plugin pages cannot change allow_ai_access, which stays the user's per-VM consent. Closing a page cancels its unfinished tasks, and running plugin tasks no longer block changing the data folder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
319 lines
15 KiB
JavaScript
319 lines
15 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import test from 'node:test'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { loadTestModule } from './load-test-module.mjs'
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
const appCSSPath = path.resolve(__dirname, '../src/styles/app.css')
|
|
|
|
const { hasVisibleDialog, isDialogElementVisible } = await loadTestModule(`export * from './lib/dialogState'`)
|
|
const { resolvePluginApprovalNotice } = await loadTestModule(`export { resolvePluginApprovalNotice } from './components/PluginParts'`)
|
|
const { createPluginTaskScope, initialFormValues, invokePluginViewAction, missingFormFields } = await loadTestModule(`export * from './lib/pluginViewActions'`)
|
|
const { openPluginView, closePluginView, emptyPluginViewState, matchViewCondition, prunePluginViews } = await loadTestModule(`export * from './lib/pluginViewRouter'`)
|
|
const { publishPluginHostEvent, subscribePluginHostEvents } = await loadTestModule(`export * from './lib/pluginViewEvents'`)
|
|
const { buildSafePluginAssetURL } = await loadTestModule(`export * from './lib/pluginAssets'`)
|
|
|
|
function fakeDialog({
|
|
display = 'block',
|
|
visibility = 'visible',
|
|
ariaHidden = null,
|
|
hidden = false,
|
|
inert = false,
|
|
dataState = null,
|
|
parentElement = null,
|
|
} = {}) {
|
|
const el = {
|
|
parentElement,
|
|
getAttribute(name) {
|
|
if (name === 'aria-hidden') return ariaHidden
|
|
if (name === 'hidden') return hidden ? '' : null
|
|
if (name === 'inert') return inert ? '' : null
|
|
if (name === 'data-state') return dataState
|
|
return null
|
|
},
|
|
closest() {
|
|
return null
|
|
},
|
|
}
|
|
return { el, style: { display, visibility } }
|
|
}
|
|
|
|
test('hidden dialog elements (including ancestor display:none, inert, and closed state) do not block shortcuts', () => {
|
|
const hiddenDisplay = fakeDialog({ display: 'none' })
|
|
const hiddenVis = fakeDialog({ visibility: 'hidden' })
|
|
const hiddenCollapse = fakeDialog({ visibility: 'collapse' })
|
|
const hiddenAttr = fakeDialog({ hidden: true })
|
|
const hiddenInert = fakeDialog({ inert: true })
|
|
const hiddenAria = fakeDialog({ ariaHidden: 'true' })
|
|
const closedState = fakeDialog({ dataState: 'closed' })
|
|
const hiddenParent = fakeDialog({ display: 'none' })
|
|
const childOfHiddenParent = fakeDialog({ parentElement: hiddenParent.el })
|
|
const visibleParent = fakeDialog({ display: 'block' })
|
|
const visible = fakeDialog({ parentElement: visibleParent.el })
|
|
|
|
const styles = new Map([
|
|
[hiddenDisplay.el, hiddenDisplay.style],
|
|
[hiddenVis.el, hiddenVis.style],
|
|
[hiddenCollapse.el, hiddenCollapse.style],
|
|
[hiddenAttr.el, hiddenAttr.style],
|
|
[hiddenInert.el, hiddenInert.style],
|
|
[hiddenAria.el, hiddenAria.style],
|
|
[closedState.el, closedState.style],
|
|
[hiddenParent.el, hiddenParent.style],
|
|
[childOfHiddenParent.el, childOfHiddenParent.style],
|
|
[visibleParent.el, visibleParent.style],
|
|
[visible.el, visible.style],
|
|
])
|
|
const getStyle = (node) => styles.get(node) ?? { display: 'block', visibility: 'visible' }
|
|
|
|
for (const hiddenCase of [
|
|
hiddenDisplay,
|
|
hiddenVis,
|
|
hiddenCollapse,
|
|
hiddenAttr,
|
|
hiddenInert,
|
|
hiddenAria,
|
|
closedState,
|
|
childOfHiddenParent,
|
|
]) {
|
|
assert.equal(isDialogElementVisible(hiddenCase.el, getStyle), false)
|
|
}
|
|
assert.equal(isDialogElementVisible(visible.el, getStyle), true)
|
|
|
|
assert.equal(
|
|
hasVisibleDialog(
|
|
{ querySelectorAll: () => [hiddenDisplay.el, hiddenVis.el, childOfHiddenParent.el, closedState.el] },
|
|
getStyle,
|
|
),
|
|
false,
|
|
)
|
|
assert.equal(
|
|
hasVisibleDialog({ querySelectorAll: () => [childOfHiddenParent.el, visible.el] }, getStyle),
|
|
true,
|
|
)
|
|
})
|
|
|
|
test('L1 ui:script plugins receive accurate, style-compliant warning instead of L3 sidecar copy', () => {
|
|
const uiScriptPlugin = {
|
|
tier: 'L1',
|
|
permissions: [{ name: 'ui:script', level: 'high' }],
|
|
}
|
|
const zhNotice = resolvePluginApprovalNotice(uiScriptPlugin, null, 'zh-CN')
|
|
const enNotice = resolvePluginApprovalNotice(uiScriptPlugin, null, 'en-US')
|
|
assert.ok(zhNotice.includes('ui:script'), `expected ui:script zh notice, got: ${zhNotice}`)
|
|
assert.ok(!zhNotice.includes('L3 Sidecar'), `L1 ui:script must not show L3 Sidecar warning: ${zhNotice}`)
|
|
assert.ok(!zhNotice.includes('请'), `zh notice should follow design guidelines (no 请): ${zhNotice}`)
|
|
assert.ok(enNotice.includes('ui:script'), `expected ui:script en notice, got: ${enNotice}`)
|
|
assert.ok(!enNotice.includes('L3 Sidecar'), `L1 ui:script must not show L3 Sidecar warning: ${enNotice}`)
|
|
assert.ok(!enNotice.endsWith('.'), `en notice should not end with a trailing period: ${enNotice}`)
|
|
|
|
const l3Plugin = { tier: 'L3', permissions: [{ name: 'driver:vm', level: 'high' }] }
|
|
const l3Notice = resolvePluginApprovalNotice(l3Plugin, null, 'en-US')
|
|
assert.ok(l3Notice.includes('L3 Sidecar'), `expected L3 Sidecar warning, got: ${l3Notice}`)
|
|
|
|
const plainTheme = { tier: 'L1', permissions: [] }
|
|
assert.equal(resolvePluginApprovalNotice(plainTheme, null, 'en-US'), '')
|
|
})
|
|
|
|
test('dialog body children and plugin scan lists disable flex shrinking so tall modals scroll instead of clipping', () => {
|
|
const css = fs.readFileSync(appCSSPath, 'utf8')
|
|
assert.match(css, /\.dialog-body\s*>\s*\*\s*\{[^}]*flex-shrink\s*:\s*0/)
|
|
assert.match(css, /\.plugin-scan-section\s*>\s*\.list\s*\{[^}]*flex-shrink\s*:\s*0/)
|
|
})
|
|
|
|
test('packaged view assets stay on the same-origin plugin asset server', () => {
|
|
assert.equal(buildSafePluginAssetURL('abc123', 'vendor.tool', 'icons/console.svg'), '/plugin-assets/abc123/vendor.tool/icons/console.svg')
|
|
assert.equal(buildSafePluginAssetURL('abc123', 'vendor.tool', '../secret'), null)
|
|
assert.equal(buildSafePluginAssetURL('abc123', 'vendor.tool', 'icons\\secret.svg'), null)
|
|
assert.equal(buildSafePluginAssetURL('abc123', 'vendor.tool', 'https://example.com/icon.svg'), null)
|
|
assert.equal(buildSafePluginAssetURL('not-safe', 'vendor.tool', 'icon.svg'), null)
|
|
})
|
|
|
|
test('plugin confirmation runs only after host validation and returns the user choice', async () => {
|
|
const steps = []
|
|
const result = await invokePluginViewAction('ui.confirm', { message: 'Publish?' }, {
|
|
invoke: async (method, params) => {
|
|
steps.push(['invoke', method, params.message])
|
|
return { message: params.message, danger: true }
|
|
},
|
|
confirm: async (request) => {
|
|
steps.push(['confirm', request.message, request.danger])
|
|
return true
|
|
},
|
|
copy: async () => { throw new Error('unexpected copy') },
|
|
})
|
|
assert.deepEqual(result, { confirmed: true })
|
|
assert.deepEqual(steps, [
|
|
['invoke', 'ui.confirm', 'Publish?'],
|
|
['confirm', 'Publish?', true],
|
|
])
|
|
})
|
|
|
|
test('plugin clipboard write waits for host approval and is skipped on rejection', async () => {
|
|
const copied = []
|
|
const result = await invokePluginViewAction('ui.copy', { text: 'first\nsecond' }, {
|
|
invoke: async () => ({ text: 'first\nsecond' }),
|
|
confirm: async () => { throw new Error('unexpected confirmation') },
|
|
copy: async (text) => { copied.push(text) },
|
|
})
|
|
assert.deepEqual(result, { ok: true })
|
|
assert.deepEqual(copied, ['first\nsecond'])
|
|
|
|
await assert.rejects(invokePluginViewAction('ui.copy', { text: 'secret' }, {
|
|
invoke: async () => { throw new Error('permission denied') },
|
|
confirm: async () => false,
|
|
copy: async (text) => { copied.push(text) },
|
|
}), /permission denied/)
|
|
assert.deepEqual(copied, ['first\nsecond'])
|
|
})
|
|
|
|
test('other plugin bridge methods pass through without UI interception', async () => {
|
|
const result = await invokePluginViewAction('storage.list', {}, {
|
|
invoke: async (method) => ({ keys: method === 'storage.list' ? ['draft'] : [] }),
|
|
confirm: async () => { throw new Error('unexpected confirmation') },
|
|
copy: async () => { throw new Error('unexpected copy') },
|
|
})
|
|
assert.deepEqual(result, { keys: ['draft'] })
|
|
})
|
|
|
|
test('plugin view router preserves return state, handles dialogs, and prunes disabled views', () => {
|
|
const sidebar = { pluginId: 'vendor.tool', viewId: 'console', location: 'sidebar' }
|
|
const settings = { pluginId: 'vendor.tool', viewId: 'settings', location: 'settings' }
|
|
const tab = { pluginId: 'vendor.tool', viewId: 'metrics', location: 'vm.tab', vm: 'builtin.macos:mac' }
|
|
const dialog = { pluginId: 'vendor.tool', viewId: 'editor', location: 'dialog' }
|
|
let state = openPluginView(emptyPluginViewState, sidebar)
|
|
state = openPluginView(state, sidebar)
|
|
assert.equal(state.returnStack.length, 0, 'duplicate open must not push history')
|
|
state = openPluginView(state, settings)
|
|
assert.equal(state.route.viewId, 'settings')
|
|
assert.equal(state.returnStack.length, 1)
|
|
state = closePluginView(state).state
|
|
assert.equal(state.route.viewId, 'console')
|
|
state = openPluginView(state, tab)
|
|
state = closePluginView(state).state
|
|
assert.equal(state.route, null, 'VM tab closes to overview')
|
|
state = openPluginView(state, dialog)
|
|
state = openPluginView(state, dialog)
|
|
assert.equal(state.dialogs.length, 1, 'duplicate dialog only focuses the existing one')
|
|
state = closePluginView(state).state
|
|
assert.equal(state.dialogs.length, 0)
|
|
state = openPluginView(state, sidebar)
|
|
state = prunePluginViews(state, new Set(), () => false)
|
|
assert.deepEqual(state, emptyPluginViewState)
|
|
})
|
|
|
|
test('plugin event bus delivers only while subscribed', () => {
|
|
const received = []
|
|
const off = subscribePluginHostEvents((event, data) => { received.push([event, data]) })
|
|
publishPluginHostEvent('task.updated', { id: 'task-1' })
|
|
off()
|
|
publishPluginHostEvent('task.updated', { id: 'task-2' })
|
|
assert.deepEqual(received, [['task.updated', { id: 'task-1' }]])
|
|
})
|
|
|
|
test('plugin form, metadata, navigation, and subscriptions execute after host validation', async () => {
|
|
const steps = []
|
|
const view = { id: 'editor', pluginId: 'vendor.tool', location: 'dialog', title: 'Editor', entry: 'index.html' }
|
|
const handlers = {
|
|
invoke: async (method) => {
|
|
steps.push(['invoke', method])
|
|
if (method === 'ui.showForm') return { title: 'Edit', schema: { properties: {} }, values: { name: 'demo' } }
|
|
if (method === 'ui.openView') return view
|
|
if (method === 'ui.setTitle') return { title: 'Live' }
|
|
if (method === 'ui.setBadge') return { text: '2', tone: 'warn' }
|
|
if (method.startsWith('events.')) return { events: method === 'events.unsubscribe' ? [] : ['task.updated'] }
|
|
if (method === 'ui.navigate') return { page: 'settings' }
|
|
return { ok: true }
|
|
},
|
|
confirm: async () => { throw new Error('unexpected confirmation') },
|
|
showForm: async (request) => { steps.push(['form', request.values.name]); return { confirmed: true, values: request.values } },
|
|
copy: async () => { throw new Error('unexpected copy') },
|
|
setTitle: (title) => { steps.push(['title', title]) },
|
|
setBadge: (text, tone) => { steps.push(['badge', text, tone]) },
|
|
openView: (opened, vm) => { steps.push(['open', opened.id, vm]) },
|
|
close: async () => ({ closed: true }),
|
|
navigate: (target) => { steps.push(['navigate', target.page]) },
|
|
subscribe: (events) => { steps.push(['subscribe', ...events]) },
|
|
unsubscribe: (events) => { steps.push(['unsubscribe', events.length]) },
|
|
}
|
|
assert.deepEqual(await invokePluginViewAction('ui.showForm', {}, handlers), { confirmed: true, values: { name: 'demo' } })
|
|
assert.deepEqual(await invokePluginViewAction('ui.openView', { vm: 'vm-1' }, handlers), { ok: true })
|
|
assert.deepEqual(await invokePluginViewAction('ui.setTitle', {}, handlers), { title: 'Live' })
|
|
assert.deepEqual(await invokePluginViewAction('ui.setBadge', {}, handlers), { text: '2', tone: 'warn' })
|
|
assert.deepEqual(await invokePluginViewAction('events.subscribe', {}, handlers), { events: ['task.updated'] })
|
|
assert.deepEqual(await invokePluginViewAction('events.unsubscribe', {}, handlers), { events: [] })
|
|
assert.deepEqual(await invokePluginViewAction('ui.navigate', {}, handlers), { ok: true })
|
|
assert.deepEqual(steps, [
|
|
['invoke', 'ui.showForm'], ['form', 'demo'],
|
|
['invoke', 'ui.openView'], ['open', 'editor', 'vm-1'],
|
|
['invoke', 'ui.setTitle'], ['title', 'Live'],
|
|
['invoke', 'ui.setBadge'], ['badge', '2', 'warn'],
|
|
['invoke', 'events.subscribe'], ['subscribe', 'task.updated'],
|
|
['invoke', 'events.unsubscribe'], ['unsubscribe', 0],
|
|
['invoke', 'ui.navigate'], ['navigate', 'settings'],
|
|
])
|
|
})
|
|
|
|
test('closing a plugin view restores the page it was opened from', () => {
|
|
const sidebar = { pluginId: 'vendor.tool', viewId: 'console', location: 'sidebar', from: 'host' }
|
|
const tab = { pluginId: 'vendor.tool', viewId: 'metrics', location: 'vm.tab', vm: 'builtin.macos:mac', from: 'builtin.macos:mac' }
|
|
const settings = { pluginId: 'vendor.tool', viewId: 'settings', location: 'settings', from: 'builtin.macos:mac' }
|
|
|
|
const back = closePluginView(openPluginView(openPluginView(emptyPluginViewState, tab), settings))
|
|
assert.equal(back.state.route.viewId, 'metrics')
|
|
assert.equal(back.page, 'builtin.macos:mac', 'the VM must be selected again under its tab')
|
|
|
|
let state = openPluginView(openPluginView(emptyPluginViewState, { ...tab, viewId: 'other' }), tab)
|
|
state = closePluginView(state).state
|
|
state = closePluginView(openPluginView(state, sidebar)).state
|
|
assert.equal(state.route, null, 'a tab left through Overview must not reopen')
|
|
})
|
|
|
|
test('reopening a plugin dialog returns to it and keeps the ones below', () => {
|
|
const dialog = (viewId) => ({ pluginId: 'vendor.tool', viewId, location: 'dialog' })
|
|
let state = ['a', 'b', 'c'].reduce((current, id) => openPluginView(current, dialog(id)), emptyPluginViewState)
|
|
state = openPluginView(state, dialog('b'))
|
|
assert.deepEqual(state.dialogs.map((item) => item.viewId), ['a', 'b'])
|
|
})
|
|
|
|
test('view conditions match the state they name', () => {
|
|
assert.equal(matchViewCondition(undefined, {}), true)
|
|
assert.equal(matchViewCondition('default', {}), true)
|
|
assert.equal(matchViewCondition('vmState == running', { vmState: 'running' }), true)
|
|
assert.equal(matchViewCondition('vmState==running', { vmState: 'stopped' }), false)
|
|
assert.equal(matchViewCondition('unknown', {}), false)
|
|
})
|
|
|
|
test('plugin forms start from the displayed values and report empty required fields', () => {
|
|
const schema = { properties: {
|
|
mode: { type: 'string', enum: ['dev', 'prod'] },
|
|
count: { type: 'integer', minimum: 2 },
|
|
enabled: { type: 'boolean' },
|
|
label: { type: 'string', default: 'x' },
|
|
name: { type: 'string', required: true },
|
|
} }
|
|
const values = initialFormValues(schema, { label: 'given' })
|
|
assert.deepEqual(values, { mode: 'dev', count: 2, enabled: false, label: 'given', name: '' })
|
|
assert.deepEqual(missingFormFields(schema, values), ['name'])
|
|
assert.deepEqual(missingFormFields(schema, { ...values, name: 'demo' }), [])
|
|
})
|
|
|
|
test('closing a plugin page cancels only its unfinished tasks', async () => {
|
|
const canceled = []
|
|
let next = 0
|
|
const scope = createPluginTaskScope(async (method, params) => {
|
|
if (method === 'task.start') return { id: `task-${++next}` }
|
|
if (params.status === 'canceled') canceled.push(params.id)
|
|
return {}
|
|
})
|
|
await scope.invoke('task.start', {})
|
|
await scope.invoke('task.start', {})
|
|
await scope.invoke('task.finish', { id: 'task-1' })
|
|
const late = scope.invoke('task.start', {})
|
|
scope.close()
|
|
await late
|
|
assert.deepEqual(canceled, ['task-2', 'task-3'])
|
|
})
|