import { describe, it, expect, vi } from 'vitest';
import { processScheduledPublish, processScheduledUnpublish, getScheduleCalendar, } from '../../scheduling/index.js';
function createMockDB(docs = []) {
    return {
        document: {
            findMany: vi.fn(async () => docs),
            update: vi.fn(async ({ where, data }) => {
                const doc = docs.find((d) => d.id === where.id);
                if (doc)
                    Object.assign(doc, data);
                return doc;
            }),
        },
    };
}
describe('processScheduledPublish', () => {
    it('publishes documents with scheduledAt in the past', async () => {
        const docs = [
            { id: 'doc-1', status: 'SCHEDULED', scheduledAt: new Date(Date.now() - 10000) },
            { id: 'doc-2', status: 'SCHEDULED', scheduledAt: new Date(Date.now() - 5000) },
        ];
        const db = createMockDB(docs);
        const result = await processScheduledPublish(db);
        expect(result.published).toBe(2);
        expect(result.errors).toHaveLength(0);
        expect(db.document.update).toHaveBeenCalledTimes(2);
    });
    it('returns empty when no documents are due', async () => {
        const db = createMockDB([]);
        const result = await processScheduledPublish(db);
        expect(result.published).toBe(0);
        expect(result.errors).toHaveLength(0);
    });
    it('captures errors for individual documents', async () => {
        const docs = [{ id: 'doc-1', status: 'SCHEDULED', scheduledAt: new Date(Date.now() - 10000) }];
        const db = createMockDB(docs);
        db.document.update.mockRejectedValueOnce(new Error('DB error'));
        const result = await processScheduledPublish(db);
        expect(result.published).toBe(0);
        expect(result.errors).toHaveLength(1);
        expect(result.errors[0].documentId).toBe('doc-1');
    });
});
describe('processScheduledUnpublish', () => {
    it('unpublishes documents with scheduledUnpublishAt in the past', async () => {
        const docs = [
            { id: 'doc-1', status: 'PUBLISHED', scheduledUnpublishAt: new Date(Date.now() - 10000) },
        ];
        const db = createMockDB(docs);
        const result = await processScheduledUnpublish(db);
        expect(result.unpublished).toBe(1);
        expect(result.errors).toHaveLength(0);
    });
});
describe('getScheduleCalendar', () => {
    it('returns entries for scheduled publish and unpublish', async () => {
        const now = new Date();
        const future = new Date(Date.now() + 86_400_000);
        const docs = [
            {
                id: 'doc-1',
                collection: 'pages',
                title: 'Page 1',
                scheduledAt: future,
                scheduledUnpublishAt: null,
            },
            {
                id: 'doc-2',
                collection: 'posts',
                title: 'Post 1',
                scheduledAt: null,
                scheduledUnpublishAt: future,
            },
        ];
        const db = createMockDB(docs);
        const from = new Date(Date.now() - 86_400_000);
        const to = new Date(Date.now() + 86_400_000 * 2);
        const entries = await getScheduleCalendar(from, to, db);
        expect(entries).toHaveLength(2);
        expect(entries[0].action).toBe('publish');
        expect(entries[1].action).toBe('unpublish');
    });
    it('returns sorted entries', async () => {
        const earlier = new Date(Date.now() + 10_000);
        const later = new Date(Date.now() + 86_400_000);
        const docs = [
            {
                id: 'doc-1',
                collection: 'pages',
                title: 'Later',
                scheduledAt: later,
                scheduledUnpublishAt: null,
            },
            {
                id: 'doc-2',
                collection: 'pages',
                title: 'Earlier',
                scheduledAt: earlier,
                scheduledUnpublishAt: null,
            },
        ];
        const db = createMockDB(docs);
        const entries = await getScheduleCalendar(new Date(), new Date(Date.now() + 86_400_000 * 2), db);
        expect(entries[0].title).toBe('Earlier');
        expect(entries[1].title).toBe('Later');
    });
});
//# sourceMappingURL=scheduling.test.js.map