import { describe, expect, test } from "bun:test"; import { readFileSync } from "node:fs"; import { join } from "node:path"; import { compareSemVer, getMirrorMode, isNewerVersion, matchAssetForPlatform } from "./update.js"; type Asset = { name: string; downloadUrl: string; size: number }; function assets(...names: string[]): Asset[] { return names.map((name) => ({ name, downloadUrl: `https://example.test/${name}`, size: 1024 })); } describe("update asset selection", () => { test("prefers the compressed archive on darwin and linux", () => { const released = assets( "seal-use-darwin-arm64", "seal-use-darwin-arm64.tar.gz", "seal-use-darwin-x64", "seal-use-darwin-x64.tar.gz" ); // ~25 MB instead of ~67 MB, and it is the asset the Gitea mirror publishes first. expect(matchAssetForPlatform(released, "darwin", "arm64")?.name).toBe("seal-use-darwin-arm64.tar.gz"); expect(matchAssetForPlatform(released, "darwin", "x64")?.name).toBe("seal-use-darwin-x64.tar.gz"); expect( matchAssetForPlatform(assets("seal-use-linux-x64", "seal-use-linux-x64.tar.gz"), "linux", "x64")?.name ).toBe("seal-use-linux-x64.tar.gz"); }); test("prefers the raw executable on windows, where zip extraction is not reliable", () => { const released = assets("seal-use-windows-x64.zip", "seal-use-windows-x64.exe"); expect(matchAssetForPlatform(released, "windows", "x64")?.name).toBe("seal-use-windows-x64.exe"); }); test("falls back to whichever asset form the release actually has", () => { expect(matchAssetForPlatform(assets("seal-use-linux-x64"), "linux", "x64")?.name).toBe("seal-use-linux-x64"); expect(matchAssetForPlatform(assets("seal-use-linux-x64.tgz"), "linux", "x64")?.name).toBe("seal-use-linux-x64.tgz"); expect(matchAssetForPlatform(assets("seal-use-windows-x64.zip"), "windows", "x64")?.name).toBe( "seal-use-windows-x64.zip" ); }); test("never selects a checksum sidecar as the asset to install", () => { expect(matchAssetForPlatform(assets("seal-use-linux-x64.sha256"), "linux", "x64")).toBeUndefined(); }); test("ignores assets for another architecture", () => { expect(matchAssetForPlatform(assets("seal-use-darwin-x64.tar.gz"), "darwin", "arm64")).toBeUndefined(); }); }); describe("update skill install", () => { const source = readFileSync(join(import.meta.dir, "update.ts"), "utf8"); // Assert on code, not prose: the rationale for this boundary is written out as a comment next to // the call, and that comment names the helper it replaced. const code = source.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^[ \t]*\/\/.*$/gm, ""); // This is the one regression a normal unit test cannot reach: the stale-skill bug only appeared // when the *caller* was the previous release's binary, because an in-process sync writes whatever // skill was compiled into the running image. Asserting on the source is the cheapest way to keep // the boundary in place — the skill must be installed by the new binary as a child process, and // the result read back. test("installs the Agent Skill through the new binary, never in-process", () => { expect(code).not.toContain("syncGlobalSkillIfPresent"); expect(code).toContain('spawnSync(execPath, ["skill", "install"]'); expect(code).toContain("readInstalledSkillVersion"); // The read-back is what turns a silently stale skill into a loud failure. expect(code).toMatch(/version !== targetVersion/); }); }); describe("update version comparator", () => { test("correctly identifies newer semantic versions", () => { expect(isNewerVersion("0.7.0", "0.6.0")).toBe(true); expect(isNewerVersion("1.0.0", "0.6.0")).toBe(true); expect(isNewerVersion("0.6.1", "0.6.0")).toBe(true); expect(isNewerVersion("0.6.0", "0.6.0")).toBe(false); expect(isNewerVersion("0.5.9", "0.6.0")).toBe(false); expect(isNewerVersion("0.6.0", "0.7.0")).toBe(false); }); test("compareSemVer handles tags, major, minor, and patch", () => { expect(compareSemVer("v0.7.1", "0.7.0")).toBe(1); expect(compareSemVer("0.7.0", "v0.7.1")).toBe(-1); expect(compareSemVer("v0.7.1", "v0.7.1")).toBe(0); expect(compareSemVer("1.0.0", "0.9.9")).toBe(1); expect(compareSemVer("0.3.3", "0.7.1")).toBe(-1); }); }); describe("getMirrorMode routing", () => { const originalEnv = { ...process.env }; test("defaults to github when CI is set", () => { delete process.env.SEAL_USE_MIRROR; delete process.env.SEAL_HOME_MIRROR; process.env.CI = "true"; expect(getMirrorMode()).toBe("github"); }); test("defaults to auto when CI is not set", () => { delete process.env.SEAL_USE_MIRROR; delete process.env.SEAL_HOME_MIRROR; delete process.env.CI; expect(getMirrorMode()).toBe("auto"); }); test("respects SEAL_USE_MIRROR override", () => { process.env.SEAL_USE_MIRROR = "gitea"; expect(getMirrorMode()).toBe("gitea"); process.env.SEAL_USE_MIRROR = "github"; expect(getMirrorMode()).toBe("github"); }); // Restore env process.env = originalEnv; });