250 lines
9.8 KiB
JavaScript
250 lines
9.8 KiB
JavaScript
import { chromium, request } from "playwright";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
const runId = `p0-mainline-alignment-${Date.now()}`;
|
|
const webBase = process.env.P0_MAINLINE_WEB_URL || "http://localhost:3000";
|
|
const apiBase = process.env.P0_MAINLINE_API_URL || "http://127.0.0.1:8010/api/v1";
|
|
const healthUrl = process.env.P0_MAINLINE_HEALTH_URL || "http://127.0.0.1:8010/health";
|
|
const adminPassword = process.env.AI_CONTRACT_ADMIN_PASSWORD || "123456";
|
|
const artifactDir =
|
|
process.env.P0_MAINLINE_ARTIFACT_DIR ||
|
|
path.resolve(__dirname, "..", "..", "project-docs", "qa-artifacts", "p0-mainline-alignment-closed-loop", stamp);
|
|
const p0PlanDocPath = path.resolve(
|
|
__dirname,
|
|
"..",
|
|
"..",
|
|
"project-docs",
|
|
"AI合同起草与审查助手-P0实现主线拆分方案-2026-05-11.md",
|
|
);
|
|
|
|
fs.mkdirSync(artifactDir, { recursive: true });
|
|
|
|
const results = [];
|
|
const state = {
|
|
api: null,
|
|
browser: null,
|
|
contexts: [],
|
|
admin: null,
|
|
};
|
|
|
|
function apiUrl(route) {
|
|
return `${apiBase.replace(/\/$/, "")}${route}`;
|
|
}
|
|
|
|
function screenshotPath(name) {
|
|
return path.join(artifactDir, `${name}.png`);
|
|
}
|
|
|
|
function assert(condition, message, details = undefined) {
|
|
if (!condition) {
|
|
const error = new Error(message);
|
|
error.details = details;
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function step(id, title, fn) {
|
|
const startedAt = new Date().toISOString();
|
|
const started = Date.now();
|
|
try {
|
|
const details = await fn();
|
|
const item = { id, title, status: "PASS", duration_ms: Date.now() - started, started_at: startedAt, details: details || {} };
|
|
results.push(item);
|
|
console.log(`PASS ${id} ${title}`);
|
|
return item;
|
|
} catch (error) {
|
|
const item = {
|
|
id,
|
|
title,
|
|
status: "FAIL",
|
|
duration_ms: Date.now() - started,
|
|
started_at: startedAt,
|
|
error: error.message,
|
|
details: error.details || {},
|
|
};
|
|
results.push(item);
|
|
console.error(`FAIL ${id} ${title}: ${error.message}`);
|
|
return item;
|
|
}
|
|
}
|
|
|
|
async function apiRequest(method, route, options = {}) {
|
|
const headers = {
|
|
...(options.authToken ? { Authorization: `Bearer ${options.authToken}` } : {}),
|
|
...(options.headers || {}),
|
|
};
|
|
const response = await state.api[method](apiUrl(route), { ...options, headers });
|
|
const text = await response.text();
|
|
let body = null;
|
|
if (text) {
|
|
try {
|
|
body = JSON.parse(text);
|
|
} catch {
|
|
body = text;
|
|
}
|
|
}
|
|
return { response, body, text };
|
|
}
|
|
|
|
function expectOk(payload, label) {
|
|
assert(payload.response.status() >= 200 && payload.response.status() < 300, `${label} HTTP ${payload.response.status()}`, {
|
|
body: payload.body,
|
|
});
|
|
if (payload.body && typeof payload.body === "object" && "code" in payload.body) {
|
|
assert(payload.body.code === 0, `${label} API code ${payload.body.code}`, { body: payload.body });
|
|
}
|
|
}
|
|
|
|
function assertP0Mainline(payload, label) {
|
|
assert(payload.version === "p0-mainline-alignment-2026-05-12", `${label} version mismatch`, payload);
|
|
assert(payload.status === "aligned", `${label} status should be aligned`, payload);
|
|
assert(payload.phase_boundary?.mainline_scope?.includes("Slice 1-8"), `${label} missing mainline scope`, payload.phase_boundary);
|
|
assert(payload.phase_boundary?.next_stage_scope?.includes("Slice 9+"), `${label} missing next stage scope`, payload.phase_boundary);
|
|
assert(Array.isArray(payload.capabilities) && payload.capabilities.length === 5, `${label} capability count mismatch`, payload.capabilities);
|
|
assert(Array.isArray(payload.guardrails) && payload.guardrails.length >= 5, `${label} guardrail count mismatch`, payload.guardrails);
|
|
assert(Array.isArray(payload.acceptance) && payload.acceptance.length >= 4, `${label} acceptance count mismatch`, payload.acceptance);
|
|
for (const capability of payload.capabilities) {
|
|
assert(capability.source_project, `${label} capability missing source project`, capability);
|
|
assert(capability.target_layer, `${label} capability missing target layer`, capability);
|
|
assert(capability.contract_platform_responsibility, `${label} capability missing contract responsibility`, capability);
|
|
assert(capability.acceptance, `${label} capability missing acceptance`, capability);
|
|
assert(Array.isArray(capability.evidence) && capability.evidence.length > 0, `${label} capability missing evidence`, capability);
|
|
}
|
|
}
|
|
|
|
async function login(account, password) {
|
|
const payload = await apiRequest("post", "/auth/login", { data: { account, password } });
|
|
expectOk(payload, `login ${account}`);
|
|
return payload.body.data;
|
|
}
|
|
|
|
async function attachApiProxy(context) {
|
|
await context.route("**/api/v1/**", async (route) => {
|
|
const browserRequest = route.request();
|
|
const browserUrl = new URL(browserRequest.url());
|
|
const targetUrl = apiUrl(browserUrl.pathname.replace(/^\/api\/v1/, "") + browserUrl.search);
|
|
const headers = { ...browserRequest.headers() };
|
|
delete headers.host;
|
|
|
|
try {
|
|
const response = await state.api.fetch(targetUrl, {
|
|
method: browserRequest.method(),
|
|
headers,
|
|
data: browserRequest.postDataBuffer() ?? undefined,
|
|
});
|
|
await route.fulfill({ response });
|
|
} catch {
|
|
await route.abort("failed").catch(() => {});
|
|
}
|
|
});
|
|
}
|
|
|
|
async function createAuthenticatedContext(userSession) {
|
|
const context = await state.browser.newContext({ viewport: { width: 1440, height: 1400 } });
|
|
await context.addInitScript(
|
|
({ token, user }) => {
|
|
window.localStorage.setItem("access_token", token);
|
|
window.localStorage.setItem("auth-store", JSON.stringify({ state: { accessToken: token, currentUser: user }, version: 0 }));
|
|
},
|
|
{ token: userSession.access_token, user: userSession.user },
|
|
);
|
|
await attachApiProxy(context);
|
|
state.contexts.push(context);
|
|
return context;
|
|
}
|
|
|
|
async function run() {
|
|
state.api = await request.newContext();
|
|
state.browser = await chromium.launch({ headless: true });
|
|
|
|
await step("ENV-001", "frontend and backend health", async () => {
|
|
const [frontend, health] = await Promise.all([state.api.get(`${webBase}/`), state.api.get(healthUrl)]);
|
|
assert(frontend.status() === 200, `frontend / status ${frontend.status()}`);
|
|
assert(health.status() === 200, `backend /health status ${health.status()}`);
|
|
return { webBase, apiBase, healthUrl, artifactDir, runId };
|
|
});
|
|
|
|
await step("DOC-001", "P0 mainline plan document exists", async () => {
|
|
assert(fs.existsSync(p0PlanDocPath), "missing P0 mainline plan doc", { p0PlanDocPath });
|
|
const content = fs.readFileSync(p0PlanDocPath, "utf8");
|
|
assert(content.includes("P0-A 文档解析"), "P0 plan missing document parsing lane", { p0PlanDocPath });
|
|
assert(content.includes("P0-E 自动审查闭环"), "P0 plan missing auto review lane", { p0PlanDocPath });
|
|
return { p0PlanDocPath };
|
|
});
|
|
|
|
await step("API-001", "p0 mainline API exposes source and ownership matrix", async () => {
|
|
const payload = await apiRequest("get", "/system/p0-mainline");
|
|
expectOk(payload, "p0 mainline");
|
|
assertP0Mainline(payload.body.data, "p0 mainline API");
|
|
return {
|
|
version: payload.body.data.version,
|
|
capability_count: payload.body.data.capabilities.length,
|
|
};
|
|
});
|
|
|
|
await step("READINESS-001", "readiness embeds p0 mainline", async () => {
|
|
const payload = await apiRequest("get", "/system/readiness");
|
|
expectOk(payload, "readiness");
|
|
assertP0Mainline(payload.body.data.p0_mainline, "readiness p0 mainline");
|
|
return {
|
|
readiness_status: payload.body.data.status,
|
|
mainline_version: payload.body.data.p0_mainline.version,
|
|
};
|
|
});
|
|
|
|
await step("AUTH-001", "default admin seed and login", async () => {
|
|
const seed = await apiRequest("post", "/auth/seed-admin");
|
|
expectOk(seed, "seed default admin");
|
|
state.admin = await login("admin@example.com", adminPassword);
|
|
assert(state.admin.user.role === "admin", "admin role mismatch", state.admin.user);
|
|
return { user: state.admin.user.username, role: state.admin.user.role };
|
|
});
|
|
|
|
await step("BROWSER-001", "pilot readiness page exposes mainline alignment", async () => {
|
|
const context = await createAuthenticatedContext(state.admin);
|
|
const page = await context.newPage();
|
|
await page.goto(`${webBase}/pilot-readiness`, { waitUntil: "domcontentloaded" });
|
|
const panel = page.getByTestId("p0-mainline-alignment");
|
|
await panel.waitFor({ state: "visible", timeout: 12000 });
|
|
const capabilityCount = await panel.getByTestId("p0-mainline-capability").count();
|
|
assert(capabilityCount === 5, "mainline alignment should expose five capabilities", { capabilityCount });
|
|
await page.getByTestId("p0-mainline-boundary").waitFor({ state: "visible", timeout: 12000 });
|
|
await page.getByTestId("p0-mainline-guardrails").waitFor({ state: "visible", timeout: 12000 });
|
|
await page.getByTestId("p0-mainline-acceptance").waitFor({ state: "visible", timeout: 12000 });
|
|
await page.screenshot({ path: screenshotPath("p0-mainline-alignment"), fullPage: true });
|
|
return { screenshot: "p0-mainline-alignment.png", capability_count: capabilityCount };
|
|
});
|
|
}
|
|
|
|
try {
|
|
await run();
|
|
} finally {
|
|
for (const context of state.contexts) {
|
|
await context.close().catch(() => {});
|
|
}
|
|
if (state.browser) {
|
|
await state.browser.close().catch(() => {});
|
|
}
|
|
if (state.api) {
|
|
await state.api.dispose().catch(() => {});
|
|
}
|
|
const summary = {
|
|
run_id: runId,
|
|
artifact_dir: artifactDir,
|
|
passed: results.filter((item) => item.status === "PASS").length,
|
|
failed: results.filter((item) => item.status === "FAIL").length,
|
|
results,
|
|
};
|
|
fs.writeFileSync(path.join(artifactDir, "summary.json"), `${JSON.stringify(summary, null, 2)}\n`, "utf8");
|
|
console.log(`Wrote ${path.join(artifactDir, "summary.json")}`);
|
|
if (summary.failed > 0) {
|
|
process.exitCode = 1;
|
|
}
|
|
}
|