import { test, expect } from "@playwright/test";
import { mockApi, 登录, 用户 } from "../测试夹具";

test.describe("账号：登录与退出", () => {
  test("01-错误密码显示统一错误且不进入应用", async ({ page }) => {
    await mockApi(page);
    await page.goto("/login");
    await page.getByLabel("姓名").fill(用户.主用户.name);
    await page.locator('input[autocomplete="current-password"]').fill("wrong-password");
    await page.getByRole("button", { name: "登录" }).click();
    await expect(page.getByRole("alert")).toHaveText("姓名或密码错误");
    await expect(page).toHaveURL(/\/login$/);
  });

  test("02-正确登录后刷新仍保持会话", async ({ page }) => {
    await mockApi(page);
    await 登录(page);
    await expect(page.getByText("个人账本")).toBeVisible();
    await page.reload();
    await expect(page).toHaveURL(/\/$/);
    await expect(page.getByLabel("账本流水")).toBeVisible();
  });

  test("03-退出登录后受保护页面回到登录页", async ({ page }) => {
    await mockApi(page);
    await 登录(page);
    await page.getByRole("link", { name: /我的/ }).click();
    await page.getByRole("button", { name: "退出登录" }).click();
    await expect(page).toHaveURL(/\/login/);
    await page.goto("/stats");
    await expect(page).toHaveURL(/\/login/);
  });
});
