Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

2.7 KB
Raw Blame
import { Page } from "@playwright/test";
import { LoginPage } from "../pages/login.page";
import { skipOnboardingIfPresent } from "./onboarding";

type TestUser = {
  email: string;
  password: string;
  id?: string;
};

/**
 * Utility functions for signin/authentication tests
 */
export class SigninUtils {
  constructor(
    private page: Page,
    private loginPage: LoginPage,
  ) {}

  /**
   * Perform login and verify success
   */
  async loginAndVerify(testUser: TestUser): Promise<void> {
    console.log(`πŸ” Logging in as: ${testUser.email}`);

    await this.page.goto("/login");
    await this.loginPage.login(testUser.email, testUser.password);

    // Wait for redirect β€” could land on /onboarding, /marketplace, or /copilot
    await this.page.waitForURL(
      (url: URL) =>
        /\/(onboarding|marketplace|copilot|home|library)/.test(url.pathname),
      { timeout: 15000 },
    );

    // Skip onboarding if present
    await skipOnboardingIfPresent(this.page, "/marketplace");

    // Verify profile menu is visible (user is authenticated)
    await this.page.getByTestId("profile-popout-menu-trigger").waitFor({
      state: "visible",
      timeout: 5000,
    });

    console.log("βœ… Login successful");
  }

  /**
   * Perform logout and verify success
   */
  async logoutAndVerify(): Promise<void> {
    console.log("πŸšͺ Logging out...");

    // Open profile menu
    await this.page.getByTestId("profile-popout-menu-trigger").click();

    // Wait for menu to be visible
    await this.page.getByRole("button", { name: "Log out" }).waitFor({
      state: "visible",
      timeout: 5000,
    });

    // Click logout
    await this.page.getByRole("button", { name: "Log out" }).click();

    // Verify we're back on login page
    await this.page.waitForURL("/login");

    console.log("βœ… Logout successful");
  }

  /**
   * Complete authentication cycle: login -> logout -> login
   */
  async fullAuthenticationCycle(testUser: TestUser): Promise<void> {
    console.log("πŸ”„ Starting full authentication cycle...");

    // First login
    await this.loginAndVerify(testUser);

    // Logout
    await this.logoutAndVerify();

    // Login again
    await this.loginAndVerify(testUser);

    console.log("βœ… Full authentication cycle completed");
  }

  /**
   * Verify user is on marketplace and authenticated
   */
  async verifyAuthenticated(): Promise<void> {
    await this.page.waitForURL("/marketplace");
    await this.page.getByTestId("profile-popout-menu-trigger").waitFor({
      state: "visible",
      timeout: 5000,
    });
  }

  /**
   * Verify user is on login page (not authenticated)
   */
  async verifyNotAuthenticated(): Promise<void> {
    await this.page.waitForURL("/login");
  }
}