import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import { describe, expect, it } from 'vitest';

import { listAllContent } from '../scripts/walk-content';

const HERE = path.dirname(fileURLToPath(import.meta.url));
const MANIFEST = path.resolve(HERE, '..', 'app', 'content', 'frontmatter.json');

/**
 * `lib/content/loader.ts` imports `app/content/frontmatter.json` synchronously
 * to resolve page titles without loading any markdown body. The file is
 * generated by `scripts/build-search-index.ts` (run on `dev`/`build`) and
 * committed, so this test guards against it drifting from the on-disk
 * frontmatter — edit a title without regenerating and CI fails here.
 */
describe('content frontmatter manifest', () => {
  it('matches the on-disk frontmatter (regenerate via build-search-index)', async () => {
    const records = await listAllContent();
    const expected: Record<
      string,
      { slug: string; locale: string; frontmatter: Record<string, unknown> }
    > = {};
    for (const record of records) {
      expected[`${record.locale}:${record.slug}`] = {
        slug: record.slug,
        locale: record.locale,
        frontmatter: record.frontmatter,
      };
    }
    const committed = JSON.parse(fs.readFileSync(MANIFEST, 'utf8'));
    expect(committed).toEqual(expected);
  });
});
