dsh-native-uia 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +431 -2
  3. package/client/client.js +81 -0
  4. package/cordis.patch.yml +11 -0
  5. package/icon.svg +11 -0
  6. package/locale/desktop/en.json +6 -0
  7. package/locale/desktop/zh.json +6 -0
  8. package/locale/en.json +6 -0
  9. package/locale/zh.json +6 -0
  10. package/package.json +80 -4
  11. package/scripts/bench-live.mjs +157 -0
  12. package/scripts/check-lease.mjs +308 -0
  13. package/scripts/install.mjs +154 -0
  14. package/scripts/lib/load-tools-source.mjs +24 -0
  15. package/scripts/measure-parse.mjs +77 -0
  16. package/scripts/regression-halt-latch.mjs +100 -0
  17. package/scripts/regression-quality.mjs +207 -0
  18. package/scripts/regression-tools.mjs +88 -0
  19. package/scripts/regression-worker.ps1 +1042 -0
  20. package/scripts/smoke.mjs +158 -0
  21. package/scripts/verify-capture-scale.mjs +37 -0
  22. package/scripts/verify-live-clipboard.mjs +108 -0
  23. package/scripts/verify-live.mjs +110 -0
  24. package/scripts/verify-locator.mjs +52 -0
  25. package/scripts/verify.mjs +1459 -0
  26. package/src/activity-window.js +212 -0
  27. package/src/dashboard.js +145 -0
  28. package/src/guard.js +191 -0
  29. package/src/index.js +377 -0
  30. package/src/native-uia-remote.js +80 -0
  31. package/src/panel-face.js +14 -0
  32. package/src/state.js +177 -0
  33. package/src/tools.js +1482 -0
  34. package/src/worker-client.js +246 -0
  35. package/web/control.html +1413 -0
  36. package/worker/bin/build-ocr.ps1 +41 -0
  37. package/worker/bin/ocr.cs +69 -0
  38. package/worker/bin/ocr.exe +0 -0
  39. package/worker/indicator-server.ps1 +1928 -0
  40. package/worker/ps-stdio.ps1 +168 -0
  41. package/worker/uia-worker.ps1 +4220 -0
