Repository Public
1import { describe, expect, it } from "vitest";2import { allocate, attend, branchBroken, delegate, gitAllowed, gitStep, hosting, initialGit, initialShell, parseCommand, runCommand, softmax, summarizeEval, tokenize, type GitAction, type Vec } from "@/modules/diagram-library/logic";3import { DIAGRAMS } from "@/modules/diagram-library/registry";4import { loadManifest } from "./helpers";56describe("diagram registry", () => {7 it("lists the fifteen diagrams of the specification, each tied to real sessions and with a stated simplification", () => {8 expect(DIAGRAMS.map((d) => d.number)).toEqual(Array.from({ length: 15 }, (_, i) => i + 1));9 const sessions = new Set(loadManifest().sessions.map((s) => s.id));10 for (const d of DIAGRAMS) { expect(d.sessions.length, d.id).toBeGreaterThan(0); for (const s of d.sessions) expect(sessions.has(s), `${d.id} → ${s}`).toBe(true); expect(d.simplification.length).toBeGreaterThan(40); }11 expect(Object.keys(loadManifest().diagrams).sort()).toEqual(DIAGRAMS.map((d) => d.id).sort());12 });13});1415describe("1. tokenisation", () => {16 const text = "Les modèles découpent.";17 it("gives different counts for the same text depending on the method", () => {18 expect(tokenize(text, "mots")).toEqual(["Les", "modèles", "découpent", "."]);19 expect(tokenize(text, "caracteres")).toHaveLength(20);20 const sub = tokenize(text, "sous-mots");21 expect(sub.length).toBeGreaterThan(4); expect(sub.length).toBeLessThan(20);22 expect(sub[0]).toBe("▁les");23 });24 it("loses no character when splitting into sub-words", () => {25 expect(tokenize(text, "sous-mots").join("").replace(/▁/g, "")).toBe("lesmodèlesdécoupent.");26 expect(tokenize("", "sous-mots")).toEqual([]);27 });28});2930describe("2. attention", () => {31 const v: Vec[] = [[1, 0], [1, 1], [0, 1], [-1, 1]];32 it("softmax sums to 1 and ignores masked scores", () => {33 expect(softmax([1, 2, 3]).reduce((a, b) => a + b, 0)).toBeCloseTo(1);34 expect(softmax([0, -Infinity])).toEqual([1, 0]);35 expect(softmax([1000, 1000])).toEqual([0.5, 0.5]); // numerically stable36 });37 it("matches a hand calculation", () => {38 // Token 3 (0;1): dot products 0, 1, 1 → scores 0, 0.7071, 0.7071 with the causal mask hiding token 4.39 const r = attend(v, v, v, 2, true);40 expect(r.masked).toEqual([false, false, false, true]);41 expect(r.scores[1]).toBeCloseTo(Math.SQRT1_2);42 const e = Math.exp(Math.SQRT1_2), z = 1 + 2 * e;43 expect(r.weights[0]).toBeCloseTo(1 / z); expect(r.weights[1]).toBeCloseTo(e / z); expect(r.weights[3]).toBe(0);44 expect(r.weights.reduce((a, b) => a + b, 0)).toBeCloseTo(1);45 expect(r.output[0]).toBeCloseTo((1 + e) / z); expect(r.output[1]).toBeCloseTo((2 * e) / z);46 });47 it("lets the first token see only itself under the causal mask, and everything without it", () => {48 expect(attend(v, v, v, 0, true).weights).toEqual([1, 0, 0, 0]);49 expect(attend(v, v, v, 0, false).weights.every((w) => w > 0)).toBe(true);50 });51});5253describe("3. context budget", () => {54 const b = { instructions: 800, documents: 4500, history: 2500, reserve: 1500 };55 it("fits when the request is within the window", () => {56 expect(allocate(16000, b, "historique")).toMatchObject({ requested: 9300, overflow: 0, fits: true, dropped: { documents: 0, history: 0 } });57 });58 it("drops history first, then documents, by exactly the overflow", () => {59 const a = allocate(8000, b, "historique");60 expect(a).toMatchObject({ overflow: 1300, fits: false, impossible: false, dropped: { history: 1300, documents: 0 } });61 expect(a.kept.instructions + a.kept.documents + a.kept.history + a.kept.reserve).toBe(8000);62 expect(allocate(4000, b, "historique").dropped).toEqual({ history: 2500, documents: 2800 });63 expect(allocate(8000, b, "documents").dropped).toEqual({ history: 0, documents: 1300 });64 });65 it("never touches instructions or the answer reserve, and says when that makes it impossible", () => {66 const a = allocate(2000, b, "historique");67 expect(a.kept).toMatchObject({ instructions: 800, reserve: 1500, documents: 0, history: 0 });68 expect(a.impossible).toBe(true);69 expect(allocate(8000, b, "aucune")).toMatchObject({ impossible: true, dropped: { history: 0, documents: 0 } });70 });71});7273describe("8. shell simulator", () => {74 const run = (...lines: string[]) => lines.reduce((acc, l) => runCommand(acc.state, l), { state: initialShell() } as ReturnType<typeof runCommand>);75 it("parses words, quotes and redirection", () => {76 expect(parseCommand('echo "bonjour le monde" > a.txt')).toEqual({ argv: ["echo", "bonjour le monde"], redirect: "a.txt" });77 expect(parseCommand(" ls src ")).toEqual({ argv: ["ls", "src"], redirect: null });78 });79 it("follows exit-code conventions", () => {80 expect(run("pwd")).toMatchObject({ stdout: "/projet", code: 0, builtin: true, pid: null });81 expect(run("ls")).toMatchObject({ stdout: "LISEZMOI.md\nnotes.txt\nsrc/", code: 0, builtin: false });82 expect(run("cat absent.txt")).toMatchObject({ code: 1 });83 expect(run("python")).toMatchObject({ code: 127, pid: null, stderr: "python : commande introuvable" });84 expect(run("grep agent notes.txt")).toMatchObject({ stdout: "agent\nagent de code", code: 0 });85 expect(run("grep licorne notes.txt")).toMatchObject({ stdout: "", stderr: "", code: 1 });86 expect(run("grep x")).toMatchObject({ code: 2 });87 });88 it("gives each external program a new process, not builtins", () => {89 const a = run("ls"), b = runCommand(a.state, "cd src"), c = runCommand(b.state, "ls");90 expect(a.pid).toBe(4201); expect(b.pid).toBeNull(); expect(c.pid).toBe(4202);91 expect(c.stdout).toBe("calcul.py");92 });93 it("changes the filesystem only through mkdir, touch and redirection, without mutating earlier states", () => {94 const start = initialShell();95 const a = runCommand(start, "mkdir essais");96 expect(a.changed).toBe("/projet/essais");97 expect(runCommand(start, "ls").stdout).not.toContain("essais"); // the original state is untouched98 expect(runCommand(a.state, "ls").stdout).toContain("essais/");99 expect(runCommand(a.state, "mkdir essais")).toMatchObject({ code: 1 });100 const w = runCommand(a.state, "echo bonjour > essais/salut.txt");101 expect(w).toMatchObject({ stdout: "", changed: "/projet/essais/salut.txt", code: 0 });102 expect(runCommand(w.state, "cat essais/salut.txt").stdout).toBe("bonjour");103 expect(runCommand(start, "cd ../..").state.cwd).toEqual([]);104 expect(runCommand(start, "cd notes.txt")).toMatchObject({ code: 1 });105 });106 it("offers no destructive command", () => {107 expect(run("rm -rf /")).toMatchObject({ code: 127 });108 });109});110111describe("9. delegation", () => {112 it("computes time and cost for both organisations", () => {113 const r = delegate({ tasks: [6, 9, 4], overhead: 2, aggregation: 3 });114 expect(r.sequential).toEqual({ time: 22, cost: 22 });115 expect(r.parallel).toEqual({ time: 14, cost: 28 });116 expect(r.speedup).toBeCloseTo(22 / 14); expect(r.extraCost).toBe(6);117 });118 it("shows that delegation can be slower and always costs at least as much", () => {119 const r = delegate({ tasks: [1, 1, 1], overhead: 5, aggregation: 0 });120 expect(r.parallel.time).toBeGreaterThan(r.sequential.time);121 expect(delegate({ tasks: [5, 5], overhead: 0, aggregation: 0 }).extraCost).toBe(0);122 });123});124125describe("11. git flow", () => {126 const play = (...actions: GitAction[]) => actions.reduce(gitStep, initialGit());127 it("requires an open request, passing tests and an approval before merging", () => {128 let s = play("branche", "commit", "pr");129 expect(gitAllowed(s, "fusionner")).toBe(false);130 s = gitStep(s, "tests"); expect(s.tests).toBe("réussis"); expect(gitAllowed(s, "fusionner")).toBe(false);131 s = gitStep(s, "approuver"); expect(gitAllowed(s, "fusionner")).toBe(true);132 s = gitStep(s, "fusionner");133 expect(s.merged).toBe(true); expect(s.commits.at(-1)).toMatchObject({ branch: "main", kind: "fusion" });134 });135 it("blocks the merge while a defect is not fixed, and re-requires tests and approval after any new commit", () => {136 let s = play("branche", "commit-defaut", "pr", "tests", "approuver");137 expect(s.tests).toBe("échoués"); expect(branchBroken(s)).toBe(true); expect(gitAllowed(s, "fusionner")).toBe(false);138 s = gitStep(s, "corriger");139 expect(s).toMatchObject({ tests: "non lancés", approved: false }); expect(branchBroken(s)).toBe(false);140 s = gitStep(gitStep(s, "tests"), "approuver");141 expect(gitAllowed(s, "fusionner")).toBe(true);142 });143 it("reverts by adding a commit, never by deleting history", () => {144 const merged = play("branche", "commit", "pr", "tests", "approuver", "fusionner");145 const reverted = gitStep(merged, "annuler");146 expect(reverted.commits.length).toBe(merged.commits.length + 1);147 expect(reverted.commits.at(-1)!.kind).toBe("annulation");148 expect(gitAllowed(reverted, "annuler")).toBe(false);149 });150 it("ignores impossible actions and keeps commit ids unique", () => {151 const s = gitStep(initialGit(), "fusionner");152 expect(s.commits).toHaveLength(1); expect(s.log).toContain("impossible");153 const many = play("branche", "commit", "commit-main", "commit");154 expect(new Set(many.commits.map((c) => c.id)).size).toBe(many.commits.length);155 expect(play("branche", "commit", "recommencer")).toEqual(initialGit());156 });157});158159describe("12. evaluation", () => {160 const cases = [{ id: "T1", label: "", category: "nominal" }, { id: "T2", label: "", category: "nominal" }, { id: "T3", label: "", category: "limite" }];161 it("finds regressions even when the pass rate improves", () => {162 const r = summarizeEval([...cases, { id: "T4", label: "", category: "hostile" }], { T1: true, T2: false, T3: false, T4: true }, { T1: true, T2: true, T3: true, T4: false }, ["nominal", "limite", "hostile"]);163 expect(r).toMatchObject({ passA: 2, passB: 3, regressions: ["T4"], fixes: ["T2", "T3"], stillFailing: [], shippable: false, uncovered: [] });164 expect(r.rateB).toBeGreaterThan(r.rateA);165 });166 it("reports categories with no test at all", () => {167 const r = summarizeEval(cases, { T1: true }, { T1: true }, ["nominal", "limite", "hostile", "panne"]);168 expect(r.uncovered).toEqual(["hostile", "panne"]); expect(r.shippable).toBe(true); expect(r.stillFailing).toEqual(["T2", "T3"]);169 expect(summarizeEval([], {}, {}, []).rateA).toBe(0);170 });171});172173describe("13. hosting break-even", () => {174 const base = { volume: 100, apiPrice: 5, hardware: 6000, months: 36, fixedMonthly: 80, capacity: 400 };175 it("computes both monthly costs and the break-even volume", () => {176 const r = hosting(base);177 expect(r.api).toBe(500); expect(r.local).toBeCloseTo(6000 / 36 + 80); expect(r.breakEven).toBeCloseTo((6000 / 36 + 80) / 5);178 expect(r.cheaper).toBe("local");179 expect(hosting({ ...base, volume: 10 }).cheaper).toBe("api");180 const be = hosting(base).breakEven!;181 expect(hosting({ ...base, volume: be }).cheaper).toBe("égal");182 });183 it("flags a volume above the machine's capacity and survives zero prices", () => {184 expect(hosting({ ...base, volume: 500 }).overCapacity).toBe(true);185 expect(hosting({ ...base, apiPrice: 0 }).breakEven).toBeNull();186 expect(hosting({ ...base, months: 0 }).local).toBe(80);187 });188});189