刘光辉
119 分钟以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
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
441
442
443
444
445
446
447
448
449
450
451
452
/* @vitest-environment happy-dom */
 
import { beforeEach, describe, expect, it, vi } from 'vitest';
 
import { createDmsFilePrintTicket, getDmsFilePageCount, renderDmsPrintPdf } from '#/api/x/dms/print';
import { onlineUtils } from '#/utils/jnpf';
 
import { buildDmsPrintableHtml, normalizeDmsPrintOptions, parseDmsPrintPages, printDmsDocument, registerDmsPrintPageVariableHandler } from '../dmsHiprint';
 
const pdfMocks = vi.hoisted(() => ({ getDocument: vi.fn() }));
 
vi.mock('#/api/request', () => ({ defHttp: { post: vi.fn() } }));
vi.mock('pdfjs-dist', () => ({ GlobalWorkerOptions: {}, getDocument: pdfMocks.getDocument }));
vi.mock('pdfjs-dist/build/pdf.worker.min.mjs?url', () => ({ default: 'pdf.worker.mjs' }));
vi.mock('#/api/x/dms/print', () => ({ createDmsFilePrintTicket: vi.fn(), getDmsFilePageCount: vi.fn(), renderDmsPrintPdf: vi.fn() }));
vi.mock('#/utils/jnpf', () => ({ onlineUtils: { hiprint: vi.fn() } }));
 
describe('dms Hiprint options', () => {
  beforeEach(() => {
    vi.mocked(getDmsFilePageCount).mockResolvedValue({ data: { fileName: 'quality.docx', fileVersionId: 'version-1', pageCount: 200 } } as any);
    vi.mocked(onlineUtils.hiprint).mockImplementation(async (config: any) => {
      config.onPrint?.({ mode: 'hiprint' });
      return { mode: 'hiprint' } as any;
    });
  });
 
  it('passes every barcode entry to the PRINT ticket before requesting printable PDF bytes', async () => {
    const customBarcode = [
      { page: 1, barValue: 'REC-001', barText: 'First label' },
      { page: 1, barValue: 'SECOND-001', barText: 'Second label' },
      { barValue: 'GLOBAL', barText: 'Every page' },
    ];
    vi.mocked(createDmsFilePrintTicket).mockResolvedValue({
      data: { accessTicket: 'print-ticket', fileName: 'quality.docx', fileVersionId: 'version-1' },
    } as any);
    vi.mocked(renderDmsPrintPdf).mockResolvedValue(new ArrayBuffer(1) as any);
    let confirmDone!: () => void;
    const onPrintSuccess = vi.fn(
      () =>
        new Promise<void>((resolve) => {
          confirmDone = resolve;
        }),
    );
    let active = true;
    let hiprintConfig: any;
    vi.mocked(onlineUtils.hiprint).mockImplementation(async (config: any) => {
      hiprintConfig = config;
      return { mode: 'hiprint' } as any;
    });
    pdfMocks.getDocument.mockReturnValue({
      promise: Promise.resolve({
        numPages: 1,
        destroy: vi.fn(),
        getPage: vi.fn(async () => ({
          cleanup: vi.fn(),
          getViewport: () => ({ height: 842, width: 595 }),
          render: () => ({ promise: Promise.resolve() }),
        })),
      }),
    });
    vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({} as CanvasRenderingContext2D);
    vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue('data:image/png;base64,page');
    let completed = false;
    const printing = printDmsDocument(
      'quality.docx',
      {
        fileVersionId: 'version-1',
        copies: 2,
        onPrintSuccess,
        pages: '1',
        customBarcode,
        shifouShoukong: 'no',
      } as any,
      () => active,
    ).finally(() => {
      completed = true;
    });
    await vi.waitFor(() => expect(hiprintConfig).toBeDefined());
    expect(completed).toBe(false);
    expect(onPrintSuccess).not.toHaveBeenCalled();
    active = false;
    hiprintConfig.onPrint({ mode: 'hiprint' });
    hiprintConfig.onPrint({ mode: 'hiprint' });
    await vi.waitFor(() => expect(onPrintSuccess).toHaveBeenCalledTimes(1));
    expect(completed).toBe(false);
    confirmDone();
    await printing;
    expect(createDmsFilePrintTicket).toHaveBeenCalledWith(
      expect.objectContaining({
        resourceId: 'version-1',
        resourceType: 'FILE_VERSION',
        copies: 2,
        printPages: '1',
        customBarcode,
      }),
    );
    expect(vi.mocked(createDmsFilePrintTicket).mock.calls[0]![0]).not.toHaveProperty('shifouShoukong');
    expect(renderDmsPrintPdf).toHaveBeenCalledWith({
      items: [{ accessTicket: 'print-ticket', fileName: 'quality.docx', fileVersionId: 'version-1', printPages: '1' }],
      source: { accessTicket: 'print-ticket', fileName: 'quality.docx', fileVersionId: 'version-1' },
    });
    expect(onlineUtils.hiprint).toHaveBeenCalledWith(
      expect.objectContaining({ clientOptions: { copies: 2, title: 'quality.docx' }, onPrint: expect.any(Function) }),
    );
    expect(onPrintSuccess).toHaveBeenCalledTimes(1);
    vi.restoreAllMocks();
  });
 
  it('rejects the DMS print when the client reports a send failure', async () => {
    vi.clearAllMocks();
    vi.mocked(getDmsFilePageCount).mockResolvedValue({ data: { fileName: 'quality.docx', fileVersionId: 'version-1', pageCount: 1 } } as any);
    vi.mocked(createDmsFilePrintTicket).mockResolvedValue({
      data: { accessTicket: 'print-ticket', fileName: 'quality.docx', fileVersionId: 'version-1' },
    } as any);
    vi.mocked(renderDmsPrintPdf).mockResolvedValue(new ArrayBuffer(1) as any);
    vi.mocked(onlineUtils.hiprint).mockImplementation(async (config: any) => {
      config.onError?.(new Error('printer unavailable'));
      return { mode: 'hiprint' } as any;
    });
    pdfMocks.getDocument.mockReturnValue({
      promise: Promise.resolve({
        destroy: vi.fn(),
        getPage: vi.fn(async () => ({
          cleanup: vi.fn(),
          getViewport: () => ({ height: 842, width: 595 }),
          render: () => ({ promise: Promise.resolve() }),
        })),
        numPages: 1,
      }),
    });
    vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({} as CanvasRenderingContext2D);
    vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue('data:image/png;base64,page');
    const onPrintSuccess = vi.fn();
 
    await expect(printDmsDocument('quality.docx', { fileVersionId: 'version-1', onPrintSuccess, pages: '1' })).rejects.toThrow('printer unavailable');
 
    expect(onPrintSuccess).not.toHaveBeenCalled();
    expect(onlineUtils.hiprint).toHaveBeenCalledWith(expect.objectContaining({ errorMessageMode: 'none', onError: expect.any(Function) }));
    vi.restoreAllMocks();
  });
 
  it('rejects the DMS print when the client does not confirm the task in time', async () => {
    vi.useFakeTimers();
    try {
      vi.clearAllMocks();
      vi.mocked(getDmsFilePageCount).mockResolvedValue({ data: { fileName: 'quality.docx', fileVersionId: 'version-1', pageCount: 1 } } as any);
      vi.mocked(createDmsFilePrintTicket).mockResolvedValue({
        data: { accessTicket: 'print-ticket', fileName: 'quality.docx', fileVersionId: 'version-1' },
      } as any);
      vi.mocked(renderDmsPrintPdf).mockResolvedValue(new ArrayBuffer(1) as any);
      vi.mocked(onlineUtils.hiprint).mockResolvedValue({ mode: 'hiprint' } as any);
      pdfMocks.getDocument.mockReturnValue({
        promise: Promise.resolve({
          destroy: vi.fn(),
          getPage: vi.fn(async () => ({
            cleanup: vi.fn(),
            getViewport: () => ({ height: 842, width: 595 }),
            render: () => ({ promise: Promise.resolve() }),
          })),
          numPages: 1,
        }),
      });
      vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({} as CanvasRenderingContext2D);
      vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue('data:image/png;base64,page');
 
      const printing = printDmsDocument('quality.docx', { fileVersionId: 'version-1', pages: '1' });
      const rejected = expect(printing).rejects.toThrow('打印客户端确认任务超时');
      await vi.advanceTimersByTimeAsync(0);
      expect(onlineUtils.hiprint).toHaveBeenCalledTimes(1);
      await vi.advanceTimersByTimeAsync(60_000);
 
      await rejected;
    } finally {
      vi.useRealTimers();
      vi.restoreAllMocks();
    }
  });
  it('parses ranges, removes duplicates and preserves original page numbers', () => {
    expect(parseDmsPrintPages('3,1-2,2', 3)).toEqual([1, 2, 3]);
    expect(parseDmsPrintPages(undefined, 3)).toBeUndefined();
  });
 
  it('filters pages beyond the converted PDF', () => {
    expect(parseDmsPrintPages('1,4,2-5', 3)).toEqual([1, 2, 3]);
    expect(parseDmsPrintPages('4-100000', 3)).toEqual([]);
  });
 
  it('does not create a print task when every selected page is out of range', async () => {
    vi.clearAllMocks();
    vi.mocked(getDmsFilePageCount).mockResolvedValue({ data: { fileName: 'quality.docx', fileVersionId: 'version-1', pageCount: 3 } } as any);
 
    await expect(printDmsDocument('quality.docx', { fileVersionId: 'version-1', pages: '4-5' })).resolves.toBeUndefined();
 
    expect(createDmsFilePrintTicket).not.toHaveBeenCalled();
    expect(renderDmsPrintPdf).not.toHaveBeenCalled();
    expect(onlineUtils.hiprint).not.toHaveBeenCalled();
  });
 
  it('normalizes defaults and validates page-variable selection', () => {
    expect(normalizeDmsPrintOptions()).toEqual({ copies: 1, copyBarcodes: undefined, pageVariables: [], pages: undefined });
    expect(() => normalizeDmsPrintOptions({ pages: '1-2', pageVariables: [{ pageNo: 3, variables: { code: 'A' } }] })).toThrow('页面变量引用了未选择的打印页');
  });
 
  it('uses each copy barcode set as authoritative without duplicating preview barcodes', async () => {
    vi.clearAllMocks();
    vi.mocked(createDmsFilePrintTicket)
      .mockResolvedValueOnce({ data: { accessTicket: 'ticket-1', fileName: 'quality.docx', fileVersionId: 'version-1' } } as any)
      .mockResolvedValueOnce({ data: { accessTicket: 'ticket-2', fileName: 'quality.docx', fileVersionId: 'version-1' } } as any);
    vi.mocked(renderDmsPrintPdf).mockResolvedValue(new ArrayBuffer(1) as any);
    const getPage = vi.fn(async (_pageNo: number) => ({
      cleanup: vi.fn(),
      getViewport: () => ({ height: 842, width: 595 }),
      render: () => ({ promise: Promise.resolve() }),
    }));
    pdfMocks.getDocument.mockReturnValue({ promise: Promise.resolve({ destroy: vi.fn(), getPage, numPages: 2 }) });
    vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({} as CanvasRenderingContext2D);
    vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue('data:image/png;base64,page');
 
    await printDmsDocument('quality.docx', {
      copies: 2,
      copyBarcodes: [
        { copyNo: 1, customBarcode: [{ page: 2, barText: 'Copy 1 Page 2', barValue: 'C1-P2' }] },
        { copyNo: 2, customBarcode: [{ page: 2, barText: 'Copy 2 Page 2', barValue: 'C2-P2' }] },
      ],
      customBarcode: [{ barText: 'Common', barValue: 'COMMON' }],
      fileVersionId: 'version-1',
      pages: '2',
    });
 
    expect(createDmsFilePrintTicket).toHaveBeenNthCalledWith(
      1,
      expect.objectContaining({
        copies: 1,
        customBarcode: [{ page: 2, barText: 'Copy 1 Page 2', barValue: 'C1-P2' }],
        printPages: '2',
      }),
    );
    expect(createDmsFilePrintTicket).toHaveBeenNthCalledWith(
      2,
      expect.objectContaining({
        copies: 1,
        customBarcode: [{ page: 2, barText: 'Copy 2 Page 2', barValue: 'C2-P2' }],
        printPages: '2',
      }),
    );
    expect(renderDmsPrintPdf).toHaveBeenCalledWith({
      items: [
        { accessTicket: 'ticket-1', fileName: 'quality.docx', fileVersionId: 'version-1', printPages: '2' },
        { accessTicket: 'ticket-2', fileName: 'quality.docx', fileVersionId: 'version-1', printPages: '2' },
      ],
      source: { accessTicket: 'ticket-1', fileName: 'quality.docx', fileVersionId: 'version-1' },
    });
    expect(getPage.mock.calls.map(([pageNo]) => pageNo)).toEqual([1, 2]);
    expect(onlineUtils.hiprint).toHaveBeenCalledWith(expect.objectContaining({ clientOptions: { copies: 1, title: 'quality.docx' } }));
    vi.restoreAllMocks();
  });
 
  it('splits copy-specific printing by both copy and physical-page limits', async () => {
    vi.clearAllMocks();
    let ticketNo = 0;
    vi.mocked(createDmsFilePrintTicket).mockImplementation(async () => {
      ticketNo++;
      return { data: { accessTicket: `ticket-${ticketNo}`, fileName: 'quality.docx', fileVersionId: 'version-1' } } as any;
    });
    vi.mocked(renderDmsPrintPdf).mockResolvedValue(new ArrayBuffer(1) as any);
    vi.mocked(onlineUtils.hiprint).mockImplementation(async (config: any) => {
      config.onPrint?.({ mode: 'hiprint' });
      return { mode: 'hiprint' } as any;
    });
    const getPage = vi.fn(async (_pageNo: number) => ({
      cleanup: vi.fn(),
      getViewport: () => ({ height: 842, width: 595 }),
      render: () => ({ promise: Promise.resolve() }),
    }));
    for (const numPages of [96, 96, 12]) {
      pdfMocks.getDocument.mockReturnValueOnce({ promise: Promise.resolve({ destroy: vi.fn(), getPage, numPages }) });
    }
    vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({} as CanvasRenderingContext2D);
    vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue('data:image/png;base64,page');
    const onPrintSuccess = vi.fn();
    const copyBarcodes = Array.from({ length: 34 }, (_, index) => ({
      copyNo: index + 1,
      customBarcode: [{ page: 1, barText: `Copy ${index + 1}`, barValue: `COPY-${index + 1}` }],
    }));
 
    await printDmsDocument('quality.docx', {
      copies: 34,
      copyBarcodes,
      fileVersionId: 'version-1',
      onPrintSuccess,
      pages: '1-6',
    });
 
    expect(vi.mocked(renderDmsPrintPdf).mock.calls.map(([request]) => (request as any).items.length)).toEqual([16, 16, 2]);
    expect(vi.mocked(renderDmsPrintPdf).mock.calls.map(([request]) => (request as any).source.accessTicket)).toEqual(['ticket-1', 'ticket-1', 'ticket-1']);
    expect(vi.mocked(onlineUtils.hiprint).mock.calls.map(([config]) => config.clientOptions?.title)).toEqual([
      'quality.docx (第 1 批)',
      'quality.docx (第 2 批)',
      'quality.docx (第 3 批)',
    ]);
    expect(onPrintSuccess).toHaveBeenCalledTimes(1);
    vi.restoreAllMocks();
  });
 
  it('splits more than 100 selected pages per copy without changing the copy ticket', async () => {
    vi.clearAllMocks();
    vi.mocked(getDmsFilePageCount).mockResolvedValue({ data: { fileName: 'quality.docx', fileVersionId: 'version-1', pageCount: 205 } } as any);
    vi.mocked(createDmsFilePrintTicket)
      .mockResolvedValueOnce({ data: { accessTicket: 'ticket-1', fileName: 'quality.docx', fileVersionId: 'version-1' } } as any)
      .mockResolvedValueOnce({ data: { accessTicket: 'ticket-2', fileName: 'quality.docx', fileVersionId: 'version-1' } } as any);
    vi.mocked(renderDmsPrintPdf).mockResolvedValue(new ArrayBuffer(1) as any);
    for (const numPages of [100, 100, 5, 100, 100, 5]) {
      pdfMocks.getDocument.mockReturnValueOnce({
        promise: Promise.resolve({
          destroy: vi.fn(),
          getPage: vi.fn(async () => ({ cleanup: vi.fn(), getViewport: () => ({ height: 842, width: 595 }), render: () => ({ promise: Promise.resolve() }) })),
          numPages,
        }),
      });
    }
    vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({} as CanvasRenderingContext2D);
    vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue('data:image/png;base64,page');
 
    await printDmsDocument('quality.docx', {
      copies: 2,
      copyBarcodes: [
        { copyNo: 1, customBarcode: [{ page: 1, barText: 'Copy 1', barValue: 'COPY-1' }] },
        { copyNo: 2, customBarcode: [{ page: 1, barText: 'Copy 2', barValue: 'COPY-2' }] },
      ],
      fileVersionId: 'version-1',
      pages: '1-205',
    });
 
    expect(vi.mocked(renderDmsPrintPdf).mock.calls.map(([request]) => (request as any).items[0])).toEqual([
      expect.objectContaining({ accessTicket: 'ticket-1', printPages: '1-100' }),
      expect.objectContaining({ accessTicket: 'ticket-1', printPages: '101-200' }),
      expect.objectContaining({ accessTicket: 'ticket-1', printPages: '201-205' }),
      expect.objectContaining({ accessTicket: 'ticket-2', printPages: '1-100' }),
      expect.objectContaining({ accessTicket: 'ticket-2', printPages: '101-200' }),
      expect.objectContaining({ accessTicket: 'ticket-2', printPages: '201-205' }),
    ]);
    vi.restoreAllMocks();
  });
 
  it('halves an oversized PDF batch and prints the smaller batches in order', async () => {
    vi.clearAllMocks();
    vi.mocked(getDmsFilePageCount).mockResolvedValue({ data: { fileName: 'quality.docx', fileVersionId: 'version-1', pageCount: 2 } } as any);
    vi.mocked(createDmsFilePrintTicket)
      .mockResolvedValueOnce({ data: { accessTicket: 'ticket-1', fileName: 'quality.docx', fileVersionId: 'version-1' } } as any)
      .mockResolvedValueOnce({ data: { accessTicket: 'ticket-2', fileName: 'quality.docx', fileVersionId: 'version-1' } } as any);
    vi.mocked(renderDmsPrintPdf)
      .mockRejectedValueOnce(new Error('单批打印 PDF 超过系统大小限制'))
      .mockResolvedValue(new ArrayBuffer(1) as any);
    for (let index = 0; index < 2; index++) {
      pdfMocks.getDocument.mockReturnValueOnce({
        promise: Promise.resolve({
          destroy: vi.fn(),
          getPage: vi.fn(async () => ({ cleanup: vi.fn(), getViewport: () => ({ height: 842, width: 595 }), render: () => ({ promise: Promise.resolve() }) })),
          numPages: 2,
        }),
      });
    }
    vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({} as CanvasRenderingContext2D);
    vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue('data:image/png;base64,page');
 
    await printDmsDocument('quality.docx', {
      copies: 2,
      copyBarcodes: [
        { copyNo: 1, customBarcode: [{ page: 1, barText: 'Copy 1', barValue: 'COPY-1' }] },
        { copyNo: 2, customBarcode: [{ page: 1, barText: 'Copy 2', barValue: 'COPY-2' }] },
      ],
      fileVersionId: 'version-1',
      pages: '1-2',
    });
 
    expect(vi.mocked(renderDmsPrintPdf).mock.calls.map(([request]) => (request as any).items.length)).toEqual([2, 1, 1]);
    expect(onlineUtils.hiprint).toHaveBeenCalledTimes(2);
    vi.restoreAllMocks();
  });
 
  it('rejects non-contiguous copy numbers and conflicting copy counts', () => {
    const copyBarcodes = [{ copyNo: 2, customBarcode: [{ page: 1, barText: 'Copy 2', barValue: 'C2' }] }];
    expect(() => normalizeDmsPrintOptions({ copyBarcodes })).toThrow('打印份序必须从 1 开始连续且不可重复');
    expect(() => normalizeDmsPrintOptions({ copies: 2, copyBarcodes: [{ ...copyBarcodes[0]!, copyNo: 1 }] })).toThrow('打印份数必须与逐份条码配置数量一致');
  });
 
  it('rejects duplicate page variables and nested objects', () => {
    expect(() =>
      normalizeDmsPrintOptions({
        pageVariables: [
          { pageNo: 1, variables: { code: 'A' } },
          { pageNo: 1, variables: { code: 'B' } },
        ],
      }),
    ).toThrow('页面打印参数无效');
    expect(() => normalizeDmsPrintOptions({ pageVariables: [{ pageNo: 1, variables: { unsafe: { nested: true } } }] })).toThrow('页面打印参数无效');
  });
 
  it('matches page variables by original page number after filtering', async () => {
    const destroy = vi.fn();
    const getPage = vi.fn(async (_pageNo: number) => ({
      cleanup: vi.fn(),
      getViewport: () => ({ height: 1684, width: 1190 }),
      render: () => ({ promise: Promise.resolve() }),
    }));
    pdfMocks.getDocument.mockReturnValue({ promise: Promise.resolve({ destroy, getPage, numPages: 3 }) });
    vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({} as CanvasRenderingContext2D);
    vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue('data:image/png;base64,page');
    const handler = vi.fn();
    const unregister = registerDmsPrintPageVariableHandler(handler);
 
    const html = await buildDmsPrintableHtml(new ArrayBuffer(1), {
      pages: '3',
      pageVariables: [{ pageNo: 3, variables: { code: 'PAGE-3' } }],
    });
 
    expect(getPage).toHaveBeenCalledWith(3);
    expect(handler).toHaveBeenCalledWith(expect.objectContaining({ pageNo: 3, variables: { code: 'PAGE-3' } }));
    expect(html).toContain('data:image/png;base64,page');
    expect(destroy).toHaveBeenCalled();
    unregister();
    vi.restoreAllMocks();
  });
 
  it('ignores page variables belonging to filtered out-of-range pages', async () => {
    const getPage = vi.fn(async (_pageNo: number) => ({
      cleanup: vi.fn(),
      getViewport: () => ({ height: 1684, width: 1190 }),
      render: () => ({ promise: Promise.resolve() }),
    }));
    pdfMocks.getDocument.mockReturnValue({ promise: Promise.resolve({ destroy: vi.fn(), getPage, numPages: 2 }) });
    vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({} as CanvasRenderingContext2D);
    vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue('data:image/png;base64,page');
    const handler = vi.fn();
    const unregister = registerDmsPrintPageVariableHandler(handler);
 
    await buildDmsPrintableHtml(new ArrayBuffer(1), {
      pages: '1,3',
      pageVariables: [
        { pageNo: 1, variables: { code: 'PAGE-1' } },
        { pageNo: 3, variables: { code: 'PAGE-3' } },
      ],
    });
 
    expect(getPage).toHaveBeenCalledTimes(1);
    expect(getPage).toHaveBeenCalledWith(1);
    expect(handler).toHaveBeenCalledWith(expect.objectContaining({ pageNo: 1, variables: { code: 'PAGE-1' } }));
    expect(handler).not.toHaveBeenCalledWith(expect.objectContaining({ pageNo: 3 }));
    unregister();
    vi.restoreAllMocks();
  });
});