import { describe, expect, it } from 'vitest';
import { applyPagesQuery, groupPages } from '../../lib/pages-service.js';
/**
 * Unit coverage for the Pages filtering/grouping logic. These are the
 * pure functions the PagesListView depends on — kept network-free so the
 * search/tag/status/sort/group contracts are locked independent of the
 * REST layer.
 */
function page(partial) {
    return {
        id: partial.id,
        title: partial.title,
        path: partial.path ?? `/${partial.title.toLowerCase()}`,
        slug: partial.slug ?? partial.title.toLowerCase(),
        parentPageId: null,
        tags: partial.tags ?? [],
        primaryTagId: partial.primaryTagId ?? partial.tags?.[0] ?? null,
        authorId: partial.authorId ?? 'u1',
        authorName: partial.authorName ?? 'Maria K.',
        authorInitials: 'MK',
        status: partial.status ?? 'PUBLISHED',
        publishDate: null,
        scheduledDate: null,
        lastModified: partial.lastModified ?? '2026-05-01',
        seoScore: partial.seoScore ?? null,
        seoTitle: null,
        seoDescription: null,
        templateId: null,
        isHomepage: partial.isHomepage ?? false,
        isSystemPage: false,
        createdAt: '2026-01-01',
        updatedAt: partial.lastModified ?? '2026-05-01',
    };
}
const TAGS = [
    {
        id: 'core',
        docId: 'd1',
        name: 'Core',
        slug: 'core',
        description: null,
        color: 'purple',
        pageCount: 0,
        sortOrder: 0,
        active: true,
        createdAt: null,
        updatedAt: null,
    },
    {
        id: 'services',
        docId: 'd2',
        name: 'Services',
        slug: 'services',
        description: null,
        color: 'green',
        pageCount: 0,
        sortOrder: 1,
        active: true,
        createdAt: null,
        updatedAt: null,
    },
];
const PAGES = [
    page({ id: '1', title: 'About', tags: ['core'], status: 'PUBLISHED', seoScore: 88 }),
    page({
        id: '2',
        title: 'Services',
        tags: ['core', 'services'],
        status: 'PUBLISHED',
        seoScore: 85,
    }),
    page({
        id: '3',
        title: 'SEO Services',
        tags: ['services'],
        status: 'DRAFT',
        seoScore: 44,
        authorId: 'u2',
        authorName: 'Sarah M.',
    }),
    page({ id: '4', title: 'Home', tags: [], status: 'PUBLISHED', isHomepage: true, seoScore: 92 }),
];
describe('applyPagesQuery', () => {
    it('defaults to title ascending', () => {
        const out = applyPagesQuery(PAGES, {});
        expect(out.map((p) => p.title)).toEqual(['About', 'Home', 'SEO Services', 'Services']);
    });
    it('filters by status', () => {
        const out = applyPagesQuery(PAGES, { status: 'DRAFT' });
        expect(out.map((p) => p.id)).toEqual(['3']);
    });
    it('filters by tag with OR semantics', () => {
        const out = applyPagesQuery(PAGES, { tagIds: ['services'] });
        expect(out.map((p) => p.id).sort()).toEqual(['2', '3']);
    });
    it('matches multiple selected tags (union)', () => {
        const out = applyPagesQuery(PAGES, { tagIds: ['core', 'services'] });
        expect(out.map((p) => p.id).sort()).toEqual(['1', '2', '3']);
    });
    it('searches title, path, and tags', () => {
        expect(applyPagesQuery(PAGES, { search: 'seo' }).map((p) => p.id)).toEqual(['3']);
        expect(applyPagesQuery(PAGES, { search: 'core' })
            .map((p) => p.id)
            .sort()).toEqual(['1', '2']);
    });
    it('sorts by seo descending', () => {
        const out = applyPagesQuery(PAGES, { sortBy: 'seo', sortDirection: 'desc' });
        expect(out[0]?.id).toBe('4'); // Home, 92
    });
});
describe('groupPages', () => {
    it('returns a single bucket for groupBy none', () => {
        const groups = groupPages(PAGES, 'none', TAGS);
        expect(groups).toHaveLength(1);
        expect(groups[0]?.pages).toHaveLength(4);
    });
    it('groups by primary tag and bucket-labels untagged pages', () => {
        const groups = groupPages(PAGES, 'tag', TAGS);
        const byKey = new Map(groups.map((g) => [g.key, g]));
        // About + Services have primary tag 'core'; SEO Services 'services';
        // Home has no tags → Untagged.
        expect(byKey
            .get('core')
            ?.pages.map((p) => p.id)
            .sort()).toEqual(['1', '2']);
        expect(byKey.get('services')?.pages.map((p) => p.id)).toEqual(['3']);
        expect(byKey.get('__untagged')?.pages.map((p) => p.id)).toEqual(['4']);
    });
    it('never duplicates a page across tag groups', () => {
        const groups = groupPages(PAGES, 'tag', TAGS);
        const total = groups.reduce((n, g) => n + g.pages.length, 0);
        expect(total).toBe(PAGES.length);
    });
    it('groups by status', () => {
        const groups = groupPages(PAGES, 'status', TAGS);
        const published = groups.find((g) => g.key === 'PUBLISHED');
        expect(published?.pages).toHaveLength(3);
    });
});
//# sourceMappingURL=pages-service.test.js.map