import { describe, expect, it } from 'vitest';
import { validateSubmissionFiles } from '../../forms/files.js';
const PNG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d]);
const PDF = Buffer.from('%PDF-1.4\n%âãÏÓ\n', 'latin1');
function fileField(overrides) {
    return {
        id: 'f',
        label: 'Attachment',
        key: 'attachment',
        type: 'file',
        required: false,
        sortOrder: 0,
        ...overrides,
    };
}
function incoming(overrides) {
    return {
        fieldKey: 'attachment',
        fileName: 'photo.png',
        mimeType: 'image/png',
        size: PNG.byteLength,
        buffer: PNG,
        ...overrides,
    };
}
describe('validateSubmissionFiles', () => {
    it('accepts a valid PNG against a file field', () => {
        const result = validateSubmissionFiles([fileField({})], [incoming({})]);
        expect(result.ok).toBe(true);
        expect(result.errors).toHaveLength(0);
    });
    it('rejects files larger than the size cap', () => {
        const result = validateSubmissionFiles([fileField({})], [incoming({ size: 999_999_999 })], {
            maxSizeBytes: 1024,
        });
        expect(result.ok).toBe(false);
        expect(result.errors[0]?.fieldKey).toBe('attachment');
    });
    it('rejects disallowed MIME types', () => {
        const result = validateSubmissionFiles([fileField({})], [incoming({ mimeType: 'application/zip', fileName: 'x.zip', buffer: PNG })]);
        expect(result.ok).toBe(false);
    });
    it('rejects content that does not match the declared MIME (magic-byte sniff)', () => {
        const result = validateSubmissionFiles([fileField({})], [
            incoming({
                mimeType: 'image/png',
                fileName: 'fake.png',
                buffer: PDF,
                size: PDF.byteLength,
            }),
        ]);
        expect(result.ok).toBe(false);
        expect(result.errors[0]?.message).toMatch(/do not match/i);
    });
    it('rejects a file uploaded to a non-file field', () => {
        const result = validateSubmissionFiles([{ id: 't', label: 'Name', key: 'name', type: 'text', required: false, sortOrder: 0 }], [incoming({ fieldKey: 'name' })]);
        expect(result.ok).toBe(false);
        expect(result.errors[0]?.message).toMatch(/unexpected/i);
    });
    it('honours per-field fileTypes to narrow the allowlist', () => {
        const result = validateSubmissionFiles([fileField({ validationRules: { fileTypes: ['application/pdf'] } })], [incoming({})]);
        expect(result.ok).toBe(false);
    });
});
//# sourceMappingURL=submission-files.test.js.map