Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions test/lib/upgrade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
);
});
});
Loading