|
|
|
@@ -1,9 +1,21 @@
|
|
|
|
|
import { readdir, readFile, stat } from "node:fs/promises";
|
|
|
|
|
import { spawnSync } from "node:child_process";
|
|
|
|
|
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
|
|
|
import { readFile } from "node:fs/promises";
|
|
|
|
|
import { tmpdir } from "node:os";
|
|
|
|
|
import path from "node:path";
|
|
|
|
|
|
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
|
|
|
|
|
|
import { selectRepositoryPaths } from "../../scripts/lib/repository-layout.ts";
|
|
|
|
|
import {
|
|
|
|
|
parseRepositoryLayout,
|
|
|
|
|
REPOSITORY_LAYOUT,
|
|
|
|
|
selectRepositoryEntries,
|
|
|
|
|
type RepositoryLayoutEntry,
|
|
|
|
|
} from "../../scripts/lib/repository-layout.ts";
|
|
|
|
|
import {
|
|
|
|
|
childProcessTimeout,
|
|
|
|
|
DEFAULT_TEST_TIMEOUT_MS,
|
|
|
|
|
} from "../helpers/child-process-timeout.ts";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* README.md, 「복사한 뒤 지워도 되는 것」, tells a new project that
|
|
|
|
@@ -14,14 +26,20 @@ import { selectRepositoryPaths } from "../../scripts/lib/repository-layout.ts";
|
|
|
|
|
* The root instruction files left the list on 2026-11-07: they are the
|
|
|
|
|
* template's contract, which a copy keeps (template-contract-documents.test.ts).
|
|
|
|
|
*/
|
|
|
|
|
// The history-free set of config/repository-layout.json (R2). The registry
|
|
|
|
|
// itself names every root item, history documents included (R1); naming a
|
|
|
|
|
// root item is not reading it.
|
|
|
|
|
const CODE_PATHS = selectRepositoryPaths("history-free");
|
|
|
|
|
// The scan follows git's files. config/repository-layout.json says which root
|
|
|
|
|
// items hold no code, and its history-free set is what the scan has to reach
|
|
|
|
|
// (R2). The registry itself names every root item, history documents included
|
|
|
|
|
// (R1); naming a root item is not reading it.
|
|
|
|
|
const NOT_CODE_KINDS: ReadonlySet<string> = new Set(["document", "history", "untracked"]);
|
|
|
|
|
const CODE_FILE = /(?:\.(?:[cm]?[jt]sx?|json|ya?ml|py|sh|css|html|conf)|Dockerfile)$/u;
|
|
|
|
|
// The longest of the tests that list the files, on desktop 2026-11-22 under
|
|
|
|
|
// load 12 to 16: 2145 ms in test:unit, 352 ms in test:coverage's suites.
|
|
|
|
|
const SCAN_TIMEOUT = childProcessTimeout(2_145);
|
|
|
|
|
const LAYOUT_REGISTRY = "config/repository-layout.json";
|
|
|
|
|
|
|
|
|
|
// Built from pieces so that this file does not match itself.
|
|
|
|
|
const REVIEWS = ["docs", "reviews", ""].join("/");
|
|
|
|
|
const HISTORY = ["docs", "history"].join("/");
|
|
|
|
|
const KEPT = `${REVIEWS}adapters/INVENTORY.md`;
|
|
|
|
|
// The 2026-09-25 round's task folder moved into the history folder on
|
|
|
|
|
// 2026-11-07 (docs/README.md §3), so the history pattern covers it. A pattern
|
|
|
|
@@ -30,7 +48,7 @@ const KEPT = `${REVIEWS}adapters/INVENTORY.md`;
|
|
|
|
|
const DELETABLE = [
|
|
|
|
|
new RegExp(`${REVIEWS}(?!adapters/INVENTORY\\.md)`, "u"),
|
|
|
|
|
new RegExp(["docs", "superpowers"].join("/"), "u"),
|
|
|
|
|
new RegExp(["docs", "history"].join("/"), "u"),
|
|
|
|
|
new RegExp(HISTORY, "u"),
|
|
|
|
|
new RegExp(`\\b${"SPEC"}\\.md\\b`, "u"),
|
|
|
|
|
];
|
|
|
|
|
/**
|
|
|
|
@@ -41,19 +59,42 @@ const DELETABLE = [
|
|
|
|
|
*/
|
|
|
|
|
const UNNAMED_SECTION = new RegExp(`\\b${"spec"} ?§`, "iu");
|
|
|
|
|
|
|
|
|
|
async function codeFiles(directory: string): Promise<string[]> {
|
|
|
|
|
const entries = await readdir(directory, { withFileTypes: true }).catch(
|
|
|
|
|
() => [],
|
|
|
|
|
function scannedFiles(
|
|
|
|
|
listed: readonly string[],
|
|
|
|
|
layout: readonly RepositoryLayoutEntry[] = REPOSITORY_LAYOUT,
|
|
|
|
|
): string[] {
|
|
|
|
|
const skipped = new Set(
|
|
|
|
|
layout.filter(({ kind }) => NOT_CODE_KINDS.has(kind)).map((entry) => entry.path),
|
|
|
|
|
);
|
|
|
|
|
const files = await Promise.all(
|
|
|
|
|
entries.map(async (entry) => {
|
|
|
|
|
const target = path.join(directory, entry.name);
|
|
|
|
|
if (entry.isDirectory())
|
|
|
|
|
return entry.name === "node_modules" ? [] : codeFiles(target);
|
|
|
|
|
return /\.(?:[cm]?[jt]sx?|json|ya?ml)$/u.test(entry.name) ? [target] : [];
|
|
|
|
|
}),
|
|
|
|
|
return listed.filter((file) => CODE_FILE.test(file) && !skipped.has(file.split("/")[0]!));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Without the GIT_* a hook sets, which would point git at another repository. */
|
|
|
|
|
function ownRepositoryEnvironment(): NodeJS.ProcessEnv {
|
|
|
|
|
return Object.fromEntries(Object.entries(process.env).filter(([name]) => !name.startsWith("GIT_")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Tracked files, and new ones git does not ignore: a file counts before `git add`. */
|
|
|
|
|
function repositoryFiles(root = "."): string[] {
|
|
|
|
|
const listed = spawnSync(
|
|
|
|
|
"git",
|
|
|
|
|
["ls-files", "-z", "--cached", "--others", "--exclude-standard"],
|
|
|
|
|
{
|
|
|
|
|
cwd: root,
|
|
|
|
|
encoding: "utf8",
|
|
|
|
|
env: ownRepositoryEnvironment(),
|
|
|
|
|
// Shorter than the test's own timeout, so a git that hangs fails as itself.
|
|
|
|
|
timeout: DEFAULT_TEST_TIMEOUT_MS,
|
|
|
|
|
killSignal: "SIGKILL",
|
|
|
|
|
// As treeFiles in scripts/lib/docs-system.ts: the list grows with files the scan skips.
|
|
|
|
|
maxBuffer: 64 * 1024 * 1024,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
return files.flat();
|
|
|
|
|
if (listed.error || listed.status !== 0) {
|
|
|
|
|
throw new Error(`git ls-files failed: ${listed.error?.message ?? listed.stderr}`);
|
|
|
|
|
}
|
|
|
|
|
// A tracked file deleted in the work tree is still listed.
|
|
|
|
|
return listed.stdout.split("\0").filter((file) => file !== "" && existsSync(path.join(root, file)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -68,35 +109,160 @@ const RECORD_FOLDER_CHECKERS: ReadonlySet<string> = new Set([
|
|
|
|
|
"tests/unit/docs-system.test.ts",
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
async function linesMatching(patterns: readonly RegExp[]): Promise<string[]> {
|
|
|
|
|
const files = (
|
|
|
|
|
await Promise.all(
|
|
|
|
|
CODE_PATHS.map(async (entry) =>
|
|
|
|
|
(await stat(entry).catch(() => null))?.isDirectory() ? codeFiles(entry) : [entry],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
).flat().filter((file) => !RECORD_FOLDER_CHECKERS.has(file));
|
|
|
|
|
const matches: string[] = [];
|
|
|
|
|
for (const file of files) {
|
|
|
|
|
/**
|
|
|
|
|
* scripts/check-work-docs.py repeats that checker's folder lists
|
|
|
|
|
* (TypeScriptRulesTest in its test keeps the two equal), and its test writes
|
|
|
|
|
* records in a temporary repository. These lines are excused, not the files:
|
|
|
|
|
* another line there that names a history document may well read it.
|
|
|
|
|
*/
|
|
|
|
|
const RECORD_FOLDER_LINES: readonly Readonly<{ file: string; text: string }>[] = [
|
|
|
|
|
{
|
|
|
|
|
file: "scripts/check-work-docs.py",
|
|
|
|
|
text: `RECORD_FOLDERS = ("docs/design/", "${HISTORY}/", "${REVIEWS}")`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
file: "scripts/check-work-docs.py",
|
|
|
|
|
text: `RECORD_KINDS = ("docs/design/", "${HISTORY}/plans/", "${HISTORY}/evidence/", "${HISTORY}/packages/",`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
file: "scripts/check-work-docs.py",
|
|
|
|
|
text: `"${HISTORY}/study/", "docs/adr/")`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
file: "scripts/tests/test_check_work_docs.py",
|
|
|
|
|
text: `("${HISTORY}/old.md", f"\\n\`{deleted}\`\\n"),`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
file: "scripts/tests/test_check_work_docs.py",
|
|
|
|
|
text: `("${REVIEWS}old-review.md", f"\\n\`{deleted}\`\\n"),`,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
type Match = Readonly<{ file: string; line: number; text: string }>;
|
|
|
|
|
|
|
|
|
|
function matchingLines(file: string, content: string, patterns: readonly RegExp[]): Match[] {
|
|
|
|
|
return content
|
|
|
|
|
.split("\n")
|
|
|
|
|
.map((text, index) => ({ file, line: index + 1, text: text.trim() }))
|
|
|
|
|
.filter(({ text }) => patterns.some((pattern) => pattern.test(text)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function unexcused(matches: readonly Match[]): Match[] {
|
|
|
|
|
return matches.filter(
|
|
|
|
|
({ file, text }) =>
|
|
|
|
|
!RECORD_FOLDER_CHECKERS.has(file) &&
|
|
|
|
|
!RECORD_FOLDER_LINES.some((line) => line.file === file && line.text === text),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function linesMatching(patterns: readonly RegExp[]): Promise<Match[]> {
|
|
|
|
|
const matches: Match[] = [];
|
|
|
|
|
for (const file of scannedFiles(repositoryFiles())) {
|
|
|
|
|
if (file === LAYOUT_REGISTRY) continue;
|
|
|
|
|
const lines = (await readFile(file, "utf8").catch(() => "")).split("\n");
|
|
|
|
|
lines.forEach((line, index) => {
|
|
|
|
|
if (patterns.some((pattern) => pattern.test(line)))
|
|
|
|
|
matches.push(`${file}:${index + 1}`);
|
|
|
|
|
});
|
|
|
|
|
matches.push(...matchingLines(file, await readFile(file, "utf8"), patterns));
|
|
|
|
|
}
|
|
|
|
|
return matches;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function locations(matches: readonly Match[]): string[] {
|
|
|
|
|
return matches.map(({ file, line }) => `${file}:${line}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe("deletable history documents", () => {
|
|
|
|
|
it("are read by no code, test, script or configuration", async () => {
|
|
|
|
|
expect(await linesMatching(DELETABLE), `only ${KEPT} may be read`).toEqual([]);
|
|
|
|
|
// REV-FE-0019. A list kept by hand left out five Playwright configurations,
|
|
|
|
|
// the service worker build and .storybook/, so the scan follows the files.
|
|
|
|
|
it("are looked for in code and configuration that no list names", () => {
|
|
|
|
|
const layout = parseRepositoryLayout({
|
|
|
|
|
schemaVersion: 1,
|
|
|
|
|
entries: [
|
|
|
|
|
{ path: "app", kind: "source", role: "code", readBy: "build", sets: ["history-free"] },
|
|
|
|
|
{ path: "notes", kind: "document", role: "text", readBy: "people" },
|
|
|
|
|
{ path: "old-rounds", kind: "history", role: "records", readBy: "people" },
|
|
|
|
|
{ path: "local-output", kind: "untracked", role: "output", readBy: "tools", optional: true },
|
|
|
|
|
{ path: ".gallery", kind: "tool-convention", role: "gallery", readBy: "tools" },
|
|
|
|
|
{ path: "gallery.config.ts", kind: "tool-convention", role: "gallery", readBy: "tools" },
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
const read = [
|
|
|
|
|
"app/main.tsx",
|
|
|
|
|
"app/tool.py",
|
|
|
|
|
"app/run.sh",
|
|
|
|
|
"app/runner.Dockerfile",
|
|
|
|
|
"app/theme.css",
|
|
|
|
|
"app/index.html",
|
|
|
|
|
"app/proxy.conf",
|
|
|
|
|
".gallery/preview.tsx",
|
|
|
|
|
"gallery.config.ts",
|
|
|
|
|
"not-registered-yet.json",
|
|
|
|
|
];
|
|
|
|
|
const left = [
|
|
|
|
|
"app/README.md",
|
|
|
|
|
"app/logo.png",
|
|
|
|
|
"notes/table.json",
|
|
|
|
|
"old-rounds/evidence.ts",
|
|
|
|
|
"local-output/report.json",
|
|
|
|
|
];
|
|
|
|
|
expect(scannedFiles([...read, ...left], layout)).toEqual(read);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("are looked for however long git's list of files is", () => {
|
|
|
|
|
const root = mkdtempSync(path.join(tmpdir(), "history-scan-"));
|
|
|
|
|
try {
|
|
|
|
|
// 1300 files under an 880 byte folder: a list just over Node's 1 MiB default.
|
|
|
|
|
const folder = Array.from({ length: 4 }, () => "f".repeat(219)).join("/");
|
|
|
|
|
const files = Array.from({ length: 1300 }, (_, index) => `${folder}/${index}.ts`).sort();
|
|
|
|
|
mkdirSync(path.join(root, folder), { recursive: true });
|
|
|
|
|
for (const file of files) writeFileSync(path.join(root, file), "");
|
|
|
|
|
const made = spawnSync("git", ["init", "--quiet"], {
|
|
|
|
|
cwd: root,
|
|
|
|
|
env: ownRepositoryEnvironment(),
|
|
|
|
|
timeout: DEFAULT_TEST_TIMEOUT_MS,
|
|
|
|
|
killSignal: "SIGKILL",
|
|
|
|
|
});
|
|
|
|
|
expect(made.status).toBe(0);
|
|
|
|
|
expect(Buffer.byteLength(files.join("\0"))).toBeGreaterThan(1024 * 1024);
|
|
|
|
|
expect(repositoryFiles(root).sort()).toEqual(files);
|
|
|
|
|
} finally {
|
|
|
|
|
rmSync(root, { recursive: true, force: true });
|
|
|
|
|
}
|
|
|
|
|
}, SCAN_TIMEOUT);
|
|
|
|
|
|
|
|
|
|
it("are excused in a work document checker by the line, not by the file", () => {
|
|
|
|
|
const named = `RECORD_FOLDERS = ("docs/design/", "${HISTORY}/", "${REVIEWS}")`;
|
|
|
|
|
const read = `text = (root / "${REVIEWS}round9.md").read_text()`;
|
|
|
|
|
const file = "scripts/check-work-docs.py";
|
|
|
|
|
const matches = matchingLines(file, [named, read].join("\n"), DELETABLE);
|
|
|
|
|
expect(locations(unexcused(matches))).toEqual([`${file}:2`]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("are looked for in each history-free root", () => {
|
|
|
|
|
const files = scannedFiles(repositoryFiles());
|
|
|
|
|
expect(files.length).toBeGreaterThan(0);
|
|
|
|
|
// A removal drill may delete an optional root, or leave it empty.
|
|
|
|
|
const unreached = selectRepositoryEntries("history-free").filter(
|
|
|
|
|
({ path: root, optional }) =>
|
|
|
|
|
!optional && !files.some((file) => file === root || file.startsWith(`${root}/`)),
|
|
|
|
|
);
|
|
|
|
|
expect(unreached, "roots the scan does not reach").toEqual([]);
|
|
|
|
|
}, SCAN_TIMEOUT);
|
|
|
|
|
|
|
|
|
|
it("are read by no code, test, script or configuration", async () => {
|
|
|
|
|
const found = await linesMatching(DELETABLE);
|
|
|
|
|
expect(locations(unexcused(found)), `only ${KEPT} may be read`).toEqual([]);
|
|
|
|
|
// A copy may lack the checker; an excuse is stale only where its file is.
|
|
|
|
|
expect(
|
|
|
|
|
RECORD_FOLDER_LINES.filter(
|
|
|
|
|
({ file, text }) =>
|
|
|
|
|
existsSync(file) && !found.some((match) => match.file === file && match.text === text),
|
|
|
|
|
),
|
|
|
|
|
"RECORD_FOLDER_LINES entries that match no line any more",
|
|
|
|
|
).toEqual([]);
|
|
|
|
|
}, SCAN_TIMEOUT);
|
|
|
|
|
|
|
|
|
|
it("are not cited as an unnamed design section", async () => {
|
|
|
|
|
expect(
|
|
|
|
|
await linesMatching([UNNAMED_SECTION]),
|
|
|
|
|
locations(unexcused(await linesMatching([UNNAMED_SECTION]))),
|
|
|
|
|
"name the document: scale-architecture.md §N",
|
|
|
|
|
).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
}, SCAN_TIMEOUT);
|
|
|
|
|
});
|
|
|
|
|