import { jsx as _jsx } from "react/jsx-runtime";
// @vitest-environment happy-dom
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen, within } from '@testing-library/react';
/**
 * Regression for the post-list navigation bug: CollectionList must open
 * post-type rows in the section editor (`/posts/:type/:id/edit`), not the
 * generic `/:collection/:id` route — which 404s when the slug is `posts`.
 */
vi.mock('../../lib/api.js', () => ({
    cmsApi: vi.fn(async () => ({ data: {}, status: 200 })),
    ensureCsrfToken: vi.fn(async () => { }),
}));
let listDocs = [];
vi.mock('../../lib/useApiData.js', () => ({
    useApiData: () => ({
        data: { docs: listDocs, total: listDocs.length },
        loading: false,
        error: null,
        refetch: vi.fn(),
    }),
}));
const { CollectionList } = await import('../../views/CollectionList.js');
const POST_CONFIG = {
    collections: {
        posts: {
            slug: 'posts',
            type: 'post',
            labels: { singular: 'Post', plural: 'Posts' },
            fields: { title: { type: 'text', label: 'Title' } },
        },
    },
};
beforeEach(() => {
    listDocs = [];
});
describe('CollectionList navigation (post collection)', () => {
    it('opens a post row in the section editor, not the generic route', () => {
        listDocs = [{ id: '1', title: 'Hello', status: 'PUBLISHED', updatedAt: '2026-06-01T00:00:00Z' }];
        const onNavigate = vi.fn();
        render(_jsx(CollectionList, { collectionSlug: "posts", config: POST_CONFIG, onNavigate: onNavigate }));
        fireEvent.click(within(screen.getByRole('table')).getByRole('button', { name: 'Hello' }));
        expect(onNavigate).toHaveBeenCalledWith('/posts/posts/1/edit');
        expect(onNavigate).not.toHaveBeenCalledWith('/posts/1');
    });
    it('starts a new post in the section editor', () => {
        const onNavigate = vi.fn();
        render(_jsx(CollectionList, { collectionSlug: "posts", config: POST_CONFIG, onNavigate: onNavigate }));
        // The empty-state CTA ("New Post") creates a document.
        fireEvent.click(screen.getByRole('button', { name: /new post/i }));
        expect(onNavigate).toHaveBeenCalledWith('/posts/posts/new');
    });
});
//# sourceMappingURL=collection-list-nav.render.test.js.map