Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

1.9 KB
Raw Blame
import { describe, expect, test } from 'vitest';
import {
  documentTypes,
  generateDocuments,
  isValidDocumentAmount,
  MAX_GENERATED_DOCUMENTS,
} from './document-generation.models';

describe('document generation', () => {
  test('defaults to one non-empty text document', async () => {
    const files = generateDocuments();
    expect(files).toHaveLength(1);
    expect(files[0]).toBeInstanceOf(File);
    expect(files[0].name).toMatch(/^dev-.*\.txt$/);
    expect(files[0].type).toBe('text/plain');
    expect(await files[0].text()).toContain('Generated by Papra devtools');
  });

  test.each(documentTypes)(
    'generates unique $value files with the correct MIME type',
    async ({ value, mimeType }) => {
      const files = generateDocuments({ type: value, amount: 5 });
      const contents = await Promise.all(files.map(async (file) => file.text()));

      expect(files).toHaveLength(5);
      expect(new Set(contents).size).toBe(5);
      for (const file of files) {
        expect(file.name.endsWith(`.${value}`)).toBe(true);
        expect(file.type).toBe(mimeType);
        expect(file.size).toBeGreaterThan(0);
      }
    },
  );

  test('generates a CSV with matching header and data columns', async () => {
    const [file] = generateDocuments({ type: 'csv' });
    const [header, row] = (await file.text()).trim().split('\n');
    expect(header).toBe('reference,company,subject,date,currency,total,note');
    expect(row.split(',')).toHaveLength(header.split(',').length);
  });

  test.each([0, -1, 1.5, Number.NaN, Number.POSITIVE_INFINITY, MAX_GENERATED_DOCUMENTS + 1])(
    'rejects invalid batch size %s',
    (amount) => {
      expect(isValidDocumentAmount(amount)).toBe(false);
      expect(() => generateDocuments({ amount })).toThrow('Choose between');
    },
  );

  test('accepts the maximum batch size', () => {
    expect(generateDocuments({ amount: MAX_GENERATED_DOCUMENTS })).toHaveLength(
      MAX_GENERATED_DOCUMENTS,
    );
  });
});