Verify window identity boundaries and launch discovery admission

This commit is contained in:
creatixchu committed 2026-10-25 21:04:25 +08:00
1 parent a818d8fed1
commit 08da24ee83
2 files changed
+63 -1

No files matched your search

@@ -36,6 +36,7 @@ it('binds all observation and keyboard operations to a selected window and suppr
await window.getAXState()
expect(f.text).toHaveBeenCalledWith('AX window')
await window.click(0)
await window.activate()
await window.typeText('hello')
await window.paste('world')
await window.pressKey('ctrl+a')
@@ -61,3 +62,19 @@ it('rejects mismatched selections before reading another window', async () => {
await expect(f.api.getWindows([7])).rejects.toThrow('does not match')
expect(f.dispatch).toHaveBeenCalledTimes(1)
})
it('supports discovery without a sink and suppresses requested emissions', async () => {
const f = fixture()
await f.api.listWindows({ emit: false })
expect(f.text).not.toHaveBeenCalled()
await createCuaWindowApi(f.dispatch).listWindows()
const capabilities = { platform: 'windows', permissions: { accessibility: true, screenRecording: true }, sessionLocked: false, methods: [], coordinateSpace: 'screenshot-pixels', browser: false }
f.dispatch.mockResolvedValueOnce(capabilities)
expect(await f.api.getCapabilities()).toEqual(capabilities)
})
it('rejects incomplete native selections without observing any window', async () => {
const f = fixture()
f.dispatch.mockResolvedValueOnce([])
await expect(f.api.getWindows([7])).rejects.toThrow('does not match')
expect(f.dispatch).toHaveBeenCalledTimes(1)
})
@@ -37,7 +37,7 @@ async function setup(choice = 'Allow for this session') {
screenshotTimeoutMs: 1000, axTimeoutMs: 100, maxRequestBytes: 4096, maxResponseBytes: 4096, graceMs: 100,
textEventIntervalMs: 0, maxClickHoldMs: 5000 }, () => { throw new Error('Mock client must not connect') })
const windows = new CuaWindowExecution()
const dispatch = vi.fn(async (_request: import('../src/types.ts').CuaOperation, _signal: AbortSignal) => ({ dispatched: true, verified: false }))
const dispatch = vi.fn<(request: import('../src/types.ts').CuaOperation, signal: AbortSignal) => Promise<unknown>>(async () => ({ dispatched: true, verified: false }))
const run = (method: string, params: object = {}) => windows.run(WindowRequest.parse({ method, params }),
owner.agent, client, access, new AbortController().signal, undefined, 20, dispatch)
return { access, run, dispatch, answer }
@@ -77,3 +77,48 @@ it('rejects duplicate IDs and mismatched native selections before asking for con
await expect(f.run('getWindows', { ids: [7] })).rejects.toThrow('NATIVE_PROTOCOL')
expect(f.answer).not.toHaveBeenCalled()
})
it('discovers without permission and preserves both Shell and explicit-path metadata', async () => {
const f = await setup()
await expect(f.run('getCapabilities')).resolves.toEqual({ dispatched: true, verified: false })
mocked.request.mockResolvedValueOnce([])
await expect(f.run('listWindows')).resolves.toEqual([])
const entries = [{ id: 'shell', displayName: 'Settings', kind: 'shell' },
{ id: 'exe', displayName: 'Editor', kind: 'executable', path: 'C:\\Editor.exe' }]
mocked.request.mockResolvedValueOnce(entries)
await expect(f.run('findApps', { query: 'e' })).resolves.toEqual(entries)
expect(mocked.request).toHaveBeenLastCalledWith('findApps', { query: 'e', maxAppMatches: 20 }, expect.any(AbortSignal), undefined)
expect(f.answer).not.toHaveBeenCalled()
})
it('returns only the selected observation and rejects changes of handle, PID or window', async () => {
const f = await setup()
mocked.request.mockResolvedValueOnce([{ appHandle: 'window-a', windowId: 7, pid: 10, authorization: identity }])
await f.run('getWindows', { ids: [7] })
const state = { appHandle: 'window-a', pid: 10, windowId: 7, tree: 'AX' }
f.dispatch.mockResolvedValueOnce(state)
await expect(f.run('getState', { windowHandle: 'window-a' })).resolves.toEqual({ windowHandle: 'window-a', pid: 10, windowId: 7, tree: 'AX' })
for (const change of [{ appHandle: 'other' }, { pid: 11 }, { windowId: 8 }]) {
f.dispatch.mockResolvedValueOnce({ ...state, ...change })
await expect(f.run('getState', { windowHandle: 'window-a' })).rejects.toThrow('NATIVE_PROTOCOL')
}
for (const [method, params] of [
['show', {}], ['screenshot', {}],
['drag', { fromX: 1, fromY: 2, toX: 3, toY: 4 }],
['click', { elementIndex: 1, snapshotId: 's1' }],
['typeText', { text: 'hello' }],
] as const) {
await f.run(method, { windowHandle: 'window-a', ...params })
const operation = f.dispatch.mock.calls.at(-1)![0]
expect(operation.params).toMatchObject({ appHandle: 'window-a', ...params })
if (method === 'show' || method === 'screenshot' || method === 'drag') expect(operation.params).toHaveProperty('windowId', 7)
else expect(operation.params).not.toHaveProperty('windowId')
}
})
it('rejects incomplete selection and reused native handles before consent', async () => {
const f = await setup()
mocked.request.mockResolvedValueOnce([])
await expect(f.run('getWindows', { ids: [7] })).rejects.toThrow('NATIVE_PROTOCOL')
mocked.request.mockResolvedValueOnce([7, 8].map(windowId => ({ appHandle: 'same', windowId, pid: 10, authorization: identity })))
await expect(f.run('getWindows', { ids: [7, 8] })).rejects.toThrow('NATIVE_PROTOCOL')
expect(f.answer).not.toHaveBeenCalled()
})