// Web console tests, independent sessions: first paint, streams, hand-off, cleanup, overview.
// Run with: node --test scripts/test-web-console-server.mjs
//
// Shared setup (a per-process console home, the CLI runner, fixtures) lives in
// web-console-test-helpers.mjs; see the comment at its top.
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { execFileSync, spawn } from "node:child_process";
import http from "node:http";
import {
ACCENTS,
CARD_WIDTHS,
cardWidth,
DEFAULT_ACCENT,
DEFAULT_CARD_WIDTH,
} from "../src/web-console/public/palette.js";
import { activityLabel, BUILD_WORKFLOWS, taskLabel, workingLine } from "../src/web-console/public/labels.js";
import {
bindLoopback,
cleanupFindings,
handOff,
PROTECTED_CONSOLE_FILES,
readOverview,
readSpecFile,
removeCleanup,
removeScratch,
removeSessions,
repoRoots,
SESSION_MAX_AGE_MS,
staleScratch,
STRAY_GRACE_MS,
staleSessions,
terminalLine,
} from "../src/web-console/sessions.mjs";
import { validate } from "../src/web-console/patches.mjs";
// Scrubs this process's git config, so the Cleanup fixtures and git-repo.mjs agree.
import { gitInit } from "./skill-script-helpers.mjs";
import { joinedCss } from "./css-helpers.mjs";
import {
ROOT,
HANDOFFS_DIR,
HOME,
ENV,
MODELS_DIR,
cookieFor,
cli,
payload,
BASE,
backdate,
cliIn,
sessionUrl,
readState,
sleep,
until,
} from "./web-console-test-helpers.mjs";
// Every page load used to fetch /state, then /looks, then open the stream,
// before drawing anything real. The first state now rides in the page.
test("the page carries its first state, title and badge, so the first paint needs no fetch", async () => {
const hostile = "Lunch $& $' app";
const res = cli(["open", "--workflow", "implement", "--title", hostile, "--no-open"]);
const m = res.json.url.match(/^(http:\/\/127\.0\.0\.1:\d+)\/s\/([^/]+)\//);
const html = await (await fetch(m[1] + "/", { headers: { cookie: cookieFor(m[1], m[2]) } })).text();
const block = html.match(/ is the block's own.
assert.equal(block[1].includes("<"), false);
assert.equal((html.match(/