207 lines
8.1 KiB
TypeScript
207 lines
8.1 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { readFileSync } from "node:fs";
|
|
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("detects Homebrew Cellar paths on Apple Silicon and Intel macOS", () => {
|
|
expect(isHomebrewInstall("/opt/homebrew/Cellar/seal-use/0.10.33/bin/seal-use")).toBe(true);
|
|
expect(isHomebrewInstall("/usr/local/Cellar/seal-use/0.10.33/bin/seal-use")).toBe(true);
|
|
expect(isHomebrewInstall("/opt/homebrew/bin/seal-use")).toBe(true);
|
|
});
|
|
|
|
test("detects Homebrew Cellar paths on Linuxbrew", () => {
|
|
expect(isHomebrewInstall("/home/linuxbrew/.linuxbrew/Cellar/seal-use/0.10.33/bin/seal-use")).toBe(true);
|
|
});
|
|
|
|
test("returns false for non-Homebrew paths", () => {
|
|
expect(isHomebrewInstall("/usr/local/bin/seal-use")).toBe(false);
|
|
expect(isHomebrewInstall("/Users/alice/.local/bin/seal-use")).toBe(false);
|
|
expect(isHomebrewInstall("/tmp/seal-use")).toBe(false);
|
|
});
|
|
});
|
|
});
|