import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { expect, it } from "vitest";
const root = resolve(__dirname, "../..");
const read = (path: string) => readFileSync(resolve(root, path), "utf8");
// RULE: The Mac app Cotton has no updater unless its build was given the Sparkle public key: the committed Info.plist names no feed and no key, and the build script writes both only under COTTON_SPARKLE_PUBLIC_KEY, so no build ever asks a feed it cannot verify.
it("stamps the feed and key only when the build is given the key", () => {
const plist = read("apps/cotton/Mac/Resources/Info.plist");
expect(plist).not.toContain("SUFeedURL");
expect(plist).not.toContain("SUPublicEDKey");
expect(plist).toMatch(/SUAutomaticallyUpdate<\/key>\s*/);
const build = read("apps/cotton/scripts/build-cotton-mac.sh");
const stamp = build.slice(build.indexOf('if [ -n "${COTTON_SPARKLE_PUBLIC_KEY:-}" ]'), build.indexOf("# ── 4."));
expect(stamp).toContain("Add :SUFeedURL string");
expect(stamp).toContain("Add :SUPublicEDKey string $COTTON_SPARKLE_PUBLIC_KEY");
expect(build.split("Add :SUFeedURL").length).toBe(2);
});
// RULE: Sparkle's nested code is signed inside out before the app — XPC services, Autoupdate, Updater.app, the framework — and the app's entitlements go on the app alone.
it("signs Sparkle inside out before the app", () => {
const build = read("apps/cotton/scripts/build-cotton-mac.sh");
const nested = build.indexOf('"$SPARKLE/Versions/B/XPCServices/"*.xpc "$SPARKLE/Versions/B/Autoupdate" "$SPARKLE/Versions/B/Updater.app" "$SPARKLE"');
const app = build.indexOf('--entitlements "$MAC_DIR/Cotton.entitlements" "$BUNDLE"');
expect(nested).toBeGreaterThan(0);
expect(app).toBeGreaterThan(nested);
});