2323import "../env-loader" ;
2424import { randomUUID } from "node:crypto" ;
2525import * as path from "node:path" ;
26- import { DocumentStatus } from "@generated/client" ;
26+ import {
27+ DocumentStatus ,
28+ FieldType ,
29+ TemplateModelStatus ,
30+ } from "@generated/client" ;
2731import axios from "axios" ;
2832import MockAdapter from "axios-mock-adapter" ;
2933import { getPrismaClient } from "../activities/database-client" ;
@@ -44,6 +48,202 @@ export const TEMPORAL_NAMESPACE =
4448
4549/** Seeded default group every test document is attached to. */
4650export const SEED_GROUP_ID = "seeddefaultgroup" ;
51+ const SEED_ACTOR_ID = "seed-test-actor" ;
52+
53+ /** Tracks whether the seed group has been upserted in this process. */
54+ let seedGroupEnsured = false ;
55+
56+ /** SDPR Monthly Report field definitions — mirrors apps/shared/prisma/seed.ts */
57+ const SDPR_FIELDS : Array < {
58+ fieldKey : string ;
59+ fieldType : FieldType ;
60+ fieldFormat ?: string ;
61+ } > = [
62+ {
63+ fieldKey : "checkbox_need_assistance_yes" ,
64+ fieldType : FieldType . selectionMark ,
65+ } ,
66+ {
67+ fieldKey : "checkbox_need_assistance_no" ,
68+ fieldType : FieldType . selectionMark ,
69+ } ,
70+ {
71+ fieldKey : "checkbox_family_assets_yes" ,
72+ fieldType : FieldType . selectionMark ,
73+ } ,
74+ { fieldKey : "checkbox_family_assets_no" , fieldType : FieldType . selectionMark } ,
75+ { fieldKey : "checkbox_shelter_yes" , fieldType : FieldType . selectionMark } ,
76+ { fieldKey : "checkbox_shelter_no" , fieldType : FieldType . selectionMark } ,
77+ { fieldKey : "checkbox_dependants_yes" , fieldType : FieldType . selectionMark } ,
78+ { fieldKey : "checkbox_dependants_no" , fieldType : FieldType . selectionMark } ,
79+ {
80+ fieldKey : "checkbox_employment_changes_yes" ,
81+ fieldType : FieldType . selectionMark ,
82+ } ,
83+ {
84+ fieldKey : "checkbox_employment_changes_no" ,
85+ fieldType : FieldType . selectionMark ,
86+ } ,
87+ {
88+ fieldKey : "checkbox_employment_changes_spouse_yes" ,
89+ fieldType : FieldType . selectionMark ,
90+ } ,
91+ {
92+ fieldKey : "checkbox_employment_changes_spouse_no" ,
93+ fieldType : FieldType . selectionMark ,
94+ } ,
95+ { fieldKey : "checkbox_school_yes" , fieldType : FieldType . selectionMark } ,
96+ { fieldKey : "checkbox_school_no" , fieldType : FieldType . selectionMark } ,
97+ {
98+ fieldKey : "checkbox_school_spouse_yes" ,
99+ fieldType : FieldType . selectionMark ,
100+ } ,
101+ { fieldKey : "checkbox_school_spouse_no" , fieldType : FieldType . selectionMark } ,
102+ { fieldKey : "checkbox_work_yes" , fieldType : FieldType . selectionMark } ,
103+ { fieldKey : "checkbox_work_no" , fieldType : FieldType . selectionMark } ,
104+ { fieldKey : "checkbox_work_spouse_yes" , fieldType : FieldType . selectionMark } ,
105+ { fieldKey : "checkbox_work_spouse_no" , fieldType : FieldType . selectionMark } ,
106+ { fieldKey : "checkbox_moved_yes" , fieldType : FieldType . selectionMark } ,
107+ { fieldKey : "checkbox_moved_no" , fieldType : FieldType . selectionMark } ,
108+ { fieldKey : "checkbox_moved_spouse_yes" , fieldType : FieldType . selectionMark } ,
109+ { fieldKey : "checkbox_moved_spouse_no" , fieldType : FieldType . selectionMark } ,
110+ { fieldKey : "checkbox_warrant_yes" , fieldType : FieldType . selectionMark } ,
111+ { fieldKey : "checkbox_warrant_no" , fieldType : FieldType . selectionMark } ,
112+ {
113+ fieldKey : "checkbox_warrant_spouse_yes" ,
114+ fieldType : FieldType . selectionMark ,
115+ } ,
116+ {
117+ fieldKey : "checkbox_warrant_spouse_no" ,
118+ fieldType : FieldType . selectionMark ,
119+ } ,
120+ { fieldKey : "explain_changes" , fieldType : FieldType . string } ,
121+ { fieldKey : "signature" , fieldType : FieldType . string } ,
122+ { fieldKey : "spouse_signature" , fieldType : FieldType . string } ,
123+ { fieldKey : "date" , fieldType : FieldType . date , fieldFormat : "ymd" } ,
124+ { fieldKey : "spouse_date" , fieldType : FieldType . date , fieldFormat : "ymd" } ,
125+ { fieldKey : "name" , fieldType : FieldType . string } ,
126+ { fieldKey : "spouse_name" , fieldType : FieldType . string } ,
127+ { fieldKey : "phone" , fieldType : FieldType . string } ,
128+ { fieldKey : "spouse_phone" , fieldType : FieldType . string } ,
129+ { fieldKey : "sin" , fieldType : FieldType . string } ,
130+ { fieldKey : "spouse_sin" , fieldType : FieldType . string } ,
131+ { fieldKey : "applicant_net_employment_income" , fieldType : FieldType . number } ,
132+ { fieldKey : "applicant_employment_insurance" , fieldType : FieldType . number } ,
133+ {
134+ fieldKey : "applicant_spousal_support_alimony" ,
135+ fieldType : FieldType . number ,
136+ } ,
137+ { fieldKey : "applicant_child_support" , fieldType : FieldType . number } ,
138+ {
139+ fieldKey : "applicant_workbc_financial_support" ,
140+ fieldType : FieldType . number ,
141+ } ,
142+ {
143+ fieldKey : "applicant_student_funding_loans_bursaries" ,
144+ fieldType : FieldType . number ,
145+ } ,
146+ { fieldKey : "applicant_rental_income" , fieldType : FieldType . number } ,
147+ { fieldKey : "applicant_room_board_income" , fieldType : FieldType . number } ,
148+ { fieldKey : "applicant_workers_compensation" , fieldType : FieldType . number } ,
149+ {
150+ fieldKey : "applicant_private_pensions_retirement_disability" ,
151+ fieldType : FieldType . number ,
152+ } ,
153+ { fieldKey : "applicant_oas_gis" , fieldType : FieldType . number } ,
154+ { fieldKey : "applicant_trust_income" , fieldType : FieldType . number } ,
155+ {
156+ fieldKey : "applicant_canada_pension_plan_cpp" ,
157+ fieldType : FieldType . number ,
158+ } ,
159+ { fieldKey : "applicant_tax_credits_gst_credit" , fieldType : FieldType . number } ,
160+ { fieldKey : "applicant_child_tax_benefits" , fieldType : FieldType . number } ,
161+ { fieldKey : "applicant_income_tax_refund" , fieldType : FieldType . number } ,
162+ {
163+ fieldKey : "applicant_other_income_money_received" ,
164+ fieldType : FieldType . number ,
165+ } ,
166+ {
167+ fieldKey : "applicant_income_of_dependent_children" ,
168+ fieldType : FieldType . number ,
169+ } ,
170+ { fieldKey : "spouse_net_employment_income" , fieldType : FieldType . number } ,
171+ { fieldKey : "spouse_employment_insurance" , fieldType : FieldType . number } ,
172+ { fieldKey : "spouse_spousal_support_alimony" , fieldType : FieldType . number } ,
173+ { fieldKey : "spouse_child_support" , fieldType : FieldType . number } ,
174+ { fieldKey : "spouse_workbc_financial_support" , fieldType : FieldType . number } ,
175+ {
176+ fieldKey : "spouse_student_funding_loans_bursaries" ,
177+ fieldType : FieldType . number ,
178+ } ,
179+ { fieldKey : "spouse_rental_income" , fieldType : FieldType . number } ,
180+ { fieldKey : "spouse_room_board_income" , fieldType : FieldType . number } ,
181+ { fieldKey : "spouse_workers_compensation" , fieldType : FieldType . number } ,
182+ {
183+ fieldKey : "spouse_private_pensions_retirement_disability" ,
184+ fieldType : FieldType . number ,
185+ } ,
186+ { fieldKey : "spouse_oas_gis" , fieldType : FieldType . number } ,
187+ { fieldKey : "spouse_trust_income" , fieldType : FieldType . number } ,
188+ { fieldKey : "spouse_canada_pension_plan_cpp" , fieldType : FieldType . number } ,
189+ { fieldKey : "spouse_tax_credits_gst_credit" , fieldType : FieldType . number } ,
190+ { fieldKey : "spouse_child_tax_benefits" , fieldType : FieldType . number } ,
191+ { fieldKey : "spouse_income_tax_refund" , fieldType : FieldType . number } ,
192+ {
193+ fieldKey : "spouse_other_income_money_received" ,
194+ fieldType : FieldType . number ,
195+ } ,
196+ ] ;
197+
198+ /**
199+ * Upsert a minimal Actor, the default seed Group, and the SDPR Monthly Report
200+ * TemplateModel so integration tests are self-contained and work even on a
201+ * freshly-migrated (un-seeded) database.
202+ */
203+ async function ensureSeedGroup ( ) : Promise < void > {
204+ if ( seedGroupEnsured ) return ;
205+ const prisma = getPrismaClient ( ) ;
206+ await prisma . actor . upsert ( {
207+ where : { id : SEED_ACTOR_ID } ,
208+ update : { } ,
209+ create : { id : SEED_ACTOR_ID } ,
210+ } ) ;
211+ await prisma . group . upsert ( {
212+ where : { id : SEED_GROUP_ID } ,
213+ update : { } ,
214+ create : {
215+ id : SEED_GROUP_ID ,
216+ name : "Default" ,
217+ created_by : SEED_ACTOR_ID ,
218+ } ,
219+ } ) ;
220+ // Upsert the SDPR template model so vlmDirect.extract and loadSeededFieldDefs
221+ // can resolve field types without requiring the full seed script.
222+ // Use model_id as the upsert key to avoid duplicate-model_id conflicts.
223+ const template = await prisma . templateModel . upsert ( {
224+ where : { model_id : "sdpr-monthly-report" } ,
225+ update : { } ,
226+ create : {
227+ id : "seed-sdpr-monthly-report-template" ,
228+ name : "SDPR Monthly Report" ,
229+ model_id : "sdpr-monthly-report" ,
230+ created_by : SEED_ACTOR_ID ,
231+ group_id : SEED_GROUP_ID ,
232+ status : TemplateModelStatus . trained ,
233+ } ,
234+ } ) ;
235+ await prisma . fieldDefinition . createMany ( {
236+ data : SDPR_FIELDS . map ( ( f , i ) => ( {
237+ template_model_id : template . id ,
238+ field_key : f . fieldKey ,
239+ field_type : f . fieldType ,
240+ field_format : f . fieldFormat ?? null ,
241+ display_order : i ,
242+ } ) ) ,
243+ skipDuplicates : true ,
244+ } ) ;
245+ seedGroupEnsured = true ;
246+ }
47247
48248/**
49249 * Absolute path to a real sample image on disk. `file.prepare`/provider
@@ -97,6 +297,7 @@ export async function seedTestDocument(opts?: {
97297 id ?: string ;
98298 fileName ?: string ;
99299} ) : Promise < { documentId : string ; cleanup : ( ) => Promise < void > } > {
300+ await ensureSeedGroup ( ) ;
100301 const prisma = getPrismaClient ( ) ;
101302 const documentId = opts ?. id ?? `itest-${ randomUUID ( ) } ` ;
102303 const fileName = opts ?. fileName ?? "1 81.jpg" ;
0 commit comments