// 验收「构建开关在热更新重建里保持」。 // // 上一版这个验证是**空的**:调试服因为参数不认识根本没起来,于是"没重建"被当成了"重建后开关还在"。 // 所以这里第一件事就是断言服务真的活着——一个不检查前置条件的验证等于没有验证。 // // 用法:node tools/checks/verify-dev-flags.mts [base] // 前提:调试服以 --with-sim 启动,且产物里本来就带模拟器。 import { appendFileSync, readdirSync, existsSync, readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { sleep } from "../cdp.mjs"; const BASE = process.argv[2] ?? "http://127.0.0.1:5173"; const CSS = "src/styles/index.css"; const RELEASES = "dist/releases"; let failures = 0; const check = (ok: unknown, label: string, detail?: unknown) => { if (!ok) failures++; console.log(` ${ok ? "✓" : "✗"} ${label}${detail !== undefined ? ` → ${String(detail)}` : ""}`); }; const simulatorCount = () => { let n = 0; for (const dir of readdirSync(RELEASES)) { if (existsSync(join(RELEASES, dir, "scripts", "wallpaper-engine.js"))) n += 1; } return n; }; const original = readFileSync(CSS, "utf8"); try { console.log("=== ① 前置条件:调试服真的活着 ==="); let alive = false; try { const res = await fetch(`${BASE}/`); alive = res.status === 200; } catch { alive = false; } check(alive, "调试服响应 GET /", alive ? BASE : "连不上——后面所有结论都不成立"); if (!alive) throw new Error("调试服没起来,验证无意义"); const before = simulatorCount(); check(before > 0, "重建前产物里带模拟器", `${before} 个分发`); if (before === 0) throw new Error("产物里没有模拟器,请先 node tools/build.ts --with-sim"); console.log("\n=== ② 保存一次源文件,等热更新重建 ==="); appendFileSync(CSS, `\n/* dev-flags probe ${Date.now()} */\n`); // 用"被改的 CSS 已经能被取到新内容"作为重建完成的判据,而不是干等固定秒数。 let rebuilt = false; const deadline = Date.now() + 30000; while (Date.now() < deadline) { await sleep(400); try { const text = await (await fetch(`${BASE}/release/collection-all/styles/index.css`)).text(); if (/dev-flags probe/.test(text)) { rebuilt = true; break; } } catch { // 重建中偶尔取不到,继续等 } } check(rebuilt, "重建完成(调试服已能取到新的 CSS)"); console.log("\n=== ③ 开关是否保持 ==="); const after = simulatorCount(); check(after === before, "--with-sim 在热更新重建后仍然生效", `${before} → ${after}`); } finally { writeFileSync(CSS, original); await sleep(3000); console.log("\n源文件已还原"); } console.log(failures === 0 ? "构建开关保持:通过" : `构建开关保持:失败 ${failures} 处`); process.exit(failures === 0 ? 0 : 1);