// Rules a screenshot cannot show and a unit test cannot reach on its own: the // store root is kept out of iCloud backup, a renderer never downloads, only a // detail view stamps a view, and a thumbnail is COLLECTED, never fetched. // StoreBackupSweepTests.swift and ThumbFetchSweepTests.swift (CottonUITests), // ported to vitest: both only ever read the Swift sources as text. The describe // blocks keep the Swift suites' names (`StoreBackupSweepTests`, // `MediaFetchSweepTests`, `MediaViewedSweepTests`, `ThumbFetchSweepTests`), so a // comment that names one still finds its guard here. `StoreBackupTests` // (CottonKitTests) still asserts the call actually sets the flag. import { describe, expect, it } from "vitest"; import { appSources, matches, withoutComments } from "./helpers/swift-source-sweep"; // ── The store root is excluded from iCloud backup ───────────────────────────── describe("StoreBackupSweepTests", () => { // RULE: Opening the database always marks its folder as excluded from iCloud backup. it("the core is never opened without excluding the store root", () => { const sources = appSources(); // RULE: The backup sweep must read source before it can enforce its storage boundary. expect(sources.length, "the sweep read no sources").toBeGreaterThan(0); const openers: string[] = []; const forgetful: string[] = []; for (const { name, text } of sources) { const code = withoutComments(text); if (matches(String.raw`CottonRuntime\.open\(`, code).length === 0) continue; openers.push(name); if (matches(String.raw`StoreBackup\.exclude\(`, code).length === 0) forgetful.push(name); } expect( forgetful, "these open the core and never call `StoreBackup.exclude`. The store root is excluded from iCloud backup and RE-ASSERTED on every open — the flag is an extended attribute, and a container a fresh install rebuilt comes back without it. If a file genuinely opens a throwaway store (a test's temp directory), it does not belong in the app's sources.", ).toEqual([]); // Anti-vacuity: a sweep that found no opener would pass on nothing. expect(openers.length, "the sweep found no site that opens the core").toBeGreaterThan(0); }); }); // ── A renderer asks where the bytes are; it never fetches them ──────────────── /** Every file whose code matches any of `patterns`, by name. */ function callers(patterns: string[]): string[] { return appSources() .filter(({ text }) => { const code = withoutComments(text); return patterns.some((pattern) => matches(pattern, code).length > 0); }) .map(({ name }) => name); } describe("MediaFetchSweepTests", () => { /** Files that own a SEAM or a single picture's detail — never a grid — each saying which. */ const mayEnsure: Record = { "CottonUI/Meetings/MeetingsCore.swift": "ONE meeting's audio, on that meeting's own transcript page, fetched by the tap the person made (a line to play from, or Share's audio). `audio(of:)` asks `read.mediaPath` first and `media.ensure` only when the bytes are not on this phone; the list of meetings and their cards ask nothing of the bytes.", "CottonUI/Faces/PhotosMedia.swift": "The photos lane's seam, the same split: `PhotosMediaSource.path` is what a tile asks and `ensure` is what a tap asks. `PhotosPicture` calls only `path`.", "CottonUI/Scaffold/CottonCoreKey.swift": "The shared file-store seam. localURL reads only local bytes; ensure\nis explicit and called only by the single file detail's Fetch tap.", "CottonUI/Sources/ReleaseTableSource.swift": "ONE release asset, on that release's own page, fetched by the act the person pressed. `ReleaseAssetBytes` is `DocumentBytes`' split under other names — `here` is `read.mediaPath` and `fetch` is the method — because a release asset in this repository's own history runs to two gigabytes. The LIST of releases asks nothing of the bytes at all.", "CottonKit/TrafficMethods.swift": "The method NAME is declared here and nowhere else. Declaring a wire string is not calling it.", "CottonUI/Chat/ChatRecordings.swift": "ONE recording's audio, on that recording's own page, fetched by the Play or scrub tap the person made (3e4d8a4a3b). `ready` plays the page's own `audio.path` (on this phone) first and asks `media.ensure` only when there is none; the Calls list's recording rows ask nothing of the bytes.", "CottonUI/Chat/ChatFaces.swift": "A SEAM, never a renderer (Android 6d83189695: faces stayed initials because nothing fetched them). After the chat list loads, its rows' face photos (a person's, a contact's, a group's) are asked one at a time in the background, never in Low Data Mode, each once until a refusal's wait runs out. The faces themselves read only `read.mediaPath` (`RecordPicture`) and redraw on the media doorbell.", "CottonUI/Chat/ChatWarmSeed.swift": "The simulator seed's invented core ANSWERS `media.ensure` (a `case` on the method name) with a bundled invented voice file for `seed-` keys; it calls nothing and never reaches the network.", }; // RULE: A screen draws the picture already on disk; only a shared loading step or a detail view starts a download. it("only a seam or a detail view can fetch media", () => { expect(appSources().length, "the sweep read no sources").toBeGreaterThan(0); const found = callers(["mediaEnsure", String.raw`"media\.ensure"`]); expect( found.filter((name) => mayEnsure[name] === undefined), "these reach `media.ensure`, which MAY GO TO THE NETWORK. A renderer asks `read.mediaPath` — where the bytes are on THIS device — and draws the absence when they are not; only a seam, or the detail view of ONE picture on a tap the person made, fetches. If this really is such a view, add it to `mayEnsure` with the sentence saying which.", ).toEqual([]); expect(found.length, "the sweep found no media caller at all").toBeGreaterThan(0); for (const [name, reason] of Object.entries(mayEnsure)) { expect(reason.trim(), `${name} is allow-listed with no reason written`).not.toBe(""); expect(found, `${name} is allow-listed and no longer touches media.ensure — delete the entry`).toContain(name); } }); }); describe("MediaViewedSweepTests", () => { /** `media.viewed` orders the budgeted phone cache: a stamp written from a grid would turn "least recently viewed" into "least recently scrolled past". */ const mayStamp: Record = { "CottonUI/Faces/PhotosMedia.swift": "The photos lane's seam. `PhotosMediaSource.viewed` is the stamp and only the one viewer (`MediaViewer`) and a page's recording call it; `PhotosPicture`, the tile, asks `path` and nothing else.", "CottonKit/TrafficMethods.swift": "The method NAME is declared here and nowhere else. Declaring a wire string is not calling it.", }; // RULE: Only a detail view stamps `media.viewed`, the column that decides which files leave the phone when the media budget is reached. it("only a detail view stamps a view", () => { // RULE: The backup sweep must read source before it can enforce its restore path. expect(appSources().length, "the sweep read no sources").toBeGreaterThan(0); const found = callers(["mediaViewed", String.raw`"media\.viewed"`]); expect( found.filter((name) => mayStamp[name] === undefined), "these stamp `media.viewed`. That column decides WHICH FILES LEAVE THIS PHONE when the media budget is reached, least recently viewed first — so a grid, a list or any renderer that runs as a finger moves must never write it. If this really is the detail view of ONE piece of media, add it to `mayStamp` with the sentence saying which.", ).toEqual([]); expect(found.length, "the sweep found no caller at all").toBeGreaterThan(0); for (const [name, reason] of Object.entries(mayStamp)) { expect(reason.trim(), `${name} is allow-listed with no reason written`).not.toBe(""); expect(found, `${name} is allow-listed and no longer stamps a view — delete the entry`).toContain(name); } }); }); // ── A thumbnail is COLLECTED, never fetched ─────────────────────────────────── // // The eager sweep (`cotton-client/src/media/thumbs.rs`) is the only thing that // asks a server for a thumbnail. A screen asks `read.mediaPath`, and the crate // answers the thumbnail, the original or an honest absence. describe("ThumbFetchSweepTests", () => { /** The suffix the Rust builds keys with (`cotton_core::media::keys::THUMB_SUFFIX`). The leading dot is load-bearing: `fingerprint256` is a real field name. */ const thumbSuffix = String.raw`\.t256|t256\.jpg`; /** The detail views that may name a tier, and the one word each may say. */ const mayNameATier: Record = { "CottonUI/Faces/PagePicture.swift": "largest", // The one viewer: ONE picture (and the two either side of it) drawn at screen size from the largest copy here. "CottonUI/Viewer/ViewerPhoto.swift": "largest", // The one viewer's player: ONE film's poster, until its first frame. "CottonUI/Viewer/ViewerPlayer.swift": "largest", // A film playing (`FilmStream.artwork`): ONE film's poster, drawn once into the lock screen's artwork. "CottonUI/Viewer/FilmStream.swift": "largest", // A page's player (`MediaStage`): ONE film's bytes, on its record's page. "CottonUI/Faces/MediaStage.swift": "original", // A slip with a player (`SlipFace.askRecordingHere`): asks only WHETHER one recording's bytes are here. "CottonUI/Faces/SlipFace.swift": "original", // A tool card's photo (`ToolPhotograph`): a cut-out keeps its alpha only in the original. "CottonUI/Paper/ToolObject.swift": "original", }; // RULE: A thumbnail is never downloaded on its own: only the full picture is fetched, and the grid draws what is already there. it("no call site hands a thumb key to media.ensure", () => { const sources = appSources(); // RULE: The thumbnail sweep must read source before it can enforce bounded fetches. expect(sources.length, "the sweep read no sources").toBeGreaterThan(0); const spellers = sources.filter(({ text }) => matches(thumbSuffix, withoutComments(text)).length > 0).map(({ name }) => name); expect( spellers, "these spell a thumbnail key. Thumbnail keys are `cotton_core::media::keys`'s and the app never builds one: a tile is handed the picture's own key and `read.mediaPath` resolves the tier. A key spelled here could be handed to `media.ensure`, which MAY GO TO THE NETWORK.", ).toEqual([]); }); // RULE: Only the detail view of ONE picture or ONE film names a tier, and only its own word (`largest` for the hero, `original` for the player); every allow-listed file still names one. it("only a detail view names a tier and only its own word", () => { const sources = appSources(); // RULE: The thumbnail sweep must read source before it can enforce one fetch path. expect(sources.length, "the sweep read no sources").toBeGreaterThan(0); const speakers: string[] = []; const strangers: string[] = []; const wrongWords: string[] = []; for (const { name, text } of sources) { const hits = matches(String.raw`tier: \.(largest|original)`, withoutComments(text)); if (hits.length === 0) continue; speakers.push(name); const word = mayNameATier[name]; if (word === undefined) { strangers.push(name); continue; } for (const hit of hits) if (!hit.endsWith(word)) wrongWords.push(`${name} says \`${hit}\`; it is allowed \`.${word}\` and nothing else`); } expect( strangers, "these ask `read.mediaPath` for a TIER. Only the detail view of ONE picture or ONE film may — a grid that asked for originals would decode a 4 MB HEIC per tile. If this really is such a view, add it to `mayNameATier` with the one word it may say.", ).toEqual([]); expect(wrongWords).toEqual([]); for (const name of Object.keys(mayNameATier)) { expect(speakers, `${name} is allow-listed and no longer names a tier — delete the entry`).toContain(name); } }); });