From 9a7111bc65c514510840a3e9c77f058bb80d367e Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Thu, 5 Feb 2026 11:35:27 +0000 Subject: [PATCH] test: add coverage for fetchLatestVersion and versionExists --- test/lib/upgrade.test.ts | 143 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/test/lib/upgrade.test.ts b/test/lib/upgrade.test.ts index 13bdda4f..e0facde5 100644 --- a/test/lib/upgrade.test.ts +++ b/test/lib/upgrade.test.ts @@ -9,7 +9,9 @@ import { UpgradeError } from "../../src/lib/errors.js"; import { fetchLatestFromGitHub, fetchLatestFromNpm, + fetchLatestVersion, parseInstallationMethod, + versionExists, } from "../../src/lib/upgrade.js"; // Store original fetch for restoration @@ -219,3 +221,144 @@ describe("UpgradeError", () => { expect(error.message).toBe("Custom error message"); }); }); + +describe("fetchLatestVersion", () => { + test("uses GitHub for curl method", async () => { + globalThis.fetch = async () => + new Response(JSON.stringify({ tag_name: "v2.0.0" }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + + const version = await fetchLatestVersion("curl"); + expect(version).toBe("2.0.0"); + }); + + test("uses npm for npm method", async () => { + globalThis.fetch = async () => + new Response(JSON.stringify({ version: "2.0.0" }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + + const version = await fetchLatestVersion("npm"); + expect(version).toBe("2.0.0"); + }); + + test("uses npm for pnpm method", async () => { + globalThis.fetch = async () => + new Response(JSON.stringify({ version: "2.0.0" }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + + const version = await fetchLatestVersion("pnpm"); + expect(version).toBe("2.0.0"); + }); + + test("uses npm for bun method", async () => { + globalThis.fetch = async () => + new Response(JSON.stringify({ version: "2.0.0" }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + + const version = await fetchLatestVersion("bun"); + expect(version).toBe("2.0.0"); + }); + + test("uses npm for yarn method", async () => { + globalThis.fetch = async () => + new Response(JSON.stringify({ version: "2.0.0" }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + + const version = await fetchLatestVersion("yarn"); + expect(version).toBe("2.0.0"); + }); + + test("uses npm for unknown method", async () => { + globalThis.fetch = async () => + new Response(JSON.stringify({ version: "2.0.0" }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + + const version = await fetchLatestVersion("unknown"); + expect(version).toBe("2.0.0"); + }); +}); + +describe("versionExists", () => { + test("checks GitHub for curl method - version exists", async () => { + globalThis.fetch = async () => new Response(null, { status: 200 }); + + const exists = await versionExists("curl", "1.0.0"); + expect(exists).toBe(true); + }); + + test("checks GitHub for curl method - version does not exist", async () => { + globalThis.fetch = async () => new Response(null, { status: 404 }); + + const exists = await versionExists("curl", "99.99.99"); + expect(exists).toBe(false); + }); + + test("checks npm for npm method - version exists", async () => { + globalThis.fetch = async () => new Response(null, { status: 200 }); + + const exists = await versionExists("npm", "1.0.0"); + expect(exists).toBe(true); + }); + + test("checks npm for npm method - version does not exist", async () => { + globalThis.fetch = async () => new Response(null, { status: 404 }); + + const exists = await versionExists("npm", "99.99.99"); + expect(exists).toBe(false); + }); + + test("checks npm for pnpm method", async () => { + globalThis.fetch = async () => new Response(null, { status: 200 }); + + const exists = await versionExists("pnpm", "1.0.0"); + expect(exists).toBe(true); + }); + + test("checks npm for bun method", async () => { + globalThis.fetch = async () => new Response(null, { status: 200 }); + + const exists = await versionExists("bun", "1.0.0"); + expect(exists).toBe(true); + }); + + test("checks npm for yarn method", async () => { + globalThis.fetch = async () => new Response(null, { status: 200 }); + + const exists = await versionExists("yarn", "1.0.0"); + expect(exists).toBe(true); + }); + + test("throws on network failure", async () => { + globalThis.fetch = async () => { + throw new TypeError("fetch failed"); + }; + + await expect(versionExists("curl", "1.0.0")).rejects.toThrow(UpgradeError); + await expect(versionExists("curl", "1.0.0")).rejects.toThrow( + "Failed to connect to GitHub" + ); + }); + + test("throws on network failure for npm", async () => { + globalThis.fetch = async () => { + throw new TypeError("fetch failed"); + }; + + await expect(versionExists("npm", "1.0.0")).rejects.toThrow(UpgradeError); + await expect(versionExists("npm", "1.0.0")).rejects.toThrow( + "Failed to connect to npm registry" + ); + }); +});