|
| 1 | +import { |
| 2 | + it, |
| 3 | + describe, |
| 4 | + expect, |
| 5 | + vi, |
| 6 | + beforeEach, |
| 7 | + afterEach, |
| 8 | + type MockInstance, |
| 9 | +} from "vitest"; |
| 10 | +import type { PackageManifest } from "query-registry"; |
| 11 | +import * as utils from "./index.js"; |
| 12 | + |
| 13 | +describe("utils", () => { |
| 14 | + describe("extractOwnerAndRepo", () => { |
| 15 | + it("is null for URLs with trailing characters", () => { |
| 16 | + expect( |
| 17 | + utils.extractOwnerAndRepo("https://github.com/org/repo.gitpewpew"), |
| 18 | + ).toBeNull(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("is null for URLs with leading characters", () => { |
| 22 | + expect( |
| 23 | + utils.extractOwnerAndRepo("pewpewhttps://github.com/org/repo.git"), |
| 24 | + ).toBeNull(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("returns org and repo for valid https URLs", () => { |
| 28 | + expect( |
| 29 | + utils.extractOwnerAndRepo("http://github.com/org/repo.git"), |
| 30 | + ).toEqual(["org", "repo"]); |
| 31 | + }); |
| 32 | + |
| 33 | + it("returns org and repo for valid http URLs", () => { |
| 34 | + expect( |
| 35 | + utils.extractOwnerAndRepo("https://github.com/org/repo.git"), |
| 36 | + ).toEqual(["org", "repo"]); |
| 37 | + }); |
| 38 | + |
| 39 | + it("returns org and repo for valid git+https URLs", () => { |
| 40 | + expect( |
| 41 | + utils.extractOwnerAndRepo("git+https://github.com/org/repo.git"), |
| 42 | + ).toEqual(["org", "repo"]); |
| 43 | + }); |
| 44 | + |
| 45 | + it("returns org and repo for valid git+http URLs", () => { |
| 46 | + expect( |
| 47 | + utils.extractOwnerAndRepo("git+http://github.com/org/repo.git"), |
| 48 | + ).toEqual(["org", "repo"]); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe("extractRepository", () => { |
| 53 | + it("returns undefined if no repository", () => { |
| 54 | + expect(utils.extractRepository({} as PackageManifest)).toBeUndefined(); |
| 55 | + }); |
| 56 | + |
| 57 | + it("returns undefined if repository is object with no URL", () => { |
| 58 | + expect( |
| 59 | + utils.extractRepository({ |
| 60 | + repository: {}, |
| 61 | + } as PackageManifest), |
| 62 | + ).toBeUndefined(); |
| 63 | + }); |
| 64 | + |
| 65 | + it("returns URL if repository is string", () => { |
| 66 | + expect( |
| 67 | + utils.extractRepository({ |
| 68 | + repository: "foo", |
| 69 | + } as PackageManifest), |
| 70 | + ).toBe("foo"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("returns URL if repository is object with URL", () => { |
| 74 | + expect( |
| 75 | + utils.extractRepository({ |
| 76 | + repository: { |
| 77 | + url: "foo", |
| 78 | + }, |
| 79 | + } as PackageManifest), |
| 80 | + ).toBe("foo"); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe("abbreviateCommitHash", () => { |
| 85 | + it("returns the first 7 characters of a hash", () => { |
| 86 | + expect( |
| 87 | + utils.abbreviateCommitHash("09efd0553374ff7d3e62b79378e3184f5eb57571"), |
| 88 | + ).toBe("09efd05"); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe("isPullRequest", () => { |
| 93 | + it("returns true if ref is non-nan number", () => { |
| 94 | + expect(utils.isPullRequest("808")).toBe(true); |
| 95 | + }); |
| 96 | + |
| 97 | + it("returns false if ref is nan number", () => { |
| 98 | + expect(utils.isPullRequest("foo")).toBe(false); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe("isWhitelisted", () => { |
| 103 | + let fetchSpy: MockInstance; |
| 104 | + let whitelist: string; |
| 105 | + |
| 106 | + beforeEach(() => { |
| 107 | + whitelist = ""; |
| 108 | + fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation(() => { |
| 109 | + return Promise.resolve(new Response(whitelist, { status: 200 })); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + afterEach(() => { |
| 114 | + vi.resetAllMocks(); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should return true if repo is in whitelist", async () => { |
| 118 | + whitelist = ` |
| 119 | + foo/bar |
| 120 | + org/repo |
| 121 | + baz/zab |
| 122 | + `; |
| 123 | + const result = await utils.isWhitelisted("org", "repo"); |
| 124 | + expect(result).toBe(true); |
| 125 | + }); |
| 126 | + |
| 127 | + it("should return false if repo is not in whitelist", async () => { |
| 128 | + const result = await utils.isWhitelisted("org", "repo"); |
| 129 | + expect(result).toBe(false); |
| 130 | + }); |
| 131 | + |
| 132 | + it("should return false if fetch fails", async () => { |
| 133 | + fetchSpy.mockRejectedValue(new Error("bleep bloop")); |
| 134 | + const result = await utils.isWhitelisted("org", "repo"); |
| 135 | + expect(result).toBe(false); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments