import test from 'node:test';
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { generatePlanPdf } from '../../src/tools/planner/pdf';
import { plannerExamples } from '../../src/tools/planner/core';
import { assertPdfTextSupported } from '../../src/tools/shared/pdf-font';

const font = async () => new Uint8Array(await readFile(new URL('../../public/tools-assets/fonts/NotoSans-Regular.ttf', import.meta.url)));

test('PDF font supports English, French, decimal and business punctuation', async () => {
  await assertPdfTextSupported(['Développement de logiciels — coût estimé : 1 234,56 $', 'Échéancier, café, œuvre, façade, Noël, 15% × 20'], await font());
});

test('unsupported glyphs fail clearly before a misleading PDF is produced', async () => {
  const plan = structuredClone(plannerExamples['customer-portal']);
  plan.title = 'Project 承認';
  const original = JSON.stringify(plan);
  await assert.rejects(generatePlanPdf(plan, { fontBytes: await font() }), /font cannot display.*U\+627F/);
  assert.equal(JSON.stringify(plan), original);
});

test('generated Planner PDFs reopen and contain separate readable timeline pages', async () => {
  const { PDFDocument } = await import('@pdfme/pdf-lib');
  const result = await generatePlanPdf(plannerExamples['customer-portal'], { fontBytes: await font() });
  const reopened = await PDFDocument.load(result);
  assert.ok(reopened.getPageCount() >= 2);
  const last = reopened.getPages().at(-1)!;
  assert.ok(last.getWidth() > last.getHeight());
  assert.equal(reopened.getTitle(), 'Customer portal');
});
