import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { ReactAdapter } from '../react.js';

describe('ReactAdapter Next.js metadata', () => {
  let tmpDir: string;

  beforeEach(() => {
    tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'web-dev-mcp-next-'));
  });

  afterEach(() => {
    fs.rmSync(tmpDir, { recursive: true, force: true });
  });

  it('reports app router and rsc for Next App Router projects', () => {
    fs.writeFileSync(
      path.join(tmpDir, 'package.json'),
      JSON.stringify({ dependencies: { react: '^18.0', next: '^14.0' } }),
    );
    fs.writeFileSync(path.join(tmpDir, 'next.config.mjs'), 'export default {}');
    fs.mkdirSync(path.join(tmpDir, 'app'));
    fs.writeFileSync(path.join(tmpDir, 'app', 'layout.tsx'), 'export default function Root({ children }) { return children; }');

    const described = new ReactAdapter(tmpDir).describe();
    expect(described.bundler).toBe('next');
    expect(described.router).toBe('app');
    expect(described.rsc).toBe(true);
  });

  it('reports pages router without rsc', () => {
    fs.writeFileSync(
      path.join(tmpDir, 'package.json'),
      JSON.stringify({ dependencies: { react: '^18.0', next: '^13.0' } }),
    );
    fs.mkdirSync(path.join(tmpDir, 'pages'));
    fs.writeFileSync(path.join(tmpDir, 'pages', 'index.tsx'), 'export default function Home() { return null; }');

    const described = new ReactAdapter(tmpDir).describe();
    expect(described.bundler).toBe('next');
    expect(described.router).toBe('pages');
    expect(described.rsc).toBe(false);
  });
});
