File size: 4,207 Bytes
05c5ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { test as setup, expect } from "@playwright/test";
import * as fs from "node:fs";
import { TEST_USERS } from "../constants/test-users";
import type { Page } from "@playwright/test";
export async function selectModel(
  page: Page,
  providerModel: string,
): Promise<void> {
  const [provider, modelName] = providerModel.split("/");

  if (!provider || !modelName) {
    throw new Error(
      `Invalid model format: ${providerModel}. Expected format: provider/modelName`,
    );
  }

  // Open model selector
  await page.getByTestId("model-selector-button").click();

  // Wait for popover to open
  await expect(page.getByTestId("model-selector-popover")).toBeVisible();

  // Find the specific model option
  const modelOption = page.getByTestId(`model-option-${provider}-${modelName}`);

  // Check if the model exists
  await expect(modelOption).toBeVisible();

  // Click on the model option
  await modelOption.click();

  // Wait for popover to close
  await expect(page.getByTestId("model-selector-popover")).not.toBeVisible();

  // Verify the model was selected
  const selectedModel = await page
    .getByTestId("selected-model-name")
    .textContent();
  expect(selectedModel).toBe(modelName);
}

export async function selectDefaultModel(page: Page) {
  const defaultModel = process.env.E2E_DEFAULT_MODEL;
  if (defaultModel) {
    await selectModel(page, defaultModel);
  }
}

async function signInViaUi(
  page: Page,
  { email, password }: { email: string; password: string },
) {
  await page.goto("/sign-in");

  // Sign in with the seeded editor user
  await page.locator("#email").fill(email);
  await page.locator("#password").fill(password);
  await page.getByRole("button", { name: "Sign in", exact: true }).click();

  // Wait for redirect after successful login
  await page.waitForURL(
    (url) => {
      const urlStr = url.toString();
      return !urlStr.includes("/sign-in") && !urlStr.includes("/sign-up");
    },
    { timeout: 10000 },
  );
}

setup.beforeAll(async () => {
  fs.mkdirSync("tests/.auth", { recursive: true });
});

// Login with already-seeded admin user and save auth state
setup("create admin auth state", async ({ page }) => {
  console.log("πŸ” Creating admin auth state...");

  // Login as the pre-seeded admin user
  await signInViaUi(page, {
    email: TEST_USERS.admin.email,
    password: TEST_USERS.admin.password,
  });

  // Save admin auth state
  await page.context().storageState({ path: TEST_USERS.admin.authFile });
  expect(fs.existsSync(TEST_USERS.admin.authFile)).toBeTruthy();
});

// Login with already-seeded editor user and save auth state
setup("create editor auth state", async ({ page }) => {
  console.log("πŸ” Creating editor auth state...");
  await signInViaUi(page, {
    email: TEST_USERS.editor.email,
    password: TEST_USERS.editor.password,
  });
  // await selectDefaultModel(page); // Skipping - model may not be available

  // Save editor user auth state
  await page.context().storageState({ path: TEST_USERS.editor.authFile });
  expect(fs.existsSync(TEST_USERS.editor.authFile)).toBeTruthy();
});

// Login with already-seeded editor user and save auth state
setup("create editor2 auth state", async ({ page }) => {
  console.log("πŸ” Creating editor auth state...");
  await signInViaUi(page, {
    email: TEST_USERS.editor2.email,
    password: TEST_USERS.editor2.password,
  });
  // await selectDefaultModel(page); // Skipping - model may not be available

  // Save editor user auth state
  await page.context().storageState({ path: TEST_USERS.editor2.authFile });
  expect(fs.existsSync(TEST_USERS.editor2.authFile)).toBeTruthy();
});

// Login with already-seeded regular user and save auth state
setup("create regular user auth state", async ({ page }) => {
  console.log("πŸ” Creating regular user auth state...");
  await signInViaUi(page, {
    email: TEST_USERS.regular.email,
    password: TEST_USERS.regular.password,
  });
  // await selectDefaultModel(page); // Skipping - model may not be available

  // Save regular user auth state
  await page.context().storageState({ path: TEST_USERS.regular.authFile });
  expect(fs.existsSync(TEST_USERS.regular.authFile)).toBeTruthy();
});