@@ -0,0 +1,88 @@
1
+ import assert from 'node:assert/strict';
2
+ import * as fs from 'node:fs/promises';
3
+ import { dirname, join } from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ import { loadToolsSource } from './lib/load-tools-source.mjs';
6
+
7
+ // Exercise production tool definitions with a fake host and fake worker: no desktop input.
8
+ const root = dirname(dirname(fileURLToPath(import.meta.url)));
9
+ const registerTools = await loadToolsSource('registerTools');
10
+ const dataDir = join(root, '.test-output', 'tools');
11
+ await fs.mkdir(dataDir, { recursive: true });
12
+ function fixture(call) {
13
+ const tools = new Map();
14
+ const store = { running: false, windows: [], pointerMode: 'background', counters: {}, step() {}, actionDone() {}, setTree(nodes, meta) { this.tree = { nodes, ...meta }; }, setTarget(target) { this.target = target; } };
15
+ const dispose = registerTools({ tools: { register(def) { tools.set(def.name, def); return () => {}; } } }, { worker: { call, epoch: 1 }, store, dataDir, owned: new Map() });
16
+ return { tools, store, dispose, execute: (name, args) => tools.get(name).execute(args, { signal: new AbortController().signal }) };
17
+ }
18
+ let passed = 0;
19
+ async function test(name, fn) { await fn(); passed++; console.log(`PASS ${name}`); }
20
+ await test('failed prepare never commits or classifies as success', async () => {
21
+ const calls = [];
22
+ const f = fixture(async (cmd) => { calls.push(cmd); return { allResolved: false, descriptors: [{ index: 0, code: 'descriptor_unresolved', error: 'missing' }] }; });
23
+ try { const result = await f.execute('computer_run', { windowId: 'w1', goal: 'preflight', steps: [{ by: { name: 'missing' }, action: 'invoke' }] }); assert.match(result, /performedCount=0/); assert.match(result, /descriptor_unresolved/); assert.deepEqual(calls, ['prepare_batch']); } finally { f.dispose(); }
24
+ });
25
+ await test('replay stops on failed preflight', async () => {
26
+ const recipes = join(dataDir, 'recipes'); await fs.mkdir(recipes, { recursive: true });
27
+ await fs.writeFile(join(recipes, 'preflight.json'), JSON.stringify({ goal: 'preflight', window: { windowId: 'w1' }, steps: [{ by: { name: 'missing' }, action: 'invoke' }] }));
28
+ const calls = [];
29
+ const f = fixture(async (cmd) => { calls.push(cmd); return { allResolved: false, descriptors: [{ index: 0, code: 'descriptor_unresolved', error: 'missing' }] }; });
30
+ try { assert.match(await f.execute('computer_replay', { name: 'preflight' }), /performedCount=0/); assert.deepEqual(calls, ['prepare_batch']); } finally { f.dispose(); }
31
+ });
32
+ await test('OCR failure preserves current UIA refs and panel tree', async () => {
33
+ const f = fixture(async (cmd) => { if (cmd === 'snapshot') return { nodes: [{ ref: 'e9', controlType: 'Button', name: 'Submit', patterns: ['Invoke'] }], revision: 9, partialReason: ['provider unavailable'] }; throw Object.assign(new Error('no OCR engine'), { code: 'ocr_unavailable' }); });
34
+ try { const result = await f.execute('computer_parse', { windowId: 'w1' }); assert.match(result, /ocr_unavailable/); assert.match(result, /e9 Button/); assert.match(result, /provider unavailable/); assert.equal(f.store.tree.revision, 9); assert.equal(f.store.tree.nodes[0].ref, 'e9'); } finally { f.dispose(); }
35
+ });
36
+ await test('recipe retains substring, duplicate index and proximity selector', async () => {
37
+ const by = { controlType: 'Button', name: 'stable', nameExact: false, nth: 2, nearPoint: { x: 40, y: 70 } };
38
+ const f = fixture(async (cmd) => cmd === 'prepare_batch' ? { allResolved: true, revision: 2, descriptors: [{ name: 'truncated display', controlType: 'Button', ownerWindow: { windowId: 'w1' } }] } : { stoppedAt: null, performedCount: 1, results: [{ index: 0, action: 'invoke', status: 'passed', performed: true }] });
39
+ try { const result = await f.execute('computer_run', { windowId: 'w1', goal: 'selector-regression', steps: [{ by, action: 'invoke' }] }); const name = result.match(/已存配方:([^(\n]+)/)[1]; const recipe = JSON.parse(await fs.readFile(join(dataDir, 'recipes', name + '.json'), 'utf8')); assert.deepEqual(recipe.steps[0].by, by); } finally { f.dispose(); }
40
+ });
41
+ await test('verification failure reports delivered action without object-string noise', async () => {
42
+ const f = fixture(async (cmd) => cmd === 'prepare_batch' ? { allResolved: true, revision: 1, descriptors: [{ name: 'Submit', controlType: 'Button' }] } : { stoppedAt: 0, performedCount: 1, results: [{ index: 0, action: 'invoke', performed: true, status: 'verify_failed', verify: { type: 'element_appears', matched: false } }] });
43
+ try { const result = await f.execute('computer_run', { windowId: 'w1', goal: 'verify', steps: [{ by: { name: 'Submit' }, action: 'invoke' }] }); assert.match(result, /performedCount=1/); assert.match(result, /勿直接重试/); assert.match(result, /未命中\(element_appears\)/); assert.doesNotMatch(result, /\[object Object\]/); } finally { f.dispose(); }
44
+ });
45
+ await test('computer_snapshot depth is an optional integer forwarded as maxDepth', async () => {
46
+ const calls = [];
47
+ const f = fixture(async (cmd, body) => { calls.push({ cmd, body }); return { nodes: [], revision: 1 }; });
48
+ try {
49
+ const def = f.tools.get('computer_snapshot');
50
+ assert.ok(def, 'computer_snapshot is not registered');
51
+ // DSL shape: depth stays optional (omitted = worker default maxDepth 30) and integer-typed.
52
+ const depth = def.parameters.depth;
53
+ assert.ok(depth, 'depth parameter missing from the computer_snapshot schema');
54
+ assert.equal(depth.type, 'integer');
55
+ assert.notEqual(depth.required, true);
56
+ // Behaviour: an omitted depth must not smuggle a maxDepth into the worker call (the worker
57
+ // keeps its own default), while a given depth is forwarded verbatim.
58
+ await f.execute('computer_snapshot', { windowId: 'w1' });
59
+ assert.equal(calls.at(-1).body.maxDepth, undefined, 'omitted depth must not reach the worker as a maxDepth key');
60
+ await f.execute('computer_snapshot', { windowId: 'w1', depth: 5 });
61
+ assert.equal(calls.at(-1).body.maxDepth, 5);
62
+ } finally { f.dispose(); }
63
+ });
64
+ await test('computer_done declares done/blocked/failed endings distinctly', async () => {
65
+ const steps = [];
66
+ const f = fixture(async () => ({}));
67
+ f.store.step = (s) => steps.push(s);
68
+ try {
69
+ const done = await f.execute('computer_done', { summary: '打开记事本并输入内容' });
70
+ assert.match(done, /已完成并收起御屏指示层/);
71
+ assert.doesNotMatch(done, /受阻未完成|失败放弃/);
72
+ const blocked = await f.execute('computer_done', { outcome: 'blocked', reason: '通道权限不足,无法点击目标' });
73
+ assert.match(blocked, /受阻未完成/);
74
+ assert.match(blocked, /原因:通道权限不足,无法点击目标/);
75
+ assert.doesNotMatch(blocked, /已完成/, 'a blocked ending must not render as 已完成');
76
+ const failed = await f.execute('computer_done', { outcome: 'failed', reason: '目标窗口持续失焦' });
77
+ assert.match(failed, /失败放弃/);
78
+ assert.doesNotMatch(failed, /已完成/, 'a failed ending must not render as 已完成');
79
+ // The panel step carries the three statuses verbatim (control.html already colours
80
+ // blocked/failed as bad); only the done ending counts as passed.
81
+ assert.deepEqual(steps.map((s) => s.status), ['passed', 'blocked', 'failed']);
82
+ assert.deepEqual(steps.map((s) => s.label), ['任务完成,收起指示层', '受阻未完成,收起指示层', '失败放弃,收起指示层']);
83
+ // An unknown/omitted outcome falls back to done.
84
+ const fallback = await f.execute('computer_done', {});
85
+ assert.match(fallback, /已完成并收起御屏指示层/);
86
+ } finally { f.dispose(); }
87
+ });
88
+ console.log(`${passed}/${passed} tool regressions passed (no desktop actions)`);