import { test, expect } from "@playwright/test";

const randomString = () => {
  const randomStr = Math.random().toString(36).substring(2, 10);
  return `${randomStr}`;
};

test.describe("register and login", () => {
  test.beforeEach(async ({ page }) => {
    await page.goto("http://localhost/register"); // Replace with the actual registration page URL
  });

  test("should register and login successfully", async ({ page }) => {
    const username = randomString();
    const password = "password123";

    await page.getByLabel("Name").fill(username);
    await page.getByLabel("Email").fill(username + "@example.com");
    await page.getByLabel("Password", { exact: true }).fill(password);
    await page.getByLabel("Confirm Password").fill(password);
    const element = page.locator(".v-input--selection-controls__ripple");

    await element.click();
    await page.getByRole("button", { name: "Register" }).click();

    // Check for success message
    await expect(
      page.getByText("Account created successfully. You can login now.")
    ).toBeVisible();

    // Navigate to login page and log in
    await page.goto("http://localhost/login"); // Replace with the actual login page URL
    await page.getByLabel("Email").fill(username + "@example.com");
    await page.getByLabel("Password").fill(password);

    await page.getByRole("button", { name: "Login" }).click();

    // Expect the URL to change to the home page
    await expect(page).toHaveURL("http://localhost/home"); // Replace with the actual home page URL

    // Alternatively, check for a specific element that indicates successful login
    await expect(page.getByRole("button", { name: username })).toBeVisible();
  });
});
