From 669c0d26560c40374eb590b2f8fad45e42d79da6 Mon Sep 17 00:00:00 2001 From: DH V2 Date: Tue, 15 Sep 2026 19:00:12 -0300 Subject: [PATCH] fix(f6.9): load PDF and image converters in production runtime --- .../inspection-act-pdf-builder.ts | 2 +- .../inspection-document-images.ts | 2 +- .../unit/f6-9-consolidated-documents.test.ts | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/api-v3/src/inspection-reports/inspection-act-pdf-builder.ts b/api-v3/src/inspection-reports/inspection-act-pdf-builder.ts index 9873b6a..510fe4e 100644 --- a/api-v3/src/inspection-reports/inspection-act-pdf-builder.ts +++ b/api-v3/src/inspection-reports/inspection-act-pdf-builder.ts @@ -1,7 +1,7 @@ import { createHash } from 'node:crypto'; import { existsSync } from 'node:fs'; import { resolve } from 'node:path'; -import PDFDocument from 'pdfkit'; +import PDFDocument = require('pdfkit'); export interface ActPdfImage { id: string; diff --git a/api-v3/src/inspection-reports/inspection-document-images.ts b/api-v3/src/inspection-reports/inspection-document-images.ts index afc475f..975f686 100644 --- a/api-v3/src/inspection-reports/inspection-document-images.ts +++ b/api-v3/src/inspection-reports/inspection-document-images.ts @@ -1,4 +1,4 @@ -import sharp from 'sharp'; +const sharp = require('sharp') as typeof import('sharp').default; // PDFKit and the Word image renderer accept JPEG/PNG. The source bytes and // their recorded SHA-256 remain untouched; WebP is only converted for display. diff --git a/api-v3/test/unit/f6-9-consolidated-documents.test.ts b/api-v3/test/unit/f6-9-consolidated-documents.test.ts index 31e1026..28a5b31 100644 --- a/api-v3/test/unit/f6-9-consolidated-documents.test.ts +++ b/api-v3/test/unit/f6-9-consolidated-documents.test.ts @@ -1,6 +1,7 @@ import assert from 'node:assert/strict'; import { createHash } from 'node:crypto'; -import { readFileSync } from 'node:fs'; +import { existsSync, readFileSync } from 'node:fs'; +import { createRequire } from 'node:module'; import { resolve } from 'node:path'; import test from 'node:test'; import { buildInspectionActPdf } from '../../src/inspection-reports/inspection-act-pdf-builder'; @@ -70,3 +71,16 @@ test('F6.9 renders WebP field photographs in both documents without changing the assert.ok(word.buffer.includes(display)); assert.ok(word.buffer.includes(Buffer.from(originalSha))); }); + +test('F6.9 built CommonJS runtime renders signed PDF and WebP evidence', async () => { + const builtPdf = resolve(process.cwd(), 'dist/inspection-reports/inspection-act-pdf-builder.js'); + const builtImage = resolve(process.cwd(), 'dist/inspection-reports/inspection-document-images.js'); + if (!existsSync(builtPdf) || !existsSync(builtImage)) return; + const requireCjs = createRequire(resolve(process.cwd(), 'package.json')); + const runtimePdf = requireCjs(builtPdf) as { buildInspectionActPdf: typeof buildInspectionActPdf }; + const runtimeImage = requireCjs(builtImage) as { renderableInspectionImage: typeof renderableInspectionImage }; + const webp = await sharp(photo).webp().toBuffer(); + const rendered = await runtimeImage.renderableInspectionImage(webp); + const pdf = await runtimePdf.buildInspectionActPdf(sealed, [{ ...evidence, buffer: rendered, sha256: digest(webp) }]); + assert.ok(pdf.buffer.subarray(0, 8).equals(Buffer.from('%PDF-1.4'))); +});