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

test.describe('Authentication End-to-End Tests', () => {
  test('should allow successful SPA access after correct passphrase entry', async ({ page }) => {
    // Assuming the app is running on http://localhost:3000
    await page.goto('http://localhost:3000');

    // Expect to be on the login page
    await expect(page.locator('h1', { hasText: 'Enter Passphrase' })).toBeVisible();

    // Fill in the passphrase (replace with actual passphrase from .env)
    await page.fill('#passphrase', 'YOUR_PASSPHRASE_HERE'); // Placeholder

    // Click the submit button
    await page.click('button[type="submit"]');

    // Expect to be redirected to the SPA content (e.g., CreateSession page)
    await expect(page.locator('h1', { hasText: 'Create New Session' })).toBeVisible();

    // Verify session token is stored (e.g., in local storage)
    const sessionToken = await page.evaluate(() => localStorage.getItem('sessionToken'));
    expect(sessionToken).not.toBeNull();
    expect(sessionToken).not.toBe('');
  });

  test('should deny SPA access and show error for incorrect passphrase entry', async ({ page }) => {
    await page.goto('http://localhost:3000');

    // Expect to be on the login page
    await expect(page.locator('h1', { hasText: 'Enter Passphrase' })).toBeVisible();

    // Fill in an incorrect passphrase
    await page.fill('#passphrase', 'incorrect-passphrase');

    // Click the submit button
    await page.click('button[type="submit"]');

    // Expect to remain on the login page and see an error message
    await expect(page.locator('h1', { hasText: 'Enter Passphrase' })).toBeVisible();
    await expect(page.locator('.MuiAlert-message', { hasText: 'Authentication failed' })).toBeVisible(); // Assuming the error message is "Authentication failed"
    
    // Verify session token is NOT stored
    const sessionToken = await page.evaluate(() => localStorage.getItem('sessionToken'));
    expect(sessionToken).toBeNull();
  });
});
