47 lines
2.6 KiB
JavaScript
47 lines
2.6 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { ComputerUseSession } from '../computer-use/session.js';
|
|
import { ComputerActuator } from '../computer-use/actuator.js';
|
|
import { ComputerAudit } from '../computer-use/audit.js';
|
|
import { mkdtemp, readFile } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import path from 'node:path';
|
|
|
|
test('computer use requires an explicit grant and enforces its step budget', () => {
|
|
let now = 1000;
|
|
const session = new ComputerUseSession({ stepsMax: 2, clock: () => now });
|
|
assert.throws(() => session.beginStep(), /inactive/);
|
|
assert.deepEqual(session.grant({ persist: true }).restore_token_present, true);
|
|
session.beginStep();
|
|
session.beginStep();
|
|
assert.throws(() => session.beginStep(), /budget/);
|
|
assert.equal(session.status().steps_used, 2);
|
|
session.revoke();
|
|
assert.equal(session.status().active, false);
|
|
now += 1;
|
|
});
|
|
|
|
test('computer use expires its wall clock grant', () => {
|
|
let now = 0;
|
|
const session = new ComputerUseSession({ clock: () => now });
|
|
session.grant();
|
|
now = 180001;
|
|
assert.throws(() => session.beginStep(), /expired/);
|
|
assert.equal(session.status().backend, 'none');
|
|
});
|
|
|
|
test('computer use semantic actuation previews, budgets, and audits actions', async () => {
|
|
const dir = await mkdtemp(path.join(tmpdir(), 'jarvis-cu-audit-'));
|
|
const audit = new ComputerAudit({ dir }); const session = new ComputerUseSession({ stepsMax: 1, audit }); const sent = [];
|
|
session.grant();
|
|
const actuator = new ComputerActuator({ session, input: { send: (event) => sent.push(event) }, find: async ({ ref }) => [{ ref, name: 'Save', role: 'push button', rect: [1, 2, 3, 4] }], atspiAction: async () => ({ semantic: true }), audit, sleep: async () => {} });
|
|
const result = await actuator.click({ ref: 'r1' }); assert.equal(result.ok, true); assert.equal(session.status().steps_used, 1);
|
|
assert.deepEqual(sent, []); const files = await (await import('node:fs/promises')).readdir(dir); assert.equal(files.length, 1); assert.match(await readFile(path.join(dir, files[0]), 'utf8'), /target_hash/);
|
|
});
|
|
|
|
test('computer use refuses password targets and unconfirmed dangerous keys', async () => {
|
|
const session = new ComputerUseSession(); session.grant(); const actuator = new ComputerActuator({ session, input: { send() {} }, find: async () => [{ name: 'Password', role: 'password text' }], sleep: async () => {} });
|
|
await assert.rejects(() => actuator.type({ ref: 'password', text: 'secret' }), /password/);
|
|
await assert.rejects(() => actuator.key({ combo: 'alt+f4' }), /confirmation/);
|
|
});
|