diff --git a/src/cli.test.ts b/src/cli.test.ts index 74e3259..6b058ba 100644 --- a/src/cli.test.ts +++ b/src/cli.test.ts @@ -185,6 +185,24 @@ describe("CLI version & help handling", () => { expect(parsed.currentVersion).toBe(VERSION); expect(Array.isArray(parsed.releases)).toBe(true); }); + + test("rejects downgrade to older version when target version is lower", () => { + const result = runCli(["update", "0.3.3"]); + expect(result.status).toBe(0); + const parsed = JSON.parse(result.stdout.trim()); + expect(parsed.updated).toBe(false); + expect(parsed.currentVersion).toBe(VERSION); + expect(parsed.targetVersion).toBe("0.3.3"); + expect(parsed.message).toContain("Cannot downgrade seal-home"); + }); + + test("treats latest keyword as latest release without downgrade error", () => { + const result = runCli(["update", "latest", "--check"]); + expect(result.status).toBe(0); + const parsed = JSON.parse(result.stdout.trim()); + expect(parsed).toHaveProperty("latestVersion"); + expect(parsed.updated).toBe(false); + }); }); function makeConfigEnv(): Record { diff --git a/src/cli/commands/update.test.ts b/src/cli/commands/update.test.ts index 07545d5..cde492e 100644 --- a/src/cli/commands/update.test.ts +++ b/src/cli/commands/update.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from "bun:test"; -import { isNewerVersion } from "./update.js"; +import { compareSemVer, isNewerVersion } from "./update.js"; describe("update version comparator", () => { test("correctly identifies newer semantic versions", () => { @@ -10,4 +10,12 @@ describe("update version comparator", () => { 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); + }); }); diff --git a/src/cli/commands/update.ts b/src/cli/commands/update.ts index ba9c7be..e6145da 100644 --- a/src/cli/commands/update.ts +++ b/src/cli/commands/update.ts @@ -32,6 +32,34 @@ export async function handleUpdateCommand(runtime: CliRuntime, options: UpdateOp }; const repo = options.repo || process.env[GITHUB_REPO_ENV]?.trim() || DEFAULT_GITHUB_REPO; + const rawTarget = options.targetVersion?.trim(); + const targetVersion = rawTarget && rawTarget.toLowerCase() !== "latest" ? rawTarget : undefined; + + // Validate targetVersion: prevent downgrade to older versions + if (targetVersion) { + const cleanTarget = targetVersion.replace(/^v/, ""); + const cmp = compareSemVer(cleanTarget, VERSION); + if (cmp < 0) { + printJson({ + updated: false, + currentVersion: VERSION, + targetVersion: cleanTarget, + mode: isCompiledBinary() ? "binary" : "source", + message: `Cannot downgrade seal-home from v${VERSION} to older version v${cleanTarget}. Updates must be forward-only.` + }); + return; + } + if (cmp === 0 && !options.force) { + printJson({ + updated: false, + currentVersion: VERSION, + targetVersion: cleanTarget, + mode: isCompiledBinary() ? "binary" : "source", + message: `seal-home is already at version v${VERSION}. Use --force to reinstall.` + }); + return; + } + } // 1. Changelog / Release History query if (options.changelogOnly) { @@ -113,9 +141,9 @@ export async function handleUpdateCommand(runtime: CliRuntime, options: UpdateOp const arch = detectArch(); const releases = await fetchGitHubReleases(repo, 30); - const release = options.targetVersion - ? (releases.find((r) => r.version === options.targetVersion || r.tagName === options.targetVersion) ?? - await fetchGitHubRelease(repo, options.targetVersion)) + const release = targetVersion + ? (releases.find((r) => r.version === targetVersion || r.tagName === targetVersion || r.tagName === `v${targetVersion}`) ?? + await fetchGitHubRelease(repo, targetVersion)) : (releases[0] ?? await fetchGitHubRelease(repo)); if (!release) { @@ -131,7 +159,7 @@ export async function handleUpdateCommand(runtime: CliRuntime, options: UpdateOp const latestVersion = release.version; const currentRelease = releases.find((r) => r.version === VERSION); const isUpgrade = isNewerVersion(latestVersion, VERSION); - const shouldUpdate = options.force || Boolean(options.targetVersion) || isUpgrade; + const shouldUpdate = isUpgrade || Boolean(options.force); const pendingReleases = releases.filter((r) => isNewerVersion(r.version, VERSION)); if (options.checkOnly) { @@ -464,16 +492,20 @@ function extractZip(archivePath: string, targetDir: string, platform: string): s return files[0] || archivePath; } -export function isNewerVersion(latest: string, current: string): boolean { - const lParts = latest.split(".").map(Number); - const cParts = current.split(".").map(Number); - for (let i = 0; i < Math.max(lParts.length, cParts.length); i++) { - const l = lParts[i] ?? 0; - const c = cParts[i] ?? 0; - if (l > c) return true; - if (l < c) return false; +export function compareSemVer(v1: string, v2: string): number { + const p1 = v1.replace(/^v/, "").split(".").map((n) => Number.parseInt(n, 10) || 0); + const p2 = v2.replace(/^v/, "").split(".").map((n) => Number.parseInt(n, 10) || 0); + for (let i = 0; i < Math.max(p1.length, p2.length); i++) { + const a = p1[i] ?? 0; + const b = p2[i] ?? 0; + if (a > b) return 1; + if (a < b) return -1; } - return false; + return 0; +} + +export function isNewerVersion(latest: string, current: string): boolean { + return compareSemVer(latest, current) > 0; } function runCommand(command: string, args: string[], options: { cwd?: string } = {}) {