Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXp2Vp62Ur3C7yRWQdSq
149 lines
6.4 KiB
TypeScript
149 lines
6.4 KiB
TypeScript
/**
|
|
* BUG-2358 (owner 2026-11-26): what the browser downloads must not carry the product's internal material (the
|
|
* model-facing writing rules, the KP / Parashari / annual house-meaning tables, the evidence card builders, the system
|
|
* prompt pieces). The 2026-11-26 build shipped PLAIN_TERMS (the term rule of VISIBLE VOICE) in a shared client chunk:
|
|
* home-cloud-sync.ts (account dialog, every page) imported parsePublicThinkingSections from
|
|
* consultation-thinking-plan.ts, and that module's `PLAIN_TERMS.replace(...)` could not be tree-shaken.
|
|
*
|
|
* Two checks: (1) static: no module reachable from a "use client" file imports one of the server-side rule modules;
|
|
* (2) when a production build is present (.next/static), no client chunk contains the marker sentences.
|
|
*/
|
|
import assert from "node:assert/strict";
|
|
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
import { dirname, join, relative, resolve } from "node:path";
|
|
import test from "node:test";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import { INSTRUCTION_PRIVACY_RULE } from "../src/lib/consult-instruction-privacy.ts";
|
|
import { D10_CANDIDATES, HOUSE_CANDIDATES } from "../src/lib/chat-house-meanings.ts";
|
|
import { PLAIN_TERMS } from "../src/lib/consultation-thinking-plan.ts";
|
|
import { GLOSSARY_LINE } from "../src/lib/chat-answer-checks.ts";
|
|
import { PLAN_TIMING_RULE } from "../src/lib/plan-timing-clarify.ts";
|
|
|
|
const FRONTEND = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
const SRC = join(FRONTEND, "src");
|
|
|
|
/**
|
|
* Server-side modules that hold model-facing rules, meaning tables or card builders. Modules whose words the page
|
|
* shows anyway (yoga status words, the annual report's house words, the internal-word replacements) are not listed;
|
|
* plan-timing-clarify.ts is used by the page (shouldClarifyPlanTiming), and its PLAN_TIMING_RULE is checked in the
|
|
* build's chunks below.
|
|
*/
|
|
const SERVER_RULE_MODULES = [
|
|
"mastra",
|
|
"lib/consultation-thinking-plan.ts",
|
|
"lib/product-runtime-contract.ts",
|
|
"lib/chat-kp-reading.ts",
|
|
"lib/chat-house-meanings.ts",
|
|
"lib/chat-kp-timing.ts",
|
|
"lib/chat-parashari-reading.ts",
|
|
"lib/consultation-evidence-card.ts",
|
|
"lib/evidence-pack.ts",
|
|
"lib/card-evidence-index.ts",
|
|
"lib/chat-answer-checks.ts",
|
|
"lib/consultation-condensed-checklist.ts",
|
|
"lib/consult-instruction-leak.ts",
|
|
].map((path) => join(SRC, path));
|
|
|
|
function sourceFiles(dir: string, out: string[] = []): string[] {
|
|
for (const name of readdirSync(dir)) {
|
|
const path = join(dir, name);
|
|
if (statSync(path).isDirectory()) sourceFiles(path, out);
|
|
else if (/\.(?:tsx?|mts)$/u.test(name)) out.push(path);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function resolveImport(from: string, spec: string): string | null {
|
|
let base: string;
|
|
if (spec.startsWith("@/")) base = join(SRC, spec.slice(2));
|
|
else if (spec.startsWith(".")) base = resolve(dirname(from), spec);
|
|
else return null;
|
|
for (const candidate of [base, `${base}.ts`, `${base}.tsx`, join(base, "index.ts"), join(base, "index.tsx")]) {
|
|
if (existsSync(candidate) && statSync(candidate).isFile()) return candidate;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Value imports and re-exports, static and dynamic. `import type` / `export type` carry no code.
|
|
const IMPORT = /(?:^|\n)\s*(?:import|export)\s+(type\s+)?(?:[^'"]*?\sfrom\s+)?["']([^"']+)["']|import\(\s*["']([^"']+)["']\s*\)/gu;
|
|
const CLIENT = /^\s*(?:\/\/[^\n]*\n|\/\*[\s\S]*?\*\/\s*)*["']use client["']/u;
|
|
|
|
test("BUG-2358: no client module imports a server-side rule module (static import graph)", () => {
|
|
const files = sourceFiles(SRC);
|
|
const edges = new Map<string, string[]>();
|
|
const text = new Map<string, string>();
|
|
for (const file of files) {
|
|
const source = readFileSync(file, "utf8");
|
|
text.set(file, source);
|
|
edges.set(file, [...source.matchAll(IMPORT)].flatMap((match) => {
|
|
if (match[1]) return [];
|
|
const target = resolveImport(file, match[2] ?? match[3] ?? "");
|
|
return target ? [target] : [];
|
|
}));
|
|
}
|
|
const banned = (file: string) => SERVER_RULE_MODULES.some((path) => file === path || file.startsWith(`${path}/`));
|
|
const leaks: string[] = [];
|
|
const clients = files.filter((file) => CLIENT.test(text.get(file) ?? ""));
|
|
assert.ok(clients.length > 50, `found ${clients.length} client files`);
|
|
for (const client of clients) {
|
|
const parent = new Map<string, string | null>([[client, null]]);
|
|
const queue = [client];
|
|
while (queue.length) {
|
|
const current = queue.shift()!;
|
|
if (banned(current)) {
|
|
const chain: string[] = [];
|
|
for (let at: string | null | undefined = current; at; at = parent.get(at)) chain.unshift(relative(SRC, at));
|
|
leaks.push(chain.join(" -> "));
|
|
continue;
|
|
}
|
|
// A module marked server-only fails the build if a client imports it; its own imports are not followed.
|
|
if (/^import ["']server-only["'];?$/mu.test(text.get(current) ?? "")) continue;
|
|
for (const next of edges.get(current) ?? []) {
|
|
if (!parent.has(next)) {
|
|
parent.set(next, current);
|
|
queue.push(next);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
assert.deepEqual([...new Set(leaks)], []);
|
|
});
|
|
|
|
function chunkFiles(dir: string, out: string[] = []): string[] {
|
|
for (const name of readdirSync(dir)) {
|
|
const path = join(dir, name);
|
|
if (statSync(path).isDirectory()) chunkFiles(path, out);
|
|
else if (name.endsWith(".js")) out.push(path);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
test("BUG-2358: a production build's client chunks carry none of the internal marker sentences", (t) => {
|
|
const staticDir = join(FRONTEND, ".next", "static");
|
|
if (!existsSync(staticDir)) {
|
|
t.skip("no .next/static build in this checkout; the static import-graph test covers it");
|
|
return;
|
|
}
|
|
const markers = [
|
|
"VISIBLE VOICE",
|
|
"NATAL REPORT CONTRACT",
|
|
PLAIN_TERMS.slice(0, 24),
|
|
GLOSSARY_LINE.slice(0, 20),
|
|
PLAN_TIMING_RULE.slice(0, 20),
|
|
INSTRUCTION_PRIVACY_RULE.slice(0, 16),
|
|
HOUSE_CANDIDATES.career[6]![0]!.text,
|
|
HOUSE_CANDIDATES.marriage[1]![0]!.text,
|
|
D10_CANDIDATES[10]!,
|
|
"consultation-evidence-packet",
|
|
"precomputed_tables",
|
|
"product-block:",
|
|
];
|
|
const hits: string[] = [];
|
|
for (const file of chunkFiles(staticDir)) {
|
|
const source = readFileSync(file, "utf8").replace(/\\u([0-9a-fA-F]{4})/gu, (_, hex: string) => String.fromCharCode(Number.parseInt(hex, 16)));
|
|
for (const marker of markers) if (source.includes(marker)) hits.push(`${relative(staticDir, file)}: ${marker}`);
|
|
}
|
|
assert.deepEqual(hits, []);
|
|
});
|