mirror of
https://github.com/pbakaus/impeccable.git
synced 2026-09-12 22:26:38 +03:00
* refactor(content): merge content/site/ into site/content/ Phase 1 step 1 of the directory restructure. The dual content tree was called out in CLAUDE.md as cleanup; both trees were already in sync except for anti-patterns-catalog.js, which moves to site/data/. - Delete content/site/skills/ and content/site/tutorials/ (duplicates of site/content/, which is what Astro's content collection actually reads). - Move content/site/anti-patterns-catalog.js -> site/data/. - Update scripts/lib/sub-pages-data.js and scripts/build.js to read from site/content/ and site/data/. - Drop content/site/ from validateProse target list (site/content was already there). - Rewrite the "Two content trees" section in CLAUDE.md as a single-tree pointer; update stale dev-server text mentioning the deleted server/index.js. Tests: 186/186 pass. Skills build: clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * refactor(skill): rename source/skills/impeccable/ -> skill/ Phase 1 step 2 of the directory restructure. The path was redundantly nested ("source/" wrapper plus "skills/impeccable/" — singular content hidden behind the plural). Collapses to flat skill/SKILL.md + skill/reference/ + skill/scripts/. - Move source/skills/impeccable/ -> skill/. - Rewrite scripts/lib/utils.js readSourceFiles(): drop the multi-skill iteration (CLAUDE.md commits to a single user-invocable skill); read skill/SKILL.md directly. - Update scripts/build.js, scripts/generate-og-image.js, and the sub-pages data layer to point at skill/. - Update tests/lib/utils.test.js: drop the "multi-skill" and "dir-name fallback" cases, update single-skill paths to skill/. - Update tests/build.test.js similarly: drop "multiple skills" integration test, update paths. - Update non-glob path joins in tests/framework-fixtures.test.mjs, tests/live-e2e/session.mjs, tests/live-e2e/agents/llm-agent.mjs, tools/live-loop.mjs. - Update prose/text references in CLAUDE.md, AGENTS.md, DEVELOP.md, README.md, scripts/lib/sub-pages-data.js, bin/commands/skills.mjs, site/data/anti-patterns-catalog.js, site/pages/docs/[...slug].astro, docs/adr-live-variant-mode.md, docs/plans/. Eval framework note: the separate impeccable-evals repo reads ../impeccable/source/skills/impeccable/ and needs a coordinated rename to ../impeccable/skill/. Tests: 186/186 pass. Skills build: clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * refactor: rename docs/ -> notes/ Phase 1 step 3 of the directory restructure. The internal docs/ dir (ADRs and plans) clashed with the site's /docs route. Renaming it "notes/" makes the difference unambiguous: notes/ is project-internal process, /docs is the user-facing route under site/pages/docs/. No code references the dir; the rename is a clean git mv. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * refactor(site): move public/ under site/public/ Phase 2 step 4 of the directory restructure. Public assets and the Astro publicDir now live alongside the rest of the site, so site/ is fully self-contained for static content. - git mv public site/public. - astro.config.mjs: add publicDir: './site/public'. Astro defaults to ./public at the project root, so the override is required. - scripts/build.js: write generated _data, _headers, _redirects, _routes.json, and js/detect-antipatterns-browser.js into site/public/. Also delete the dead _REMOVED() Bun static-site builder (replaced by Astro at #130; the placeholder no longer earns its keep). - scripts/build.js validateProse: replace the stale public/index.html reference (deleted at the Astro migration) with site/pages/index.astro in the count-validation file list, restoring homepage drift detection. - scripts/generate-og-image.js: write OG image into site/public/. - scripts/screenshot-antipatterns.js: read examples from + write screenshots to site/public/antipattern-{examples,images}/. - scripts/lib/sub-pages-data.js: load command demos from site/public/js/demos/commands. - .gitignore: rename the public/* generator-output entries to site/public/*. - CLAUDE.md: refresh CSS/data-file paths (still pointing at the old pre-Astro public/css/ + public/js/ tree), point the changelog and command-add checklists at site/pages/index.astro and site/scripts/data.js + site/scripts/components/framework-viz.js. Cloudflare Pages note: functions/ stays at the repo root because CF Pages auto-discovers it there with no configuration knob to relocate. Moving it under site/ would either break deployment or require a build-time copy step that adds more complexity than the cleanup is worth. Tests: 186/186 pass. Skills + site build clean. _headers, _redirects, _routes.json, _data/ all land in build/ correctly. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * refactor(cli): consolidate bin/ + src/ + lib/ under cli/ Phase 2 step 5 of the directory restructure. The CLI surface was split across three top-level dirs whose names were easy to mistake for each other (especially src/ vs source/ pre-step-2). Consolidates under cli/. - git mv bin -> cli/bin (CLI entry + skills sub-command) - git mv src -> cli/engine (detect-antipatterns engine + browser variant) - git mv lib -> cli/lib (download-providers helper) Update package.json: - bin.impeccable: cli/bin/cli.js - main + exports: cli/engine/detect-antipatterns.mjs and the ./browser variant - files: ["cli/", "LICENSE"] Update internal references: - cli/bin/cli.js: dynamic import points at ../engine/, package.json read goes one level deeper (../../package.json). - functions/api/download/[type]/[provider]/[id].js + bundle/[provider].js: cli/lib/download-providers.js path. - scripts/build.js, scripts/build-browser-detector.js, scripts/build-extension.js: cli/engine path constants. - scripts/lib/sub-pages-data.js, scripts/lib/utils.js, skill/scripts/ live-server.mjs: comment refs. - tests/detect-antipatterns{,-browser,-fixtures}.test.{js,mjs}, tests/windows-path-fix.test.js: import + read paths. - AGENTS.md, CLAUDE.md: doc paths. Verified: - npx node cli/bin/cli.js --version, --help, detect --help all work. - bun run build, bun run build:browser, bun run build:extension all clean. Browser detector lands at cli/engine/detect-antipatterns-browser.js; extension/detector/detect.js still emits to the same location. - bun run test: 186/186 pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: update browser-detector paths missed in cli/ rename Bugbot caught two runtime path leaks where the comment got renamed to cli/engine/ but the actual code still used the old src/ segment. - skill/scripts/live-server.mjs: detectPaths array now joins cli, engine, detect-antipatterns-browser.js for both the repo-relative lookup (4 dirs up from .claude/skills/impeccable/scripts/ to repo root) and the npm node_modules fallback. Without this fix, the detection overlay would silently not load during live-server sessions. - scripts/build.js: the post-build copy of the browser detector into site/public/js/ was reading from src/. The if (fs.existsSync(...)) guard meant the copy was silently skipping, so antipattern-examples pages would 404 on /js/detect-antipatterns-browser.js once the site was deployed. Tests: 186/186 pass. Build clean. site/public/js/detect-antipatterns-browser.js re-emits as expected. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: cleanup-deprecated import path missed an extra .. in cli/ rename Bugbot caught three call sites in cli/bin/commands/skills.mjs that import '../../skill/scripts/cleanup-deprecated.mjs'. Pre-rename, that was correct from bin/commands/ (one parent to bin/, one to repo root). After moving the file from bin/commands/ to cli/bin/commands/, the path is one directory deeper, so it needs three .. segments to reach the repo root. Without the fix, every cleanup invocation throws on import and gets swallowed by the surrounding try/catch — silent skip. cli/bin/cli.js's package.json read already uses '../../package.json' (the same depth pattern), confirming three levels is correct. Verified: dynamic import resolves and exports the expected functions. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore: sweep stale path/file references missed in the restructure Same root cause as the two bugbot finds: some references in moved or related files weren't tracked because they didn't match a simple sed pattern. Caught the rest by walking each moved dir's depth and each Astro-migration deletion. Stale path references (post-Astro migration, missed earlier): - CLAUDE.md: legacy URL redirects "live in server/index.js" -> point at the actual sources (scripts/build.js generateCFConfig + site/public/_redirects). - AGENTS.md: counts.js path (public/ -> site/public/), changelog file (public/index.html -> site/pages/index.astro), screenshots note (public/ -> site/), source-of-truth dirs (source/, src/ -> skill/, cli/). - tests/detect-antipatterns-browser.test.mjs: comment about routes "in server/index.js". - skill/reference/live.md: workflow.css example for "this repo" was pre-Astro (public/css/) -> site/styles/. (User-project Vite/Next example unchanged.) Stale path that pointed at moved files: - tests/skills-cli.test.js: CLI path was '..', 'bin', 'cli.js'; now '..', 'cli', 'bin', 'cli.js'. Test isn't wired into bun run test but it would have failed if invoked. Dead files (orphaned by Astro migration, never cleaned up): - tests/server/download-validation.test.js: imported from ../../server/lib/{validation,api-handlers}.js which were deleted inb8f09c8. Test was a silent failure waiting to happen. - scripts/lib/render-markdown.js: 156-line module with zero consumers (the only caller, scripts/lib/render-page.js, was deleted in the Astro cleanup). - scripts/build.js: dead commented-out generateSubPages import. Tests: 186/186 pass. Build clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(build): remove invalid Corepack packageManager spec Cloudflare Pages rejects the build with `Unsupported package manager specification (bun@1.3.11)`. The packageManager field follows Corepack's syntax which only validates npm/pnpm/yarn — `bun@X.Y.Z` parses as a malformed Corepack directive even though Bun itself treats it as a hint. Pre-existing on main sinced874af0(CF Pages deploy on main also failing); just surfaces here because the PR triggers a fresh deploy. CF Pages auto-detects Bun anyway (the build log confirms: "Detected the following tools from environment: bun@1.3.11, pnpm@10.11.1, nodejs@22.16.0"). Removing the field unblocks the deploy without changing local dev behavior. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Paul Bakaus <paulbakaus@pauls-mbp-3.lan> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
761 lines
29 KiB
JavaScript
761 lines
29 KiB
JavaScript
/**
|
|
* Tests for the live variant server.
|
|
* Run with: node --test tests/live-server.test.mjs
|
|
*/
|
|
|
|
import { describe, it, before, after } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { existsSync, mkdtempSync, readFileSync, writeFileSync, mkdirSync, rmSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { tmpdir } from 'node:os';
|
|
import { execFileSync, execSync, spawn } from 'node:child_process';
|
|
import {
|
|
getDesignSidecarPath,
|
|
getLegacyLiveServerPath,
|
|
getLegacyLiveSessionsDir,
|
|
getLiveServerPath,
|
|
getLiveSessionsDir,
|
|
} from '../skill/scripts/impeccable-paths.mjs';
|
|
|
|
const REPO_ROOT = process.cwd();
|
|
const SERVER_SCRIPT = join(REPO_ROOT, 'skill/scripts/live-server.mjs');
|
|
const COMPLETE_SCRIPT = join(REPO_ROOT, 'skill/scripts/live-complete.mjs');
|
|
// ---------------------------------------------------------------------------
|
|
// Helper: start/stop server for integration tests
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function startServer(port = 8499, { cwd = REPO_ROOT } = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
const proc = spawn('node', [SERVER_SCRIPT, '--port=' + port], {
|
|
cwd,
|
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
env: { ...process.env },
|
|
});
|
|
let output = '';
|
|
proc.stdout.on('data', (d) => {
|
|
output += d.toString();
|
|
if (output.includes('running on')) {
|
|
// Read token from PID file
|
|
try {
|
|
const info = JSON.parse(readFileSync(getLiveServerPath(cwd), 'utf-8'));
|
|
resolve({ proc, port: info.port, token: info.token, cwd });
|
|
} catch {
|
|
reject(new Error('Server started but PID file not readable'));
|
|
}
|
|
}
|
|
});
|
|
proc.stderr.on('data', (d) => { output += d.toString(); });
|
|
proc.on('error', reject);
|
|
setTimeout(() => reject(new Error('Server start timeout. Output: ' + output)), 5000);
|
|
});
|
|
}
|
|
|
|
async function stopServer(port, token) {
|
|
try {
|
|
await fetch(`http://localhost:${port}/stop?token=${token}`);
|
|
} catch { /* server already gone */ }
|
|
}
|
|
|
|
async function drainPolls(server) {
|
|
let drained;
|
|
do {
|
|
const r = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=50&leaseMs=1`);
|
|
drained = await r.json();
|
|
if (drained.id) {
|
|
await fetch(`http://localhost:${server.port}/poll`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token: server.token, id: drained.id, type: 'done' }),
|
|
});
|
|
}
|
|
} while (drained.type !== 'timeout');
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Server integration tests
|
|
// ---------------------------------------------------------------------------
|
|
|
|
describe('live-server integration', () => {
|
|
let server;
|
|
|
|
before(async () => {
|
|
rmSync(getLiveSessionsDir(REPO_ROOT), { recursive: true, force: true });
|
|
rmSync(getLegacyLiveSessionsDir(REPO_ROOT), { recursive: true, force: true });
|
|
rmSync(getLiveServerPath(REPO_ROOT), { force: true });
|
|
rmSync(getLegacyLiveServerPath(REPO_ROOT), { force: true });
|
|
server = await startServer(8499);
|
|
});
|
|
|
|
after(async () => {
|
|
if (server) {
|
|
await stopServer(server.port, server.token);
|
|
server.proc.kill();
|
|
}
|
|
});
|
|
|
|
it('/health returns correct status', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/health`);
|
|
assert.equal(res.status, 200);
|
|
const data = await res.json();
|
|
assert.equal(data.status, 'ok');
|
|
assert.equal(data.port, server.port);
|
|
assert.equal(data.mode, 'variant');
|
|
assert.equal(typeof data.hasProjectContext, 'boolean');
|
|
assert.equal(data.connectedClients, 0);
|
|
});
|
|
|
|
it('/status returns durable recovery state', async () => {
|
|
await drainPolls(server);
|
|
const eventRes = await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
token: server.token,
|
|
type: 'generate',
|
|
id: 'a1b2c3d5',
|
|
action: 'impeccable',
|
|
count: 1,
|
|
pageUrl: '/',
|
|
element: { outerHTML: '<button>Book</button>' },
|
|
}),
|
|
});
|
|
assert.equal(eventRes.status, 200);
|
|
|
|
const res = await fetch(`http://localhost:${server.port}/status?token=${server.token}`);
|
|
assert.equal(res.status, 200);
|
|
const data = await res.json();
|
|
assert.equal(data.status, 'ok');
|
|
assert.equal(data.activeSessions.some((s) => s.id === 'a1b2c3d5'), true);
|
|
assert.equal(data.pendingEvents.some((e) => e.id === 'a1b2c3d5' && e.type === 'generate'), true);
|
|
|
|
await drainPolls(server);
|
|
});
|
|
|
|
it('/live.js serves script with token injected', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/live.js`);
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.headers.get('content-type'), 'application/javascript');
|
|
const text = await res.text();
|
|
assert.ok(text.includes('__IMPECCABLE_TOKEN__'));
|
|
assert.ok(text.includes(server.token));
|
|
assert.ok(text.includes('__IMPECCABLE_PORT__'));
|
|
const sessionHelperIndex = text.indexOf('__IMPECCABLE_LIVE_SESSION__');
|
|
const browserInitIndex = text.indexOf('__IMPECCABLE_LIVE_INIT__');
|
|
assert.ok(sessionHelperIndex !== -1);
|
|
assert.ok(browserInitIndex !== -1);
|
|
assert.ok(
|
|
sessionHelperIndex < browserInitIndex,
|
|
'event=live_server.browser_helper_order actor=browser operation=load_live_js risk=session_helper_missing_before_browser_init expected=session helper before live init actual=' + sessionHelperIndex + ':' + browserInitIndex,
|
|
);
|
|
});
|
|
|
|
it('/design-system.json reads DESIGN.md plus .impeccable/design.json', async () => {
|
|
const tmp = mkdtempSync(join(tmpdir(), 'impeccable-design-system-'));
|
|
let designServer;
|
|
try {
|
|
writeFileSync(join(tmp, 'DESIGN.md'), `---
|
|
name: Temp System
|
|
description: Temporary design context
|
|
colors: {}
|
|
---
|
|
|
|
# Temp System
|
|
`);
|
|
const sidecarPath = getDesignSidecarPath(tmp);
|
|
mkdirSync(join(tmp, '.impeccable'), { recursive: true });
|
|
writeFileSync(sidecarPath, JSON.stringify({ version: 2, source: 'new-sidecar' }));
|
|
|
|
designServer = await startServer(8520, { cwd: tmp });
|
|
const res = await fetch(`http://localhost:${designServer.port}/design-system.json?token=${designServer.token}`);
|
|
const data = await res.json();
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(data.hasMd, true);
|
|
assert.equal(data.hasSidecar, true);
|
|
assert.equal(data.parsed.frontmatter.name, 'Temp System');
|
|
assert.equal(data.sidecar.source, 'new-sidecar');
|
|
} finally {
|
|
if (designServer) {
|
|
await stopServer(designServer.port, designServer.token);
|
|
designServer.proc.kill();
|
|
}
|
|
rmSync(tmp, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('/design-system.json falls back to legacy root DESIGN.json', async () => {
|
|
const tmp = mkdtempSync(join(tmpdir(), 'impeccable-design-system-legacy-'));
|
|
let designServer;
|
|
try {
|
|
writeFileSync(join(tmp, 'DESIGN.md'), `---
|
|
name: Legacy System
|
|
description: Legacy design context
|
|
colors: {}
|
|
---
|
|
|
|
# Legacy System
|
|
`);
|
|
writeFileSync(join(tmp, 'DESIGN.json'), JSON.stringify({ version: 2, source: 'legacy-sidecar' }));
|
|
|
|
designServer = await startServer(8521, { cwd: tmp });
|
|
const res = await fetch(`http://localhost:${designServer.port}/design-system.json?token=${designServer.token}`);
|
|
const data = await res.json();
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(data.hasMd, true);
|
|
assert.equal(data.hasSidecar, true);
|
|
assert.equal(data.parsed.frontmatter.name, 'Legacy System');
|
|
assert.equal(data.sidecar.source, 'legacy-sidecar');
|
|
} finally {
|
|
if (designServer) {
|
|
await stopServer(designServer.port, designServer.token);
|
|
designServer.proc.kill();
|
|
}
|
|
rmSync(tmp, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('/detect.js serves the detection overlay', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/detect.js`);
|
|
// May 404 if detect-antipatterns-browser.js hasn't been built
|
|
assert.ok(res.status === 200 || res.status === 404);
|
|
});
|
|
|
|
it('/poll returns timeout when no events queued', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=500`);
|
|
assert.equal(res.status, 200);
|
|
const data = await res.json();
|
|
assert.equal(data.type, 'timeout');
|
|
});
|
|
|
|
it('/poll rejects invalid token', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/poll?token=wrong&timeout=100`);
|
|
assert.equal(res.status, 401);
|
|
});
|
|
|
|
it('/stop rejects invalid token', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/stop?token=wrong`);
|
|
assert.equal(res.status, 401);
|
|
});
|
|
|
|
it('POST /events rejects invalid token', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token: 'wrong', type: 'exit' }),
|
|
});
|
|
assert.equal(res.status, 401);
|
|
});
|
|
|
|
it('POST /events validates event structure', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token: server.token, type: 'generate' }), // missing required fields
|
|
});
|
|
assert.equal(res.status, 400);
|
|
const data = await res.json();
|
|
assert.ok(data.error.includes('generate'));
|
|
});
|
|
|
|
// Regression: ids reach `execFileSync` argv and DOM attribute selectors.
|
|
// Anything outside the strict generator pattern must be rejected before it
|
|
// can leak into a downstream child_process or selector.
|
|
it('POST /events rejects accept with shell metacharacters in id', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
token: server.token,
|
|
type: 'accept',
|
|
id: '"; rm -rf /; #',
|
|
variantId: '0',
|
|
}),
|
|
});
|
|
assert.equal(res.status, 400);
|
|
const data = await res.json();
|
|
assert.ok(data.error.includes('id'));
|
|
});
|
|
|
|
it('POST /events rejects accept with non-numeric variantId', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
token: server.token,
|
|
type: 'accept',
|
|
id: 'a1b2c3d4',
|
|
variantId: '0; touch /tmp/owned',
|
|
}),
|
|
});
|
|
assert.equal(res.status, 400);
|
|
const data = await res.json();
|
|
assert.ok(data.error.includes('variantId'));
|
|
});
|
|
|
|
it('POST /events rejects discard with malformed id', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token: server.token, type: 'discard', id: 'not a uuid' }),
|
|
});
|
|
assert.equal(res.status, 400);
|
|
const data = await res.json();
|
|
assert.ok(data.error.includes('id'));
|
|
});
|
|
|
|
it('POST /events accepts valid exit event', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token: server.token, type: 'exit' }),
|
|
});
|
|
assert.equal(res.status, 200);
|
|
const data = await res.json();
|
|
assert.equal(data.ok, true);
|
|
});
|
|
|
|
it('events flow from browser POST to agent poll', async () => {
|
|
// Drain any queued events from previous tests
|
|
await drainPolls(server);
|
|
|
|
// Start a poll (will block until event arrives or timeout)
|
|
const pollPromise = fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=5000`)
|
|
.then(r => r.json());
|
|
|
|
// Give the poll a moment to register
|
|
await new Promise(r => setTimeout(r, 100));
|
|
|
|
// Send a generate event (simulating browser)
|
|
const postRes = await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
token: server.token,
|
|
type: 'generate',
|
|
id: 'a1b2c3d4',
|
|
action: 'bolder',
|
|
count: 2,
|
|
element: { outerHTML: '<div>test</div>', tagName: 'div' },
|
|
}),
|
|
});
|
|
assert.equal(postRes.status, 200);
|
|
|
|
// Poll should resolve with the event
|
|
const event = await pollPromise;
|
|
assert.equal(event.type, 'generate');
|
|
assert.equal(event.id, 'a1b2c3d4');
|
|
assert.equal(event.action, 'bolder');
|
|
assert.equal(event.count, 2);
|
|
|
|
await fetch(`http://localhost:${server.port}/poll`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token: server.token, id: 'test-e2e-1', type: 'done' }),
|
|
});
|
|
});
|
|
|
|
it('persists browser events to the durable session journal before poll delivery', async () => {
|
|
await drainPolls(server);
|
|
const journalPath = join(getLiveSessionsDir(REPO_ROOT), 'a1b2c3d6.jsonl');
|
|
rmSync(journalPath, { force: true });
|
|
|
|
const postRes = await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
token: server.token,
|
|
type: 'generate',
|
|
id: 'a1b2c3d6',
|
|
action: 'layout',
|
|
count: 3,
|
|
pageUrl: 'http://localhost:4321/',
|
|
element: { outerHTML: '<section>persist</section>', tagName: 'section' },
|
|
}),
|
|
});
|
|
assert.equal(postRes.status, 200);
|
|
|
|
assert.equal(
|
|
existsSync(journalPath),
|
|
true,
|
|
'event=live_server.journal_before_poll actor=browser operation=post_generate risk=server_restart_loses_unpolled_event expected=journal exists before agent poll actual=missing suggestion=append to live-session-store before enqueueing event',
|
|
);
|
|
const journal = readFileSync(journalPath, 'utf-8');
|
|
assert.match(journal, /"type":"generate"/);
|
|
|
|
await fetch(`http://localhost:${server.port}/poll`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token: server.token, id: 'a1b2c3d6', type: 'done' }),
|
|
});
|
|
});
|
|
|
|
it('accepts checkpoint events without exposing them as agent poll work', async () => {
|
|
await drainPolls(server);
|
|
const res = await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
token: server.token,
|
|
type: 'checkpoint',
|
|
id: 'a1b2c3d7',
|
|
phase: 'cycling',
|
|
revision: 2,
|
|
owner: 'browser-a',
|
|
arrivedVariants: 3,
|
|
visibleVariant: 2,
|
|
paramValues: { density: 'packed' },
|
|
}),
|
|
});
|
|
assert.equal(res.status, 200);
|
|
|
|
const polled = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=50`).then(r => r.json());
|
|
assert.equal(
|
|
polled.type,
|
|
'timeout',
|
|
'event=live_server.checkpoint_not_polled actor=browser operation=checkpoint risk=checkpoint_starves_agent_queue expected=timeout actual=' + polled.type + ' suggestion=journal checkpoint without enqueueing agent work',
|
|
);
|
|
|
|
const snapshot = JSON.parse(readFileSync(join(getLiveSessionsDir(REPO_ROOT), 'a1b2c3d7.snapshot.json'), 'utf-8'));
|
|
assert.equal(snapshot.visibleVariant, 2);
|
|
assert.deepEqual(snapshot.paramValues, { density: 'packed' });
|
|
});
|
|
|
|
it('redelivers an unacknowledged browser event after helper server restart', async () => {
|
|
const tmp = mkdtempSync(join(tmpdir(), 'impeccable-server-restart-'));
|
|
let firstServer;
|
|
let restarted;
|
|
try {
|
|
firstServer = await startServer(8519, { cwd: tmp });
|
|
const postRes = await fetch(`http://localhost:${firstServer.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
token: firstServer.token,
|
|
type: 'generate',
|
|
id: 'a1b2c3d8',
|
|
action: 'polish',
|
|
count: 2,
|
|
pageUrl: 'http://localhost:4321/',
|
|
element: { outerHTML: '<section>restart</section>', tagName: 'section' },
|
|
}),
|
|
});
|
|
assert.equal(postRes.status, 200);
|
|
|
|
await stopServer(firstServer.port, firstServer.token);
|
|
firstServer.proc.kill();
|
|
firstServer = null;
|
|
|
|
restarted = await startServer(8519, { cwd: tmp });
|
|
const replayed = await fetch(`http://localhost:${restarted.port}/poll?token=${restarted.token}&timeout=250&leaseMs=50`).then(r => r.json());
|
|
|
|
assert.equal(
|
|
replayed.id,
|
|
'a1b2c3d8',
|
|
'event=live_server.restart_replay actor=agent operation=poll_after_helper_restart risk=server_restart_loses_unpolled_event expected=a1b2c3d8 actual=' + replayed.id + ' suggestion=rebuild pending poll queue from live-session-store active snapshots on startup',
|
|
);
|
|
assert.equal(replayed.type, 'generate');
|
|
} finally {
|
|
if (firstServer) {
|
|
await stopServer(firstServer.port, firstServer.token);
|
|
firstServer.proc.kill();
|
|
}
|
|
if (restarted) {
|
|
await stopServer(restarted.port, restarted.token);
|
|
restarted.proc.kill();
|
|
}
|
|
rmSync(tmp, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('records explicit completion acknowledgements as completed durable sessions', async () => {
|
|
await drainPolls(server);
|
|
await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
token: server.token,
|
|
type: 'generate',
|
|
id: 'a1b2c3d9',
|
|
action: 'impeccable',
|
|
count: 1,
|
|
pageUrl: '/',
|
|
element: { outerHTML: '<button>Done</button>' },
|
|
}),
|
|
});
|
|
const polled = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=50`).then(r => r.json());
|
|
assert.equal(polled.id, 'a1b2c3d9');
|
|
const ack = await fetch(`http://localhost:${server.port}/poll`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token: server.token, id: 'a1b2c3d9', type: 'complete' }),
|
|
});
|
|
assert.equal(ack.status, 200);
|
|
const snapshot = JSON.parse(readFileSync(join(getLiveSessionsDir(REPO_ROOT), 'a1b2c3d9.snapshot.json'), 'utf-8'));
|
|
assert.equal(snapshot.phase, 'completed');
|
|
});
|
|
|
|
it('manual live-complete acknowledges the running helper queue before writing fallback journal state', async () => {
|
|
await drainPolls(server);
|
|
await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
token: server.token,
|
|
type: 'generate',
|
|
id: 'a1b2c3dc',
|
|
action: 'impeccable',
|
|
count: 1,
|
|
pageUrl: '/',
|
|
element: { outerHTML: '<button>Manual</button>' },
|
|
}),
|
|
});
|
|
const polled = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=50&leaseMs=50`).then(r => r.json());
|
|
assert.equal(polled.id, 'a1b2c3dc');
|
|
|
|
const completed = JSON.parse(execFileSync(process.execPath, [COMPLETE_SCRIPT, '--id', 'a1b2c3dc'], { cwd: REPO_ROOT, encoding: 'utf-8' }));
|
|
assert.equal(completed.phase, 'completed');
|
|
|
|
await new Promise(r => setTimeout(r, 75));
|
|
const stale = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=50&leaseMs=50`).then(r => r.json());
|
|
assert.equal(
|
|
stale.type,
|
|
'timeout',
|
|
'event=live_complete.running_server_ack actor=agent operation=manual_complete risk=completed_session_redelivered_from_memory expected=timeout actual=' + stale.id,
|
|
);
|
|
});
|
|
|
|
it('does not drop polled events until the agent acknowledges them', async () => {
|
|
await drainPolls(server);
|
|
|
|
const postRes = await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
token: server.token,
|
|
type: 'generate',
|
|
id: 'a1b2c3da',
|
|
action: 'polish',
|
|
count: 2,
|
|
element: { outerHTML: '<section>lease</section>', tagName: 'section' },
|
|
}),
|
|
});
|
|
assert.equal(postRes.status, 200);
|
|
|
|
const first = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=100&leaseMs=50`).then(r => r.json());
|
|
assert.equal(first.id, 'a1b2c3da');
|
|
|
|
const leased = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=25&leaseMs=50`).then(r => r.json());
|
|
assert.equal(leased.type, 'timeout', 'leased event should not be redelivered before lease expiry');
|
|
|
|
await new Promise(r => setTimeout(r, 75));
|
|
const redelivered = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=100&leaseMs=50`).then(r => r.json());
|
|
assert.equal(
|
|
redelivered.id,
|
|
'a1b2c3da',
|
|
'event=live_poll.lease_redelivery actor=agent operation=poll_after_missed_ack risk=agent_missed_event_loses_live_state expected=same event redelivered after lease expiry actual=' + redelivered.id + ' suggestion=inspect pending event lease bookkeeping',
|
|
);
|
|
|
|
await fetch(`http://localhost:${server.port}/poll`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token: server.token, id: 'a1b2c3da', type: 'done' }),
|
|
});
|
|
const acked = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=50&leaseMs=50`).then(r => r.json());
|
|
assert.equal(acked.type, 'timeout', 'acked event should be removed from the poll queue');
|
|
});
|
|
|
|
it('wakes a parked poll as soon as a missed-ack lease expires', async () => {
|
|
await drainPolls(server);
|
|
|
|
const postRes = await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
token: server.token,
|
|
type: 'generate',
|
|
id: 'a1b2c3db',
|
|
action: 'polish',
|
|
count: 1,
|
|
element: { outerHTML: '<section>wakeup</section>', tagName: 'section' },
|
|
}),
|
|
});
|
|
assert.equal(postRes.status, 200);
|
|
|
|
const first = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=100&leaseMs=60`).then(r => r.json());
|
|
assert.equal(first.id, 'a1b2c3db');
|
|
|
|
const startedAt = Date.now();
|
|
const redelivered = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=500&leaseMs=60`).then(r => r.json());
|
|
const elapsed = Date.now() - startedAt;
|
|
|
|
assert.equal(
|
|
redelivered.id,
|
|
'a1b2c3db',
|
|
'event=live_poll.lease_expiry_wakeup actor=agent operation=poll_before_lease_expiry risk=parked_poll_waits_full_timeout expected=a1b2c3db actual=' + redelivered.id,
|
|
);
|
|
assert.ok(
|
|
elapsed < 250,
|
|
'event=live_poll.lease_expiry_latency actor=agent operation=poll_before_lease_expiry risk=redelivery_waits_full_timeout expected=<250 actual=' + elapsed,
|
|
);
|
|
|
|
await fetch(`http://localhost:${server.port}/poll`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token: server.token, id: 'a1b2c3db', type: 'done' }),
|
|
});
|
|
});
|
|
|
|
it('agent reply is forwarded via SSE to browser', async () => {
|
|
// Use raw HTTP to read SSE (no EventSource in Node.js)
|
|
const controller = new AbortController();
|
|
const sseRes = await fetch(
|
|
`http://localhost:${server.port}/events?token=${server.token}`,
|
|
{ signal: controller.signal }
|
|
);
|
|
assert.equal(sseRes.status, 200);
|
|
assert.equal(sseRes.headers.get('content-type'), 'text/event-stream');
|
|
|
|
// Read the first message (should be "connected")
|
|
const reader = sseRes.body.getReader();
|
|
const decoder = new TextDecoder();
|
|
const { value: chunk1 } = await reader.read();
|
|
const text1 = decoder.decode(chunk1);
|
|
assert.ok(text1.includes('"connected"'));
|
|
|
|
// Send a reply from the agent
|
|
await fetch(`http://localhost:${server.port}/poll`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token: server.token, id: 'sse-test', type: 'done', file: 'x.html' }),
|
|
});
|
|
|
|
// Read the next SSE message
|
|
const { value: chunk2 } = await reader.read();
|
|
const text2 = decoder.decode(chunk2);
|
|
assert.ok(text2.includes('"done"'));
|
|
assert.ok(text2.includes('sse-test'));
|
|
|
|
controller.abort();
|
|
});
|
|
|
|
it('/source reads project files with valid token', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/source?token=${server.token}&path=package.json`);
|
|
assert.equal(res.status, 200);
|
|
const text = await res.text();
|
|
assert.ok(text.includes('"impeccable"'));
|
|
});
|
|
|
|
it('/source rejects path traversal', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/source?token=${server.token}&path=../../../etc/passwd`);
|
|
assert.equal(res.status, 400);
|
|
});
|
|
|
|
it('/source rejects invalid token', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/source?token=wrong&path=package.json`);
|
|
assert.equal(res.status, 401);
|
|
});
|
|
|
|
it('/source returns 404 for missing files', async () => {
|
|
try {
|
|
const res = await fetch(`http://localhost:${server.port}/source?token=${server.token}&path=nonexistent.xyz`);
|
|
assert.equal(res.status, 404);
|
|
} catch {
|
|
// Server may close socket on 404 for some Node versions
|
|
assert.ok(true, 'Server rejected request for missing file');
|
|
}
|
|
});
|
|
|
|
it('/modern-screenshot.js serves the vendored UMD build', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/modern-screenshot.js`);
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.headers.get('content-type'), 'application/javascript');
|
|
const text = await res.text();
|
|
// Sanity: the UMD build self-registers as window.modernScreenshot.
|
|
assert.ok(text.includes('modernScreenshot'));
|
|
});
|
|
|
|
it('POST /annotation rejects invalid token', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/annotation?token=wrong&eventId=abc`, {
|
|
method: 'POST', headers: { 'Content-Type': 'image/png' }, body: new Uint8Array([0x89, 0x50, 0x4e, 0x47]),
|
|
});
|
|
assert.equal(res.status, 401);
|
|
});
|
|
|
|
it('POST /annotation rejects invalid eventId', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/annotation?token=${server.token}&eventId=has%20spaces`, {
|
|
method: 'POST', headers: { 'Content-Type': 'image/png' }, body: new Uint8Array([0x89]),
|
|
});
|
|
assert.equal(res.status, 400);
|
|
});
|
|
|
|
it('POST /annotation rejects non-PNG content-type', async () => {
|
|
const res = await fetch(`http://localhost:${server.port}/annotation?token=${server.token}&eventId=abc`, {
|
|
method: 'POST', headers: { 'Content-Type': 'application/octet-stream' }, body: new Uint8Array([0x89]),
|
|
});
|
|
assert.equal(res.status, 415);
|
|
});
|
|
|
|
it('POST /annotation writes PNG to session dir and returns path', async () => {
|
|
const eventId = 'test-' + Math.random().toString(36).slice(2, 10);
|
|
// Minimal valid PNG header + IEND chunk (enough to prove we wrote bytes)
|
|
const png = new Uint8Array([
|
|
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
|
|
0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
|
|
]);
|
|
const res = await fetch(`http://localhost:${server.port}/annotation?token=${server.token}&eventId=${eventId}`, {
|
|
method: 'POST', headers: { 'Content-Type': 'image/png' }, body: png,
|
|
});
|
|
assert.equal(res.status, 200);
|
|
const data = await res.json();
|
|
assert.equal(data.ok, true);
|
|
assert.ok(data.path.endsWith(eventId + '.png'));
|
|
const written = readFileSync(data.path);
|
|
assert.equal(written.length, png.length);
|
|
});
|
|
|
|
it('POST /events accepts generate with optional annotation fields', async () => {
|
|
// Drain any queued events from previous tests
|
|
let drained;
|
|
do {
|
|
const r = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=100`);
|
|
drained = await r.json();
|
|
} while (drained.type !== 'timeout');
|
|
|
|
const postRes = await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
token: server.token, type: 'generate',
|
|
id: 'aa11bb22', action: 'polish', count: 2,
|
|
element: { outerHTML: '<div>x</div>', tagName: 'div' },
|
|
screenshotPath: '/tmp/fake.png',
|
|
comments: [{ x: 10, y: 20, text: 'tighten this' }],
|
|
strokes: [{ points: [[0, 0], [10, 10]] }],
|
|
}),
|
|
});
|
|
assert.equal(postRes.status, 200);
|
|
|
|
const pollRes = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=2000`);
|
|
const event = await pollRes.json();
|
|
assert.equal(event.id, 'aa11bb22');
|
|
assert.equal(event.screenshotPath, '/tmp/fake.png');
|
|
assert.equal(event.comments.length, 1);
|
|
assert.equal(event.strokes.length, 1);
|
|
});
|
|
|
|
it('POST /events rejects generate with malformed annotation fields', async () => {
|
|
const postRes = await fetch(`http://localhost:${server.port}/events`, {
|
|
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
token: server.token, type: 'generate',
|
|
id: 'cc33dd44', action: 'polish', count: 2,
|
|
element: { outerHTML: '<div>x</div>', tagName: 'div' },
|
|
comments: 'not-an-array',
|
|
}),
|
|
});
|
|
assert.equal(postRes.status, 400);
|
|
const data = await postRes.json();
|
|
assert.ok(data.error.includes('comments'));
|
|
});
|
|
});
|