Download src/app/api/codes/enter/route.test.ts from HonzaH/Letschat: direct link, hf CLI and curl.
- Browser
- Download file 1.45 kB
-
https://huggingface.co/spaces/HonzaH/Letschat/resolve/main/src/app/api/codes/enter/route.test.ts
- Command line
-
hf download hf://spaces/HonzaH/Letschat/src/app/api/codes/enter/route.test.ts
-
curl -L -o route.test.ts https://huggingface.co/spaces/HonzaH/Letschat/resolve/main/src/app/api/codes/enter/route.test.ts
1.45 kB
| import { describe, it, expect, vi, beforeEach } from 'vitest' | |
| beforeEach(() => { | |
| vi.resetModules() | |
| }) | |
| describe('POST /api/codes/enter', () => { | |
| it('returns room and marks code as used when room exists', async () => { | |
| vi.doMock('lib/logger', () => ({ logEvent: vi.fn() })) | |
| const codeRow = { id: 'code-1', code: 'ABCDE', room_id: 'room-1', linked_to: 'code-2', used: null } | |
| const getCodeByIdMock = vi.fn().mockResolvedValue(codeRow) | |
| const insertCodeMock = vi.fn() | |
| const updateCodeMock = vi.fn() | |
| vi.doMock('lib/persistence', () => ({ | |
| getCodeById: getCodeByIdMock, | |
| insertCode: insertCodeMock, | |
| updateCode: updateCodeMock | |
| })) | |
| vi.doMock('lib/appSession', () => ({ getAppSession: async () => null })) | |
| vi.doMock('lib/supabase/server', () => ({ isSupabaseConfigured: () => true })) | |
| // Mock markCodeAsUsed and createRoom | |
| const markMock = vi.fn().mockResolvedValue(undefined) | |
| vi.doMock('lib/roomService', () => ({ markCodeAsUsed: markMock, createRoom: async () => 'room-1' })) | |
| const route = await import('./route') | |
| const req = { json: async () => ({ codeId: 'code-1', userAgent: 'UA' }) } as any | |
| const res: any = await route.POST(req) | |
| expect(res.status).toBe(200) | |
| expect(markMock).toHaveBeenCalledWith('code-1', undefined, expect.any(String)) | |
| expect(getCodeByIdMock).toHaveBeenCalledWith('code-1') | |
| expect(insertCodeMock).not.toHaveBeenCalled() | |
| }) | |
| }) | |