/** * A TEST IN `cotton-tools` GETS ITS WORKSPACE FILE FROM THE * TEMPLATE, NEVER FROM THE MIGRATION LADDER. * * `create_workspace` runs every rung of the ladder from zero, and nearly every * test in this crate builds at least one new file. Measured 29 Sep 2026 * on this repo's Mac with SQLite optimised: 111 ms for the in-memory ladder and * 131 ms on disk, against 3.5 ms and 22 ms for a copy of the prebuilt template * (`cotton_store::test_template`). Across the same 4,966 tests that was 1,006 s * of test-binary time with the ladder and 235 s with the copy — most of the * `rust` check job's test run, which is the slowest of the six checks. * * So a fixture calls one of the helpers below instead, and a new call to the * ladder in a test source fails here: * * cotton_store::test_template create_workspace_in_memory_for_test * create_workspace_for_test * * The copy is the ladder's own output — `cotton-store`'s * `a_copy_has_the_same_schema_as_a_file_built_from_zero` holds that — so no * test loses anything it was checking. No file is allowed the ladder: the * suites whose subject it was went with the server. */ import { readdirSync, readFileSync, statSync } from "node:fs"; import { join, relative } from "node:path"; import { describe, expect, it } from "vitest"; const ROOT = join(__dirname, "..", ".."); const CRATES = ["crates/cotton-tools"]; /** The three ways to run the ladder: the store's own entry, the server's, and `FileStore::ensure`. */ const LADDER = [/(? { const path = join(dir, name); return statSync(path).isDirectory() ? walk(path) : [path]; }); } function isTestSource(path: string): boolean { const rel = relative(ROOT, path); if (!rel.endsWith(".rs")) return false; const name = rel.split("/").pop() ?? ""; return ( /\/tests\//.test(rel) || name.endsWith("_tests.rs") || name === "tests.rs" || name === "test_support.rs" || name === "test_files.rs" ); } function ladderCalls(): string[] { const found: string[] = []; for (const crate of CRATES) { for (const path of walk(join(ROOT, crate)).filter(isTestSource)) { const rel = relative(ROOT, path); readFileSync(path, "utf8") .split("\n") .forEach((line, i) => { if (line.trim().startsWith("//")) return; if (LADDER.some((pattern) => pattern.test(line))) found.push(`${rel}:${i + 1}: ${line.trim()}`); }); } } return found; } describe("test fixtures in cotton-tools copy the template", () => { it("no test source outside the allowed suites runs the migration ladder", () => { expect(ladderCalls()).toEqual([]); }); it("finds a ladder call when one is there, so an empty answer means something", () => { const sample = ' let db = Driver::open_in_memory().unwrap();\n create_workspace(&db, &meta).unwrap();'; expect(sample.split("\n").some((line) => LADDER.some((pattern) => pattern.test(line)))).toBe(true); expect(LADDER.some((pattern) => pattern.test("create_workspace_in_memory_for_test(&meta)"))).toBe(false); expect(LADDER.some((pattern) => pattern.test("create_workspace_for_test(&path, &meta)"))).toBe(false); }); });