Download src/app/api/codes/setup/route.test.ts from HonzaH/Letschat: direct link, hf CLI and curl.
- Browser
- Download file 1.54 kB
-
https://huggingface.co/spaces/HonzaH/Letschat/resolve/main/src/app/api/codes/setup/route.test.ts
- Command line
-
hf download hf://spaces/HonzaH/Letschat/src/app/api/codes/setup/route.test.ts
-
curl -L -o route.test.ts https://huggingface.co/spaces/HonzaH/Letschat/resolve/main/src/app/api/codes/setup/route.test.ts
1.54 kB
| import { describe, it, expect, vi, beforeEach } from 'vitest' | |
| beforeEach(() => { | |
| vi.resetModules() | |
| }) | |
| describe('POST /api/codes/setup', () => { | |
| it('creates room and marks code used for existing code without PIN', async () => { | |
| vi.doMock('lib/logger', () => ({ logEvent: vi.fn() })) | |
| const updateCodeMock = vi.fn().mockResolvedValue({ id: 'code-1' }) | |
| const getCodeByCodeMock = vi.fn().mockResolvedValue({ id: 'code-1', code: 'EXIST', linked_to: 'code-2', used: null }) | |
| const getCodeByIdMock = vi.fn().mockResolvedValue({ id: 'code-2', code: 'LINK', linked_to: 'code-1', used: null }) | |
| const insertCodeMock = vi.fn() | |
| vi.doMock('lib/persistence', () => ({ | |
| updateCode: updateCodeMock, | |
| getCodeByCode: getCodeByCodeMock, | |
| getCodeById: getCodeByIdMock, | |
| insertCode: insertCodeMock, | |
| updateRoom: vi.fn() | |
| })) | |
| const createRoomMock = vi.fn().mockResolvedValue('room-123') | |
| vi.doMock('lib/roomService', () => ({ createRoom: createRoomMock })) | |
| const setAnonMock = vi.fn().mockResolvedValue(null) | |
| vi.doMock('lib/session', () => ({ setAnonymousSession: setAnonMock })) | |
| const route = await import('./route') | |
| const req = { json: async () => ({ code: 'EXIST', userAgent: 'UA' }) } as any | |
| const res = await route.POST(req) | |
| expect(res.status).toBe(200) | |
| expect(createRoomMock).toHaveBeenCalledWith('code-1', 'code-2') | |
| expect(updateCodeMock).toHaveBeenCalled() | |
| expect(insertCodeMock).not.toHaveBeenCalled() | |
| expect(setAnonMock).toHaveBeenCalled() | |
| }) | |
| }) | |