@@ -70,6 +70,139 @@ describe('[CFSP] cmcFormSpec helpers', function () {
7070 assert . equal ( cmcFormSpec . isChatOnlyFormSpec ( undefined ) , false ) ;
7171 } ) ;
7272 } ) ;
73+
74+ describe ( '[CFSR] eventToFormSpecRecord (pure helper)' , function ( ) {
75+ function eventOn ( streamId , content = basicSpec ( ) ) {
76+ return { id : 'evt-' + streamId , streamIds : [ streamId ] , content } ;
77+ }
78+
79+ it ( '[CFS30] extracts collectorId from a hds-collector sub-scope' , ( ) => {
80+ const rec = cmcFormSpec . eventToFormSpecRecord ( eventOn ( ':_cmc:apps:hds-collector:abc123' ) ) ;
81+ assert . equal ( rec . collectorId , 'abc123' ) ;
82+ assert . equal ( rec . formSpec . title . en , 'Hello World' ) ;
83+ assert . equal ( rec . event . id , 'evt-:_cmc:apps:hds-collector:abc123' ) ;
84+ } ) ;
85+
86+ it ( '[CFS31] honors a custom appCode parameter' , ( ) => {
87+ const rec = cmcFormSpec . eventToFormSpecRecord (
88+ eventOn ( ':_cmc:apps:hds-patient:xyz' ) ,
89+ 'hds-patient'
90+ ) ;
91+ assert . equal ( rec . collectorId , 'xyz' ) ;
92+ } ) ;
93+
94+ it ( '[CFS32] picks the matching stream when an event has multiple streamIds' , ( ) => {
95+ const ev = { id : 'evt' , streamIds : [ 'other-stream' , ':_cmc:apps:hds-collector:formA' ] , content : basicSpec ( ) } ;
96+ const rec = cmcFormSpec . eventToFormSpecRecord ( ev ) ;
97+ assert . equal ( rec . collectorId , 'formA' ) ;
98+ } ) ;
99+
100+ it ( '[CFS33] returns the raw streamId when no matching scope marker is found (fallback)' , ( ) => {
101+ const ev = { id : 'evt' , streamIds : [ 'orphan' ] , content : basicSpec ( ) } ;
102+ const rec = cmcFormSpec . eventToFormSpecRecord ( ev ) ;
103+ // extractAppSubScopeSuffix returns the input unchanged when the prefix is missing.
104+ assert . equal ( rec . collectorId , 'orphan' ) ;
105+ } ) ;
106+
107+ it ( '[CFS34] tolerates a missing streamIds array' , ( ) => {
108+ const ev = { id : 'evt' , content : basicSpec ( ) } ;
109+ const rec = cmcFormSpec . eventToFormSpecRecord ( ev ) ;
110+ assert . equal ( rec . collectorId , '' ) ;
111+ } ) ;
112+ } ) ;
113+
114+ describe ( '[CFSL] listFormSpecs' , function ( ) {
115+ function fakeConnection ( events ) {
116+ const calls = [ ] ;
117+ const conn = {
118+ apiOne ( method , params , resultKey ) {
119+ calls . push ( { method, params, resultKey } ) ;
120+ if ( method === 'events.get' ) return events ;
121+ throw new Error ( 'unexpected apiOne method ' + method ) ;
122+ }
123+ } ;
124+ return { conn, calls } ;
125+ }
126+
127+ it ( '[CFS40] queries the :_cmc:apps:hds-collector parent stream filtered to hds:form-spec-v1' , async ( ) => {
128+ const { conn, calls } = fakeConnection ( [ ] ) ;
129+ await cmcFormSpec . listFormSpecs ( conn ) ;
130+ assert . equal ( calls . length , 1 ) ;
131+ assert . equal ( calls [ 0 ] . method , 'events.get' ) ;
132+ assert . deepEqual ( calls [ 0 ] . params . streams , [ ':_cmc:apps:hds-collector' ] ) ;
133+ assert . deepEqual ( calls [ 0 ] . params . types , [ 'hds:form-spec-v1' ] ) ;
134+ assert . equal ( calls [ 0 ] . params . limit , 1000 ) ;
135+ assert . equal ( calls [ 0 ] . resultKey , 'events' ) ;
136+ } ) ;
137+
138+ it ( '[CFS41] maps every returned event to a FormSpecRecord with collectorId set' , async ( ) => {
139+ const events = [
140+ { id : 'e1' , streamIds : [ ':_cmc:apps:hds-collector:form-a' ] , content : basicSpec ( { title : { en : 'Form A' } } ) } ,
141+ { id : 'e2' , streamIds : [ ':_cmc:apps:hds-collector:form-b' ] , content : basicSpec ( { title : { en : 'Form B' } } ) }
142+ ] ;
143+ const { conn } = fakeConnection ( events ) ;
144+ const records = await cmcFormSpec . listFormSpecs ( conn ) ;
145+ assert . equal ( records . length , 2 ) ;
146+ assert . deepEqual ( records . map ( r => r . collectorId ) , [ 'form-a' , 'form-b' ] ) ;
147+ assert . equal ( records [ 0 ] . formSpec . title . en , 'Form A' ) ;
148+ assert . equal ( records [ 1 ] . event . id , 'e2' ) ;
149+ } ) ;
150+
151+ it ( '[CFS42] returns [] when the api returns null/undefined' , async ( ) => {
152+ const { conn } = fakeConnection ( null ) ;
153+ const records = await cmcFormSpec . listFormSpecs ( conn ) ;
154+ assert . deepEqual ( records , [ ] ) ;
155+ } ) ;
156+
157+ it ( '[CFS43] honors a custom appCode opt' , async ( ) => {
158+ const { conn, calls } = fakeConnection ( [ ] ) ;
159+ await cmcFormSpec . listFormSpecs ( conn , { appCode : 'hds-patient' } ) ;
160+ assert . deepEqual ( calls [ 0 ] . params . streams , [ ':_cmc:apps:hds-patient' ] ) ;
161+ } ) ;
162+
163+ it ( '[CFS44] honors a custom limit opt' , async ( ) => {
164+ const { conn, calls } = fakeConnection ( [ ] ) ;
165+ await cmcFormSpec . listFormSpecs ( conn , { limit : 50 } ) ;
166+ assert . equal ( calls [ 0 ] . params . limit , 50 ) ;
167+ } ) ;
168+ } ) ;
169+
170+ describe ( '[CFSG] getFormSpecById' , function ( ) {
171+ it ( '[CFS50] returns null when loadFormSpec finds nothing' , async ( ) => {
172+ const conn = { apiOne : async ( ) => [ ] } ;
173+ const rec = await cmcFormSpec . getFormSpecById ( conn , 'missing' ) ;
174+ assert . equal ( rec , null ) ;
175+ } ) ;
176+
177+ it ( '[CFS51] returns a FormSpecRecord with the requested collectorId when found' , async ( ) => {
178+ const event = { id : 'evt-xyz' , streamIds : [ ':_cmc:apps:hds-collector:xyz' ] , content : basicSpec ( { title : { en : 'XYZ' } } ) } ;
179+ const calls = [ ] ;
180+ const conn = {
181+ apiOne ( method , params , resultKey ) {
182+ calls . push ( { method, params, resultKey } ) ;
183+ return [ event ] ;
184+ }
185+ } ;
186+ const rec = await cmcFormSpec . getFormSpecById ( conn , 'xyz' ) ;
187+ assert . ok ( rec ) ;
188+ assert . equal ( rec . collectorId , 'xyz' ) ;
189+ assert . equal ( rec . formSpec . title . en , 'XYZ' ) ;
190+ // Verify the underlying loadFormSpec was scoped to the exact sub-scope stream
191+ assert . deepEqual ( calls [ 0 ] . params . streams , [ ':_cmc:apps:hds-collector:xyz' ] ) ;
192+ } ) ;
193+
194+ it ( '[CFS52] honors a custom appCode opt' , async ( ) => {
195+ const calls = [ ] ;
196+ const conn = {
197+ apiOne ( method , params ) {
198+ calls . push ( params ) ;
199+ return [ ] ;
200+ }
201+ } ;
202+ await cmcFormSpec . getFormSpecById ( conn , 'foo' , { appCode : 'hds-patient' } ) ;
203+ assert . deepEqual ( calls [ 0 ] . streams , [ ':_cmc:apps:hds-patient:foo' ] ) ;
204+ } ) ;
205+ } ) ;
73206} ) ;
74207
75208describe ( '[CTFS] Contact.aggregateCmc hdsFormSpec pass-through' , function ( ) {
0 commit comments