Skip to content

Commit

Permalink
test: add vitest and initial utils tests (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j authored Jan 20, 2025
1 parent 44d48a3 commit 26e2f11
Show file tree
Hide file tree
Showing 5 changed files with 977 additions and 6 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"lint:format": "prettier --check --cache .",
"lint:types": "pnpm -r --parallel run typecheck",
"release": "tsx script/release.ts",
"test": "tsx e2e/publish.test.mts"
"test": "tsx e2e/publish.test.mts",
"test:unit": "vitest run"
},
"keywords": [],
"author": "",
Expand All @@ -35,6 +36,7 @@
"tsx": "^4.10.5",
"typescript": "^5.4.5",
"uncrypto": "^0.1.3",
"vitest": "^3.0.2",
"wait-port": "^1.1.0"
},
"pnpm": {
Expand Down
138 changes: 138 additions & 0 deletions packages/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import {
it,
describe,
expect,
vi,
beforeEach,
afterEach,
type MockInstance,
} from "vitest";
import type { PackageManifest } from "query-registry";
import * as utils from "./index.js";

describe("utils", () => {
describe("extractOwnerAndRepo", () => {
it("is null for URLs with trailing characters", () => {
expect(
utils.extractOwnerAndRepo("https://github.com/org/repo.gitpewpew"),
).toBeNull();
});

it("is null for URLs with leading characters", () => {
expect(
utils.extractOwnerAndRepo("pewpewhttps://github.com/org/repo.git"),
).toBeNull();
});

it("returns org and repo for valid https URLs", () => {
expect(
utils.extractOwnerAndRepo("http://github.com/org/repo.git"),
).toEqual(["org", "repo"]);
});

it("returns org and repo for valid http URLs", () => {
expect(
utils.extractOwnerAndRepo("https://github.com/org/repo.git"),
).toEqual(["org", "repo"]);
});

it("returns org and repo for valid git+https URLs", () => {
expect(
utils.extractOwnerAndRepo("git+https://github.com/org/repo.git"),
).toEqual(["org", "repo"]);
});

it("returns org and repo for valid git+http URLs", () => {
expect(
utils.extractOwnerAndRepo("git+http://github.com/org/repo.git"),
).toEqual(["org", "repo"]);
});
});

describe("extractRepository", () => {
it("returns undefined if no repository", () => {
expect(utils.extractRepository({} as PackageManifest)).toBeUndefined();
});

it("returns undefined if repository is object with no URL", () => {
expect(
utils.extractRepository({
repository: {},
} as PackageManifest),
).toBeUndefined();
});

it("returns URL if repository is string", () => {
expect(
utils.extractRepository({
repository: "foo",
} as PackageManifest),
).toBe("foo");
});

it("returns URL if repository is object with URL", () => {
expect(
utils.extractRepository({
repository: {
url: "foo",
},
} as PackageManifest),
).toBe("foo");
});
});

describe("abbreviateCommitHash", () => {
it("returns the first 7 characters of a hash", () => {
expect(
utils.abbreviateCommitHash("09efd0553374ff7d3e62b79378e3184f5eb57571"),
).toBe("09efd05");
});
});

describe("isPullRequest", () => {
it("returns true if ref is non-nan number", () => {
expect(utils.isPullRequest("808")).toBe(true);
});

it("returns false if ref is nan number", () => {
expect(utils.isPullRequest("foo")).toBe(false);
});
});

describe("isWhitelisted", () => {
let fetchSpy: MockInstance;
let whitelist: string;

beforeEach(() => {
whitelist = "";
fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation(() => {
return Promise.resolve(new Response(whitelist, { status: 200 }));
});
});

afterEach(() => {
vi.resetAllMocks();
});

it("should return true if repo is in whitelist", async () => {
whitelist = `
foo/bar
org/repo
baz/zab
`;
const result = await utils.isWhitelisted("org", "repo");
expect(result).toBe(true);
});

it("should return false if repo is not in whitelist", async () => {
const result = await utils.isWhitelisted("org", "repo");
expect(result).toBe(false);
});

it("should return false if fetch fails", async () => {
fetchSpy.mockRejectedValue(new Error("bleep bloop"));
const result = await utils.isWhitelisted("org", "repo");
expect(result).toBe(false);
});
});
});
2 changes: 1 addition & 1 deletion packages/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PackageManifest } from "query-registry";

const githubUrlRegex =
/(?:git\+)?https?:\/\/github\.com\/([^\/]+)\/([^\/]+)\.git/; // TODO: Don't trust this, it's chatgbd :)
/^(?:git\+)?https?:\/\/github\.com\/([^\/]+)\/([^\/]+)\.git$/;

export function extractOwnerAndRepo(
repositoryUrl: string,
Expand Down
Loading

0 comments on commit 26e2f11

Please sign in to comment.