@firstpick/pi-package-webui 0.7.6 → 0.7.8
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.
- package/README.md +60 -4
- package/bin/pi-webui-rpc-supervisor.mjs +612 -0
- package/bin/pi-webui.mjs +1051 -95
- package/lib/WEBUI_TUI_NATIVE_PARITY.json +13 -0
- package/lib/rpc-supervisor-client.mjs +317 -0
- package/lib/rpc-supervisor-protocol.mjs +245 -0
- package/lib/rpc-supervisor-state.mjs +165 -0
- package/lib/trust-boundaries.mjs +1 -0
- package/package.json +4 -4
- package/public/app.js +1986 -170
- package/public/index.html +388 -25
- package/public/issue-bot-client.mjs +344 -0
- package/public/issue-wizard-state.mjs +443 -0
- package/public/service-worker.js +8 -1
- package/public/styles.css +578 -52
- package/public/subagent-gate-visibility.mjs +36 -0
- package/public/workflow-status-stack.mjs +69 -0
- package/tests/app-runner-process-tree-harness.test.mjs +37 -3
- package/tests/boot-failure-diagnostics.test.mjs +180 -0
- package/tests/completion-signal-contract.test.mjs +156 -0
- package/tests/cwd-footer-reliability-static.test.mjs +107 -0
- package/tests/durable-rpc-supervisor-harness.test.mjs +275 -0
- package/tests/fast-mode-client-static.test.mjs +2 -2
- package/tests/fixtures/fake-pi.mjs +90 -6
- package/tests/footer-context-contrast-static.test.mjs +10 -0
- package/tests/git-panel-file-preview-static.test.mjs +221 -0
- package/tests/git-panel-render-stability-static.test.mjs +11 -1
- package/tests/http-endpoints-harness.test.mjs +458 -33
- package/tests/issue-bot-client.test.mjs +135 -0
- package/tests/issue-wizard-state.test.mjs +126 -0
- package/tests/mobile-static.test.mjs +79 -16
- package/tests/native-parity-harness.test.mjs +2 -1
- package/tests/native-parity.test.mjs +13 -2
- package/tests/numpad-decimal-input-static.test.mjs +128 -0
- package/tests/open-issue-wizard-static.test.mjs +75 -0
- package/tests/prompt-routing-input-clear-static.test.mjs +34 -0
- package/tests/prompt-routing-tab-status.test.mjs +108 -0
- package/tests/rpc-supervisor-host.test.mjs +408 -0
- package/tests/rpc-supervisor-protocol.test.mjs +93 -0
- package/tests/rpc-supervisor-state.test.mjs +62 -0
- package/tests/run-all.mjs +4 -1
- package/tests/streaming-ui-coupling.test.mjs +18 -2
- package/tests/subagent-gate-visibility.test.mjs +66 -0
- package/tests/subagent-launch-slots-static.test.mjs +1 -1
- package/tests/subagents-helper.test.mjs +136 -0
- package/tests/terminal-tab-workspace-static.test.mjs +99 -0
- package/tests/workflow-status-stack.test.mjs +87 -0
- package/webui-rpc-helper.mjs +160 -2
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import vm from "node:vm";
|
|
6
|
+
|
|
7
|
+
const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
8
|
+
const app = await readFile(join(root, "public", "app.js"), "utf8");
|
|
9
|
+
|
|
10
|
+
function functionSource(name, nextName) {
|
|
11
|
+
const start = app.indexOf(`function ${name}(`);
|
|
12
|
+
const end = app.indexOf(`\nfunction ${nextName}(`, start);
|
|
13
|
+
assert.ok(start >= 0 && end > start, `${name} should remain a standalone frontend helper`);
|
|
14
|
+
return app.slice(start, end);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class TestInputEvent {
|
|
18
|
+
constructor(type, init = {}) {
|
|
19
|
+
this.type = type;
|
|
20
|
+
Object.assign(this, init);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const context = { InputEvent: TestInputEvent };
|
|
25
|
+
vm.runInNewContext(
|
|
26
|
+
`${functionSource("insertNumpadDecimal", "renderMessages")}\nthis.insertNumpadDecimal = insertNumpadDecimal;`,
|
|
27
|
+
context,
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
function createInput(value, selectionStart, selectionEnd = selectionStart) {
|
|
31
|
+
return {
|
|
32
|
+
value,
|
|
33
|
+
selectionStart,
|
|
34
|
+
selectionEnd,
|
|
35
|
+
disabled: false,
|
|
36
|
+
readOnly: false,
|
|
37
|
+
inputEvents: [],
|
|
38
|
+
setRangeText(replacement, start, end, selectionMode) {
|
|
39
|
+
assert.equal(selectionMode, "end");
|
|
40
|
+
this.value = `${this.value.slice(0, start)}${replacement}${this.value.slice(end)}`;
|
|
41
|
+
this.selectionStart = start + replacement.length;
|
|
42
|
+
this.selectionEnd = this.selectionStart;
|
|
43
|
+
},
|
|
44
|
+
dispatchEvent(event) {
|
|
45
|
+
this.inputEvents.push(event);
|
|
46
|
+
return true;
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function createKeyEvent(input, overrides = {}) {
|
|
52
|
+
return {
|
|
53
|
+
currentTarget: input,
|
|
54
|
+
code: "NumpadDecimal",
|
|
55
|
+
key: ",",
|
|
56
|
+
keyCode: 0,
|
|
57
|
+
location: 3,
|
|
58
|
+
defaultPrevented: false,
|
|
59
|
+
ctrlKey: false,
|
|
60
|
+
metaKey: false,
|
|
61
|
+
altKey: false,
|
|
62
|
+
shiftKey: false,
|
|
63
|
+
isComposing: false,
|
|
64
|
+
preventDefault() { this.defaultPrevented = true; },
|
|
65
|
+
...overrides,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const localizedInput = createInput("12,34", 2, 3);
|
|
70
|
+
const localizedEvent = createKeyEvent(localizedInput);
|
|
71
|
+
assert.equal(context.insertNumpadDecimal(localizedEvent), true, "localized numpad decimal should be handled");
|
|
72
|
+
assert.equal(localizedInput.value, "12.34", "localized numpad decimal should insert a dot at the selection");
|
|
73
|
+
assert.equal(localizedInput.selectionStart, 3, "cursor should follow the inserted dot");
|
|
74
|
+
assert.equal(localizedEvent.defaultPrevented, true, "native localized insertion should be replaced");
|
|
75
|
+
assert.equal(localizedInput.inputEvents.length, 1, "replacement should emit one input event");
|
|
76
|
+
assert.equal(localizedInput.inputEvents[0].type, "input");
|
|
77
|
+
assert.equal(localizedInput.inputEvents[0].data, ".");
|
|
78
|
+
assert.equal(localizedInput.inputEvents[0].inputType, "insertText");
|
|
79
|
+
assert.equal(localizedInput.inputEvents[0].bubbles, true);
|
|
80
|
+
|
|
81
|
+
const reportedDotInput = createInput("12", 2);
|
|
82
|
+
const reportedDotEvent = createKeyEvent(reportedDotInput, { key: "." });
|
|
83
|
+
assert.equal(context.insertNumpadDecimal(reportedDotEvent), true, "numpad decimal should be handled even when the browser reports a dot");
|
|
84
|
+
assert.equal(reportedDotInput.value, "12.", "explicit handling should not depend on native numpad insertion");
|
|
85
|
+
assert.equal(reportedDotEvent.defaultPrevented, true);
|
|
86
|
+
|
|
87
|
+
for (const overrides of [
|
|
88
|
+
{ code: "", key: "Decimal", location: 3 },
|
|
89
|
+
{ code: "", key: "Unidentified", keyCode: 110, location: 0 },
|
|
90
|
+
]) {
|
|
91
|
+
const input = createInput("fallback", 8);
|
|
92
|
+
assert.equal(context.insertNumpadDecimal(createKeyEvent(input, overrides)), true, `legacy numpad identity should be handled: ${JSON.stringify(overrides)}`);
|
|
93
|
+
assert.equal(input.value, "fallback.");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
for (const overrides of [
|
|
97
|
+
{ code: "Period", key: ".", location: 0 },
|
|
98
|
+
{ ctrlKey: true },
|
|
99
|
+
{ metaKey: true },
|
|
100
|
+
{ altKey: true },
|
|
101
|
+
{ shiftKey: true },
|
|
102
|
+
{ isComposing: true },
|
|
103
|
+
{ defaultPrevented: true },
|
|
104
|
+
]) {
|
|
105
|
+
const input = createInput("unchanged", 4);
|
|
106
|
+
const event = createKeyEvent(input, overrides);
|
|
107
|
+
assert.equal(context.insertNumpadDecimal(event), false, `unrelated or modified key should not be handled: ${JSON.stringify(overrides)}`);
|
|
108
|
+
assert.equal(input.value, "unchanged");
|
|
109
|
+
assert.equal(input.inputEvents.length, 0);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const readOnlyInput = createInput("read only", 4);
|
|
113
|
+
readOnlyInput.readOnly = true;
|
|
114
|
+
assert.equal(context.insertNumpadDecimal(createKeyEvent(readOnlyInput)), false, "read-only fields should remain untouched");
|
|
115
|
+
assert.equal(readOnlyInput.value, "read only");
|
|
116
|
+
|
|
117
|
+
assert.match(
|
|
118
|
+
app,
|
|
119
|
+
/function renderAppRunnerInputForm\(run\)[\s\S]*input\.addEventListener\("keydown", \(event\) => \{\s*if \(insertNumpadDecimal\(event\)\) return;/,
|
|
120
|
+
"app-runner stdin should apply the numpad decimal helper",
|
|
121
|
+
);
|
|
122
|
+
assert.match(
|
|
123
|
+
app,
|
|
124
|
+
/elements\.promptInput\.addEventListener\("keydown", \(event\) => \{\s*if \(event\.defaultPrevented \|\| insertNumpadDecimal\(event\)\) return;/,
|
|
125
|
+
"main prompt should apply the numpad decimal helper",
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
console.log("numpad decimal input static tests passed");
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
|
|
4
|
+
const [app, html, css, stateModule, botClient, server, serviceWorker, packageJson] = await Promise.all([
|
|
5
|
+
readFile(new URL("../public/app.js", import.meta.url), "utf8"),
|
|
6
|
+
readFile(new URL("../public/index.html", import.meta.url), "utf8"),
|
|
7
|
+
readFile(new URL("../public/styles.css", import.meta.url), "utf8"),
|
|
8
|
+
readFile(new URL("../public/issue-wizard-state.mjs", import.meta.url), "utf8"),
|
|
9
|
+
readFile(new URL("../public/issue-bot-client.mjs", import.meta.url), "utf8"),
|
|
10
|
+
readFile(new URL("../bin/pi-webui.mjs", import.meta.url), "utf8"),
|
|
11
|
+
readFile(new URL("../public/service-worker.js", import.meta.url), "utf8"),
|
|
12
|
+
readFile(new URL("../package.json", import.meta.url), "utf8"),
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
assert.match(app, /from "\.\/issue-wizard-state\.mjs"/, "the browser controller must import the approved pure wizard module");
|
|
16
|
+
assert.match(app, /from "\.\/issue-bot-client\.mjs"/, "network behavior must live in a separate issue-bot client module");
|
|
17
|
+
assert.match(app, /createIssueWizardCatalog\(OPTIONAL_FEATURES\.map\(\(feature\) => feature\.label\)\)/, "optional feature labels must come from the existing catalog");
|
|
18
|
+
assert.match(app, /page\.hidden = step !== issueWizardState\.step/, "the controller must expose exactly one wizard page at a time");
|
|
19
|
+
assert.match(app, /action === "select-template"[\s\S]*item\.fields\.map\(\(field\) => field\.label\)/, "template choices must preview structured fields");
|
|
20
|
+
assert.match(app, /function focusIssueWizardChoice\([\s\S]*target\?\.focus/, "rerendered selections must preserve keyboard focus");
|
|
21
|
+
assert.match(app, /button\.setAttribute\("aria-pressed", selected \? "true" : "false"\)/, "selection buttons must keep toggle semantics");
|
|
22
|
+
assert.doesNotMatch(app, /setAttribute\("role", "radio(?:group)?"\)/, "the wizard must not advertise unimplemented radio keyboard semantics");
|
|
23
|
+
assert.match(app, /structuredIssueWizardState\(\)[\s\S]*?categoryId:[\s\S]*?fields:/, "the adapter must submit structured wizard state");
|
|
24
|
+
assert.match(app, /buildIssuePayload\(issueWizardState, issueWizardCatalog\)[\s\S]*?never send title\/body to the gateway/, "the canonical builder remains the review authority without becoming a network payload");
|
|
25
|
+
assert.doesNotMatch(app.match(/function structuredIssueWizardState\(\)[\s\S]*?\n}/)?.[0] || "", /\btitle\b|\bbody\b/, "structured submission must not add editable title/body fields");
|
|
26
|
+
assert.match(app, /new AbortController\(\)/, "submission and refresh calls must support cancellation");
|
|
27
|
+
assert.match(app, /issueWizardDialog\.addEventListener\("close", \(\) => clearIssueBotSubmission\(\{ abort: true \}\)\)/, "closing the dialog must abort the active request");
|
|
28
|
+
assert.match(app, /issueBotRequestActive/, "the wizard must prevent duplicate in-flight submissions");
|
|
29
|
+
assert.match(app, /issueBotSubmissionHandle\.refresh/, "bounded polling must retain an in-memory manual refresh path");
|
|
30
|
+
assert.match(app, /reasonCode === "sensitive_content"/, "sensitive outcomes must route to private guidance without reading submitted prose");
|
|
31
|
+
assert.doesNotMatch(app, /submitIssueToGithubBot/, "the old unavailable state-module seam must not handle browser network submission");
|
|
32
|
+
assert.match(stateModule, /export async function submitIssueToGithubBot[\s\S]*?status: "unavailable"/, "the pure state-module fallback remains offline by default");
|
|
33
|
+
assert.match(stateModule, /description: "[^"]+"[\s\S]*?fields:/, "template definitions must retain preview descriptions");
|
|
34
|
+
assert.match(app, /issueClipboardText\(payload\)/, "copy must include the complete title and body payload");
|
|
35
|
+
assert.doesNotMatch(stateModule.match(/export async function submitIssueToGithubBot[\s\S]*?\n}/)?.[0] || "", /\bfetch\s*\(/, "the pure fallback must perform no network I/O");
|
|
36
|
+
|
|
37
|
+
assert.match(botClient, /ISSUE_BOT_DEFAULT_RUNTIME_CONFIG[\s\S]*?enabled: false/, "public runtime configuration must be disabled by default");
|
|
38
|
+
assert.match(botClient, /schemaVersion: 1, idempotencyKey, turnstileToken, issue/, "admission must use the exact versioned structured envelope");
|
|
39
|
+
assert.match(botClient, /credentials: "omit"/, "browser requests must not send ambient credentials");
|
|
40
|
+
assert.match(botClient, /authorization: `Bearer \$\{statusToken\}`/, "only the status capability may authorize polling");
|
|
41
|
+
assert.match(botClient, /maxPollDurationMs = DEFAULT_MAX_POLL_DURATION_MS/, "polling must have a documented bounded duration");
|
|
42
|
+
assert.match(botClient, /Math\.min\(MAX_POLL_AFTER_MS, Math\.max\(delayMs \* 2/, "polling must apply bounded exponential backoff");
|
|
43
|
+
assert.match(botClient, /STATUS_TOKEN/, "the capability token must be shape-validated before use");
|
|
44
|
+
assert.doesNotMatch(botClient, /localStorage|sessionStorage|document\.cookie/, "the client must not persist drafts or capability tokens");
|
|
45
|
+
assert.doesNotMatch(botClient, /OPENAI_API_KEY|GITHUB_APP_|PRIVATE_KEY/, "the browser client must not contain service credentials");
|
|
46
|
+
|
|
47
|
+
assert.match(html, /__PI_WEBUI_ISSUE_BOT_CONFIG__[\s\S]*?enabled: false/, "the page must expose only disabled-by-default public runtime configuration");
|
|
48
|
+
assert.match(html, /id="openIssueButton"[\s\S]*?aria-controls="issueWizardDialog"/, "the persistent Control Deck action must target the dialog");
|
|
49
|
+
assert.match(html, /<footer class="side-panel-footer">[\s\S]*?id="openIssueButton"/, "the action must stay outside scrolling Control Deck content");
|
|
50
|
+
assert.match(html, /<dialog id="issueWizardDialog"[\s\S]*?aria-labelledby="issueWizardTitle"[\s\S]*?aria-describedby="issueWizardDescription"/, "the dialog must have an accessible name and description");
|
|
51
|
+
for (const step of [1, 2, 3, 4, 5]) assert.match(html, new RegExp(`data-issue-wizard-page="${step}"`), `step ${step} markup must exist`);
|
|
52
|
+
assert.match(html, /id="issueWizardStatus"[^>]*role="status"[^>]*aria-live="polite"/, "wizard progress must retain its accessible live region");
|
|
53
|
+
assert.doesNotMatch(html, /id="issueWizardProgress"[^>]*aria-live/, "the progress label must not duplicate live announcements");
|
|
54
|
+
assert.match(html, /id="issueWizardSubmissionStatus"[^>]*role="status"[^>]*aria-live="polite"/, "submission state needs a persistent dialog live region");
|
|
55
|
+
assert.match(html, /id="issueWizardSubmissionLink"[^>]*target="_blank"[^>]*rel="noopener noreferrer"/, "confirmed issue links must prevent opener access");
|
|
56
|
+
assert.match(html, /id="issueWizardSecurityLink"[^>]*target="_blank"[^>]*rel="noopener noreferrer"/, "private security guidance links must be safe");
|
|
57
|
+
assert.match(html, /id="issueWizardBotButton"[^>]*disabled[^>]*>Send</, "the bot control must be disabled until safe runtime configuration enables it");
|
|
58
|
+
assert.match(html, /id="issueWizardCopyButton"[^>]*>Copy complete issue</, "copy fallback must remain available");
|
|
59
|
+
|
|
60
|
+
assert.match(css, /\.side-panel-footer\s*\{[\s\S]*justify-content:\s*flex-end/, "the Control Deck action must anchor at the footer's right edge");
|
|
61
|
+
assert.match(css, /\.issue-wizard-dialog\s*\{[\s\S]*max-height:/, "the dialog must bound its height");
|
|
62
|
+
assert.match(css, /\.issue-wizard-pages\s*\{[\s\S]*overflow:\s*auto/, "wizard page content must remain scrollable");
|
|
63
|
+
assert.match(css, /\.issue-wizard-choice-description,[\s\S]*\.issue-wizard-choice-preview/, "template descriptions and field previews need dedicated styling");
|
|
64
|
+
assert.match(css, /\.issue-wizard-submission-status\s*\{/, "persistent bot status needs dedicated styling");
|
|
65
|
+
assert.match(css, /@media \(max-width: 720px\)[\s\S]*\.issue-wizard-choice-grid/, "wizard choices must adapt for mobile");
|
|
66
|
+
|
|
67
|
+
assert.match(server, /"issue-wizard-state\.mjs"/, "the static allowlist must continue serving the pure wizard module");
|
|
68
|
+
assert.match(server, /"issue-bot-client\.mjs"/, "the static allowlist must serve the browser client module");
|
|
69
|
+
assert.match(serviceWorker, /"\/issue-wizard-state\.mjs"/, "the PWA shell must keep caching the pure wizard module");
|
|
70
|
+
assert.match(serviceWorker, /"\/issue-bot-client\.mjs"/, "the PWA shell must cache the browser client module");
|
|
71
|
+
assert.match(serviceWorker, /pi-webui-pwa-v48/, "the PWA cache name must change with the new static module");
|
|
72
|
+
assert.match(packageJson, /node --check public\/issue-wizard-state\.mjs/, "the package check must keep syntax-checking the pure wizard module");
|
|
73
|
+
assert.match(packageJson, /node --check public\/issue-bot-client\.mjs/, "the package check must syntax-check the browser client module");
|
|
74
|
+
|
|
75
|
+
console.log("open-issue-wizard-static.test.mjs passed");
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
7
|
+
const app = await readFile(join(root, "public", "app.js"), "utf8");
|
|
8
|
+
|
|
9
|
+
function functionSource(name, nextName) {
|
|
10
|
+
const start = app.indexOf(`function ${name}(`);
|
|
11
|
+
const end = app.indexOf(`\nfunction ${nextName}(`, start);
|
|
12
|
+
assert.ok(start >= 0 && end > start, `${name} should remain a standalone frontend helper`);
|
|
13
|
+
return app.slice(start, end);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const clearSource = functionSource("clearPromptInputForRouting", "restorePromptInputAfterRoutingError");
|
|
17
|
+
assert.match(clearSource, /if \(!usesPromptInput\) return;/, "programmatic prompt sends should preserve the visible draft");
|
|
18
|
+
assert.match(clearSource, /tabDrafts\.set\(targetTabId, ""\);[\s\S]*elements\.promptInput\.value = "";[\s\S]*resizePromptInput\(\);[\s\S]*hideCommandSuggestions\(\);/, "input-backed sends should immediately clear their tab draft, textarea, and stale suggestions");
|
|
19
|
+
|
|
20
|
+
const sendSource = functionSource("sendPrompt", "hasQueuedDialogRequest");
|
|
21
|
+
const clearIndex = sendSource.indexOf("clearPromptInputForRouting({ usesPromptInput, targetTabId, tabContext });");
|
|
22
|
+
const routingIndex = sendSource.indexOf("if (startsRun) {");
|
|
23
|
+
const requestIndex = sendSource.indexOf('await api("/api/prompt"');
|
|
24
|
+
assert.ok(clearIndex >= 0, "sendPrompt should clear the captured input-backed draft");
|
|
25
|
+
assert.ok(routingIndex >= 0 && clearIndex < routingIndex, "the submitted prompt should be cleared before routing progress starts");
|
|
26
|
+
assert.ok(requestIndex >= 0 && clearIndex < requestIndex, "the submitted prompt should be cleared before the prompt request starts");
|
|
27
|
+
assert.match(sendSource, /catch \(error\) \{\n\s+restorePromptInputAfterRoutingError\(inputMessage,/, "a routing failure should restore the sent input when no replacement draft exists");
|
|
28
|
+
|
|
29
|
+
const successStart = sendSource.indexOf("applyResponseTab(response);");
|
|
30
|
+
const catchStart = sendSource.indexOf("} catch (error)", successStart);
|
|
31
|
+
assert.ok(successStart >= 0 && catchStart > successStart, "sendPrompt should keep a bounded success continuation");
|
|
32
|
+
assert.doesNotMatch(sendSource.slice(successStart, catchStart), /elements\.promptInput\.value = "";/, "a delayed routing response must not clear a newer draft");
|
|
33
|
+
|
|
34
|
+
console.log("prompt routing input-clear static checks passed");
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import vm from "node:vm";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
|
|
7
|
+
const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
8
|
+
const app = await readFile(join(root, "public", "app.js"), "utf8");
|
|
9
|
+
|
|
10
|
+
function functionSource(name) {
|
|
11
|
+
const start = app.indexOf(`function ${name}(`);
|
|
12
|
+
assert.ok(start >= 0, `${name} should exist`);
|
|
13
|
+
const next = app.slice(start + 1).match(/\nfunction [A-Za-z0-9_$]+\(/);
|
|
14
|
+
const end = next ? start + 1 + next.index : app.length;
|
|
15
|
+
return app.slice(start, end);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const context = vm.createContext({ console });
|
|
19
|
+
vm.runInContext(`
|
|
20
|
+
let activeTabId = "tab-1";
|
|
21
|
+
let tabs = [{
|
|
22
|
+
id: "tab-1",
|
|
23
|
+
activity: {
|
|
24
|
+
status: "done",
|
|
25
|
+
isWorking: false,
|
|
26
|
+
completionSerial: 2,
|
|
27
|
+
lastStartedAt: "2020-01-01T00:00:00.000Z",
|
|
28
|
+
lastChangedAt: "2020-01-01T00:00:01.000Z",
|
|
29
|
+
},
|
|
30
|
+
}];
|
|
31
|
+
let tabActivities = new Map();
|
|
32
|
+
const promptRoutingTabs = new Set();
|
|
33
|
+
let tabSeenCompletionSerials = new Map();
|
|
34
|
+
function scheduleTabsRender() {}
|
|
35
|
+
function suppressPendingAgentDoneNotificationsForTab() {}
|
|
36
|
+
|
|
37
|
+
${functionSource("normalizeTabActivity")}
|
|
38
|
+
${functionSource("tabActivityStateChanged")}
|
|
39
|
+
${functionSource("setTabActivity")}
|
|
40
|
+
${functionSource("activityForTab")}
|
|
41
|
+
${functionSource("markTabWorkingLocally")}
|
|
42
|
+
${functionSource("markTabIdleLocally")}
|
|
43
|
+
${functionSource("markTabDoneLocally")}
|
|
44
|
+
|
|
45
|
+
globalThis.activityApi = {
|
|
46
|
+
startRouting() {
|
|
47
|
+
promptRoutingTabs.add("tab-1");
|
|
48
|
+
return markTabWorkingLocally("tab-1");
|
|
49
|
+
},
|
|
50
|
+
finishRouting() {
|
|
51
|
+
promptRoutingTabs.delete("tab-1");
|
|
52
|
+
},
|
|
53
|
+
ingest(activity) {
|
|
54
|
+
return setTabActivity("tab-1", activity);
|
|
55
|
+
},
|
|
56
|
+
markDone() {
|
|
57
|
+
return markTabDoneLocally("tab-1");
|
|
58
|
+
},
|
|
59
|
+
current() {
|
|
60
|
+
return tabActivities.get("tab-1");
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
`, context);
|
|
64
|
+
|
|
65
|
+
const api = context.activityApi;
|
|
66
|
+
api.ingest(context.tabs?.[0]?.activity || {
|
|
67
|
+
status: "done",
|
|
68
|
+
isWorking: false,
|
|
69
|
+
completionSerial: 2,
|
|
70
|
+
lastStartedAt: "2020-01-01T00:00:00.000Z",
|
|
71
|
+
lastChangedAt: "2020-01-01T00:00:01.000Z",
|
|
72
|
+
});
|
|
73
|
+
assert.equal(api.startRouting(), true);
|
|
74
|
+
const routingActivity = api.current();
|
|
75
|
+
assert.equal(routingActivity.status, "working");
|
|
76
|
+
assert.equal(routingActivity.isWorking, true);
|
|
77
|
+
assert.notEqual(routingActivity.lastStartedAt, "2020-01-01T00:00:00.000Z", "optimistic routing should start a fresh reconciliation grace period");
|
|
78
|
+
assert.equal(routingActivity.lastChangedAt, routingActivity.lastStartedAt);
|
|
79
|
+
|
|
80
|
+
const staleDone = {
|
|
81
|
+
status: "done",
|
|
82
|
+
isWorking: false,
|
|
83
|
+
completionSerial: 3,
|
|
84
|
+
lastCompletedAt: new Date().toISOString(),
|
|
85
|
+
};
|
|
86
|
+
assert.equal(api.ingest(staleDone).isWorking, true, "an idle/done snapshot must not replace active routing");
|
|
87
|
+
assert.equal(api.markDone(), false, "idle state reconciliation must not synthesize completion during routing");
|
|
88
|
+
assert.equal(api.current().completionSerial, 2, "routing must not create a false completion serial");
|
|
89
|
+
|
|
90
|
+
api.finishRouting();
|
|
91
|
+
assert.equal(api.markDone(), true, "idle reconciliation should leave a submitted-but-never-started prompt idle");
|
|
92
|
+
assert.equal(api.current().status, "idle");
|
|
93
|
+
assert.equal(api.current().completionSerial, 2, "a prompt that never emitted agent_start must not synthesize completion");
|
|
94
|
+
assert.equal(api.ingest(staleDone).status, "done", "authoritative completion remains accepted after routing finishes");
|
|
95
|
+
|
|
96
|
+
const sendSource = functionSource("sendPrompt");
|
|
97
|
+
const routingAdd = sendSource.indexOf("promptRoutingTabs.add(targetTabId);");
|
|
98
|
+
const workingMark = sendSource.indexOf("markTabWorkingLocally(targetTabId);");
|
|
99
|
+
const responseApply = sendSource.indexOf("applyResponseTab(response);");
|
|
100
|
+
const successDelete = sendSource.indexOf("promptRoutingTabs.delete(targetTabId);", responseApply);
|
|
101
|
+
const catchStart = sendSource.indexOf("} catch (error)", successDelete);
|
|
102
|
+
const failureDelete = sendSource.indexOf("promptRoutingTabs.delete(targetTabId);", catchStart);
|
|
103
|
+
const idleMark = sendSource.indexOf("markTabIdleLocally(targetTabId);", catchStart);
|
|
104
|
+
assert.ok(routingAdd >= 0 && routingAdd < workingMark, "fresh prompts should enter routing protection before rendering working state");
|
|
105
|
+
assert.ok(responseApply >= 0 && successDelete > responseApply && successDelete < catchStart, "successful routing protection should end only after applying the server handoff response");
|
|
106
|
+
assert.ok(failureDelete > catchStart && failureDelete < idleMark, "failed routing should clear protection before restoring idle state");
|
|
107
|
+
|
|
108
|
+
console.log("prompt routing tab-status checks passed");
|