-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintegration-harness.ts
More file actions
440 lines (424 loc) · 16.6 KB
/
Copy pathintegration-harness.ts
File metadata and controls
440 lines (424 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/**
* Shared "mock only paid services" integration-test harness.
*
* The experiment workflow tests run the REAL Temporal worker + REAL activities
* (file prep, blob reads/writes, the confidence gate, DB upserts) against the
* live local stack (Temporal `localhost:7233`, the app Postgres, MinIO/Azure
* blob storage). The ONLY things stubbed are the paid external APIs:
*
* - **Azure Document Intelligence** (SDK-based, can't be seen by
* axios-mock-adapter) → the activities' built-in `MOCK_AZURE_OCR=true`
* env-seam returns a canned `prebuilt-layout` response (still written to
* real blob storage, still resolved by the real downstream activities).
* - **Azure OpenAI / Mistral / Content Understanding** (raw `axios`) →
* intercepted with `axios-mock-adapter` on the default axios instance, so
* the real provider activity code (HTTP build → parse → canonical mapping)
* runs end-to-end against a recorded payload.
*
* Everything else is the production code path. A real `document` row (in the
* seeded `seeddefaultgroup`) is created per test so the OCR activities can
* resolve its group, write payload blobs, run the gate, and upsert results.
*/
import "../env-loader";
import { randomUUID } from "node:crypto";
import * as path from "node:path";
import {
DocumentStatus,
FieldType,
TemplateModelStatus,
} from "@generated/client";
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import { getPrismaClient } from "../activities/database-client";
import { getActivityRegistry } from "../activity-registry";
import { computeConfigHash } from "../config-hash";
import type {
GraphWorkflowConfig,
GraphWorkflowInput,
} from "../graph-workflow-types";
import type { CuAnalyzeResult } from "../ocr-providers/azure-content-understanding/cu-types";
import type { MistralOcrApiResponse } from "../ocr-providers/mistral/mistral-ocr-types";
import type { OCRResponse } from "../types";
export const TEMPORAL_ADDRESS =
process.env.TEMPORAL_TEST_ADDRESS ?? "localhost:7233";
export const TEMPORAL_NAMESPACE =
process.env.TEMPORAL_TEST_NAMESPACE ?? "default";
/** Seeded default group every test document is attached to. */
export const SEED_GROUP_ID = "seeddefaultgroup";
const SEED_ACTOR_ID = "seed-test-actor";
/** Tracks whether the seed group has been upserted in this process. */
let seedGroupEnsured = false;
/** SDPR Monthly Report field definitions — mirrors apps/shared/prisma/seed.ts */
const SDPR_FIELDS: Array<{
fieldKey: string;
fieldType: FieldType;
fieldFormat?: string;
}> = [
{
fieldKey: "checkbox_need_assistance_yes",
fieldType: FieldType.selectionMark,
},
{
fieldKey: "checkbox_need_assistance_no",
fieldType: FieldType.selectionMark,
},
{
fieldKey: "checkbox_family_assets_yes",
fieldType: FieldType.selectionMark,
},
{ fieldKey: "checkbox_family_assets_no", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_shelter_yes", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_shelter_no", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_dependants_yes", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_dependants_no", fieldType: FieldType.selectionMark },
{
fieldKey: "checkbox_employment_changes_yes",
fieldType: FieldType.selectionMark,
},
{
fieldKey: "checkbox_employment_changes_no",
fieldType: FieldType.selectionMark,
},
{
fieldKey: "checkbox_employment_changes_spouse_yes",
fieldType: FieldType.selectionMark,
},
{
fieldKey: "checkbox_employment_changes_spouse_no",
fieldType: FieldType.selectionMark,
},
{ fieldKey: "checkbox_school_yes", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_school_no", fieldType: FieldType.selectionMark },
{
fieldKey: "checkbox_school_spouse_yes",
fieldType: FieldType.selectionMark,
},
{ fieldKey: "checkbox_school_spouse_no", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_work_yes", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_work_no", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_work_spouse_yes", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_work_spouse_no", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_moved_yes", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_moved_no", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_moved_spouse_yes", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_moved_spouse_no", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_warrant_yes", fieldType: FieldType.selectionMark },
{ fieldKey: "checkbox_warrant_no", fieldType: FieldType.selectionMark },
{
fieldKey: "checkbox_warrant_spouse_yes",
fieldType: FieldType.selectionMark,
},
{
fieldKey: "checkbox_warrant_spouse_no",
fieldType: FieldType.selectionMark,
},
{ fieldKey: "explain_changes", fieldType: FieldType.string },
{ fieldKey: "signature", fieldType: FieldType.string },
{ fieldKey: "spouse_signature", fieldType: FieldType.string },
{ fieldKey: "date", fieldType: FieldType.date, fieldFormat: "ymd" },
{ fieldKey: "spouse_date", fieldType: FieldType.date, fieldFormat: "ymd" },
{ fieldKey: "name", fieldType: FieldType.string },
{ fieldKey: "spouse_name", fieldType: FieldType.string },
{ fieldKey: "phone", fieldType: FieldType.string },
{ fieldKey: "spouse_phone", fieldType: FieldType.string },
{ fieldKey: "sin", fieldType: FieldType.string },
{ fieldKey: "spouse_sin", fieldType: FieldType.string },
{ fieldKey: "applicant_net_employment_income", fieldType: FieldType.number },
{ fieldKey: "applicant_employment_insurance", fieldType: FieldType.number },
{
fieldKey: "applicant_spousal_support_alimony",
fieldType: FieldType.number,
},
{ fieldKey: "applicant_child_support", fieldType: FieldType.number },
{
fieldKey: "applicant_workbc_financial_support",
fieldType: FieldType.number,
},
{
fieldKey: "applicant_student_funding_loans_bursaries",
fieldType: FieldType.number,
},
{ fieldKey: "applicant_rental_income", fieldType: FieldType.number },
{ fieldKey: "applicant_room_board_income", fieldType: FieldType.number },
{ fieldKey: "applicant_workers_compensation", fieldType: FieldType.number },
{
fieldKey: "applicant_private_pensions_retirement_disability",
fieldType: FieldType.number,
},
{ fieldKey: "applicant_oas_gis", fieldType: FieldType.number },
{ fieldKey: "applicant_trust_income", fieldType: FieldType.number },
{
fieldKey: "applicant_canada_pension_plan_cpp",
fieldType: FieldType.number,
},
{ fieldKey: "applicant_tax_credits_gst_credit", fieldType: FieldType.number },
{ fieldKey: "applicant_child_tax_benefits", fieldType: FieldType.number },
{ fieldKey: "applicant_income_tax_refund", fieldType: FieldType.number },
{
fieldKey: "applicant_other_income_money_received",
fieldType: FieldType.number,
},
{
fieldKey: "applicant_income_of_dependent_children",
fieldType: FieldType.number,
},
{ fieldKey: "spouse_net_employment_income", fieldType: FieldType.number },
{ fieldKey: "spouse_employment_insurance", fieldType: FieldType.number },
{ fieldKey: "spouse_spousal_support_alimony", fieldType: FieldType.number },
{ fieldKey: "spouse_child_support", fieldType: FieldType.number },
{ fieldKey: "spouse_workbc_financial_support", fieldType: FieldType.number },
{
fieldKey: "spouse_student_funding_loans_bursaries",
fieldType: FieldType.number,
},
{ fieldKey: "spouse_rental_income", fieldType: FieldType.number },
{ fieldKey: "spouse_room_board_income", fieldType: FieldType.number },
{ fieldKey: "spouse_workers_compensation", fieldType: FieldType.number },
{
fieldKey: "spouse_private_pensions_retirement_disability",
fieldType: FieldType.number,
},
{ fieldKey: "spouse_oas_gis", fieldType: FieldType.number },
{ fieldKey: "spouse_trust_income", fieldType: FieldType.number },
{ fieldKey: "spouse_canada_pension_plan_cpp", fieldType: FieldType.number },
{ fieldKey: "spouse_tax_credits_gst_credit", fieldType: FieldType.number },
{ fieldKey: "spouse_child_tax_benefits", fieldType: FieldType.number },
{ fieldKey: "spouse_income_tax_refund", fieldType: FieldType.number },
{
fieldKey: "spouse_other_income_money_received",
fieldType: FieldType.number,
},
];
/**
* Upsert a minimal Actor, the default seed Group, and the SDPR Monthly Report
* TemplateModel so integration tests are self-contained and work even on a
* freshly-migrated (un-seeded) database.
*/
async function ensureSeedGroup(): Promise<void> {
if (seedGroupEnsured) return;
const prisma = getPrismaClient();
await prisma.actor.upsert({
where: { id: SEED_ACTOR_ID },
update: {},
create: { id: SEED_ACTOR_ID },
});
await prisma.group.upsert({
where: { id: SEED_GROUP_ID },
update: {},
create: {
id: SEED_GROUP_ID,
name: "Default",
created_by: SEED_ACTOR_ID,
},
});
// Upsert the SDPR template model so vlmDirect.extract and loadSeededFieldDefs
// can resolve field types without requiring the full seed script.
// Use model_id as the upsert key to avoid duplicate-model_id conflicts.
const template = await prisma.templateModel.upsert({
where: { model_id: "sdpr-monthly-report" },
update: {},
create: {
id: "seed-sdpr-monthly-report-template",
name: "SDPR Monthly Report",
model_id: "sdpr-monthly-report",
created_by: SEED_ACTOR_ID,
group_id: SEED_GROUP_ID,
status: TemplateModelStatus.trained,
},
});
await prisma.fieldDefinition.createMany({
data: SDPR_FIELDS.map((f, i) => ({
template_model_id: template.id,
field_key: f.fieldKey,
field_type: f.fieldType,
field_format: f.fieldFormat ?? null,
display_order: i,
})),
skipDuplicates: true,
});
seedGroupEnsured = true;
}
/**
* Absolute path to a real sample image on disk. `file.prepare`/provider
* activities read absolute blobKeys straight from the filesystem, so pointing
* the workflow's `blobKey` here avoids having to upload to blob storage.
*/
export const SAMPLE_IMAGE_ABS_PATH = path.resolve(
__dirname,
"..",
"..",
"..",
"..",
"data",
"datasets",
"samples-mix",
"public",
"1 81.jpg",
);
type ActivityFn = (...args: unknown[]) => Promise<unknown>;
/**
* Build the REAL activity map (mirrors `worker.ts`), overriding ONLY the
* graph-config loader so the on-disk template under test is executed. That
* override is test-graph injection (develop loads the graph from the DB by
* `workflowVersionId`), not a paid-service mock — every other activity is the
* production implementation.
*/
export function buildRealActivities(
graph: GraphWorkflowConfig,
workflowVersionId: string,
): Record<string, ActivityFn> {
const activities: Record<string, ActivityFn> = {};
for (const [activityType, entry] of getActivityRegistry()) {
activities[activityType] = entry.activityFn as ActivityFn;
}
activities.getWorkflowGraphConfig = (async () => ({
graph,
workflowVersionId,
configHash: computeConfigHash(graph),
})) as ActivityFn;
return activities;
}
/**
* Create a real `document` row in the seeded default group so the OCR
* activities can resolve its group, write payload blobs, and upsert results.
* The returned `cleanup` deletes it (the `ocr_results` FK cascades).
*/
export async function seedTestDocument(opts?: {
id?: string;
fileName?: string;
}): Promise<{ documentId: string; cleanup: () => Promise<void> }> {
await ensureSeedGroup();
const prisma = getPrismaClient();
const documentId = opts?.id ?? `itest-${randomUUID()}`;
const fileName = opts?.fileName ?? "1 81.jpg";
await prisma.document.create({
data: {
id: documentId,
title: fileName,
original_filename: fileName,
file_path: SAMPLE_IMAGE_ABS_PATH,
file_type: "image",
file_size: 0,
source: "integration-test",
status: DocumentStatus.pre_ocr,
group_id: SEED_GROUP_ID,
},
});
return {
documentId,
cleanup: async () => {
await prisma.document.deleteMany({ where: { id: documentId } });
},
};
}
/** Recorded VLM `{ fields, source_quotes }` payload returned by the stub. */
export interface VlmPayloadStub {
fields: Record<string, unknown>;
source_quotes: Record<string, string>;
}
export interface PaidApiMockConfig {
/**
* Stub the Azure OpenAI chat-completions call (vlmDirect.extract /
* vlmOcrHybrid.extract). The payload is returned as fenced JSON in
* `choices[0].message.content`, exactly as the real model would, so the
* activity's real parse + canonical mapping runs.
*/
vlm?: VlmPayloadStub;
/**
* Stub the Mistral Document AI OCR call (mistralOcr.process, native or azure
* transport). The given raw response is returned verbatim so the activity's
* real canonical mapping + ref persistence runs.
*/
mistral?: MistralOcrApiResponse;
/**
* Stub the Azure Document Intelligence analyze response (SDK-based; consumed
* by the `MOCK_AZURE_OCR` env-seam in poll-ocr-results). The given
* `OCRResponse` is written to blob and resolved by the real `azureOcr.extract`
* + downstream activities, so its confidence drives the real gate.
*/
di?: OCRResponse;
/**
* Stub the Azure Content Understanding analyze result (axios-based; consumed
* by the `MOCK_AZURE_CU` env-seam in azure-cu-analyze + azure-cu-deploy). The
* given `CuAnalyzeResult` is mapped by the real activity so its per-field
* confidence drives the real gate.
*/
cu?: CuAnalyzeResult;
}
/**
* Stub ONLY the paid external APIs and run everything else for real:
* - Azure DI via the `MOCK_AZURE_OCR` env-seam (SDK-based; canned layout).
* - Azure OpenAI VLM + Mistral OCR via axios-mock-adapter (raw axios).
* Forces dummy provider creds so the real activity builds its request URL but
* never reaches the network (axios is intercepted). Returns a `restore` that
* removes the interceptor and reverts the env it changed.
*/
export function installPaidApiMocks(cfg: PaidApiMockConfig): {
restore: () => void;
} {
const prevEnv: Record<string, string | undefined> = {
MOCK_AZURE_OCR: process.env.MOCK_AZURE_OCR,
AZURE_OPENAI_ENDPOINT: process.env.AZURE_OPENAI_ENDPOINT,
AZURE_OPENAI_API_KEY: process.env.AZURE_OPENAI_API_KEY,
MOCK_MISTRAL_OCR: process.env.MOCK_MISTRAL_OCR,
MOCK_MISTRAL_AZURE_OCR: process.env.MOCK_MISTRAL_AZURE_OCR,
MISTRAL_API_KEY: process.env.MISTRAL_API_KEY,
MISTRAL_DOC_AI_AZURE_ENDPOINT: process.env.MISTRAL_DOC_AI_AZURE_ENDPOINT,
MISTRAL_DOC_AI_AZURE_KEY: process.env.MISTRAL_DOC_AI_AZURE_KEY,
MOCK_AZURE_OCR_RESPONSE: process.env.MOCK_AZURE_OCR_RESPONSE,
MOCK_AZURE_CU: process.env.MOCK_AZURE_CU,
MOCK_AZURE_CU_RESULT: process.env.MOCK_AZURE_CU_RESULT,
};
process.env.MOCK_AZURE_OCR = "true";
if (cfg.di) {
process.env.MOCK_AZURE_OCR_RESPONSE = JSON.stringify(cfg.di);
}
if (cfg.cu) {
process.env.MOCK_AZURE_CU = "true";
process.env.MOCK_AZURE_CU_RESULT = JSON.stringify(cfg.cu);
}
// Force mock creds so no real call can leak out even if a URL matcher ever
// misses; axios is intercepted regardless.
process.env.AZURE_OPENAI_ENDPOINT = "https://mock-openai.local";
process.env.AZURE_OPENAI_API_KEY = "mock-key";
const mock = new MockAdapter(axios, { onNoMatch: "passthrough" });
if (cfg.vlm) {
const content = `\`\`\`json\n${JSON.stringify(cfg.vlm)}\n\`\`\``;
mock.onPost(/\/chat\/completions/).reply(200, {
choices: [{ message: { content } }],
usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
});
}
if (cfg.mistral) {
// Go through the real axios path (not the MOCK_MISTRAL_* env seams) so the
// given payload's confidence drives the real gate. Provide dummy creds for
// both transports; the interceptor matches either `/ocr` endpoint.
delete process.env.MOCK_MISTRAL_OCR;
delete process.env.MOCK_MISTRAL_AZURE_OCR;
process.env.MISTRAL_API_KEY = "mock-key";
process.env.MISTRAL_DOC_AI_AZURE_ENDPOINT = "https://mock-foundry.local";
process.env.MISTRAL_DOC_AI_AZURE_KEY = "mock-key";
mock.onPost(/\/ocr(\?|$)/).reply(200, cfg.mistral);
}
return {
restore: () => {
mock.restore();
for (const [k, v] of Object.entries(prevEnv)) {
if (v === undefined) delete process.env[k];
else process.env[k] = v;
}
},
};
}
/** Build a `GraphWorkflowInput` for the test graph + initial ctx. */
export function makeWorkflowInput(
graph: GraphWorkflowConfig,
initialCtx: Record<string, unknown>,
): GraphWorkflowInput {
return {
workflowVersionId: "itest-workflow-version-id",
initialCtx,
configHash: computeConfigHash(graph),
runnerVersion: "1.0.0",
};
}