|
| 1 | +// @vitest-environment node |
| 2 | +import { TRPCError } from '@trpc/server'; |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { UserModel, UserNotFoundError } from '@/database/server/models/user'; |
| 6 | + |
| 7 | +describe('UserNotFoundError', () => { |
| 8 | + it('should extend TRPCError with correct code and message', () => { |
| 9 | + const error = new UserNotFoundError(); |
| 10 | + |
| 11 | + expect(error).toBeInstanceOf(TRPCError); |
| 12 | + expect(error.code).toBe('UNAUTHORIZED'); |
| 13 | + expect(error.message).toBe('user not found'); |
| 14 | + }); |
| 15 | +}); |
| 16 | + |
| 17 | +describe('UserModel', () => { |
| 18 | + const mockDb = { |
| 19 | + query: { |
| 20 | + users: { |
| 21 | + findFirst: vi.fn(), |
| 22 | + }, |
| 23 | + }, |
| 24 | + }; |
| 25 | + |
| 26 | + const mockUserId = 'test-user-id'; |
| 27 | + const userModel = new UserModel(mockDb as any, mockUserId); |
| 28 | + |
| 29 | + describe('getUserRegistrationDuration', () => { |
| 30 | + it('should return default values when user not found', async () => { |
| 31 | + mockDb.query.users.findFirst.mockResolvedValue(null); |
| 32 | + |
| 33 | + const result = await userModel.getUserRegistrationDuration(); |
| 34 | + |
| 35 | + expect(result).toEqual({ |
| 36 | + createdAt: expect.any(String), |
| 37 | + duration: 1, |
| 38 | + updatedAt: expect.any(String), |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should calculate duration correctly for existing user', async () => { |
| 43 | + const createdAt = new Date('2024-01-01'); |
| 44 | + mockDb.query.users.findFirst.mockResolvedValue({ |
| 45 | + createdAt, |
| 46 | + }); |
| 47 | + |
| 48 | + const result = await userModel.getUserRegistrationDuration(); |
| 49 | + |
| 50 | + expect(result).toEqual({ |
| 51 | + createdAt: '2024-01-01', |
| 52 | + duration: expect.any(Number), |
| 53 | + updatedAt: expect.any(String), |
| 54 | + }); |
| 55 | + expect(result.duration).toBeGreaterThan(0); |
| 56 | + }); |
| 57 | + }); |
| 58 | +}); |
0 commit comments