323 lines
10 KiB
TypeScript
323 lines
10 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { readdir, readFile, stat } from "node:fs/promises";
|
|
import path from "node:path";
|
|
|
|
const root = process.cwd();
|
|
const failures: string[] = [];
|
|
|
|
async function text(relativePath: string): Promise<string> {
|
|
return readFile(path.join(root, relativePath), "utf8");
|
|
}
|
|
|
|
async function exists(relativePath: string): Promise<boolean> {
|
|
try {
|
|
await stat(path.join(root, relativePath));
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function sha256(relativePath: string): Promise<string> {
|
|
return createHash("sha256")
|
|
.update(await readFile(path.join(root, relativePath)))
|
|
.digest("hex");
|
|
}
|
|
|
|
async function walk(relativeDirectory: string): Promise<string[]> {
|
|
const absolute = path.join(root, relativeDirectory);
|
|
const entries = await readdir(absolute, { withFileTypes: true });
|
|
const files: string[] = [];
|
|
for (const entry of entries) {
|
|
const relative = path.join(relativeDirectory, entry.name);
|
|
if (entry.isDirectory()) {
|
|
files.push(...(await walk(relative)));
|
|
} else if (entry.isFile()) {
|
|
files.push(relative);
|
|
}
|
|
}
|
|
return files;
|
|
}
|
|
|
|
function fail(message: string): void {
|
|
failures.push(message);
|
|
}
|
|
|
|
const indexHtml = await text("index.html");
|
|
for (const [id, pattern] of [
|
|
["STATIC_PAGE_MARKUP", /class=["'][^"']*\bpage\b/u],
|
|
["STATIC_MODAL_MARKUP", /class=["'][^"']*\bmodal\b/u],
|
|
["BETA_SCRIPT", /beta\.js/u],
|
|
["BETA_STYLESHEET", /beta\.css/u],
|
|
["STATIC_LOGIN", /login-v2\.html/u],
|
|
["STATIC_REGISTER", /register-v2\.html/u],
|
|
] as const) {
|
|
if (pattern.test(indexHtml)) {
|
|
fail(`${id}: index.html contains product prototype markup/assets`);
|
|
}
|
|
}
|
|
|
|
for (const obsolete of [
|
|
"public/beta.js",
|
|
"public/beta.css",
|
|
"public/login-v2.html",
|
|
"public/register-v2.html",
|
|
"src/features/memento-web/presentation/memento-runtime-bridge.tsx",
|
|
"src/features/memento-web/presentation/memento-prototype-integration.ts",
|
|
"src/features/memento-web/adapters/http-memento-gateway.ts",
|
|
"src/features/memento-web/adapters/create-memento-gateway.ts",
|
|
] as const) {
|
|
if (await exists(obsolete)) {
|
|
fail(`OBSOLETE_ASSET: ${obsolete} must not return`);
|
|
}
|
|
}
|
|
|
|
const presentationRoot = "src/features/memento-web/presentation";
|
|
for (const relative of await walk(presentationRoot)) {
|
|
if (!/\.(?:ts|tsx)$/u.test(relative)) continue;
|
|
const source = await text(relative);
|
|
for (const [id, pattern] of [
|
|
["DOM_QUERY", /\b(?:document\.)?(?:querySelector|querySelectorAll|getElementById)\s*\(/u],
|
|
["GLOBAL_CLICK_HANDLER", /\bdocument\.addEventListener\s*\(\s*["']click["']/u],
|
|
["DIRECT_FETCH", /\b(?:globalThis\.)?fetch\s*\(/u],
|
|
] as const) {
|
|
if (pattern.test(source)) {
|
|
fail(`${id}: ${relative}`);
|
|
}
|
|
}
|
|
|
|
for (const match of source.matchAll(/from\s+["']([^"']*presentation\/design-system\/[^"']+)["']/gu)) {
|
|
const imported = match[1] ?? "";
|
|
if (!imported.endsWith("/design-system/index.ts")) {
|
|
fail(`DESIGN_SYSTEM_DEEP_IMPORT: ${relative} -> ${imported}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
const featureRoot = "src/features/memento-web";
|
|
for (const relative of await walk(featureRoot)) {
|
|
if (!/\.(?:ts|tsx)$/u.test(relative)) continue;
|
|
const source = await text(relative);
|
|
if (/\b(?:globalThis\.)?fetch\s*\(/u.test(source)) {
|
|
fail(`FEATURE_DIRECT_FETCH: ${relative}`);
|
|
}
|
|
}
|
|
|
|
for (const relative of (await walk("public")).filter((file) => file.endsWith(".js"))) {
|
|
const source = await text(relative);
|
|
if (
|
|
/\b(?:querySelector|querySelectorAll|getElementById|classList|onclick|addEventListener)\b/u.test(
|
|
source,
|
|
)
|
|
) {
|
|
fail(`PUBLIC_IMPERATIVE_UI_JS: ${relative}`);
|
|
}
|
|
}
|
|
|
|
const serviceWorkerGenerator = await text("scripts/generate-service-worker-assets.ts");
|
|
if (/beta\.(?:js|css)/u.test(serviceWorkerGenerator)) {
|
|
fail("SERVICE_WORKER_BETA_ASSET: beta asset remains in service-worker generator");
|
|
}
|
|
|
|
const contractSource = await text(
|
|
"src/features/memento-web/contracts/memento-feature-contract.ts",
|
|
);
|
|
const runtimeSource = await text(
|
|
"src/features/memento-web/presentation/memento-feature-runtime.tsx",
|
|
);
|
|
for (const routeId of [
|
|
"APP_HOME",
|
|
"MEMENTO_LIBRARY",
|
|
"MEMENTO_MEMORIES",
|
|
"MEMENTO_MEMORY_DETAIL",
|
|
"MEMENTO_ALBUMS",
|
|
"MEMENTO_ALBUM_DETAIL",
|
|
"MEMENTO_SEARCH",
|
|
"MEMENTO_PEOPLE",
|
|
"MEMENTO_PERSON_DETAIL",
|
|
"MEMENTO_PLACES",
|
|
"MEMENTO_PLACE_DETAIL",
|
|
"MEMENTO_SETTINGS",
|
|
"MEMENTO_LOGIN",
|
|
"MEMENTO_REGISTER",
|
|
] as const) {
|
|
if (!contractSource.includes(`${routeId}: route({`)) {
|
|
fail(`ROUTE_CONTRACT_MISSING: ${routeId}`);
|
|
}
|
|
if (!runtimeSource.includes(`${routeId}: Object.freeze({`)) {
|
|
fail(`ROUTE_RUNTIME_MISSING: ${routeId}`);
|
|
}
|
|
}
|
|
|
|
|
|
const approvedLegacyFixture = Object.freeze({
|
|
"tests/fixtures/memento-visual-baseline/legacy/index.html":
|
|
"d4ec52fcb9092468f18da4f13b462ac85bc60eef89fcfaabb31a5493387f9cde",
|
|
"tests/fixtures/memento-visual-baseline/legacy/beta.css":
|
|
"83476b0b188a8c89e64b693f33714f244e0ec30d107fc2485eb4e61ff40de420",
|
|
"tests/fixtures/memento-visual-baseline/legacy/beta.js":
|
|
"74790fa2dbb07de3c95eaeba709e2477d6710a2a8fbac4f07ca19b078b39a689",
|
|
"tests/fixtures/memento-visual-baseline/legacy/login-v2.html":
|
|
"891c3b05153a4a3c97f68c5cb19fc954884457a4b24b30e310ec61f073f8bf98",
|
|
"tests/fixtures/memento-visual-baseline/legacy/register-v2.html":
|
|
"53ce076096589408e9ccda0f856e96b974b82a0a0ab2d1fdde6bf3aea8e739c2",
|
|
} as const);
|
|
|
|
for (const [fixture, expectedHash] of Object.entries(approvedLegacyFixture)) {
|
|
if (!(await exists(fixture))) {
|
|
fail(`LEGACY_VISUAL_FIXTURE_MISSING: ${fixture}`);
|
|
continue;
|
|
}
|
|
const actualHash = await sha256(fixture);
|
|
if (actualHash !== expectedHash) {
|
|
fail(
|
|
`LEGACY_VISUAL_FIXTURE_CHANGED: ${fixture} expected ${expectedHash} got ${actualHash}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
for (const evidence of [
|
|
"tests/fixtures/memento-visual-baseline/README.md",
|
|
"tests/visual/MEMENTO_VISUAL_BASELINE.md",
|
|
"tests/visual/memento-legacy-parity.visual.spec.ts",
|
|
"playwright.memento-legacy.config.ts",
|
|
] as const) {
|
|
if (!(await exists(evidence))) {
|
|
fail(`LEGACY_VISUAL_EVIDENCE_MISSING: ${evidence}`);
|
|
}
|
|
}
|
|
|
|
|
|
function splitSelectorList(prelude: string): string[] {
|
|
const selectors: string[] = [];
|
|
let buffer = "";
|
|
let parentheses = 0;
|
|
let brackets = 0;
|
|
let quote: "'" | '"' | null = null;
|
|
for (let index = 0; index < prelude.length; index += 1) {
|
|
const character = prelude[index]!;
|
|
if (quote) {
|
|
buffer += character;
|
|
if (character === "\\") {
|
|
buffer += prelude[index + 1] ?? "";
|
|
index += 1;
|
|
} else if (character === quote) {
|
|
quote = null;
|
|
}
|
|
continue;
|
|
}
|
|
if (character === "'" || character === '"') {
|
|
quote = character;
|
|
buffer += character;
|
|
continue;
|
|
}
|
|
if (character === "(") parentheses += 1;
|
|
else if (character === ")") parentheses -= 1;
|
|
else if (character === "[") brackets += 1;
|
|
else if (character === "]") brackets -= 1;
|
|
if (character === "," && parentheses === 0 && brackets === 0) {
|
|
selectors.push(buffer.trim());
|
|
buffer = "";
|
|
continue;
|
|
}
|
|
buffer += character;
|
|
}
|
|
if (buffer.trim()) selectors.push(buffer.trim());
|
|
return selectors;
|
|
}
|
|
|
|
function matchingCssBrace(source: string, openIndex: number): number {
|
|
let depth = 0;
|
|
let quote: "'" | '"' | null = null;
|
|
for (let index = openIndex; index < source.length; index += 1) {
|
|
const character = source[index]!;
|
|
if (quote) {
|
|
if (character === "\\") index += 1;
|
|
else if (character === quote) quote = null;
|
|
continue;
|
|
}
|
|
if (character === "'" || character === '"') {
|
|
quote = character;
|
|
continue;
|
|
}
|
|
if (source.startsWith("/*", index)) {
|
|
const end = source.indexOf("*/", index + 2);
|
|
if (end < 0) return source.length - 1;
|
|
index = end + 1;
|
|
continue;
|
|
}
|
|
if (character === "{") depth += 1;
|
|
else if (character === "}") {
|
|
depth -= 1;
|
|
if (depth === 0) return index;
|
|
}
|
|
}
|
|
throw new Error("Unbalanced Memento CSS");
|
|
}
|
|
|
|
function collectCssSelectors(source: string): string[] {
|
|
const selectors: string[] = [];
|
|
let index = 0;
|
|
while (index < source.length) {
|
|
while (index < source.length && /\s/u.test(source[index]!)) index += 1;
|
|
if (source.startsWith("/*", index)) {
|
|
const end = source.indexOf("*/", index + 2);
|
|
index = end < 0 ? source.length : end + 2;
|
|
continue;
|
|
}
|
|
const open = source.indexOf("{", index);
|
|
if (open < 0) break;
|
|
const prelude = source.slice(index, open).trim();
|
|
const close = matchingCssBrace(source, open);
|
|
const body = source.slice(open + 1, close);
|
|
if (/^@(?:media|supports|layer|container)\b/u.test(prelude)) {
|
|
selectors.push(...collectCssSelectors(body));
|
|
} else if (!prelude.startsWith("@")) {
|
|
selectors.push(...splitSelectorList(prelude));
|
|
}
|
|
index = close + 1;
|
|
}
|
|
return selectors;
|
|
}
|
|
|
|
const mementoStylesheet = await text(
|
|
"src/presentation/design-system/memento/memento.css",
|
|
);
|
|
for (const selector of collectCssSelectors(mementoStylesheet)) {
|
|
if (
|
|
!selector.includes(".memento-app") &&
|
|
!selector.includes(".memento-auth-")
|
|
) {
|
|
fail(`MEMENTO_CSS_UNSCOPED: ${selector}`);
|
|
}
|
|
}
|
|
|
|
const visualSpec = "tests/visual/memento-prototype.visual.spec.ts";
|
|
if (!(await exists(visualSpec))) {
|
|
fail("VISUAL_SPEC_MISSING: Memento visual regression spec is required");
|
|
}
|
|
const snapshotDirectory =
|
|
"tests/visual/__snapshots__/memento-prototype.visual.spec.ts-snapshots";
|
|
if (!(await exists(snapshotDirectory))) {
|
|
fail("VISUAL_BASELINE_MISSING: Memento snapshot directory is required");
|
|
} else {
|
|
const snapshots = (await readdir(path.join(root, snapshotDirectory))).filter(
|
|
(name) => name.endsWith(".png"),
|
|
);
|
|
if (snapshots.length < 26) {
|
|
fail(
|
|
`VISUAL_BASELINE_INCOMPLETE: expected at least 26 snapshots, found ${snapshots.length}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
if (failures.length > 0) {
|
|
console.error("Memento assurance gate: FAIL");
|
|
for (const failure of failures) console.error(`- ${failure}`);
|
|
process.exitCode = 1;
|
|
} else {
|
|
console.log(
|
|
"Memento assurance gate: PASS (React ownership, route/runtime, HTTP boundary, prototype removal, visual baseline)",
|
|
);
|
|
}
|