import assert from 'node:assert/strict';
import test from 'node:test';
import { PDFDocument } from '@pdfme/pdf-lib';
import {
  assertCanDeleteDraftVersion,
  assertLifecycleTransition,
  parsePdfUpload
} from './validation';

test('assertLifecycleTransition rejects invalid active archive transition', () => {
  assert.throws(
    () => assertLifecycleTransition('active', 'archived'),
    /Invalid lifecycle transition/
  );
});

test('assertCanDeleteDraftVersion rejects non-draft versions', () => {
  assert.throws(
    () => assertCanDeleteDraftVersion('published', false),
    /Only inactive draft versions can be deleted/
  );
});

test('parsePdfUpload accepts readable pdf and extracts page count', async () => {
  const pdf = await PDFDocument.create();
  pdf.addPage([200, 200]);
  const bytes = await pdf.save();
  const parsed = await parsePdfUpload({
    fileName: 'sample.pdf',
    mimeType: 'application/pdf',
    size: bytes.length,
    buffer: Buffer.from(bytes)
  });

  assert.equal(parsed.pageCount, 1);
  assert.equal(parsed.contentType, 'application/pdf');
  assert.equal(parsed.fileName, 'sample.pdf');
});
