|
| 1 | +import { compareVersions, getLatestCliVersion, upgrade } from "../upgrade.js" |
| 2 | + |
| 3 | +function createFetchResponse(body: unknown, init: { ok?: boolean; status?: number } = {}): Response { |
| 4 | + const { ok = true, status = 200 } = init |
| 5 | + return { |
| 6 | + ok, |
| 7 | + status, |
| 8 | + json: async () => body, |
| 9 | + } as Response |
| 10 | +} |
| 11 | + |
| 12 | +describe("compareVersions", () => { |
| 13 | + it("returns 1 when first version is newer", () => { |
| 14 | + expect(compareVersions("0.2.0", "0.1.9")).toBe(1) |
| 15 | + }) |
| 16 | + |
| 17 | + it("returns -1 when first version is older", () => { |
| 18 | + expect(compareVersions("0.1.4", "0.1.5")).toBe(-1) |
| 19 | + }) |
| 20 | + |
| 21 | + it("returns 0 when versions are equivalent", () => { |
| 22 | + expect(compareVersions("v1.2.0", "1.2")).toBe(0) |
| 23 | + }) |
| 24 | + |
| 25 | + it("supports cli tag prefixes and prerelease metadata", () => { |
| 26 | + expect(compareVersions("cli-v1.2.3", "1.2.2")).toBe(1) |
| 27 | + expect(compareVersions("1.2.3-beta.1", "1.2.3")).toBe(0) |
| 28 | + }) |
| 29 | +}) |
| 30 | + |
| 31 | +describe("getLatestCliVersion", () => { |
| 32 | + it("returns the first cli-v release tag from GitHub releases", async () => { |
| 33 | + const fetchImpl = (async () => |
| 34 | + createFetchResponse([ |
| 35 | + { tag_name: "v9.9.9" }, |
| 36 | + { tag_name: "cli-v0.3.1" }, |
| 37 | + { tag_name: "cli-v0.3.0" }, |
| 38 | + ])) as typeof fetch |
| 39 | + |
| 40 | + await expect(getLatestCliVersion(fetchImpl)).resolves.toBe("0.3.1") |
| 41 | + }) |
| 42 | + |
| 43 | + it("throws when release check fails", async () => { |
| 44 | + const fetchImpl = (async () => createFetchResponse({}, { ok: false, status: 503 })) as typeof fetch |
| 45 | + |
| 46 | + await expect(getLatestCliVersion(fetchImpl)).rejects.toThrow("Failed to check latest version") |
| 47 | + }) |
| 48 | +}) |
| 49 | + |
| 50 | +describe("upgrade", () => { |
| 51 | + let logSpy: ReturnType<typeof vi.spyOn> |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined) |
| 55 | + }) |
| 56 | + |
| 57 | + afterEach(() => { |
| 58 | + logSpy.mockRestore() |
| 59 | + }) |
| 60 | + |
| 61 | + it("does not run installer when already up to date", async () => { |
| 62 | + const runInstaller = vi.fn(async () => undefined) |
| 63 | + const fetchImpl = (async () => createFetchResponse([{ tag_name: "cli-v0.1.4" }])) as typeof fetch |
| 64 | + |
| 65 | + await upgrade({ |
| 66 | + currentVersion: "0.1.4", |
| 67 | + fetchImpl, |
| 68 | + runInstaller, |
| 69 | + }) |
| 70 | + |
| 71 | + expect(runInstaller).not.toHaveBeenCalled() |
| 72 | + expect(logSpy).toHaveBeenCalledWith("Roo CLI is already up to date.") |
| 73 | + }) |
| 74 | + |
| 75 | + it("runs installer when a newer version is available", async () => { |
| 76 | + const runInstaller = vi.fn(async () => undefined) |
| 77 | + const fetchImpl = (async () => createFetchResponse([{ tag_name: "cli-v0.2.0" }])) as typeof fetch |
| 78 | + |
| 79 | + await upgrade({ |
| 80 | + currentVersion: "0.1.4", |
| 81 | + fetchImpl, |
| 82 | + runInstaller, |
| 83 | + }) |
| 84 | + |
| 85 | + expect(runInstaller).toHaveBeenCalledTimes(1) |
| 86 | + expect(logSpy).toHaveBeenCalledWith("✓ Upgrade completed.") |
| 87 | + }) |
| 88 | +}) |
0 commit comments