Files
hose-use/src/cli/commands/update.test.ts
T

83 lines
3.4 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import {
compareSemVer,
isNewerVersion,
matchAssetForPlatform,
normalizeRepo,
isValidRepo,
type GitHubAsset,
} from "./update.js";
describe("update version comparator", () => {
test("correctly identifies newer semantic versions", () => {
expect(isNewerVersion("0.2.1", "0.2.0")).toBe(true);
expect(isNewerVersion("1.0.0", "0.2.0")).toBe(true);
expect(isNewerVersion("0.3.0", "0.2.1")).toBe(true);
expect(isNewerVersion("0.2.0", "0.2.0")).toBe(false);
expect(isNewerVersion("0.1.9", "0.2.0")).toBe(false);
expect(isNewerVersion("0.2.0", "0.2.1")).toBe(false);
});
test("compareSemVer handles tags, major, minor, and patch", () => {
expect(compareSemVer("v0.2.1", "0.2.0")).toBe(1);
expect(compareSemVer("0.2.0", "v0.2.1")).toBe(-1);
expect(compareSemVer("v0.2.1", "v0.2.1")).toBe(0);
expect(compareSemVer("1.0.0", "0.9.9")).toBe(1);
expect(compareSemVer("0.1.0", "0.2.1")).toBe(-1);
});
});
describe("normalizeRepo and isValidRepo", () => {
test("normalizeRepo trims and cleans various GitHub URL formats", () => {
expect(normalizeRepo("yunyingk/hose-use")).toBe("yunyingk/hose-use");
expect(normalizeRepo("https://github.com/yunyingk/hose-use")).toBe("yunyingk/hose-use");
expect(normalizeRepo("https://github.com/yunyingk/hose-use.git")).toBe("yunyingk/hose-use");
expect(normalizeRepo("http://www.github.com/my-org/my-fork/")).toBe("my-org/my-fork");
expect(normalizeRepo(" custom/project.git ")).toBe("custom/project");
expect(normalizeRepo("github.com/test-org/repo")).toBe("test-org/repo");
});
test("isValidRepo validates owner/repo format and rejects illegal inputs", () => {
expect(isValidRepo("yunyingk/hose-use")).toBe(true);
expect(isValidRepo("my-org/hose-use-fork")).toBe(true);
expect(isValidRepo("user.name/repo_name.v2")).toBe(true);
expect(isValidRepo("")).toBe(false);
expect(isValidRepo("single-word")).toBe(false);
expect(isValidRepo("too/many/parts/here")).toBe(false);
expect(isValidRepo("../path/traversal")).toBe(false);
expect(isValidRepo("invalid character/repo")).toBe(false);
});
});
describe("matchAssetForPlatform", () => {
const assets: GitHubAsset[] = [
{ name: "hose-use-darwin-arm64", downloadUrl: "https://example.com/darwin-arm64", size: 100 },
{ name: "hose-use-darwin-x64", downloadUrl: "https://example.com/darwin-x64", size: 100 },
{ name: "hose-use-linux-x64.tar.gz", downloadUrl: "https://example.com/linux-x64.tar.gz", size: 100 },
{ name: "hose-use-linux-arm64", downloadUrl: "https://example.com/linux-arm64", size: 100 },
{ name: "hose-use-windows-x64.zip", downloadUrl: "https://example.com/win-x64.zip", size: 100 },
];
test("matches darwin arm64", () => {
const matched = matchAssetForPlatform(assets, "darwin", "arm64");
expect(matched?.name).toBe("hose-use-darwin-arm64");
});
test("matches darwin x64", () => {
const matched = matchAssetForPlatform(assets, "darwin", "x64");
expect(matched?.name).toBe("hose-use-darwin-x64");
});
test("matches linux x64 archive", () => {
const matched = matchAssetForPlatform(assets, "linux", "x64");
expect(matched?.name).toBe("hose-use-linux-x64.tar.gz");
});
test("matches windows x64 archive", () => {
const matched = matchAssetForPlatform(assets, "windows", "x64");
expect(matched?.name).toBe("hose-use-windows-x64.zip");
});
});