import { describe, expect, it } from 'vitest';
import { createPreviewAdapter, DEFAULT_PREVIEW_TOKEN_TTL_SECONDS, MAX_PREVIEW_TOKEN_TTL_SECONDS, } from '../preview/index.js';
const SECRET = 'a'.repeat(48);
function fakeDb(doc) {
    return {
        document: {
            findFirst: async () => doc,
        },
    };
}
describe('preview adapter', () => {
    it('defaults to a 7-day token lifetime', async () => {
        const adapter = createPreviewAdapter(SECRET, fakeDb(null));
        const session = await adapter.createPreviewSession('posts', 'doc-1');
        const ttlMs = session.expiresAt.getTime() - Date.now();
        // Allow a small window for the difference between now() inside the adapter
        // and our assertion clock; the default is exactly 7 days.
        expect(ttlMs).toBeGreaterThan((DEFAULT_PREVIEW_TOKEN_TTL_SECONDS - 5) * 1000);
        expect(ttlMs).toBeLessThanOrEqual(DEFAULT_PREVIEW_TOKEN_TTL_SECONDS * 1000 + 1000);
    });
    it('clamps a requested TTL above the maximum down to the ceiling', async () => {
        const adapter = createPreviewAdapter(SECRET, fakeDb(null));
        const session = await adapter.createPreviewSession('posts', 'doc-1', {
            ttlSeconds: 60 * 60 * 24 * 365,
        });
        const ttlMs = session.expiresAt.getTime() - Date.now();
        expect(ttlMs).toBeLessThanOrEqual(MAX_PREVIEW_TOKEN_TTL_SECONDS * 1000 + 1000);
        expect(ttlMs).toBeGreaterThan((MAX_PREVIEW_TOKEN_TTL_SECONDS - 5) * 1000);
    });
    it('clamps a tiny TTL up to the 60s floor so single-digit requests are usable', async () => {
        const adapter = createPreviewAdapter(SECRET, fakeDb(null));
        const session = await adapter.createPreviewSession('posts', 'doc-1', { ttlSeconds: 1 });
        const ttlMs = session.expiresAt.getTime() - Date.now();
        expect(ttlMs).toBeGreaterThan(59 * 1000);
    });
    it('round-trips collection + documentId + issuedBy through the JWT', async () => {
        const adapter = createPreviewAdapter(SECRET, fakeDb(null));
        const session = await adapter.createPreviewSession('blog-posts', 'doc-xyz', {
            issuedBy: 'user-42',
            ttlSeconds: 600,
        });
        const decoded = await adapter.validatePreviewToken(session.token);
        expect(decoded).not.toBeNull();
        expect(decoded?.collection).toBe('blog-posts');
        expect(decoded?.documentId).toBe('doc-xyz');
        expect(decoded?.issuedBy).toBe('user-42');
    });
    it('rejects tokens signed with a different secret', async () => {
        const adapter = createPreviewAdapter(SECRET, fakeDb(null));
        const otherAdapter = createPreviewAdapter('b'.repeat(48), fakeDb(null));
        const session = await otherAdapter.createPreviewSession('posts', 'doc-1');
        expect(await adapter.validatePreviewToken(session.token)).toBeNull();
    });
    it('rejects tokens issued with a different issuer claim', async () => {
        const adapter = createPreviewAdapter(SECRET, fakeDb(null));
        expect(await adapter.validatePreviewToken('not.a.real.jwt-shaped-thing')).toBeNull();
    });
    it('returns the document via getPreviewData when one exists', async () => {
        const doc = {
            id: 'doc-1',
            collection: 'posts',
            data: { title: 'Hello' },
            status: 'DRAFT',
        };
        const adapter = createPreviewAdapter(SECRET, fakeDb(doc));
        const session = await adapter.createPreviewSession('posts', 'doc-1');
        const data = await adapter.getPreviewData(session);
        expect(data).toMatchObject({ id: 'doc-1', collection: 'posts', status: 'DRAFT' });
    });
});
//# sourceMappingURL=preview.test.js.map