// Page tests for the agent-less setup page (`console.mjs open --workflow // setup`, with or without --for-cli): the three found states and their way // forward, the forCli ordering, Check again, Use it from here, a template // becoming a custom agent, Start and its grace line, the /handover move, and // the quiet chrome an agent-less session keeps. Run with: // node --test scripts/test-web-console-page-setup.mjs // // Each test drives the real console in a real Chrome or Edge through // scripts/page-test-helpers.mjs: one browser for the file, a fresh console // session per test. The found states are deterministic: a shipped agents.json // written under MODELS_DIR/bin before the server starts, fake .cmd files (one // file name is found on every OS), PATH pointed at a fixture dir, npm's // global folder at an empty prefix and the saved Windows PATH switched off. // That is the same recipe test-web-console-server.mjs's agentFixtures() uses. // // The page is the agents view's "setup" mount (agents-view.js on // .setup-agents), a master-detail split: button.split-row[data-key] rows in // [data-av="table"] on the left, the picked row's state and way forward // (Start, Use it from here, the install lines, a template's Use) drawn as // .setup-detail in [data-av="form"] on the right after the row is clicked. // Row badges are drawn on first load; everything actionable wants the click. import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import assert from "node:assert/strict"; import { pageSuite, openSession, tempProject, textOf, scrollCard } from "./page-test-helpers.mjs"; import { cli, sessionUrl, readState, until, MODELS_DIR } from "./web-console-test-helpers.mjs"; const suite = pageSuite(test); // The desktop-app markers a spawned console must never carry into these // tests: test-web-console-server.mjs's NO_APP, plus the setup hand-over pair, // which would turn a plain `open` into a hand-over of its own. const NO_APP = { PLAN2CODE_IN_APP: undefined, PLAN2CODE_APP_TOKEN: undefined, PLAN2CODE_APP_ANNOUNCE: undefined, PLAN2CODE_EMBED_ORIGIN: undefined, PLAN2CODE_CONSOLE_SID: undefined, PLAN2CODE_APP_VERSION: undefined, PLAN2CODE_SETUP_ANNOUNCE: undefined, PLAN2CODE_SETUP_TOKEN: undefined, }; const PATH_KEY = Object.keys(process.env).find((k) => k.toUpperCase() === "PATH") || "PATH"; const OS_KEY = process.platform === "win32" ? "win" : process.platform === "darwin" ? "mac" : "linux"; const SHIPPED_FILE = path.join(MODELS_DIR, "bin", "agents.json"); const USER_AGENTS_FILE = path.join(MODELS_DIR, "agents.json"); // The missing builtin's install lines, one list per OS; the server serves // only this OS's. Windows carries a note so that part of the row is checked. const INSTALL = { win: [{ label: "PowerShell", run: "irm https://example.com/gone | iex", note: "Run it in an elevated shell." }], mac: [{ label: "Terminal", run: "brew install gone-agent" }], linux: [{ label: "Terminal", run: "apt install gone-agent" }], }; // Fake commands and a shipped list that put one builtin in each state: two on // PATH (for the --for-cli ordering), one only in its installDirs, one // nowhere. `npmCommands` are bare names dropped into the fake npm global bin, // where a custom agent's command reads as "outside". Everything is written // before the session's server starts, and removed after the test. function agentFixtures(t, { userAgents = [], npmCommands = [] } = {}) { const base = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-setup-")); const onPath = path.join(base, "onpath"); const outside = path.join(base, "outside"); const npmPrefix = path.join(base, "npm-prefix"); const npmBin = process.platform === "win32" ? npmPrefix : path.join(npmPrefix, "bin"); fs.mkdirSync(onPath, { recursive: true }); fs.mkdirSync(outside, { recursive: true }); fs.mkdirSync(npmBin, { recursive: true }); fs.writeFileSync(path.join(onPath, "found-one.cmd"), "@rem stand-in\r\n"); fs.writeFileSync(path.join(onPath, "found-two.cmd"), "@rem stand-in\r\n"); fs.writeFileSync(path.join(outside, "outer.cmd"), "@rem stand-in\r\n"); for (const name of npmCommands) fs.writeFileSync(path.join(npmBin, name), "@rem stand-in\r\n"); // The template's command is a path, so the agent its Save writes finds a file. const templar = path.join(base, "templar.cmd"); fs.writeFileSync(templar, "@rem stand-in\r\n"); const shipped = { schema: 1, agents: [ { id: "found-one", name: "Found One", kind: "builtin", command: "found-one.cmd" }, { id: "found-two", name: "Found Two", kind: "builtin", command: "found-two.cmd" }, { id: "outer", name: "Outer Agent", kind: "builtin", command: "outer.cmd", installDirs: { win: [outside], mac: [outside], linux: [outside] }, }, { id: "gone", name: "Gone Agent", kind: "builtin", command: "p2c-gone-xyz.cmd", installUrl: "https://example.com/gone", install: INSTALL, }, { id: "templar", name: "Templar", kind: "template", command: templar, prompt: "/plan2code", args: ["{prompt}"], note: "A starter agent.", }, ], }; fs.mkdirSync(path.dirname(SHIPPED_FILE), { recursive: true }); fs.writeFileSync(SHIPPED_FILE, JSON.stringify(shipped)); if (userAgents.length) { fs.writeFileSync(USER_AGENTS_FILE, JSON.stringify({ schema: 1, agents: userAgents })); } else { fs.rmSync(USER_AGENTS_FILE, { force: true }); } t.after(() => { fs.rmSync(SHIPPED_FILE, { force: true }); fs.rmSync(USER_AGENTS_FILE, { force: true }); fs.rmSync(base, { recursive: true, force: true }); }); const env = { ...NO_APP, PLAN2CODE_NO_SAVED_PATH: "1", npm_config_prefix: npmPrefix, [PATH_KEY]: onPath + path.delimiter + (process.env[PATH_KEY] || ""), }; return { base, onPath, outside, npmBin, templar, env }; } // The split's left column and the detail pane. Rows are found by data-key // ("setup:" for builtins and custom agents, "setup: