// Tests for plan2code-app/ui/meter.js and the app bar's own shape. Run with: // node --test scripts/test-app-meter.mjs // // app.js itself cannot be imported here (it touches `document` and // `window.__TAURI__` at module scope), which is why the bar's pure decisions // live in their own module and the rest is checked as plain text, the same // way the web console's own "shape" tests work. import test from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; import path from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; import { helloRetryAction, retryLabel, RETRY_AFTER_SECONDS, attentionSignal, barNextSteps, continueKeys, defaultRightFraction, folderName, folderPickerRows, pickDefaultAgent, taskbarSwitch, windowTitleFor, deadConsoleAction, loopBanner, loopRunText, loopLocks, loopStopLabels, loopWindowTitle, CONSOLE_ASIDE_BREAKPOINT_PX, NEXT_STEP_LABELS, NEW_RELEASE_COMMAND } from "../plan2code-app/ui/meter.js"; import { findBrowser, launchBrowser } from "./browser.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const INDEX_HTML_PATH = path.join(HERE, "../plan2code-app/ui/index.html"); const APP_JS = fs.readFileSync(path.join(HERE, "../plan2code-app/ui/app.js"), "utf8"); const INDEX_HTML = fs.readFileSync(INDEX_HTML_PATH, "utf8"); test("barNextSteps offers nothing while the console runs and the versions agree", () => { assert.deepEqual(barNextSteps(), []); assert.deepEqual(barNextSteps({ consoleDead: false, version: null }), []); }); test("barNextSteps puts the most pressing step first: a done update, then a stopped console, then the skills", () => { const ids = (input) => barNextSteps(input).map((step) => step.id); assert.deepEqual(ids({ consoleDead: true }), ["console-dead"]); assert.deepEqual(ids({ version: { kind: "mismatch", skills: "3.0.0", app: "3.1.0" } }), ["mismatch"]); assert.deepEqual(ids({ consoleDead: true, version: { kind: "mismatch", skills: "3.0.0", app: "3.1.0" } }), ["console-dead", "mismatch"]); assert.deepEqual(ids({ consoleDead: true, version: { kind: "too-old" } }), ["console-dead", "too-old"]); assert.deepEqual(ids({ version: { kind: "updated" } }), ["updated"]); }); test("only one control for reconnecting the console is ever offered: after an update, its Reconnect covers a stopped console too", () => { const steps = barNextSteps({ consoleDead: true, version: { kind: "updated" } }); assert.deepEqual(steps.map((step) => step.action), ["restart-console"], "the plain reconnect steps aside for the one that stops the old console first"); }); test("a newer GitHub release is offered last, with the command to copy; a mismatch or a stopped console comes first", () => { const newRelease = { latest: "3.0.1" }; const ids = (input) => barNextSteps(input).map((step) => step.id); assert.deepEqual(ids({ newRelease }), ["new-release"]); assert.deepEqual(ids({ consoleDead: true, version: { kind: "mismatch", skills: "3.0.0", app: "3.1.0" }, newRelease }), ["console-dead", "mismatch", "new-release"]); const [step] = barNextSteps({ newRelease }); assert.equal(step.text, "Update available: v3.0.1"); assert.equal(step.label, "Copy update command"); assert.equal(step.action, "copy-update"); assert.equal(NEW_RELEASE_COMMAND, "plan2code update"); assert.deepEqual(barNextSteps({ newRelease: null }), [], "nothing newer, nothing shown"); }); test("after the copy the bar says what to do next; if the clipboard was refused it spells the command out", () => { const [copied] = barNextSteps({ newRelease: { latest: "3.0.1", copied: true } }); assert.match(copied.text, /^Copied: run `plan2code update` in a terminal/); assert.equal(copied.action, null); const [manual] = barNextSteps({ newRelease: { latest: "3.0.1", manual: true } }); assert.match(manual.text, /^Run `plan2code update` in a terminal/); assert.equal(manual.action, null); }); test("the app asks once at start for a newer release, and the bar is wired to the copy action", () => { assert.match(APP_JS, /invoke\('check_new_release'\)/); assert.match(APP_JS, /'copy-update': copyUpdateCommand/); assert.match(APP_JS, /barNextSteps\(\{ consoleDead, version: versionState, newRelease \}\)/); }); test("each version state reads and acts as the old version bar did", () => { const [mismatch] = barNextSteps({ version: { kind: "mismatch", skills: "3.0.0", app: "3.1.0" } }); assert.equal(mismatch.text, "Skills v3.0.0, app v3.1.0"); assert.equal(mismatch.label, "Update skills"); const [ahead] = barNextSteps({ version: { kind: "ahead", skills: "3.2.0", app: "3.1.0" } }); assert.equal(ahead.action, null, "another Update skills run cannot help when the skills are ahead"); assert.match(ahead.title, /newer than this app \(v3\.1\.0\)\. Get the new app\./); const [updating] = barNextSteps({ version: { kind: "updating", line: "Installing skills (4 of 9)" } }); assert.deepEqual([updating.text, updating.busy, updating.action], ["Installing skills (4 of 9)", true, null], "no button while it runs"); const [remains] = barNextSteps({ version: { kind: "remains", skills: "3.0.0", app: "3.1.0" } }); assert.equal(remains.action, null); assert.equal(remains.title, "Updated, but the mismatch remains (skills v3.0.0, app v3.1.0)."); const [failed] = barNextSteps({ version: { kind: "failed", error: "npm exited with code 1" } }); assert.deepEqual([failed.tone, failed.title, failed.label], ["err", "npm exited with code 1", "Update skills"], "a failed update can be tried again"); const [tooOld] = barNextSteps({ version: { kind: "too-old" } }); assert.equal(tooOld.label, "Update skills", "an old console is fixed by the skills update"); }); test("every control that reconnects the console carries the same label, never 'Restart the console' or 'Start it again'", () => { assert.equal(NEXT_STEP_LABELS.reconnect, "Reconnect the console"); assert.equal(NEXT_STEP_LABELS["restart-console"], "Reconnect the console"); assert.doesNotMatch(APP_JS, /'Restart the console'|"Start it again"/, "no stale label text left in app.js"); }); test("app.js draws the bar's right end through barNextSteps, not a hand-rolled copy of its order", () => { assert.match(APP_JS, /const steps = barNextSteps\(\{ consoleDead, version: versionState, newRelease \}\);/); for (const gone of ["console-again", "version-update", "permissions-note", "console-status", "project-dir"]) { assert.ok(!INDEX_HTML.includes(`id="${gone}"`), `index.html still has #${gone}`); } }); // Claude Code holds typed text as a queued message while its turn is busy, // and the continue button only shows once the agent stopped checking in, so // by then the turn is stuck and the queued word would never send by itself. test("continueKeys types continue and Enter, and under Claude Code also its send-now chord (ctrl+x ctrl+s)", () => { assert.deepEqual(continueKeys("claude"), ["continue", "\r", "\x18\x13"]); for (const kind of ["devin", "codex", null, undefined, "something-new"]) { assert.deepEqual(continueKeys(kind), ["continue", "\r"], `${kind} gets the plain word and Enter`); } }); test("app.js types the continue button's keystrokes through continueKeys, for the running agent's kind", () => { const typeContinue = APP_JS.slice(APP_JS.indexOf("function typeContinue"), APP_JS.indexOf("function typeContinue") + 800); assert.match(typeContinue, /continueKeys\(agent\.kind\)/, "the keystrokes come from continueKeys"); assert.doesNotMatch(typeContinue, /data: 'continue'/, "no hand-typed word left beside it"); }); // The console page's What to check lines make "terminal" a button while the // pane is collapsed (core.js renderStatus), so it must hear the pane's state. test("app.js tells the console page whether the terminal pane is collapsed, and expands only a collapsed pane on terminal-open", () => { const layout = APP_JS.slice(APP_JS.indexOf("function applyPaneLayout"), APP_JS.indexOf("function savePaneSettings")); assert.match(layout, /updateBadge\(\);\s+tellTerminalState\(\);\s+\}/, "every layout change is told"); assert.match(layout, /plan2code: 'terminal-state', collapsed: paneCollapsed/, "a named message with one boolean"); assert.match(layout, /if \(!force && toldCollapsed === paneCollapsed\) return;/, "only a change is told, not every drag"); assert.match(APP_JS, /plan2code: 'taskbar-state', on \}, '\*'\);\s+tellTerminalState\(true\);/, "the page hears it on its hello"); assert.match(APP_JS, /kind === 'terminal-open'\) \{[^}]*if \(paneCollapsed\) termCollapseBtn\.click\(\);/, "the pane's own button expands it"); }); test("the bar reads project menu, New Session, agent menu, with both menus outside the scrolling row", () => { const row = INDEX_HTML.slice(INDEX_HTML.indexOf('