From 0a718dac8f564ed273803937ebd4a3f672e95668 Mon Sep 17 00:00:00 2001 From: InstalZDLL Date: Sun, 15 Mar 2026 15:58:43 +0100 Subject: [PATCH] fix: address CodeRabbit review feedback on tests - digital-sign-pdf.test.ts: stub VITE_CORS_PROXY_URL and VITE_CORS_PROXY_SECRET env vars for test isolation - timestamp-node.test.ts: replace regex-only test with actual data() method tests covering timestampPdf call, output filename suffix, and error wrapping with TSA context --- src/tests/digital-sign-pdf.test.ts | 2 ++ src/tests/timestamp-node.test.ts | 47 ++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/tests/digital-sign-pdf.test.ts b/src/tests/digital-sign-pdf.test.ts index 9cc1fa1..6be3fcb 100644 --- a/src/tests/digital-sign-pdf.test.ts +++ b/src/tests/digital-sign-pdf.test.ts @@ -30,6 +30,8 @@ describe('timestampPdf', () => { beforeEach(() => { vi.clearAllMocks(); + vi.stubEnv('VITE_CORS_PROXY_URL', ''); + vi.stubEnv('VITE_CORS_PROXY_SECRET', ''); samplePdfBytes = new Uint8Array(fs.readFileSync(SAMPLE_PDF_PATH)); }); diff --git a/src/tests/timestamp-node.test.ts b/src/tests/timestamp-node.test.ts index 3e2f785..bdcf230 100644 --- a/src/tests/timestamp-node.test.ts +++ b/src/tests/timestamp-node.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, vi } from 'vitest'; +import { describe, it, expect, vi, beforeEach } from 'vitest'; import { TIMESTAMP_TSA_PRESETS } from '@/js/config/timestamp-tsa'; // Mock external dependencies before importing the node @@ -74,8 +74,13 @@ vi.mock('@/js/workflow/types', () => ({ })); import { TimestampNode } from '@/js/workflow/nodes/timestamp-node'; +import { timestampPdf } from '@/js/logic/digital-sign-pdf'; describe('TimestampNode', () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + it('should be instantiable', () => { const node = new TimestampNode(); expect(node).toBeDefined(); @@ -109,9 +114,41 @@ describe('TimestampNode', () => { expect(presets[0].url).toBe(TIMESTAMP_TSA_PRESETS[0].url); }); - it('should generate _timestamped suffix in output filename', () => { - const input = 'report.pdf'; - const output = input.replace(/\.pdf$/i, '_timestamped.pdf'); - expect(output).toBe('report_timestamped.pdf'); + it('should call timestampPdf with correct TSA URL via data()', async () => { + const node = new TimestampNode(); + const mockInput = [ + { bytes: new Uint8Array([1, 2, 3]), filename: 'test.pdf' }, + ]; + + await node.data({ pdf: mockInput }); + + expect(timestampPdf).toHaveBeenCalledWith( + mockInput[0].bytes, + TIMESTAMP_TSA_PRESETS[0].url + ); + }); + + it('should generate _timestamped suffix in output filename via data()', async () => { + const node = new TimestampNode(); + const mockInput = [ + { bytes: new Uint8Array([1, 2, 3]), filename: 'report.pdf' }, + ]; + + const result = (await node.data({ pdf: mockInput })) as { + pdf: Array<{ filename: string }>; + }; + + expect(result.pdf[0].filename).toBe('report_timestamped.pdf'); + }); + + it('should wrap errors from timestampPdf with TSA context', async () => { + vi.mocked(timestampPdf).mockRejectedValueOnce(new Error('Network error')); + const node = new TimestampNode(); + + await expect( + node.data({ + pdf: [{ bytes: new Uint8Array([1]), filename: 'test.pdf' }], + }) + ).rejects.toThrow(/Failed to timestamp using TSA/); }); });