import { describe, expect, it } from 'vitest';
import { buildPostsOrderBy, buildPostsWhere, extractSeoScore, resolveQueryCollections, } from '../query.js';
const ALLOWED = ['blog', 'news', 'case-studies'];
describe('resolveQueryCollections', () => {
    it('returns all allowed collections when no type or "all"', () => {
        expect(resolveQueryCollections({ allowedCollections: ALLOWED })).toEqual(ALLOWED);
        expect(resolveQueryCollections({ allowedCollections: ALLOWED, postType: 'all' })).toEqual(ALLOWED);
    });
    it('narrows to a single allowed type', () => {
        expect(resolveQueryCollections({ allowedCollections: ALLOWED, postType: 'news' })).toEqual([
            'news',
        ]);
    });
    it('collapses to empty for a forbidden/unknown type (no leakage)', () => {
        expect(resolveQueryCollections({ allowedCollections: ALLOWED, postType: 'secret' })).toEqual([]);
    });
});
describe('buildPostsWhere', () => {
    it('always scopes to the allowed collections and excludes soft-deleted', () => {
        const w = buildPostsWhere({ allowedCollections: ALLOWED });
        expect(w.collection).toEqual({ in: ALLOWED });
        expect(w.deletedAt).toBeNull();
        expect(w.status).toBeUndefined();
        expect(w.OR).toBeUndefined();
    });
    it('applies a valid status and ignores "all" / bogus statuses', () => {
        expect(buildPostsWhere({ allowedCollections: ALLOWED, status: 'PUBLISHED' }).status).toBe('PUBLISHED');
        expect(buildPostsWhere({ allowedCollections: ALLOWED, status: 'all' }).status).toBeUndefined();
        expect(buildPostsWhere({ allowedCollections: ALLOWED, status: 'BOGUS' }).status).toBeUndefined();
    });
    it('filters by author id', () => {
        expect(buildPostsWhere({ allowedCollections: ALLOWED, author: 'u1' }).createdById).toBe('u1');
        expect(buildPostsWhere({ allowedCollections: ALLOWED, author: 'all' }).createdById).toBeUndefined();
    });
    it('builds a case-insensitive OR search across title/slug/plainText', () => {
        const w = buildPostsWhere({ allowedCollections: ALLOWED, search: '  Hello  ' });
        expect(w.OR).toEqual([
            { title: { contains: 'Hello', mode: 'insensitive' } },
            { slug: { contains: 'Hello', mode: 'insensitive' } },
            { plainText: { contains: 'Hello', mode: 'insensitive' } },
        ]);
    });
});
describe('buildPostsOrderBy', () => {
    it('maps scalar sorts', () => {
        expect(buildPostsOrderBy('title', 'asc')).toEqual({ title: 'asc' });
        expect(buildPostsOrderBy('status', 'desc')).toEqual({ status: 'desc' });
        expect(buildPostsOrderBy('type', 'asc')).toEqual({ collection: 'asc' });
        expect(buildPostsOrderBy('date', 'desc')).toEqual({ updatedAt: 'desc' });
    });
    it('orders author by the related user name', () => {
        expect(buildPostsOrderBy('author', 'asc')).toEqual({ createdBy: { name: 'asc' } });
    });
    it('falls back to updatedAt for the non-DB-sortable seo field', () => {
        expect(buildPostsOrderBy('seo', 'desc')).toEqual({ updatedAt: 'desc' });
    });
    it('defaults to most-recently-updated', () => {
        expect(buildPostsOrderBy()).toEqual({ updatedAt: 'desc' });
    });
});
describe('extractSeoScore', () => {
    it('reads supported keys and rejects non-numbers', () => {
        expect(extractSeoScore({ seoScore: 82 })).toBe(82);
        expect(extractSeoScore({ metaScore: 50 })).toBe(50);
        expect(extractSeoScore({ seoScore: 'high' })).toBeNull();
        expect(extractSeoScore(null)).toBeNull();
        expect(extractSeoScore('nope')).toBeNull();
    });
});
//# sourceMappingURL=query.test.js.map