// The catalogue deriver's two judgements: what key a sentence gets, and when it // refuses to guess one. // // The refusal is the point of the test. An earlier ad-hoc version of this script // guessed a specifier from the interpolation's NAME, and two runs left // `Localizable.xcstrings` carrying `"%@ MB so far"` beside `"%lld MB so far"` — // one sentence, two keys, and the app rendering from whichever one Swift // happened to compute. So an interpolation the script cannot type must produce // no key at all, and say so. import { describe, expect, it } from "vitest"; // @ts-expect-error — a plain ESM script with no type declarations, imported for // its two pure functions rather than re-implemented here. import { keyFor, looseShape, sentencesIn, shape, staleKeys } from "../ios-strings-catalogue.mjs"; const typed = (pairs: [string, string][]) => new Map(pairs); describe("keyFor", () => { it("leaves a sentence with no interpolation exactly as it is", () => { expect(keyFor("Open TestFlight", typed([])).key).toBe("Open TestFlight"); }); it("takes the specifier from the interpolated value's TYPE, never its name", () => { expect(keyFor("\\(count) days late", typed([["count", "Int"]])).key).toBe("%lld days late"); expect(keyFor("\\(count) days late", typed([["count", "String"]])).key).toBe("%@ days late"); expect(keyFor("\\(score) out of 5", typed([["score", "Double"]])).key).toBe("%lf out of 5"); }); it("refuses a sentence whose interpolation it cannot type, and names it", () => { const { key, untyped } = keyFor("\\(spoken(value)) out of 5", typed([])); expect(key).toBeNull(); expect(untyped).toBe("spoken(value)"); }); it("resolves a Swift unicode escape rather than treating it as an interpolation", () => { expect(keyFor("Written by \\u{00B7} you", typed([])).key).toBe("Written by · you"); }); it("carries several interpolations in order", () => { const types = typed([ ["who", "String"], ["n", "Int"], ]); expect(keyFor("\\(who) left \\(n)", types).key).toBe("%@ left %lld"); }); }); describe("sentencesIn", () => { it("types each literal from the declaration it sits in, not from the file", () => { const source = [ "public static var plain: String { local(\"Plain\") }", "public static func late(_ n: Int) -> String { local(\"\\(n) days late\") }", "public static func who(_ n: String) -> String { local(\"\\(n) asked\") }", ].join("\n"); expect(sentencesIn(source).found).toEqual(["Plain", "%lld days late", "%@ asked"]); }); it("reads a literal wrapped onto its own line — the shape swift-format gives a long sentence", () => { // 2026-09-10: the single-line pattern reported "0 sentences missing" while // 91 wrapped literals on phone-4h-swift were invisible to it — the whole // privacy policy and terms among them. const source = [ "public static var long: String {", " local(", " \"A sentence long enough that the formatter wrapped it.\"", " )", "}", "public static func late(_ n: Int) -> String {", " local(", " \"\\(n) days late, wrapped\"", " )", "}", ].join("\n"); expect(sentencesIn(source).found).toEqual([ "A sentence long enough that the formatter wrapped it.", "%lld days late, wrapped", ]); }); it("reports an untypeable interpolation instead of inventing a key for it", () => { const source = "public static func x(_ a: Double) -> String { local(\"\\(spoken(a)) out\") }"; const { found, untyped } = sentencesIn(source); expect(found).toEqual([]); expect(untyped).toEqual([{ literal: "\\(spoken(a)) out", expr: "spoken(a)" }]); }); }); describe("shape", () => { it("blanks every specifier so one sentence cannot enter the catalogue twice", () => { expect(shape("%lld MB so far")).toBe(shape("%@ MB so far")); expect(shape("%lld added")).not.toBe(shape("%lld removed")); }); }); describe("staleKeys", () => { it("names the catalogue entries no table says any more", () => { const have = ["Done", "Archived", "A sentence nothing says"]; expect(staleKeys(have, ["Done", "Archived"])).toEqual(["A sentence nothing says"]); }); it("keeps a sentence whose interpolation changed TYPE, rather than emptying it out", () => { // The add pass declines to mint `"%lld MB so far"` while `"%@ MB so far"` // is already there, so a prune on the exact key would delete the entry // nothing is going to replace — one sentence, then no sentence. expect(staleKeys(["%@ MB so far"], ["%lld MB so far"])).toEqual([]); }); it("is the empty list when every entry is still said", () => { expect(staleKeys(["Done"], ["Done", "Archived"])).toEqual([]); }); it("takes the whole catalogue when the tables say nothing — the I4 end state", () => { expect(staleKeys(["Done", "Archived"], [])).toEqual(["Archived", "Done"]); }); it("sorts, so two runs over the same drift print the same list", () => { expect(staleKeys(["Zed", "Alpha", "Mid"], [])).toEqual(["Alpha", "Mid", "Zed"]); }); it("reads the two sets it is handed and nothing else", () => { const have = new Set(["Done"]); const live = new Set(); expect(staleKeys(have, live)).toEqual(["Done"]); expect([...have]).toEqual(["Done"]); expect([...live]).toEqual([]); }); }); describe("looseShape", () => { it("shapes a literal the script refuses to key, so the prune cannot call it dead", () => { // `\(groupedFigure(count))` cannot be typed, so it yields no key and is // absent from the live set — and ten sentences the app renders today would // read as stale. Their SHAPE is computable, and shape is what the prune // compares on. expect(looseShape("\\(groupedFigure(count)) thoughts to file")).toBe( shape("%lld thoughts to file"), ); }); it("agrees with a typed key's shape whatever the specifier would have been", () => { expect(looseShape("\\(n) days late")).toBe(shape(keyFor("\\(n) days late", new Map([["n", "Int"]])).key)); }); it("still tells two different sentences apart", () => { expect(looseShape("\\(x) added")).not.toBe(looseShape("\\(x) removed")); }); it("resolves a unicode escape, as the key would", () => { expect(looseShape("Synced \\u{00B7} \\(counted)")).toBe(shape("Synced \u00b7 %@")); }); });