import { describe, expect, test } from "bun:test"; import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { compareSemVer, getMirrorMode, isHomebrewInstall, 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; }); describe("update command bgSave and cache integration", () => { test("bgSave option saves cache silently without stdout output", async () => { const { mkdtempSync, rmSync } = await import("node:fs"); const { tmpdir } = await import("node:os"); const { handleUpdateCommand } = await import("./update.js"); const { loadUpdateCache, UPDATE_CACHE_FILE_ENV } = await import("../../core/update-cache.js"); const tempDir = mkdtempSync(join(tmpdir(), "seal-use-bg-save-test-")); const tempCacheFile = join(tempDir, "update-cache.json"); const prevEnv = process.env[UPDATE_CACHE_FILE_ENV]; process.env[UPDATE_CACHE_FILE_ENV] = tempCacheFile; try { const mockRuntime = { appRoot: process.cwd(), entryFile: join(process.cwd(), "src/cli.ts") }; // Mock fetch to avoid real network calls during test const originalFetch = globalThis.fetch; globalThis.fetch = (async () => new Response( JSON.stringify([ { tag_name: "v0.10.99", published_at: "2026-09-20T12:00:00Z", html_url: "https://github.com/yunyingk/seal-use/releases/tag/v0.10.99", assets: [] } ]), { status: 200, headers: { "Content-Type": "application/json" } } )) as any; let stdoutCaptured = ""; const originalLog = console.log; console.log = (msg: string) => { stdoutCaptured += msg; }; try { await handleUpdateCommand(mockRuntime, { checkOnly: true, bgSave: true }); // bgSave should NOT print to stdout expect(stdoutCaptured).toBe(""); // Cache should be saved with latestVersion const cache = loadUpdateCache(tempCacheFile); expect(cache).not.toBeNull(); expect(cache?.latestVersion).toBe("0.10.99"); expect(cache?.publishedAt).toBe("2026-09-20T12:00:00Z"); } finally { globalThis.fetch = originalFetch; console.log = originalLog; } } finally { if (prevEnv !== undefined) { process.env[UPDATE_CACHE_FILE_ENV] = prevEnv; } else { delete process.env[UPDATE_CACHE_FILE_ENV]; } rmSync(tempDir, { recursive: true, force: true }); } }); describe("isHomebrewInstall", () => { test("requires the executable to resolve inside Homebrew's registered formula prefix", () => { const root = mkdtempSync(join(tmpdir(), "seal-use-homebrew-detect-")); try { const formulaPrefix = join(root, "opt", "seal-use"); const cellarBin = join(root, "Cellar", "seal-use", "0.10.33", "bin"); const nativeBin = join(root, "bin"); const formulaExecutable = join(cellarBin, "seal-use"); const nativeExecutable = join(nativeBin, "seal-use"); mkdirSync(cellarBin, { recursive: true }); mkdirSync(nativeBin, { recursive: true }); mkdirSync(join(root, "opt"), { recursive: true }); writeFileSync(formulaExecutable, "homebrew binary"); writeFileSync(nativeExecutable, "native binary"); symlinkSync(join(root, "Cellar", "seal-use", "0.10.33"), formulaPrefix); expect(isHomebrewInstall(formulaExecutable, formulaPrefix)).toBe(true); expect(isHomebrewInstall(nativeExecutable, formulaPrefix)).toBe(false); } finally { rmSync(root, { recursive: true, force: true }); } }); test("does not infer Homebrew from a matching directory name alone", () => { expect(isHomebrewInstall("/opt/homebrew/bin/seal-use", "/tmp/not-installed-seal-use")).toBe(false); }); }); });