import { mkdirSync } from 'node:fs';
import { resolve } from 'node:path';
import { expect, test } from '@playwright/test';

const evidenceDirectory = resolve('test-results/evidence');

test.beforeAll(() => {
  mkdirSync(evidenceDirectory, { recursive: true });
});

test('2D DWG와 DXF 구동 증거를 캡처합니다', async ({ page }) => {
  await page.goto('http://127.0.0.1:5173/');

  await page.getByRole('button', { name: 'Sample DWG' }).click();
  await expect(page.locator('#status')).toContainText('7309 entities');
  await page.screenshot({
    path: resolve(evidenceDirectory, 'viewer2d-dwg.png'),
    fullPage: true,
  });

  await page.getByRole('button', { name: 'Sample DXF' }).click();
  await expect(page.locator('#status')).toContainText('6 entities');
  await page.screenshot({
    path: resolve(evidenceDirectory, 'viewer2d-dxf.png'),
    fullPage: true,
  });
});

for (const [label, fixture, output] of [
  ['GLB', 'Box.glb', 'viewer3d-three-r185-glb.png'],
  ['PLY', 'DoublePrecision.ply', 'viewer3d-three-r185-ply.png'],
] as const) {
  test(`Three.js r185 ${label} 구동 증거를 캡처합니다`, async ({ page }) => {
    await page.goto(`http://127.0.0.1:3333/?model=/samples/${fixture}`);
    await page.waitForFunction(
      () =>
        Boolean(
          (
            window as Window & {
              __viewer?: { current?: object };
            }
          ).__viewer?.current,
        ),
      undefined,
      { timeout: 20_000 },
    );
    await expect(page.locator('canvas')).toHaveCount(1);
    await page.screenshot({
      path: resolve(evidenceDirectory, output),
      fullPage: true,
    });
  });
}
