architecture-harness 0.22.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -2
- package/src/cli/cli.test.mjs +6 -1
- package/src/cli/codex-implement.mjs +7 -2
- package/src/cli/codex-implement.test.mjs +29 -0
- package/src/cli/dispatch-reminder.mjs +4 -1
- package/src/cli/dispatch-reminder.test.mjs +5 -1
- package/src/cli/git-hooks.test.mjs +5 -0
- package/src/cli/qc.mjs +14 -1
- package/src/cli/screens-discovery.mjs +60 -0
- package/src/cli/screens.mjs +84 -0
- package/src/cli/stack.mjs +14 -0
- package/src/screens/auth-context.test.mjs +41 -0
- package/src/screens/auth-run.test.mjs +144 -0
- package/src/screens/auth.mjs +223 -0
- package/src/screens/auth.test.mjs +76 -0
- package/src/screens/capture-idle.test.mjs +26 -0
- package/src/screens/capture.browser-test.mjs +73 -0
- package/src/screens/capture.mjs +175 -0
- package/src/screens/capture.test.mjs +28 -0
- package/src/screens/catalog.browser-test.mjs +32 -0
- package/src/screens/catalog.mjs +72 -0
- package/src/screens/catalog.test.mjs +36 -0
- package/src/screens/compare.mjs +40 -0
- package/src/screens/compare.test.mjs +30 -0
- package/src/screens/discovery.browser-test.mjs +29 -0
- package/src/screens/discovery.mjs +158 -0
- package/src/screens/discovery.test.mjs +27 -0
- package/src/screens/inventory.mjs +69 -0
- package/src/screens/inventory.test.mjs +10 -0
- package/src/screens/map.mjs +63 -0
- package/src/screens/map.test.mjs +35 -0
- package/src/screens/open.browser-test.mjs +50 -0
- package/src/screens/regions.mjs +137 -0
- package/src/screens/regions.test.mjs +38 -0
- package/src/screens/session-files.mjs +39 -0
- package/src/screens/session-files.test.mjs +39 -0
- package/src/screens/setup.browser-test.mjs +49 -0
- package/src/screens/setup.mjs +78 -0
- package/src/screens/stack-auth.browser-test.mjs +30 -0
- package/src/screens/stack-auth.mjs +106 -0
- package/src/screens/stack-auth.test.mjs +65 -0
- package/src/screens/write-guard.mjs +26 -0
- package/src/stack/stack.mjs +131 -0
- package/src/stack/stack.test.mjs +40 -0
- package/src/stack/supervisor.mjs +30 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "architecture-harness",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Architecture gates, lint rules and scaffolding for the bounded-vertical-slice architecture",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"./eslint": "./src/eslint/index.mjs",
|
|
40
40
|
"./preset": "./src/eslint/preset.mjs",
|
|
41
41
|
"./config": "./src/config.mjs",
|
|
42
|
+
"./screens": "./src/screens/catalog.mjs",
|
|
42
43
|
"./gates/*": "./src/gates/*.mjs",
|
|
43
44
|
"./package.json": "./package.json",
|
|
44
45
|
"./ui/*": "./src/ui/*.mjs"
|
|
@@ -49,7 +50,8 @@
|
|
|
49
50
|
"README.md"
|
|
50
51
|
],
|
|
51
52
|
"scripts": {
|
|
52
|
-
"test": "node src/eslint/rules.test.mjs && node --test \"src/**/*.test.mjs\""
|
|
53
|
+
"test": "node src/eslint/rules.test.mjs && node --test \"src/**/*.test.mjs\"",
|
|
54
|
+
"test:browser": "node --test \"src/**/*.browser-test.mjs\""
|
|
53
55
|
},
|
|
54
56
|
"peerDependencies": {
|
|
55
57
|
"@typescript-eslint/eslint-plugin": ">=8",
|
|
@@ -98,5 +100,10 @@
|
|
|
98
100
|
"eslint-plugin-sonarjs": "^3",
|
|
99
101
|
"typescript": "^5",
|
|
100
102
|
"yaml": "^2.9.1"
|
|
103
|
+
},
|
|
104
|
+
"dependencies": {
|
|
105
|
+
"pixelmatch": "^8.0.0",
|
|
106
|
+
"playwright": "^1.64.1",
|
|
107
|
+
"pngjs": "^7.0.0"
|
|
101
108
|
}
|
|
102
109
|
}
|
package/src/cli/cli.test.mjs
CHANGED
|
@@ -267,8 +267,13 @@ test("a clean mirror run names every root it judged", async () => {
|
|
|
267
267
|
rmSync(dir, { recursive: true, force: true });
|
|
268
268
|
});
|
|
269
269
|
|
|
270
|
-
test("a fresh init repository with the default config passes the hooks check", async
|
|
270
|
+
test("a fresh init repository with the default config passes the hooks check", async t => {
|
|
271
271
|
const dir = repo();
|
|
272
|
+
const globalConfig = process.env.GIT_CONFIG_GLOBAL;
|
|
273
|
+
const fixtureConfig = path.join(dir, ".git", "fixture-global-config");
|
|
274
|
+
writeFileSync(fixtureConfig, "");
|
|
275
|
+
process.env.GIT_CONFIG_GLOBAL = fixtureConfig;
|
|
276
|
+
t.after(() => { if (globalConfig === undefined) delete process.env.GIT_CONFIG_GLOBAL; else process.env.GIT_CONFIG_GLOBAL = globalConfig; });
|
|
272
277
|
await quiet(() => runInit(load(dir)));
|
|
273
278
|
const config = load(dir);
|
|
274
279
|
assert.deepEqual(hookDrift(dir, requiredHooks(config)).problems, []);
|
|
@@ -40,7 +40,7 @@ export function parseArgs(argv) {
|
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
const isDone = (progress) => existsSync(progress) && readFileSync(progress, "utf8").
|
|
43
|
+
const isDone = (progress) => existsSync(progress) && readFileSync(progress, "utf8").trimEnd().split(/\r?\n/).at(-1).trim() === DONE_MARKER;
|
|
44
44
|
|
|
45
45
|
const SKILL_PLUGIN = "superpowers@claude-plugins-official";
|
|
46
46
|
const TOOL_OUTPUT_TOKENS = 4000;
|
|
@@ -60,12 +60,17 @@ const LEAN_RULES = [
|
|
|
60
60
|
"- Before the push, run the live database tests of the server code you touched through `npx qc throwaway-db --`. Never use port 5432 or another port the local stack owns.",
|
|
61
61
|
"- Work up to about 200K tokens in a session. Then leave the progress file exact and stop, so a fresh session continues.",
|
|
62
62
|
"- Keep the report under 15 lines.",
|
|
63
|
+
"- These rules never override the brief on commits or pushes. Where the brief says not to commit, make no commit. Where it says not to push, push nothing.",
|
|
63
64
|
].join("\n");
|
|
64
65
|
|
|
65
66
|
function continuationNote(progress) {
|
|
66
67
|
return (
|
|
67
68
|
`\n\n---\nThis may be a fresh session that continues earlier work. Read \`git log --oneline\` and \`git status --short\` ` +
|
|
68
|
-
`in the worktree, and the progress file ${progress}. Continue from the first unfinished task.\n\n
|
|
69
|
+
`in the worktree, and the progress file ${progress}. Continue from the first unfinished task.\n\n` +
|
|
70
|
+
`The runner owns the done marker: ${DONE_MARKER}. The brief never needs to supply it. ` +
|
|
71
|
+
`Done means the last non-empty line of the progress file equals the marker exactly, after trimming whitespace. ` +
|
|
72
|
+
`Write it as the last line, alone, only when the exit criteria hold. ` +
|
|
73
|
+
`Never quote it anywhere else. Never wait for anyone to supply it.\n\n${LEAN_RULES}`
|
|
69
74
|
);
|
|
70
75
|
}
|
|
71
76
|
|
|
@@ -21,6 +21,29 @@ function setup(t, { progress = "" } = {}) {
|
|
|
21
21
|
|
|
22
22
|
const quiet = () => {};
|
|
23
23
|
|
|
24
|
+
for (const [name, progress] of [
|
|
25
|
+
["marker in prose", `Do not append ${DONE_MARKER} until the push succeeds.\n`],
|
|
26
|
+
["marker before more lines", `${DONE_MARKER}\nPush remains unfinished.\n`],
|
|
27
|
+
["quoted marker", `"${DONE_MARKER}"\n`],
|
|
28
|
+
["empty progress", "\n \n"],
|
|
29
|
+
]) {
|
|
30
|
+
test(`${name} never ends the job before or after a run`, async (t) => {
|
|
31
|
+
const { argv } = setup(t, { progress });
|
|
32
|
+
let calls = 0;
|
|
33
|
+
const run = async () => {
|
|
34
|
+
calls += 1;
|
|
35
|
+
return { status: 0, tail: "work remains" };
|
|
36
|
+
};
|
|
37
|
+
assert.equal(await runCodexImplement([...argv, "--max-runs", "1"], { run, say: quiet }), 3);
|
|
38
|
+
assert.equal(calls, 1);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
test("the last non-empty line accepts the exact marker with surrounding whitespace", async (t) => {
|
|
43
|
+
const { argv } = setup(t, { progress: `Tasks pushed.\r\n \t${DONE_MARKER}\t \r\n \r\n\t\r\n` });
|
|
44
|
+
assert.equal(await runCodexImplement(argv, { run: async () => assert.fail("no run"), say: quiet }), 0);
|
|
45
|
+
});
|
|
46
|
+
|
|
24
47
|
test("a progress file that already says ALL TASKS DONE exits 0 with no run", async (t) => {
|
|
25
48
|
const { argv } = setup(t, { progress: `task 1 committed\n${DONE_MARKER}\n` });
|
|
26
49
|
let calls = 0;
|
|
@@ -79,11 +102,17 @@ test("every run prompt carries the lean rules after the brief", async (t) => {
|
|
|
79
102
|
assert.equal(seen.length, 2);
|
|
80
103
|
for (const prompt of seen) {
|
|
81
104
|
assert.ok(prompt.startsWith(readFileSync(brief, "utf8")));
|
|
105
|
+
assert.ok(prompt.includes(DONE_MARKER));
|
|
106
|
+
assert.match(prompt, /last non-empty line.*equals.*exactly.*trimming whitespace/i);
|
|
107
|
+
assert.match(prompt, /last line, alone.*only when.*exit criteria/i);
|
|
108
|
+
assert.match(prompt, /never quote.*anywhere else/i);
|
|
109
|
+
assert.match(prompt, /never wait for anyone to supply/i);
|
|
82
110
|
assert.match(prompt, /at most 40 lines/);
|
|
83
111
|
assert.match(prompt, /re-verify/);
|
|
84
112
|
assert.match(prompt, /line ranges/);
|
|
85
113
|
assert.match(prompt, /helper agents/);
|
|
86
114
|
assert.match(prompt, /Then run the local check once, make one commit, and push once\./);
|
|
115
|
+
assert.match(prompt, /never override the brief on commits or pushes/);
|
|
87
116
|
assert.match(prompt, /npx qc gate-slot --/);
|
|
88
117
|
assert.match(prompt, /npx qc throwaway-db --/);
|
|
89
118
|
assert.match(prompt, /Never use port 5432/);
|
|
@@ -32,7 +32,10 @@ export const WORKER_NOTE =
|
|
|
32
32
|
"Write every task in your brief: write the failing test, run only that test file, make it pass, " +
|
|
33
33
|
"and run typecheck and lint on what you touched. Then run the local check once, make one commit, and push once. " +
|
|
34
34
|
"Do not run the full gate (`pnpm check`) or e2e; CI runs them. " +
|
|
35
|
-
"
|
|
35
|
+
"The runner owns the done marker `ALL TASKS DONE`; the brief never needs to supply it. " +
|
|
36
|
+
"Done means the last non-empty line of the progress file equals the marker exactly, after trimming whitespace. " +
|
|
37
|
+
"Write it as the last line, alone, only when the exit criteria hold and every task is pushed. " +
|
|
38
|
+
"Never quote it anywhere else. Never wait for anyone to supply it. " +
|
|
36
39
|
"If your context gets large, write notes to the progress file and end the turn: a fresh session continues.";
|
|
37
40
|
|
|
38
41
|
const ROLES = ["sdd-planner", "sdd-implementer", "sdd-reviewer", "sdd-branch-reviewer"];
|
|
@@ -296,7 +296,11 @@ test("the worker note says to work inline, write every task, check and commit on
|
|
|
296
296
|
/typecheck and lint on what you touched/,
|
|
297
297
|
/Then run the local check once, make one commit, and push once./,
|
|
298
298
|
/Do not run the full gate \(`pnpm check`\) or e2e; CI runs them/,
|
|
299
|
-
/
|
|
299
|
+
/runner owns the done marker `ALL TASKS DONE`/,
|
|
300
|
+
/last non-empty line.*equals.*exactly.*trimming whitespace/,
|
|
301
|
+
/last line, alone, only when the exit criteria hold/,
|
|
302
|
+
/Never quote it anywhere else/,
|
|
303
|
+
/Never wait for anyone to supply it/,
|
|
300
304
|
/context gets large.*end the turn.*fresh session continues/,
|
|
301
305
|
]) {
|
|
302
306
|
assert.match(WORKER_NOTE, phrase);
|
|
@@ -15,6 +15,11 @@ function repo(t) {
|
|
|
15
15
|
const root = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-git-hooks-")));
|
|
16
16
|
t.after(() => rmSync(root, { recursive: true, force: true }));
|
|
17
17
|
execFileSync("git", ["init", "-q"], { cwd: root, stdio: "pipe" });
|
|
18
|
+
const globalConfig = process.env.GIT_CONFIG_GLOBAL;
|
|
19
|
+
const fixtureConfig = path.join(root, ".git", "fixture-global-config");
|
|
20
|
+
writeFileSync(fixtureConfig, "");
|
|
21
|
+
process.env.GIT_CONFIG_GLOBAL = fixtureConfig;
|
|
22
|
+
t.after(() => { if (globalConfig === undefined) delete process.env.GIT_CONFIG_GLOBAL; else process.env.GIT_CONFIG_GLOBAL = globalConfig; });
|
|
18
23
|
const git = (...args) => execFileSync("git", args, { cwd: root, stdio: "pipe" });
|
|
19
24
|
const hook = (dir, name, text) => {
|
|
20
25
|
mkdirSync(path.join(root, dir), { recursive: true });
|
package/src/cli/qc.mjs
CHANGED
|
@@ -24,11 +24,16 @@ const USAGE = `qc — architecture gates
|
|
|
24
24
|
qc decisions [id] where decisions are cited; --squash closes the gaps
|
|
25
25
|
qc ledger [--cost] [branch] the workflow ledger: dispatches, stops, verdicts, merges, issue updates; --cost totals tokens and tool calls
|
|
26
26
|
qc codex-implement --brief <file> --worktree <path> --progress <file> [--log <file>] [--max-runs <n>]
|
|
27
|
-
run a brief through codex exec as an implementer
|
|
27
|
+
run a brief through codex exec as an implementer; done means the last non-empty progress line equals ALL TASKS DONE after trimming whitespace
|
|
28
|
+
the runner supplies the marker; write it as the last line, alone, only when the exit criteria hold; never quote it elsewhere or wait for it; exit 2 on a Codex limit
|
|
28
29
|
qc gate-slot [--slots <n>] -- <command...>
|
|
29
30
|
run a heavy local command in one of the machine's gate slots; --status lists them
|
|
30
31
|
qc watch [--pr <n>]... [--codex <wrapper.log>]... [--run <workflow>@<branch>]... [--max <minutes>] [--repo <owner/name>] [--interval <seconds>]
|
|
31
32
|
block until the first event among the items, print one EVENT line; exit 0 done, 4 bound (default 55 minutes), 5 failed, 6 queued past 15 minutes
|
|
33
|
+
qc screens --map <json> --targets <left,right> --out <dir> [--only <names>] [--changed <target>]
|
|
34
|
+
capture authenticated screen pairs, compare pixels, and write a static gallery
|
|
35
|
+
qc stack up|down|status [names...] [--timeout <milliseconds>]
|
|
36
|
+
run repository plugins from ~/.qc/stacks.json
|
|
32
37
|
qc plan-check <plan.md> each task names files, a test step and a commit step, within the tool-call budget
|
|
33
38
|
qc tokens [--session <id>] [--project <dir>] [--claude-dir <dir>] [--json]
|
|
34
39
|
one session's tokens and cache hit rate per agent type, from its transcripts
|
|
@@ -54,6 +59,14 @@ Every gate reads qc.config.json. A key it does not set keeps the reference defau
|
|
|
54
59
|
|
|
55
60
|
async function main() {
|
|
56
61
|
const [command, ...rest] = process.argv.slice(2);
|
|
62
|
+
if (command === "stack") {
|
|
63
|
+
const { runStack } = await import("./stack.mjs");
|
|
64
|
+
process.exit(await runStack(rest));
|
|
65
|
+
}
|
|
66
|
+
if (command === "screens") {
|
|
67
|
+
const { runScreens } = await import("./screens.mjs");
|
|
68
|
+
process.exit(await runScreens(rest));
|
|
69
|
+
}
|
|
57
70
|
// A broken qc.config.json must not keep a lease in place, so the lease runs before the load.
|
|
58
71
|
if (command === "lease") {
|
|
59
72
|
const { runLease } = await import("./lease.mjs");
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { discover, pairStates, catalogCoverage, scaffoldEntry } from "../screens/discovery.mjs";
|
|
4
|
+
import { targetSession } from "../screens/stack-auth.mjs";
|
|
5
|
+
|
|
6
|
+
/** Trigger: discovery artifacts drive pairing, coverage, and catalog scaffolds. */
|
|
7
|
+
export async function runDiscovery(command, map, values, { chromium, say }) {
|
|
8
|
+
const out = path.resolve(values.out ?? ".");
|
|
9
|
+
mkdirSync(out, { recursive: true });
|
|
10
|
+
const read = name => JSON.parse(readFileSync(path.join(out, `discovered-${name}.json`), "utf8"));
|
|
11
|
+
if (command === "discover") {
|
|
12
|
+
const target = map.targets[values.target];
|
|
13
|
+
if (!target) throw new Error("Choose --target from the map");
|
|
14
|
+
const timeout = Number(values.timeout ?? 15000);
|
|
15
|
+
const depth = Number(values.depth ?? 4);
|
|
16
|
+
const maxStates = Number(values["max-states"] ?? 500);
|
|
17
|
+
if (!Number.isInteger(depth) || depth < 0 || !Number.isInteger(maxStates) || maxStates < 1) throw new Error("Invalid depth or state bound");
|
|
18
|
+
const session = await targetSession(chromium, target, { timeout, signInTimeout: Number(values["sign-in-timeout"] ?? 180000), targetName: values.target, say });
|
|
19
|
+
try {
|
|
20
|
+
const routes = [...new Map(map.screens.flatMap(screen => screen.targets[values.target]?.path ? [screen.targets[values.target].path] : []).map(route => [route, route])).values()];
|
|
21
|
+
const result = await discover(session, { ...target, routes: target.routes ?? routes }, { out, targetName: values.target, widths: map.widths, depth, timeout, maxStates, allowWrites: values["allow-writes"], say });
|
|
22
|
+
say(JSON.stringify({ target: result.target, states: result.states.length, counts: result.counts, errors: result.errors.length, unresolvedRoutes: result.unresolvedRoutes, blockedRequests: result.blockedRequests, complete: result.complete }));
|
|
23
|
+
return result.complete ? 0 : 1;
|
|
24
|
+
} finally { await session.close(); }
|
|
25
|
+
}
|
|
26
|
+
if (command === "pair") {
|
|
27
|
+
const names = values.targets?.split(",");
|
|
28
|
+
if (names?.length !== 2) throw new Error("pair requires --targets <legacy,mono>");
|
|
29
|
+
const pairs = pairStates(read(names[0]).states, read(names[1]).states);
|
|
30
|
+
const result = { targets: names, pairs, counts: Object.fromEntries(["paired", "legacy-only", "mono-only"].map(kind => [kind, pairs.filter(item => item.kind === kind).length])) };
|
|
31
|
+
writeFileSync(path.join(out, "pairs.json"), JSON.stringify(result, null, 2));
|
|
32
|
+
say(JSON.stringify(result.counts));
|
|
33
|
+
return 0;
|
|
34
|
+
}
|
|
35
|
+
const names = values.target ? [values.target] : readdirSync(out).filter(name => /^discovered-.+\.json$/.test(name)).map(name => name.slice(11, -5));
|
|
36
|
+
if (!names.length) throw new Error("No discoveries found");
|
|
37
|
+
if (command === "coverage") {
|
|
38
|
+
const rows = names.map(name => {
|
|
39
|
+
const discovered = read(name);
|
|
40
|
+
return { target: name, complete: discovered.complete, ...catalogCoverage(discovered.states, map.screens, map.excluded, name) };
|
|
41
|
+
});
|
|
42
|
+
writeFileSync(path.join(out, "coverage.json"), JSON.stringify(rows, null, 2));
|
|
43
|
+
for (const row of rows) say(JSON.stringify({ target: row.target, complete: row.complete, covered: row.covered, total: row.total, missing: row.missing.length }));
|
|
44
|
+
return rows.some(row => !row.complete || row.missing.length) ? 1 : 0;
|
|
45
|
+
}
|
|
46
|
+
const directory = path.resolve(values["catalog-out"] ?? path.join(out, "catalog"));
|
|
47
|
+
mkdirSync(directory, { recursive: true });
|
|
48
|
+
let written = 0;
|
|
49
|
+
for (const name of names) {
|
|
50
|
+
const family = map.targets[name]?.family ?? name.split("-")[0];
|
|
51
|
+
for (const state of read(name).states) {
|
|
52
|
+
const file = path.join(directory, `${name}-${state.id}.ts`);
|
|
53
|
+
if (existsSync(file)) continue;
|
|
54
|
+
writeFileSync(file, scaffoldEntry(state, family), { flag: "wx" });
|
|
55
|
+
written++;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
say(JSON.stringify({ directory, written }));
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { loadMap } from "../screens/map.mjs";
|
|
4
|
+
import { captureScreens } from "../screens/capture.mjs";
|
|
5
|
+
import { authStatus, targetSession } from "../screens/stack-auth.mjs";
|
|
6
|
+
import { applyCatalog, openScreen } from "../screens/catalog.mjs";
|
|
7
|
+
import { runDiscovery } from "./screens-discovery.mjs";
|
|
8
|
+
import { setupLocal } from "../screens/setup.mjs";
|
|
9
|
+
import { installWriteGuard } from "../screens/write-guard.mjs";
|
|
10
|
+
|
|
11
|
+
export const USAGE = `qc screens [run [names...]|list|open <name>|auth status|auth <target> --headed|setup]
|
|
12
|
+
qc screens discover|pair|coverage|scaffold --map <json> --out <dir>
|
|
13
|
+
--map <json> [--targets <left,right>] [--target <name>] [--out <dir>]
|
|
14
|
+
[--only <names>] [--changed <target>] [--height <pixels>]
|
|
15
|
+
[--timeout <milliseconds>] [--sign-in-timeout <milliseconds>]
|
|
16
|
+
[--depth <levels>] [--max-states <count>] [--allow-writes] [--catalog-out <dir>]
|
|
17
|
+
|
|
18
|
+
Chrome must be installed. Sessions stay in ~/.qc/screens.
|
|
19
|
+
Catalog modules live outside the kit. open waits until you close its window.`;
|
|
20
|
+
|
|
21
|
+
/** Trigger: screen commands share the same catalog, sessions, and map. */
|
|
22
|
+
export async function runScreens(argv, { say = console.error, sessionFactory = targetSession } = {}) {
|
|
23
|
+
let values, positionals;
|
|
24
|
+
const command = argv[0]?.startsWith("--") || !argv.length ? "run" : argv.shift();
|
|
25
|
+
const authTarget = command === "auth" ? argv.shift() : undefined;
|
|
26
|
+
const authCommand = command === "auth" && authTarget === "status";
|
|
27
|
+
try {
|
|
28
|
+
({ values, positionals } = parseArgs({ args: argv, allowPositionals: true, strict: true, options: { map: { type: "string" }, targets: { type: "string" }, target: { type: "string" }, out: { type: "string" }, only: { type: "string" }, changed: { type: "string" }, height: { type: "string" }, timeout: { type: "string" }, "sign-in-timeout": { type: "string" }, depth: { type: "string" }, "max-states": { type: "string" }, "allow-writes": { type: "boolean" }, "catalog-out": { type: "string" }, headed: { type: "boolean" }, help: { type: "boolean" } } }));
|
|
29
|
+
if (values.help) { say(USAGE); return 0; }
|
|
30
|
+
if (!values.map) throw new Error("--map is required");
|
|
31
|
+
for (const field of ["height", "timeout", "sign-in-timeout"]) if (values[field] !== undefined && (!Number.isInteger(Number(values[field])) || Number(values[field]) < 1)) throw new Error(`--${field} must be a positive integer`);
|
|
32
|
+
const selectedTargets = values.targets?.split(",") ?? (values.target ? [values.target] : command === "auth" && !authCommand && authTarget ? [authTarget] : command === "list" ? [] : undefined);
|
|
33
|
+
const map = await applyCatalog(loadMap(path.resolve(values.map), { targets: selectedTargets }), path.resolve(values.map));
|
|
34
|
+
if (command === "list") {
|
|
35
|
+
for (const item of map.screens) say(`${item.name}\t${Object.keys(item.targets).join(", ")}`);
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
const { chromium } = await import("playwright");
|
|
39
|
+
if (command === "setup") {
|
|
40
|
+
const target = map.targets[values.target];
|
|
41
|
+
if (!target) throw new Error("Choose --target from the map");
|
|
42
|
+
say(JSON.stringify(await setupLocal(target, values.target, { chromium, timeout: Number(values.timeout ?? 15000), signInTimeout: Number(values["sign-in-timeout"] ?? 180000), say })));
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
if (["discover", "pair", "coverage", "scaffold"].includes(command)) return await runDiscovery(command, map, values, { chromium, say });
|
|
46
|
+
const timeout = Number(values.timeout ?? 15000);
|
|
47
|
+
const signInTimeout = Number(values["sign-in-timeout"] ?? 180000);
|
|
48
|
+
if (authCommand) {
|
|
49
|
+
const targets = values.target ? { [values.target]: map.targets[values.target] } : map.targets;
|
|
50
|
+
if (Object.values(targets).some(target => !target)) throw new Error("Unknown target");
|
|
51
|
+
for (const row of await authStatus(chromium, targets, { timeout })) say(JSON.stringify(row));
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
54
|
+
if (command === "auth") {
|
|
55
|
+
const target = map.targets[authTarget];
|
|
56
|
+
if (!target || !values.headed) throw new Error("Choose auth <target> --headed from the map");
|
|
57
|
+
const session = await targetSession(chromium, target, { timeout, signInTimeout, say, targetName: authTarget, allowInteractive: true, headed: true });
|
|
58
|
+
await session.close();
|
|
59
|
+
say(`${authTarget}: signed in`);
|
|
60
|
+
return 0;
|
|
61
|
+
}
|
|
62
|
+
if (command === "open") {
|
|
63
|
+
const target = map.targets[values.target];
|
|
64
|
+
const item = map.screens.find(item => item.name === positionals[0]);
|
|
65
|
+
const route = item?.targets[values.target];
|
|
66
|
+
if (!target || !route) throw new Error("Choose a screen name and --target from the map");
|
|
67
|
+
const allowWrites = values["allow-writes"] === true;
|
|
68
|
+
const session = await sessionFactory(chromium, target, { timeout, signInTimeout, say, targetName: values.target, headed: true, allowWrites });
|
|
69
|
+
try {
|
|
70
|
+
await installWriteGuard(session.context, target, { allowWrites });
|
|
71
|
+
await session.page.setViewportSize({ width: map.widths[0], height: Number(values.height ?? 900) });
|
|
72
|
+
if (route.open) await openScreen(session.page, target, route, { targetName: values.target, screenName: item.name, out: path.resolve(values.out ?? "."), timeout });
|
|
73
|
+
else await session.page.goto(new URL(route.path, target.baseUrl).href, { timeout });
|
|
74
|
+
await new Promise(resolve => session.page.once("close", resolve));
|
|
75
|
+
} finally { await session.close().catch(() => {}); }
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
if (command !== "run") throw new Error(`Unknown screens command ${command}`);
|
|
79
|
+
if (!values.targets || !values.out) throw new Error("--targets and --out are required");
|
|
80
|
+
const report = await captureScreens(map, { targets: values.targets.split(",").map(name => name.trim()), out: path.resolve(values.out), only: positionals.length ? positionals.join(",") : values.only, changed: values.changed, height: Number(values.height ?? 900), timeout, signInTimeout, allowWrites: values["allow-writes"] === true }, { chromium, say });
|
|
81
|
+
for (const row of report) say(JSON.stringify(row));
|
|
82
|
+
return report.some(row => row.error) ? 1 : 0;
|
|
83
|
+
} catch (error) { say(error.exitCode === 4 ? error.message : `qc screens: ${error.message}`); return error.exitCode === 4 ? 4 : 1; }
|
|
84
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { stackAction } from "../stack/stack.mjs";
|
|
3
|
+
|
|
4
|
+
/** Trigger: a repository plugin owns each stack's lifecycle. */
|
|
5
|
+
export async function runStack(argv, { say = console.log } = {}) {
|
|
6
|
+
try {
|
|
7
|
+
const [action, ...rest] = argv;
|
|
8
|
+
const { values, positionals } = parseArgs({ args: rest, allowPositionals: true, options: { timeout: { type: "string", default: "120000" } } });
|
|
9
|
+
const timeout = Number(values.timeout);
|
|
10
|
+
if (!Number.isInteger(timeout) || timeout < 1) throw new Error("--timeout must be a positive integer");
|
|
11
|
+
for (const row of await stackAction(action, positionals, { timeout })) say(JSON.stringify(row));
|
|
12
|
+
return 0;
|
|
13
|
+
} catch (error) { say(`qc stack: ${error.message}`); return 1; }
|
|
14
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { browserSession, seedSession } from "./auth.mjs";
|
|
7
|
+
|
|
8
|
+
for (const method of ["profile", "state", "headed", "seed"]) {
|
|
9
|
+
test(`${method} context blocks service workers`, async t => {
|
|
10
|
+
const root = mkdtempSync(path.join(os.tmpdir(), "qc-context-"));
|
|
11
|
+
t.after(() => rmSync(root, { recursive: true, force: true }));
|
|
12
|
+
t.mock.method(os, "homedir", () => root);
|
|
13
|
+
const directory = path.join(root, ".qc", "screens");
|
|
14
|
+
mkdirSync(path.join(directory, "state"), { recursive: true });
|
|
15
|
+
const emptyState = { cookies: [], origins: [] };
|
|
16
|
+
if (method === "state") writeFileSync(path.join(directory, "state", "tenant.test.json"), JSON.stringify(emptyState));
|
|
17
|
+
if (method === "seed") writeFileSync(path.join(directory, "local-4567.json"), JSON.stringify(emptyState));
|
|
18
|
+
const source = path.join(root, "chrome");
|
|
19
|
+
if (method === "profile") {
|
|
20
|
+
mkdirSync(source);
|
|
21
|
+
writeFileSync(path.join(source, "Local State"), JSON.stringify({ profile: { last_used: "Default" } }));
|
|
22
|
+
}
|
|
23
|
+
const target = { baseUrl: "http://localhost:4567", auth: { domain: "tenant.test", signedIn: "main" } };
|
|
24
|
+
const context = {
|
|
25
|
+
close: async () => {}, route: async () => {}, storageState: async () => emptyState,
|
|
26
|
+
newPage: async () => ({ goto: async () => {}, url: () => target.baseUrl, locator: () => ({ first: () => ({ waitFor: async () => {} }) }) }),
|
|
27
|
+
};
|
|
28
|
+
const factories = [];
|
|
29
|
+
const chromium = {
|
|
30
|
+
launchPersistentContext: async (_directory, options) => { factories.push(["persistent", options]); return context; },
|
|
31
|
+
launch: async () => ({ close: async () => {}, newContext: async options => { factories.push(["new", options]); return context; } }),
|
|
32
|
+
};
|
|
33
|
+
const session = await (method === "seed"
|
|
34
|
+
? seedSession(chromium, target, { savedOnly: true })
|
|
35
|
+
: browserSession(chromium, target, { chromeProfile: source, allowInteractive: method !== "state", say: () => {} }));
|
|
36
|
+
await session.close();
|
|
37
|
+
assert.equal(factories.length, 1);
|
|
38
|
+
assert.equal(factories[0][0], method === "profile" ? "persistent" : "new");
|
|
39
|
+
assert.equal(factories[0][1].serviceWorkers, "block");
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import http from "node:http";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, readdirSync, rmSync } from "node:fs";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { chromium } from "playwright";
|
|
9
|
+
import { browserSession, seedSession } from "./auth.mjs";
|
|
10
|
+
import { runScreens } from "../cli/screens.mjs";
|
|
11
|
+
import { captureScreens } from "./capture.mjs";
|
|
12
|
+
|
|
13
|
+
async function fixture(t) {
|
|
14
|
+
const root = mkdtempSync(path.join(os.tmpdir(), "qc-auth-run-"));
|
|
15
|
+
t.mock.method(os, "homedir", () => root);
|
|
16
|
+
const requests = [];
|
|
17
|
+
const server = http.createServer((req, res) => {
|
|
18
|
+
requests.push(req.url);
|
|
19
|
+
res.setHeader("content-type", "text/html");
|
|
20
|
+
if (req.url.startsWith("/bff/login") && req.headers.cookie?.includes("sso=yes")) {
|
|
21
|
+
res.writeHead(302, { "set-cookie": "app=yes; Path=/; HttpOnly", location: "/projects" }).end();
|
|
22
|
+
} else if (req.url === "/projects" && req.headers.cookie?.includes("app=yes")) res.end('<main id="signed">Projects</main>');
|
|
23
|
+
else res.end('<input id="user"><input id="pass" type="password"><button id="submit">Login</button>');
|
|
24
|
+
});
|
|
25
|
+
await new Promise(resolve => server.listen(0, "127.0.0.1", resolve));
|
|
26
|
+
t.after(async () => { await new Promise(resolve => server.close(resolve)); rmSync(root, { recursive: true, force: true }); });
|
|
27
|
+
const baseUrl = `http://127.0.0.1:${server.address().port}`;
|
|
28
|
+
const target = { baseUrl, auth: { mode: "browser-session", domain: "fixture.auth0.test", path: "/projects", signedIn: "#signed" } };
|
|
29
|
+
const state = path.join(root, ".qc/screens/state/fixture.auth0.test.json");
|
|
30
|
+
mkdirSync(path.dirname(state), { recursive: true });
|
|
31
|
+
const save = cookies => writeFileSync(state, JSON.stringify({ cookies: cookies.map(name => ({ name, value: "yes", domain: "127.0.0.1", path: "/", expires: -1, httpOnly: true, secure: false, sameSite: "Lax" })), origins: [] }));
|
|
32
|
+
return { root, requests, target, state, save };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
test("expired application state refreshes once through the login path and persists the renewed cookie", async t => {
|
|
36
|
+
const f = await fixture(t);
|
|
37
|
+
f.save(["sso"]);
|
|
38
|
+
const session = await browserSession(chromium, f.target, { targetName: "mono-local", timeout: 3000, allowInteractive: false });
|
|
39
|
+
await session.close();
|
|
40
|
+
assert.equal(f.requests.filter(p => p.startsWith("/bff/login")).length, 1);
|
|
41
|
+
assert.ok(JSON.parse(readFileSync(f.state)).cookies.some(c => c.name === "app"));
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("seed errors name the target and failing field without exposing browser diagnostics or credentials", async t => {
|
|
45
|
+
const f = await fixture(t);
|
|
46
|
+
const target = { ...f.target, auth: { ...f.target.auth, mode: "seed-login", path: "/login", userSelector: "#user", passwordSelector: "#missing", submitSelector: "#submit" } };
|
|
47
|
+
await assert.rejects(seedSession(chromium, target, { targetName: "legacy-local", credentials: { user: "test", password: randomUUID() }, timeout: 3000 }), error => {
|
|
48
|
+
assert.equal(error.message, "legacy-local: seed sign-in failed at the password field");
|
|
49
|
+
return true;
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("run stops with code four and one human instruction without capturing or retrying", async t => {
|
|
54
|
+
const f = await fixture(t);
|
|
55
|
+
f.save([]);
|
|
56
|
+
const map = path.join(f.root, "map.json");
|
|
57
|
+
writeFileSync(map, JSON.stringify({ widths: [200], targets: { "mono-local": f.target, other: f.target }, screens: [{ name: "projects", targets: { "mono-local": { path: "/projects" }, other: { path: "/projects" } } }] }));
|
|
58
|
+
const lines = [];
|
|
59
|
+
const code = await runScreens(["run", "projects", "--map", map, "--targets", "mono-local,other", "--out", path.join(f.root, "out"), "--timeout", "3000", "--sign-in-timeout", "100"], { say: line => lines.push(line) });
|
|
60
|
+
assert.equal(code, 4);
|
|
61
|
+
assert.deepEqual(lines, ["mono-local: needs a person to sign in (run qc screens auth mono-local --headed)"]);
|
|
62
|
+
assert.equal(f.requests.filter(p => p.startsWith("/bff/login")).length, 1);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("capture checks each target once before navigating any screen and closes earlier sessions on failure", async t => {
|
|
66
|
+
const f = await fixture(t);
|
|
67
|
+
const events = [];
|
|
68
|
+
await assert.rejects(captureScreens({ widths: [200], targets: { first: f.target, second: f.target }, screens: [{ name: "projects", targets: { first: { path: "/projects" }, second: { path: "/projects" } } }] }, { targets: ["first", "second"], out: path.join(f.root, "out") }, {
|
|
69
|
+
sessionFactory: async (_chromium, _target, options) => {
|
|
70
|
+
events.push(options.targetName);
|
|
71
|
+
assert.equal(options.allowInteractive, false);
|
|
72
|
+
if (options.targetName === "second") throw new Error("second failed");
|
|
73
|
+
return { context: { route: async () => {} }, page: { setViewportSize: async () => { throw new Error("capture before preflight"); } }, close: async () => events.push("closed") };
|
|
74
|
+
},
|
|
75
|
+
}), /second failed/);
|
|
76
|
+
assert.deepEqual(events, ["first", "second", "closed"]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
// A headed browser needs a display, which a Linux CI runner lacks.
|
|
81
|
+
test("headed auth accepts the named target and saves a valid session", { skip: process.platform === "linux" && !process.env.DISPLAY }, async t => {
|
|
82
|
+
const f = await fixture(t);
|
|
83
|
+
f.save(["app"]);
|
|
84
|
+
const map = path.join(f.root, "map.json");
|
|
85
|
+
writeFileSync(map, JSON.stringify({ widths: [200], targets: { "mono-local": f.target, other: f.target }, screens: [{ name: "projects", targets: { "mono-local": { path: "/projects" }, other: { path: "/projects" } } }] }));
|
|
86
|
+
const lines = [];
|
|
87
|
+
assert.equal(await runScreens(["auth", "mono-local", "--headed", "--map", map, "--timeout", "3000"], { say: line => lines.push(line) }), 0);
|
|
88
|
+
assert.deepEqual(lines, ["mono-local: signed in"]);
|
|
89
|
+
assert.ok(JSON.parse(readFileSync(f.state)).cookies.some(c => c.name === "app"));
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("parallel sessions preserve the shared saved state and write a complete refresh ledger", async t => {
|
|
93
|
+
const f = await fixture(t);
|
|
94
|
+
f.save(["sso"]);
|
|
95
|
+
const { targetSession } = await import("./stack-auth.mjs");
|
|
96
|
+
const sessions = await Promise.all(Array.from({ length: 3 }, () => targetSession(chromium, f.target, { targetName: "mono-local", timeout: 3000, allowInteractive: false })));
|
|
97
|
+
await Promise.all(sessions.map(s => s.close()));
|
|
98
|
+
assert.equal(f.requests.filter(p => p.startsWith("/bff/login")).length, 1);
|
|
99
|
+
assert.ok(JSON.parse(readFileSync(f.state)).cookies.some(c => c.name === "app"));
|
|
100
|
+
const files = readdirSync(path.join(f.root, ".qc/screens"));
|
|
101
|
+
const ledger = files.filter(name => /^session-.*\.json$/.test(name));
|
|
102
|
+
assert.equal(ledger.length, 1);
|
|
103
|
+
const record = JSON.parse(readFileSync(path.join(f.root, ".qc/screens", ledger[0])));
|
|
104
|
+
assert.equal(record.target.baseUrl, f.target.baseUrl);
|
|
105
|
+
assert.ok(Number.isFinite(Date.parse(record.lastRefreshed)));
|
|
106
|
+
assert.equal(files.some(name => name.endsWith(".lock") || name.endsWith(".tmp")), false);
|
|
107
|
+
assert.deepEqual(readdirSync(path.dirname(f.state)), ["fixture.auth0.test.json"]);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("a browser launch failure names the target and method without asking for human sign-in", async t => {
|
|
111
|
+
const f = await fixture(t);
|
|
112
|
+
f.save([]);
|
|
113
|
+
await assert.rejects(browserSession({ launch: async () => { throw new Error("private browser diagnostics"); } }, f.target, { targetName: "mono-local", allowInteractive: false }), error => {
|
|
114
|
+
assert.equal(error.message, "mono-local: browser-session sign-in failed at the browser");
|
|
115
|
+
assert.equal(error.exitCode, undefined);
|
|
116
|
+
return true;
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("--allow-writes reaches the capture write guard as well as the session", async t => {
|
|
121
|
+
const f = await fixture(t);
|
|
122
|
+
const verdicts = [];
|
|
123
|
+
const report = await captureScreens({ widths: [200], targets: { first: f.target, second: f.target }, screens: [{ name: "projects", targets: { first: { path: "/projects" }, second: { path: "/projects" } } }] }, { targets: ["first", "second"], out: path.join(f.root, "out"), allowWrites: true }, {
|
|
124
|
+
say: () => {},
|
|
125
|
+
sessionFactory: async (_chromium, _target, options) => {
|
|
126
|
+
assert.equal(options.allowWrites, true);
|
|
127
|
+
const context = { route: async (_pattern, handler) => {
|
|
128
|
+
await handler({ request: () => ({ method: () => "POST", url: () => `${f.target.baseUrl}/api/items` }), fallback: async () => verdicts.push("allowed"), abort: async () => verdicts.push("blocked") });
|
|
129
|
+
} };
|
|
130
|
+
return { context, page: { setViewportSize: async () => { throw new Error("stop after the guard"); } }, close: async () => {} };
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
assert.equal(report[0].error.message, "stop after the guard");
|
|
134
|
+
assert.deepEqual(verdicts, ["allowed", "allowed"]);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("a cached target also passes preflight before a changed-target capture", async t => {
|
|
138
|
+
const f = await fixture(t);
|
|
139
|
+
const names = [];
|
|
140
|
+
await assert.rejects(captureScreens({ widths: [200], targets: { first: f.target, second: f.target }, screens: [{ name: "projects", targets: { first: { path: "/projects" }, second: { path: "/projects" } } }] }, { targets: ["first", "second"], changed: "second", out: path.join(f.root, "out") }, {
|
|
141
|
+
sessionFactory: async (_chromium, _target, options) => { names.push(options.targetName); throw new Error("preflight failed"); },
|
|
142
|
+
}), /preflight failed/);
|
|
143
|
+
assert.deepEqual(names, ["first"]);
|
|
144
|
+
});
|