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