feat: complete TASK-WP0-03 browser gate

This commit is contained in:
suyx
2026-07-27 18:23:20 +08:00
parent dbe3e73b91
commit 878788b1e2
25 changed files with 3227 additions and 21 deletions
+13 -1
View File
@@ -40,6 +40,11 @@ function operationResult(operation) {
return response.schema ? schemaType(response.schema) : "unknown";
}
function operationBodyType(operation) {
const schema = operation.requestBody?.content?.["application/json"]?.schema;
return schema ? schemaType(schema) : undefined;
}
function operationMediaType(operation) {
const content = operation.responses?.["200"]?.content;
if (!content) return undefined;
@@ -68,16 +73,23 @@ export function generateClient(input, output) {
].join("\n\n");
const operationList = operations(document);
const importedTypes = [...new Set(operationList.map(({ operation }) => operationResult(operation)).filter((type) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(type)))];
const importedTypes = [...new Set(operationList.flatMap(({ operation }) => [
operationResult(operation),
operationBodyType(operation),
]).filter((type) => type && /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(type)))];
const sdk = [
"// Generated from openapi/openapi.json. Do not edit by hand.",
importedTypes.length ? `import type { ${importedTypes.join(", ")} } from "./types.gen.js";` : "",
"export interface ClientOptions { baseUrl?: string; fetch?: typeof globalThis.fetch; headers?: HeadersInit; }",
...operationList.map(({ method, operation, path }) => {
const resultType = operationResult(operation);
const bodyType = operationBodyType(operation);
if (operationMediaType(operation) === "text/event-stream") {
return `export function ${identifier(operation.operationId)}(options: Pick<ClientOptions, "baseUrl"> = {}): string {\n return \`${"${options.baseUrl ?? \"\"}"}${path}\`;\n}`;
}
if (bodyType) {
return `export async function ${identifier(operation.operationId)}(body: ${bodyType}, options: ClientOptions = {}): Promise<${resultType}> {\n const request = options.fetch ?? globalThis.fetch;\n const headers = new Headers(options.headers);\n headers.set("Content-Type", "application/json");\n const response = await request(\`${"${options.baseUrl ?? \"\"}"}${path}\`, { body: JSON.stringify(body), method: "${method.toUpperCase()}", headers });\n if (!response.ok) throw new Error(\`HTTP ${"${response.status}"}\`);\n return response.json() as Promise<${resultType}>;\n}`;
}
return `export async function ${identifier(operation.operationId)}(options: ClientOptions = {}): Promise<${resultType}> {\n const request = options.fetch ?? globalThis.fetch;\n const response = await request(\`${"${options.baseUrl ?? \"\"}"}${path}\`, { method: "${method.toUpperCase()}", headers: options.headers ?? {} });\n if (!response.ok) throw new Error(\`HTTP ${"${response.status}"}\`);\n return response.json() as Promise<${resultType}>;\n}`;
}),
"",
+117
View File
@@ -0,0 +1,117 @@
import { createHash } from "node:crypto";
import { execFileSync, spawn } from "node:child_process";
import { once } from "node:events";
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { request } from "node:http";
import { resolve } from "node:path";
const host = "127.0.0.1";
const port = 43121;
function sha256(value) {
return createHash("sha256").update(value).digest("hex").toUpperCase();
}
function firewallSnapshot() {
return execFileSync(
"netsh",
["advfirewall", "firewall", "show", "rule", "name=all", "dir=in"],
{ encoding: "utf8", windowsHide: true },
);
}
function httpRequest(headers = {}) {
return new Promise((resolveRequest, reject) => {
const outgoing = request({ headers, host, method: "GET", path: "/", port }, (response) => {
response.resume();
response.once("end", () => resolveRequest({ headers: response.headers, status: response.statusCode }));
});
outgoing.once("error", reject);
outgoing.end();
});
}
async function waitUntilReady() {
for (let attempt = 0; attempt < 40; attempt += 1) {
try {
const response = await httpRequest();
if (response.status === 200) return response;
} catch {
// The fixed listener may still be starting.
}
await new Promise((resolveWait) => setTimeout(resolveWait, 125));
}
throw new Error("The fixed loopback listener did not become ready.");
}
const firewallBefore = firewallSnapshot();
const child = spawn(process.execPath, ["apps/api/dist/main.js"], {
stdio: ["ignore", "pipe", "pipe"],
windowsHide: true,
});
let stderr = "";
child.stderr.on("data", (chunk) => {
stderr += chunk.toString();
});
let rootResponse;
let rejectedResponse;
let listeners;
try {
rootResponse = await waitUntilReady();
rejectedResponse = await httpRequest({ host: "192.168.1.10:43121" });
const netstat = execFileSync("netstat", ["-ano", "-p", "tcp"], { encoding: "utf8", windowsHide: true });
const matchingListeners = netstat
.split(/\r?\n/)
.map((line) => line.trim().split(/\s+/))
.filter((tokens) => tokens[0]?.toUpperCase() === "TCP" && tokens.at(-1) === String(child.pid) && tokens[1]?.endsWith(`:${port}`))
.map((tokens) => ({ local_address: tokens[1], pid: child.pid }));
listeners = [...new Map(matchingListeners.map((listener) => [listener.local_address, listener])).values()];
} finally {
child.kill();
await Promise.race([
once(child, "exit"),
new Promise((_, reject) => setTimeout(() => reject(new Error("API process did not stop.")), 5000)),
]);
}
if (stderr.trim()) throw new Error("The API wrote to stderr during loopback smoke.");
const firewallAfter = firewallSnapshot();
const expectedListener = `${host}:${port}`;
const socketResult = {
expected: expectedListener,
listeners,
status: listeners.length > 0 && listeners.every(({ local_address }) => local_address === expectedListener)
? "passed"
: "failed",
};
const responseResult = {
invalid_host: rejectedResponse,
localhost: rootResponse,
status: rootResponse.status === 200 && rejectedResponse.status === 426 ? "passed" : "failed",
};
const firewallResult = {
after_sha256: sha256(firewallAfter),
before_sha256: sha256(firewallBefore),
status: firewallAfter === firewallBefore ? "passed" : "failed",
unchanged: firewallAfter === firewallBefore,
};
const evidenceDirectory = process.env.DADA_EVIDENCE_DIR_BND;
if (evidenceDirectory) {
mkdirSync(evidenceDirectory, { recursive: true });
writeFileSync(resolve(evidenceDirectory, "socket-listeners.json"), `${JSON.stringify(socketResult, null, 2)}\n`);
writeFileSync(resolve(evidenceDirectory, "response.json"), `${JSON.stringify(responseResult, null, 2)}\n`);
writeFileSync(resolve(evidenceDirectory, "firewall-diff.json"), `${JSON.stringify(firewallResult, null, 2)}\n`);
}
const result = {
firewall: firewallResult.status,
responses: responseResult.status,
sockets: socketResult.status,
status: [firewallResult.status, responseResult.status, socketResult.status].every((status) => status === "passed")
? "passed"
: "failed",
};
console.log(JSON.stringify(result, null, 2));
if (result.status !== "passed") process.exit(1);
+1 -1
View File
@@ -2,7 +2,7 @@ import { readdirSync, readFileSync, statSync } from "node:fs";
import { extname, join, relative } from "node:path";
const scanRoots = ["apps", "packages", "scripts", "supervisor", "tests"];
const textExtensions = new Set([".cs", ".json", ".mjs", ".ts", ".tsx", ".yaml", ".yml"]);
const textExtensions = new Set([".cs", ".css", ".html", ".js", ".json", ".mjs", ".ts", ".tsx", ".yaml", ".yml"]);
const findings = [];
function visit(path) {
+242
View File
@@ -0,0 +1,242 @@
import { createHash } from "node:crypto";
import { spawnSync } from "node:child_process";
import {
copyFileSync,
existsSync,
mkdirSync,
readFileSync,
readdirSync,
statSync,
writeFileSync,
} from "node:fs";
import { resolve } from "node:path";
const runId = process.env.DADA_TDD_RUN_ID ?? `wp0-03-green-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const boundaryCaseId = "TDD-WP0-BND-001-loopback-origin";
const supportedCaseId = "TDD-WP0-BRW-001-real-supported";
const blockedCaseId = "TDD-WP0-BRW-002-hard-block";
const boundaryDirectory = resolve(runDirectory, "cases", boundaryCaseId);
const supportedDirectory = resolve(runDirectory, "cases", supportedCaseId);
const blockedDirectory = resolve(runDirectory, "cases", blockedCaseId);
const playwrightDirectory = resolve(runDirectory, "playwright");
if (existsSync(runDirectory)) throw new Error(`Evidence run already exists: ${runId}`);
for (const directory of [boundaryDirectory, supportedDirectory, blockedDirectory]) {
mkdirSync(directory, { recursive: true });
}
const commandDefinitions = [
{ command: "pnpm test:api", args: ["test:api"] },
{ command: "pnpm test:security", args: ["test:security"] },
{ command: "pnpm test:package", args: ["test:package"] },
{ command: "pnpm test:e2e", args: ["test:e2e"] },
{ command: "pnpm validate:tdd-trace", args: ["validate:tdd-trace"] },
];
const startedAt = new Date().toISOString();
const commands = [];
for (const definition of commandDefinitions) {
const commandStartedAt = new Date().toISOString();
const executable = process.platform === "win32" ? (process.env.ComSpec ?? "cmd.exe") : "pnpm";
const args = process.platform === "win32"
? ["/d", "/s", "/c", `pnpm ${definition.args.join(" ")}`]
: definition.args;
const execution = spawnSync(executable, args, {
env: {
...process.env,
DADA_EVIDENCE_DIR_BND: boundaryDirectory,
DADA_EVIDENCE_DIR_BRW_BLOCKED: blockedDirectory,
DADA_EVIDENCE_DIR_BRW_SUPPORTED: supportedDirectory,
DADA_PLAYWRIGHT_OUTPUT_DIR: playwrightDirectory,
},
stdio: "inherit",
});
commands.push({
command: definition.command,
exit_code: execution.status ?? 1,
finished_at: new Date().toISOString(),
started_at: commandStartedAt,
});
}
function findFiles(root, target) {
if (!existsSync(root)) return [];
const files = [];
for (const name of readdirSync(root)) {
const child = resolve(root, name);
if (statSync(child).isDirectory()) files.push(...findFiles(child, target));
else if (name === target) files.push(child);
}
return files;
}
for (const trace of findFiles(playwrightDirectory, "trace.zip")) {
const normalized = trace.replaceAll("\\", "/");
if (normalized.includes("support-gate-a-real-Edge")) {
const target = resolve(supportedDirectory, "edge", "trace.zip");
mkdirSync(resolve(supportedDirectory, "edge"), { recursive: true });
copyFileSync(trace, target);
}
if (normalized.includes("support-gate-the-hard-bloc")) {
copyFileSync(trace, resolve(blockedDirectory, "trace.zip"));
}
}
const chromeDirectory = resolve(supportedDirectory, "chrome");
mkdirSync(resolve(chromeDirectory, "screenshots"), { recursive: true });
const chromeCandidates = [
"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
];
const chromeInstalled = chromeCandidates.some((path) => existsSync(path));
writeFileSync(
resolve(chromeDirectory, "environment.json"),
`${JSON.stringify({ browser: "Google Chrome", final_release: false, installed: chromeInstalled, status: "pending_manual" }, null, 2)}\n`,
);
writeFileSync(
resolve(chromeDirectory, "response.json"),
`${JSON.stringify({ reason: "Final RELEASE.json and real Chrome validation belong to WP-7.", status: "not_run" }, null, 2)}\n`,
);
const commandEvidence = { commands, phase: "green", run_id: runId, schema_version: "1.0" };
for (const directory of [boundaryDirectory, supportedDirectory, blockedDirectory]) {
writeFileSync(resolve(directory, "commands.json"), `${JSON.stringify(commandEvidence, null, 2)}\n`);
}
const manifestBytes = readFileSync("tasks.manifest.json");
const manifest = {
path: "tasks.manifest.json",
sha256: createHash("sha256").update(manifestBytes).digest("hex").toUpperCase(),
};
const commit = spawnSync("git", ["rev-parse", "--short", "HEAD"], { encoding: "utf8" }).stdout.trim();
const worktreeDirty = spawnSync("git", ["status", "--porcelain"], { encoding: "utf8" }).stdout.trim().length > 0;
const environment = { arch: process.arch, node: process.version.slice(1), os: process.platform };
const commandsPassed = commands.every(({ exit_code }) => exit_code === 0);
function writeResult(directory, result) {
writeFileSync(resolve(directory, "result.json"), `${JSON.stringify(result, null, 2)}\n`);
return result;
}
const boundaryEvidence = ["socket-listeners.json", "response.json", "firewall-diff.json"];
const boundaryMissing = boundaryEvidence.filter((file) => !existsSync(resolve(boundaryDirectory, file)));
const boundaryResult = writeResult(boundaryDirectory, {
acceptance_criteria: ["AC-24"],
automation: ["automated"],
commit,
environment,
evidence_refs: boundaryEvidence,
finished_at: new Date().toISOString(),
layer: ["API", "PKG-SEC"],
manifest,
missing_evidence: boundaryMissing,
parent_family: "TDD-WP0-BND-001",
phase: "green",
release_gate: ["work_package:WP-0", "release:P0-A"],
requirements: ["NFR-09"],
run_id: runId,
schema_version: "1.0",
started_at: startedAt,
status: commandsPassed && boundaryMissing.length === 0 ? "passed" : "failed",
task_id: "TASK-WP0-03",
test_id: boundaryCaseId,
work_package: "WP-0",
worktree_under_test: worktreeDirty ? "uncommitted implementation" : "clean committed implementation",
});
const blockedEvidence = [
"response.json",
"db-access.json",
"external-calls.json",
"trace.zip",
"screenshots/blocked.png",
];
const blockedMissing = blockedEvidence.filter((file) => !existsSync(resolve(blockedDirectory, file)));
const blockedResult = writeResult(blockedDirectory, {
acceptance_criteria: ["AC-24"],
automation: ["automated"],
commit,
environment,
evidence_refs: blockedEvidence,
finished_at: new Date().toISOString(),
layer: ["API", "E2E"],
manifest,
missing_evidence: blockedMissing,
parent_family: "TDD-WP0-BRW-002",
phase: "green",
release_gate: ["work_package:WP-0", "release:P0-A"],
requirements: ["NFR-01"],
run_id: runId,
schema_version: "1.0",
started_at: startedAt,
status: commandsPassed && blockedMissing.length === 0 ? "passed" : "failed",
task_id: "TASK-WP0-03",
test_id: blockedCaseId,
work_package: "WP-0",
worktree_under_test: worktreeDirty ? "uncommitted implementation" : "clean committed implementation",
});
const supportedAutomatedEvidence = [
"edge/environment.json",
"edge/response.json",
"edge/trace.zip",
"edge/screenshots/supported.png",
];
const supportedAutomatedMissing = supportedAutomatedEvidence.filter(
(file) => !existsSync(resolve(supportedDirectory, file)),
);
const supportedExternalMissing = [
"final/RELEASE.json",
"chrome/trace.zip",
"chrome/screenshots/supported.png",
"final Chrome/Edge AC-24 evidence",
];
const supportedResult = writeResult(supportedDirectory, {
acceptance_criteria: ["AC-24", "AC-41"],
automation: ["automated", "manual_review"],
automation_status: commandsPassed && supportedAutomatedMissing.length === 0 ? "passed" : "failed",
commit,
environment,
evidence_refs: [
...supportedAutomatedEvidence,
"chrome/environment.json",
"chrome/response.json",
],
external_blockers: [
"Final RELEASE.json is created only after WP-7 candidate and AC validation.",
"A real installed Chrome full-version run is not available in this workspace.",
"The Edge run uses a test candidate release and cannot replace final release evidence.",
],
finished_at: new Date().toISOString(),
layer: ["E2E", "MANUAL"],
manifest,
missing_evidence: supportedExternalMissing,
missing_automated_evidence: supportedAutomatedMissing,
parent_family: "TDD-WP0-BRW-001",
phase: "green",
release_gate: ["work_package:WP-0", "release:P0-A"],
requirements: ["NFR-01"],
run_id: runId,
schema_version: "1.0",
started_at: startedAt,
status: commandsPassed && supportedAutomatedMissing.length === 0 ? "externally_blocked" : "failed",
task_id: "TASK-WP0-03",
test_id: supportedCaseId,
work_package: "WP-0",
worktree_under_test: worktreeDirty ? "uncommitted implementation" : "clean committed implementation",
});
const automatedPassed = boundaryResult.status === "passed" && blockedResult.status === "passed" && supportedResult.automation_status === "passed";
const summary = {
cases: [boundaryResult, supportedResult, blockedResult].map(({ missing_evidence, status, test_id }) => ({
missing_evidence,
status,
test_id,
})),
run_id: runId,
status: automatedPassed ? "green_with_external_block" : "failed",
};
writeFileSync(resolve(runDirectory, "evidence.json"), `${JSON.stringify(summary, null, 2)}\n`);
console.log(JSON.stringify(summary, null, 2));
if (!automatedPassed) process.exit(1);