import { test, expect } from '@playwright/test';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = dirname(fileURLToPath(import.meta.url));

async function loadFile(page: import('@playwright/test').Page, fixture: string) {
  await page.locator('#file-input').setInputFiles(resolve(__dirname, `../fixtures/${fixture}`));
  await page.waitForTimeout(3000);
}

test.describe('Filter Execution via Menu', () => {
  test('Laplacian Smooth via menu dialog', async ({ page }) => {
    await page.goto('/');
    await page.locator('canvas').first().waitFor({ state: 'visible', timeout: 10000 });
    await loadFile(page, 'cube.obj');

    await page.click('[data-testid="menu-Filters"]');
    await page.locator('[data-testid="menu-bar"]').locator('text=Smoothing').click();
    await page.click('text=Laplacian Smooth...');

    await expect(page.locator('[data-testid="filter-dialog"]')).toBeVisible();
    await page.click('[data-testid="filter-apply"]');
    await page.waitForTimeout(1000);
    await page.click('[data-testid="filter-close"]');

    await expect(page.locator('[data-testid="status-message"]')).toContainText('laplacian_smooth');
  });

  test('parameterless filter runs directly from menu', async ({ page }) => {
    await page.goto('/');
    await page.locator('canvas').first().waitFor({ state: 'visible', timeout: 10000 });
    await loadFile(page, 'cube.obj');

    await page.click('[data-testid="menu-Filters"]');
    await page.locator('[data-testid="menu-bar"]').locator('text=Normals').click();
    await page.click('text=Recompute Face Normals');

    await expect(page.locator('[data-testid="filter-dialog"]')).not.toBeVisible();
    await page.waitForTimeout(1000);
    await expect(page.locator('[data-testid="status-message"]')).toContainText('compute_normals');
  });
});
