// 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 { looksFromMessage, terminalFontStep, TERMINAL_FONT_DEFAULT, TERMINAL_FONT_MIN, TERMINAL_FONT_MAX, splitterFraction, splitterAria, terminalTitle, barFit, terminalAnsi, TERMINAL_ANSI, hexToRgba, helloRetryAction, retryLabel, RETRY_AFTER_SECONDS, attentionSignal, commandSegments, plainCommandText, menuStep, pageNotShownView, pageDetailsLine, redactSessionUrl, barNextSteps, continueKeys, defaultRightFraction, folderName, folderPickerRows, pickDefaultAgent, taskbarSwitch, windowTitleFor, deadConsoleAction, loopBanner, loopRunText, loopLocks, loopStopLabels, loopWindowTitle, CONSOLE_ASIDE_BREAKPOINT_PX, NEXT_STEP_LABELS, NEW_RELEASE_COMMAND, STILL_WAITING_MS, STILL_WAITING_TEXT, waitingView, friendlyError, endedView, confirmButtons, trapTab, SHORTCUTS, shortcutAction, shortcutLabel, tipWithShortcut, shortcutFromMessage, clipboardAction, releaseAfterCheck, releaseNotesUrl, reasonSentence, UPDATE_RECHECK_MS } 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"); const METER_JS = fs.readFileSync(path.join(HERE, "../plan2code-app/ui/meter.js"), "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, as Update and restart with a What's new link; 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, "Update and restart"); assert.equal(step.action, "update-restart"); assert.deepEqual(step.link, { label: "What's new", url: "https://github.com/jparkerweb/plan2code/releases/tag/v3.0.1" }); assert.equal(releaseNotesUrl("v3.0.1"), step.link.url, "a leading v is not doubled"); assert.equal(NEW_RELEASE_COMMAND, "plan2code update", "still the fallback command a failed update names"); assert.deepEqual(barNextSteps({ newRelease: null }), [], "nothing newer, nothing shown"); }); test("the update states: running shows its line and no button, a failure keeps the app up and says why, an installed one offers the restart", () => { const [running] = barNextSteps({ newRelease: { latest: "3.2.0", updating: "Installing skills (4 of 9)" } }); assert.deepEqual([running.text, running.busy, running.action, running.link ?? null], ["Installing skills (4 of 9)", true, null, null]); const [starting] = barNextSteps({ newRelease: { latest: "3.2.0", updating: "" } }); assert.equal(starting.text, "Updating to v3.2.0...", "a first line before the installer says anything"); const [failed] = barNextSteps({ newRelease: { latest: "3.2.0", failed: "The update did not finish: npm exited with code 1." } }); assert.deepEqual([failed.text, failed.tone, failed.action, failed.label], ["Update failed", "err", "update-restart", "Update and restart"], "it can be tried again"); assert.match(failed.title, /^The update did not finish: npm exited with code 1\. Plan2Code is still running as it was\./, "the reason, in full, for the tooltip and the list"); assert.match(failed.title, /run `plan2code update` in a terminal/, "the old way stays as the fallback"); const [restart] = barNextSteps({ newRelease: { latest: "3.2.0", restartNeeded: true } }); assert.deepEqual([restart.text, restart.action, restart.label, restart.tone], ["Updated to v3.2.0", "restart-app", "Restart Plan2Code", "ok"]); const [problem] = barNextSteps({ newRelease: { problem: "Could not check for updates: git was not found on PATH." } }); assert.deepEqual([problem.id, problem.text, problem.action], ["update-check", "Could not check for updates", null], "a missing git is said, not silent"); assert.match(problem.title, /git was not found/); const [same] = barNextSteps({ notReplaced: "3.1.0" }); assert.deepEqual([same.id, same.text, same.tone, same.action], ["not-replaced", "App not replaced", "warn", null], "the same version after a restart is not 'Updated to'"); assert.match(same.title, /no desktop app for this system yet, so Plan2Code is still on v3\.1\.0\./); const [after] = barNextSteps({ updatedTo: "3.2.0" }); assert.deepEqual([after.id, after.text, after.tone, after.action], ["updated-to", "Updated to v3.2.0", "ok", null]); }); test("releaseAfterCheck: a recheck never replaces a running or finished update, finds a version, reports a problem and clears an old one", () => { assert.deepEqual(releaseAfterCheck(null, { latest: "3.2.0", problem: null }), { latest: "3.2.0" }); assert.deepEqual(releaseAfterCheck(null, { latest: null, problem: "Could not check for updates: git was not found on PATH." }), { problem: "Could not check for updates: git was not found on PATH." }); assert.equal(releaseAfterCheck({ latest: "3.2.0" }, { latest: null, problem: null }), null, "current now: the old notice goes"); assert.equal(releaseAfterCheck({ problem: "x" }, { latest: null, problem: null }), null); const running = { latest: "3.2.0", updating: "Installing" }; assert.equal(releaseAfterCheck(running, { latest: "3.2.0" }), running, "an update that runs is left alone"); const waiting = { latest: "3.2.0", restartNeeded: true }; assert.equal(releaseAfterCheck(waiting, { latest: "3.3.0" }), waiting, "so is one waiting for its restart"); assert.equal(releaseAfterCheck(null, null), null, "no answer, nothing to show"); assert.equal(UPDATE_RECHECK_MS, 4 * 60 * 60 * 1000, "every few hours, not once per launch"); }); test("reasonSentence keeps the installer's reason readable: a capital and a full stop", () => { assert.equal(reasonSentence("the update did not finish: npm exited with code 1"), "The update did not finish: npm exited with code 1."); assert.equal(reasonSentence("Already a sentence."), "Already a sentence."); assert.equal(reasonSentence(" two spaces "), "Two spaces."); assert.equal(reasonSentence(""), "The update did not finish."); }); test("the app asks at start and every few hours for a newer release, and the bar is wired to Update and restart", () => { assert.match(APP_JS, /invoke\('check_new_release'\)/); assert.match(APP_JS, /setInterval\(checkNewRelease, UPDATE_RECHECK_MS\)/); assert.match(APP_JS, /'update-restart': \(\) => updateAndRestart\(\)/); assert.match(APP_JS, /'restart-app': \(\) => restartApp\(\)/); assert.doesNotMatch(APP_JS, /copyUpdateCommand|'copy-update'/, "the copy-the-command flow is gone"); const update = APP_JS.slice(APP_JS.indexOf("async function updateAndRestart"), APP_JS.indexOf("async function restartApp")); assert.match(update, /invoke\('update_to_newest', \{ progress \}\)/, "the unpinned update, through the progress channel"); assert.match(update, /setNewRelease\(\{ latest, failed: reasonSentence\(error\) \}\);\s*\n\s*return;/, "a failure returns before any restart"); const restart = APP_JS.slice(APP_JS.indexOf("async function restartApp"), APP_JS.indexOf("async function showUpdatedTo")); assert.match(restart, /invoke\('restart_app', \{ confirmed: false \}\)/, "the quit check guards the respawn"); assert.match(APP_JS, /if \(state\.updatedFrom\) void showUpdatedTo\(\);/, "Updated to vX once after the restart"); const noted = APP_JS.slice(APP_JS.indexOf("async function showUpdatedTo"), APP_JS.indexOf("// The About dialog")); assert.match(noted, /now === appState\.updatedFrom[\s\S]*?notReplaced = now;/, "an unchanged version is said so, never 'Updated to' it"); assert.match(APP_JS, /barNextSteps\(\{ consoleDead, version: versionState, newRelease, updatedTo, notReplaced \}\)/); }); 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, updatedTo, notReplaced \}\);/); 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('
'), INDEX_HTML.indexOf('
row.indexOf(`id="${id}"`); assert.ok(at("project-menu-button") >= 0 && at("agent-restart") > at("project-menu-button"), "New Session follows the project menu"); assert.ok(at("agent-menu-button") > at("agent-restart"), "New Session sits just left of the agent menu"); assert.ok(!row.includes('id="project-menu"') && !row.includes('id="agent-menu"'), "the menus are not inside #bar-row, whose overflow would clip them"); assert.match(INDEX_HTML, /