|
| 1 | +import { beforeEach, describe, expect, test, vi } from 'vitest'; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(() => ({ |
| 4 | + authContext: undefined as unknown, |
| 5 | +})); |
| 6 | + |
| 7 | +vi.mock('@/middleware/sew', () => ({ |
| 8 | + sew: (callback: () => unknown) => callback(), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock('@/middleware/withAuth', () => ({ |
| 12 | + withOptionalAuth: vi.fn((callback: (context: unknown) => unknown) => callback(mocks.authContext)), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock('@/ee/features/audit/audit', () => ({ |
| 16 | + createAudit: vi.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock('@sourcebot/shared', () => ({ |
| 20 | + env: { AUTH_URL: 'https://sourcebot.example.com' }, |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock('next/headers', () => ({ |
| 24 | + headers: vi.fn(async () => new Headers()), |
| 25 | +})); |
| 26 | + |
| 27 | +const { listRepos } = await import('./listReposApi'); |
| 28 | +const { listReposQueryParamsSchema } = await import('@/lib/schemas'); |
| 29 | + |
| 30 | +function createPrismaMock() { |
| 31 | + return { |
| 32 | + repo: { |
| 33 | + findMany: vi.fn().mockResolvedValue([]), |
| 34 | + count: vi.fn().mockResolvedValue(0), |
| 35 | + }, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +beforeEach(() => { |
| 40 | + vi.clearAllMocks(); |
| 41 | +}); |
| 42 | + |
| 43 | +describe('listRepos connection filtering', () => { |
| 44 | + test('filters both repositories and the total count by connection', async () => { |
| 45 | + const prisma = createPrismaMock(); |
| 46 | + mocks.authContext = { |
| 47 | + org: { id: 7 }, |
| 48 | + user: undefined, |
| 49 | + prisma, |
| 50 | + }; |
| 51 | + |
| 52 | + await listRepos({ |
| 53 | + page: 2, |
| 54 | + perPage: 20, |
| 55 | + sort: 'name', |
| 56 | + direction: 'asc', |
| 57 | + query: 'sourcebot', |
| 58 | + connectionId: 42, |
| 59 | + }); |
| 60 | + |
| 61 | + const where = { |
| 62 | + orgId: 7, |
| 63 | + name: { contains: 'sourcebot', mode: 'insensitive' }, |
| 64 | + connections: { |
| 65 | + some: { connectionId: 42 }, |
| 66 | + }, |
| 67 | + }; |
| 68 | + expect(prisma.repo.findMany).toHaveBeenCalledWith({ |
| 69 | + where, |
| 70 | + skip: 20, |
| 71 | + take: 20, |
| 72 | + orderBy: { name: 'asc' }, |
| 73 | + }); |
| 74 | + expect(prisma.repo.count).toHaveBeenCalledWith({ where }); |
| 75 | + }); |
| 76 | + |
| 77 | + test('does not add a connection relation filter when none is requested', async () => { |
| 78 | + const prisma = createPrismaMock(); |
| 79 | + mocks.authContext = { |
| 80 | + org: { id: 7 }, |
| 81 | + user: undefined, |
| 82 | + prisma, |
| 83 | + }; |
| 84 | + |
| 85 | + await listRepos({ |
| 86 | + page: 1, |
| 87 | + perPage: 30, |
| 88 | + sort: 'name', |
| 89 | + direction: 'asc', |
| 90 | + }); |
| 91 | + |
| 92 | + expect(prisma.repo.findMany).toHaveBeenCalledWith(expect.objectContaining({ |
| 93 | + where: { orgId: 7 }, |
| 94 | + })); |
| 95 | + expect(prisma.repo.count).toHaveBeenCalledWith({ |
| 96 | + where: { orgId: 7 }, |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + test('accepts a positive integer connectionId query parameter', () => { |
| 101 | + expect(listReposQueryParamsSchema.parse({ connectionId: '42' }).connectionId).toBe(42); |
| 102 | + expect(listReposQueryParamsSchema.safeParse({ connectionId: '0' }).success).toBe(false); |
| 103 | + expect(listReposQueryParamsSchema.safeParse({ connectionId: '1.5' }).success).toBe(false); |
| 104 | + }); |
| 105 | +}); |
0 commit comments