From eafdef4d5afb34ae2a375da39618a4afe3b478b1 Mon Sep 17 00:00:00 2001 From: Madhura68 Date: Sat, 25 Apr 2026 18:31:35 +0200 Subject: [PATCH] test(products): add unit tests for GET /api/products Co-Authored-By: Claude Sonnet 4.6 --- __tests__/api/products.test.ts | 64 +++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/__tests__/api/products.test.ts b/__tests__/api/products.test.ts index 8e46189..062f8d2 100644 --- a/__tests__/api/products.test.ts +++ b/__tests__/api/products.test.ts @@ -31,17 +31,73 @@ function makeRequest(): Request { describe('GET /api/products', () => { beforeEach(() => { vi.clearAllMocks() + mockAuth.mockResolvedValue({ userId: 'user-1', isDemo: false }) }) // TC-P-04 - it.todo('returns products owned by the authenticated user') + it('returns products owned by the authenticated user', async () => { + mockPrisma.product.findMany.mockResolvedValue([ + { id: 'prod-1', name: 'Alpha', repo_url: null }, + { id: 'prod-2', name: 'Beta', repo_url: 'https://github.com/example/beta' }, + ]) + + const res = await getProducts(makeRequest()) + const data = await res.json() + + expect(res.status).toBe(200) + expect(data).toHaveLength(2) + expect(data[0]).toMatchObject({ id: 'prod-1', name: 'Alpha' }) + expect(data[1]).toMatchObject({ id: 'prod-2', name: 'Beta' }) + }) // TC-P-05 - it.todo('returns products where user is a team member') + it('includes products where the user is a team member via productAccessFilter', async () => { + mockPrisma.product.findMany.mockResolvedValue([ + { id: 'prod-shared', name: 'Shared Product', repo_url: null }, + ]) + + const res = await getProducts(makeRequest()) + const data = await res.json() + + expect(res.status).toBe(200) + expect(data).toHaveLength(1) + expect(mockPrisma.product.findMany).toHaveBeenCalledWith( + expect.objectContaining({ + where: expect.objectContaining({ + archived: false, + OR: expect.arrayContaining([ + { user_id: 'user-1' }, + { members: { some: { user_id: 'user-1' } } }, + ]), + }), + }) + ) + }) // TC-P-06 - it.todo('returns empty array when user has no products') + it('returns empty array when user has no products', async () => { + mockPrisma.product.findMany.mockResolvedValue([]) + + const res = await getProducts(makeRequest()) + const data = await res.json() + + expect(res.status).toBe(200) + expect(data).toEqual([]) + }) // TC-P-07 - it.todo('excludes archived products') + it('excludes archived products by querying with archived: false', async () => { + mockPrisma.product.findMany.mockResolvedValue([ + { id: 'prod-active', name: 'Active', repo_url: null }, + ]) + + const res = await getProducts(makeRequest()) + + expect(res.status).toBe(200) + expect(mockPrisma.product.findMany).toHaveBeenCalledWith( + expect.objectContaining({ + where: expect.objectContaining({ archived: false }), + }) + ) + }) })