// tests/e2e/deployment.e2e.test.ts
import { test, expect } from '@playwright/test';

test.describe('Deployment End-to-End Tests', () => {

  // This test requires a special setup that runs the application with specific
  // environment variables for the frontend and backend to simulate a real deployment.
  // The test would be executed against the deployed environment.

  test('should load the application on a custom domain without CORS errors', async ({ page }) => {
    // Step 1: Before running this test, the application must be started
    // with docker-compose, using .env files that point to the custom domains.
    // For example:
    // In frontend/.env: REACT_APP_API_URL=http://backend.unisono.test
    // In backend/.env: CORS_ORIGIN=http://frontend.unisono.test
    // And the local machine must resolve these domains (e.g., via /etc/hosts).

    const frontendUrl = 'http://frontend.unisono.test:3000'; // Example URL

    // Step 2: Capture console errors, specifically looking for CORS issues.
    const consoleErrors: string[] = [];
    page.on('console', msg => {
      if (msg.type() === 'error') {
        consoleErrors.push(msg.text());
      }
    });

    // Step 3: Navigate to the frontend URL.
    await page.goto(frontendUrl);

    // Step 4: Interact with the page to trigger API calls.
    // In this case, just loading the login page should be enough to
    // confirm the frontend can potentially connect to the backend.
    // We will check for the login page content.
    await expect(page.locator('h1', { hasText: 'Enter Passphrase' })).toBeVisible();

    // Step 5: Assert that no CORS errors were logged to the console.
    const corsError = consoleErrors.find(error => error.includes('Cross-Origin Resource Sharing') || error.includes('CORS'));
    expect(corsError).toBeUndefined();

    // Optional: Further interaction to test a real API call after login.
    // This would require a valid passphrase for the test environment.
    // await page.fill('#passphrase', process.env.TEST_AUTH_PASSPHRASE);
    // await page.click('button[type="submit"]');
    // await expect(page.locator('h1', { hasText: 'Create New Session' })).toBeVisible();
    // expect(corsError).toBeUndefined(); // Re-assert after API calls
  });
});
