import { describe, expect, it, vi } from 'vitest';
const db = {
    document: { findFirst: vi.fn() },
    formSubmission: { findMany: vi.fn() },
};
vi.mock('../../db.js', () => ({ getDB: () => db }));
const { exportFormEntries } = await import('../../forms/entries.js');
describe('exportFormEntries', () => {
    it('returns null when the form does not exist', async () => {
        db.document.findFirst.mockResolvedValueOnce(null);
        expect(await exportFormEntries('missing')).toBeNull();
    });
    it('builds a CSV with metadata + field columns and decrypted plaintext values', async () => {
        db.document.findFirst.mockResolvedValueOnce({
            title: 'Contact',
            slug: 'contact',
            data: {
                name: 'Contact Us',
                slug: 'contact-us',
                fields: [
                    { key: 'name', label: 'Full Name', type: 'text' },
                    { key: 'message', label: 'Message', type: 'textarea' },
                ],
            },
        });
        db.formSubmission.findMany.mockResolvedValueOnce([
            {
                id: 's1',
                entryNumber: 1,
                formId: 'f1',
                data: { name: 'Ada', message: 'Hello, world' },
                senderName: 'Ada',
                senderEmail: 'ada@example.com',
                spamStatus: 'clean',
                submittedAt: new Date('2026-01-01T00:00:00.000Z'),
            },
        ]);
        const result = await exportFormEntries('f1');
        expect(result).not.toBeNull();
        expect(result.count).toBe(1);
        expect(result.formSlug).toBe('contact-us');
        const [header, row] = result.csv.split('\r\n');
        expect(header).toBe('Entry #,Submitted At,Status,Name,Email,Phone,Full Name,Message');
        // "Hello, world" contains a comma → must be quoted.
        expect(row).toContain('"Hello, world"');
        expect(row).toContain('ada@example.com');
    });
    it('neutralises CSV/formula injection in cell values', async () => {
        db.document.findFirst.mockResolvedValueOnce({
            title: 'F',
            slug: 'f',
            data: { name: 'F', slug: 'f', fields: [{ key: 'note', label: 'Note', type: 'text' }] },
        });
        db.formSubmission.findMany.mockResolvedValueOnce([
            {
                id: 's1',
                entryNumber: 1,
                formId: 'f1',
                data: { note: '=SUM(A1:A2)' },
                spamStatus: 'clean',
                submittedAt: new Date(),
            },
        ]);
        const result = await exportFormEntries('f1');
        // Leading "=" is prefixed with a single quote so spreadsheets don't execute it.
        expect(result.csv).toContain("'=SUM(A1:A2)");
    });
});
//# sourceMappingURL=entries-export.test.js.map