import { test, expect } from "./fixtures";
import { loginAsTestUser } from "./auth-helpers";

test.describe("Authenticated pages", () => {
  test("redirects unauthenticated users to login page", async ({ page }) => {
    const response = await page.goto("/editor/dashboard");
    // Should redirect to login
    expect(response?.url()).toContain("/users/log-in");
  });

  test("can log in and access the editor dashboard", async ({ page }) => {
    await loginAsTestUser(page);

    // After login, we should be on the home page
    await expect(page).toHaveURL("/");
    await expect(page.locator("body")).not.toBeEmpty();
  });

  test("can log out", async ({ page }) => {
    await loginAsTestUser(page);

    // Now log out
    await page.goto("/users/log-out");

    // Should redirect to home
    await expect(page).toHaveURL("/");

    // Try accessing an authenticated page
    await page.goto("/editor/dashboard");
    // Should redirect to login
    await expect(page).toHaveURL(/\/users\/log-in/);
  });
});