|
| 1 | +jest.mock('@/utils/db', () => ({ |
| 2 | + db: jest.fn(), |
| 3 | +})); |
| 4 | + |
| 5 | +import { db } from '@/utils/db'; |
| 6 | +import * as githubUtils from '../utils'; |
| 7 | +import { DEFAULT_GH_URL } from '@/constants/urls'; |
| 8 | + |
| 9 | +describe('GitHub URL utilities', () => { |
| 10 | + afterEach(() => { |
| 11 | + jest.resetAllMocks(); |
| 12 | + }); |
| 13 | + |
| 14 | + describe('getGitHubCustomDomain', () => { |
| 15 | + it('returns custom_domain when present', async () => { |
| 16 | + const mockMeta = [{ custom_domain: 'custom.sujai.com' }]; |
| 17 | + (db as jest.Mock).mockReturnValue({ |
| 18 | + where: jest.fn().mockReturnThis(), |
| 19 | + then: jest.fn().mockResolvedValue(mockMeta) |
| 20 | + }); |
| 21 | + |
| 22 | + const domain = await githubUtils.getGitHubCustomDomain(); |
| 23 | + expect(domain).toBe('custom.sujai.com'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns null when no provider_meta found', async () => { |
| 27 | + (db as jest.Mock).mockReturnValue({ |
| 28 | + where: jest.fn().mockReturnThis(), |
| 29 | + then: jest.fn().mockResolvedValue([]) |
| 30 | + }); |
| 31 | + |
| 32 | + const domain = await githubUtils.getGitHubCustomDomain(); |
| 33 | + expect(domain).toBeNull(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('returns null on db error and logs error', async () => { |
| 37 | + const consoleSpy = jest.spyOn(console, 'error').mockImplementation(); |
| 38 | + (db as jest.Mock).mockImplementation(() => { |
| 39 | + throw new Error('DB failure'); |
| 40 | + }); |
| 41 | + |
| 42 | + const domain = await githubUtils.getGitHubCustomDomain(); |
| 43 | + expect(domain).toBeNull(); |
| 44 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 45 | + 'Error occured while getting custom domain from database:', |
| 46 | + expect.any(Error) |
| 47 | + ); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('getGitHubRestApiUrl', () => { |
| 52 | + it('uses default URL when no custom domain', async () => { |
| 53 | + jest.spyOn(githubUtils, 'getGitHubCustomDomain').mockResolvedValue(null); |
| 54 | + const url = await githubUtils.getGitHubRestApiUrl('path/to/repo'); |
| 55 | + expect(url).toBe(`${DEFAULT_GH_URL}/path/to/repo`); |
| 56 | + }); |
| 57 | + |
| 58 | + it('uses custom domain when provided', async () => { |
| 59 | + jest.spyOn(githubUtils, 'getGitHubCustomDomain').mockResolvedValue('git.sujai.com'); |
| 60 | + const url = await githubUtils.getGitHubRestApiUrl('repos/owner/repo'); |
| 61 | + expect(url).toBe('https://git.sujai.com/api/v3/repos/owner/repo'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('normalizes multiple slashes in URL', async () => { |
| 65 | + jest.spyOn(githubUtils, 'getGitHubCustomDomain').mockResolvedValue('git.sujai.com/'); |
| 66 | + const url = await githubUtils.getGitHubRestApiUrl('/repos//owner//repo'); |
| 67 | + expect(url).toBe('https://git.sujai.com/api/v3/repos/owner/repo'); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('getGitHubGraphQLUrl', () => { |
| 72 | + it('uses default GraphQL endpoint when no custom domain', async () => { |
| 73 | + jest.spyOn(githubUtils, 'getGitHubCustomDomain').mockResolvedValue(null); |
| 74 | + const url = await githubUtils.getGitHubGraphQLUrl(); |
| 75 | + expect(url).toBe(`${DEFAULT_GH_URL}/graphql`); |
| 76 | + }); |
| 77 | + |
| 78 | + it('uses custom domain for GraphQL endpoint', async () => { |
| 79 | + jest.spyOn(githubUtils, 'getGitHubCustomDomain').mockResolvedValue('api.github.local'); |
| 80 | + const url = await githubUtils.getGitHubGraphQLUrl(); |
| 81 | + expect(url).toBe('https://api.github.local/api/graphql'); |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
0 commit comments