@@ -4,6 +4,7 @@ import { MockFactory } from '@douglasneuroinformatics/libnest/testing';
44import type { MockedInstance } from '@douglasneuroinformatics/libnest/testing' ;
55import { NotFoundException } from '@nestjs/common' ;
66import { Test } from '@nestjs/testing' ;
7+ import type { InstrumentRecord , Session , Subject , User } from '@prisma/client' ;
78import { beforeEach , describe , expect , it } from 'vitest' ;
89
910import { GroupsService } from '../../groups/groups.service' ;
@@ -16,11 +17,11 @@ import { InstrumentRecordsService } from '../instrument-records.service';
1617describe ( 'InstrumentRecordsService' , ( ) => {
1718 let instrumentRecordsService : InstrumentRecordsService ;
1819 let instrumentRecordModel : MockedInstance < Model < 'InstrumentRecord' > > ;
19- let _groupsService : MockedInstance < GroupsService > ;
20- let _instrumentMeasuresService : MockedInstance < InstrumentMeasuresService > ;
21- let _instrumentsService : MockedInstance < InstrumentsService > ;
22- let _sessionsService : MockedInstance < SessionsService > ;
23- let _subjectsService : MockedInstance < SubjectsService > ;
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>;
2425
2526 beforeEach ( async ( ) => {
2627 const moduleRef = await Test . createTestingModule ( {
@@ -37,11 +38,11 @@ describe('InstrumentRecordsService', () => {
3738
3839 instrumentRecordModel = moduleRef . get ( getModelToken ( 'InstrumentRecord' ) ) ;
3940 instrumentRecordsService = moduleRef . get ( InstrumentRecordsService ) ;
40- _groupsService = moduleRef . get ( GroupsService ) ;
41- _instrumentMeasuresService = moduleRef . get ( InstrumentMeasuresService ) ;
42- _instrumentsService = moduleRef . get ( InstrumentsService ) ;
43- _sessionsService = moduleRef . get ( SessionsService ) ;
44- _subjectsService = moduleRef . get ( SubjectsService ) ;
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);
4546 } ) ;
4647
4748 describe ( 'findById' , ( ) => {
@@ -74,4 +75,68 @@ describe('InstrumentRecordsService', () => {
7475 expect ( result . date ) . toBeInstanceOf ( Date ) ;
7576 } ) ;
7677 } ) ;
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 . findMany . mockResolvedValueOnce ( mockRecords ) ;
122+ instrumentsService . findById . mockResolvedValueOnce ( mockInstruments [ 0 ] ) ;
123+
124+ const result = await instrumentRecordsService . exportRecords ( ) ;
125+
126+ expect ( Array . isArray ( result ) ) . toBe ( true ) ;
127+ expect ( result . length ) . toBeGreaterThan ( 0 ) ;
128+ expect ( result [ 0 ] ) . toMatchObject ( {
129+ groupId : 'group-1' ,
130+ instrumentEdition : 1 ,
131+ instrumentName : 'Test Instrument' ,
132+ measure : 'score' ,
133+ sessionId : 'session-1' ,
134+ sessionType : 'IN_PERSON' ,
135+ subjectId : expect . any ( String ) ,
136+ timestamp : expect . any ( String ) ,
137+ username : 'testuser' ,
138+ value : 85
139+ } ) ;
140+ } ) ;
141+ } ) ;
77142} ) ;
0 commit comments