Cotton code
93 lines · 5.5 KB · TypeScriptRaw
import { readFileSync } from "node:fs";import { resolve } from "node:path";import { describe, expect, it } from "vitest";// RULE: What sits under a conversation's notes — where an owner's session message got to (delivery, delivered_on), and a repository comment's verdict and place in the diff (verdict, verdict_said, place) — is typed facts the core fills and the definitions word; no Rust or Swift path composes it, so a rewording is data. The definitions that word these facts are records only since the 72nd amendment (21 Oct 2026), so this guard holds the code side.const root = resolve(__dirname, "../..");const read = (path: string): string => readFileSync(resolve(root, path), "utf8");/** The body of the first function whose signature matches, braces balanced * (string and comment contents are not code, so their braces are skipped). */function body(source: string, signature: RegExp, file: string): string {  const at = source.search(signature);  if (at < 0) throw Error(`${file}: no function matches ${signature} — if it moved, move this guard with it`);  const open = source.indexOf("{", at);  let depth = 0;  for (let i = open; i < source.length; i++) {    const c = source[i];    if (c === "/" && source[i + 1] === "/") { i = source.indexOf("\n", i); continue; }    if (c === '"') { i++; while (i < source.length && source[i] !== '"') { if (source[i] === "\\") i++; i++; } continue; }    if (c === "{") depth++;    if (c === "}" && --depth === 0) return source.slice(open, i + 1);  }  throw Error(`${file}: unbalanced function at ${signature}`);}/** String literals in code (comments dropped). */function literals(code: string): string[] {  const literals: string[] = [];  for (let i = 0; i < code.length; i++) {    if (code[i] === "/" && code[i + 1] === "/") { const end = code.indexOf("\n", i); i = end < 0 ? code.length : end; continue; }    if (code[i] !== '"') continue;    let text = "";    for (i++; i < code.length && code[i] !== '"'; i++) { if (code[i] === "\\") text += code[i++]; text += code[i]; }    literals.push(text);  }  return literals;}/** String literals that hold more than one word: the shape of a composed sentence. */const sentences = (code: string): string[] => literals(code).filter(text => /\p{L}+\s+\p{L}+/u.test(text));/** String literals that are not a key (a column's, or an answer's camelCase * one) or a value one holds: any word a * person reads (a capital, a space, a full stop) once `{…}` holes are set aside. */const words = (code: string): string[] =>  literals(code).filter(text => /\p{L}/u.test(text.replace(/\{[^}]*\}/g, "")) && !/^[a-z][A-Za-z0-9_]*$/.test(text));// Each place that touches a session message's delivery, and the function in it.const paths: [string, RegExp][] = [  ["crates/cotton-core/src/rules/session_delivery.rs", /pub fn delivery\(/],  ["crates/cotton-core/src/rules/session_delivery.rs", /pub fn as_str\(/],  ["crates/cotton-tools/src/mcp/handlers/tables/builtin.rs", /pub\(super\) fn session_delivery\(/],  ["crates/cotton-tools/src/mcp/handlers/tables/builtin.rs", /pub\(super\) fn conversation_footers\(/],  ["apps/cotton/Sources/CottonUI/Sources/SessionFacts.swift", /static func messageFooter\(/],  ["apps/cotton/Sources/CottonUI/Sources/SessionMessageTableSource.swift", /static func record\(/],];describe("session message delivery is typed facts the definitions word", () => {  it("no engine or app path composes a sentence for it", () => {    for (const [file, signature] of paths) expect(sentences(body(read(file), signature, file)), `${file} ${signature}`).toEqual([]);    // The repository comment's footer engine never takes session messages back.    const footers = body(read(paths[3][0]), paths[3][1], paths[3][0]);    expect(footers).not.toContain("session_message");    // The phone's records carry the facts, never a footer it worded.    expect(read(paths[5][0])).not.toMatch(/"footer"/);  });  it("the guard sees a composed sentence or a word where one is written", () => {    expect(words('{ author.unwrap_or_else(|| "Somebody".to_string()) }')).toEqual(["Somebody"]);    expect(words('{ insert("verdict_said", "changes_requested"); format!("{path}:{line}") }')).toEqual([]);    expect(sentences('{ format!("typed into the terminal on {machine}") }')).toEqual(["typed into the terminal on {machine}"]);    expect(sentences('{ return metaLine("picked up", at) }')).toEqual(["picked up"]);    expect(sentences('{ insert("delivery", "waiting") // "a comment says words"\n}')).toEqual([]);  });});// Each place that works out what sits under a repository comment, and the function in it.const repoPaths: [string, RegExp][] = [  ["crates/cotton-core/src/rules/repo_comment_facts.rs", /pub fn facts\(/],  ["crates/cotton-core/src/rules/repo_comment_facts.rs", /pub fn as_str\(/],  ["crates/cotton-tools/src/mcp/handlers/tables/builtin.rs", /pub\(super\) fn conversation_footers\(/],  // The phone's RepoCommentTableSource went with the Repository TableSources (2e6e50f9e):  // repository tables are drawn from definitions now, so no Swift path words a footer.];describe("a repository comment's footer is typed facts the definitions word", () => {  it("no engine or app path writes a word for it", () => {    for (const [file, signature] of repoPaths) expect(words(body(read(file), signature, file)), `${file} ${signature}`).toEqual([]);    // The server's footer engine composes nothing at all: it only copies the core's facts.    const footers = body(read(repoPaths[2][0]), repoPaths[2][1], repoPaths[2][0]);    expect(footers).not.toMatch(/format!|meta_line|review_word|\bago\(/);  });});