|
| 1 | +import type { Model } from '@douglasneuroinformatics/libnest'; |
| 2 | +import { getModelToken } from '@douglasneuroinformatics/libnest'; |
| 3 | +import { MockFactory } from '@douglasneuroinformatics/libnest/testing'; |
| 4 | +import type { MockedInstance } from '@douglasneuroinformatics/libnest/testing'; |
| 5 | +import { NotFoundException } from '@nestjs/common'; |
| 6 | +import { Test } from '@nestjs/testing'; |
| 7 | +import type { InstrumentRecord, Session, Subject, User } from '@prisma/client'; |
| 8 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 9 | + |
| 10 | +import { GroupsService } from '../../groups/groups.service'; |
| 11 | +import { InstrumentsService } from '../../instruments/instruments.service'; |
| 12 | +import { SessionsService } from '../../sessions/sessions.service'; |
| 13 | +import { SubjectsService } from '../../subjects/subjects.service'; |
| 14 | +import { InstrumentMeasuresService } from '../instrument-measures.service'; |
| 15 | +import { InstrumentRecordsService } from '../instrument-records.service'; |
| 16 | + |
| 17 | +describe('InstrumentRecordsService', () => { |
| 18 | + let instrumentRecordsService: InstrumentRecordsService; |
| 19 | + let instrumentRecordModel: MockedInstance<Model<'InstrumentRecord'>>; |
| 20 | + // let _groupsService: MockedInstance<GroupsService>; |
| 21 | + // let _instrumentMeasuresService: MockedInstance<InstrumentMeasuresService>; |
| 22 | + let instrumentsService: MockedInstance<InstrumentsService>; |
| 23 | + // let _sessionsService: MockedInstance<SessionsService>; |
| 24 | + // let _subjectsService: MockedInstance<SubjectsService>; |
| 25 | + |
| 26 | + beforeEach(async () => { |
| 27 | + const moduleRef = await Test.createTestingModule({ |
| 28 | + providers: [ |
| 29 | + InstrumentRecordsService, |
| 30 | + MockFactory.createForModelToken(getModelToken('InstrumentRecord')), |
| 31 | + MockFactory.createForService(GroupsService), |
| 32 | + MockFactory.createForService(InstrumentMeasuresService), |
| 33 | + MockFactory.createForService(InstrumentsService), |
| 34 | + MockFactory.createForService(SessionsService), |
| 35 | + MockFactory.createForService(SubjectsService) |
| 36 | + ] |
| 37 | + }).compile(); |
| 38 | + |
| 39 | + instrumentRecordModel = moduleRef.get(getModelToken('InstrumentRecord')); |
| 40 | + instrumentRecordsService = moduleRef.get(InstrumentRecordsService); |
| 41 | + // _groupsService = moduleRef.get(GroupsService); |
| 42 | + // _instrumentMeasuresService = moduleRef.get(InstrumentMeasuresService); |
| 43 | + instrumentsService = moduleRef.get(InstrumentsService); |
| 44 | + // _sessionsService = moduleRef.get(SessionsService); |
| 45 | + // _subjectsService = moduleRef.get(SubjectsService); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('findById', () => { |
| 49 | + it('should throw a NotFoundException if no record is found', async () => { |
| 50 | + instrumentRecordModel.findFirst.mockResolvedValueOnce(null); |
| 51 | + await expect(instrumentRecordsService.findById('nonexistent-id')).rejects.toBeInstanceOf(NotFoundException); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should return the instrument record with the correct shape', async () => { |
| 55 | + const mockRecord = { |
| 56 | + data: { test: 'data' }, |
| 57 | + date: new Date(), |
| 58 | + id: 'test-record-id', |
| 59 | + instrumentId: 'test-instrument-id', |
| 60 | + sessionId: 'test-session-id', |
| 61 | + subjectId: 'test-subject-id' |
| 62 | + }; |
| 63 | + |
| 64 | + instrumentRecordModel.findFirst.mockResolvedValueOnce(mockRecord); |
| 65 | + |
| 66 | + const result = await instrumentRecordsService.findById('test-record-id'); |
| 67 | + |
| 68 | + expect(result).toMatchObject({ |
| 69 | + data: { test: 'data' }, |
| 70 | + id: 'test-record-id', |
| 71 | + instrumentId: 'test-instrument-id', |
| 72 | + sessionId: 'test-session-id', |
| 73 | + subjectId: 'test-subject-id' |
| 74 | + }); |
| 75 | + expect(result.date).toBeInstanceOf(Date); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('exportRecords', () => { |
| 80 | + it('should return an array of export records with correct shape', async () => { |
| 81 | + const mockRecords = [ |
| 82 | + { |
| 83 | + assignmentId: '123', |
| 84 | + computedMeasures: { score: 85 }, |
| 85 | + createdAt: new Date(), |
| 86 | + data: { |
| 87 | + score: 85 |
| 88 | + }, |
| 89 | + date: new Date('2023-01-01'), |
| 90 | + groupId: '123', |
| 91 | + id: 'record-1', |
| 92 | + instrumentId: 'instrument-1', |
| 93 | + session: { |
| 94 | + date: new Date('2023-01-01'), |
| 95 | + id: 'session-1', |
| 96 | + type: 'IN_PERSON' as const, |
| 97 | + user: { username: 'testuser' } |
| 98 | + }, |
| 99 | + sessionId: '123', |
| 100 | + subject: { |
| 101 | + dateOfBirth: new Date('1990-01-01'), |
| 102 | + groupIds: ['group-1'], |
| 103 | + id: 'subject-1', |
| 104 | + sex: 'MALE' as const |
| 105 | + }, |
| 106 | + subjectId: '123', |
| 107 | + updatedAt: new Date() |
| 108 | + } |
| 109 | + ] satisfies (InstrumentRecord & { |
| 110 | + session: Partial<Session> & { user: Partial<User> }; |
| 111 | + subject: Partial<Subject>; |
| 112 | + })[]; |
| 113 | + |
| 114 | + const mockInstruments = [ |
| 115 | + { |
| 116 | + id: 'instrument-1', |
| 117 | + internal: { edition: 1, name: 'Test Instrument' } |
| 118 | + } |
| 119 | + ]; |
| 120 | + |
| 121 | + instrumentRecordModel.aggregateRaw.mockResolvedValueOnce(mockRecords); |
| 122 | + instrumentsService.findById.mockResolvedValueOnce(mockInstruments[0]); |
| 123 | + |
| 124 | + const ability = { can: () => true } as any; |
| 125 | + |
| 126 | + const result = await instrumentRecordsService.exportRecords({}, { ability }); |
| 127 | + |
| 128 | + expect(Array.isArray(result)).toBe(true); |
| 129 | + expect(result.length).toBeGreaterThan(0); |
| 130 | + expect(result[0]).toMatchObject({ |
| 131 | + groupId: '123', |
| 132 | + instrumentEdition: 1, |
| 133 | + instrumentName: 'Test Instrument', |
| 134 | + measure: 'score', |
| 135 | + sessionId: 'session-1', |
| 136 | + sessionType: 'IN_PERSON', |
| 137 | + subjectId: expect.any(String), |
| 138 | + timestamp: expect.any(String), |
| 139 | + username: 'testuser', |
| 140 | + value: 85 |
| 141 | + }); |
| 142 | + }); |
| 143 | + }); |
| 144 | +}); |
0 commit comments