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

test.describe("流水：查看、编辑和删除", () => {
  test("01-流水列表显示账目并可进入详情", async ({ page }) => {
    await mockApi(page, { initialEntries: [makeEntry()] });
    await 登录(page);
    await expect(page.getByText("午餐")).toBeVisible();
    await page.getByText("午餐").click();
    await expect(page).toHaveURL(/\/entries\/entry-lunch$/);
    await expect(page.getByText("条目明细")).toBeVisible();
  });

  test("02-修改备注后保存，刷新后仍保留", async ({ page }) => {
    await mockApi(page, { initialEntries: [makeEntry()] });
    await 登录(page);
    await page.getByText("午餐").click();
    await page.getByLabel("备注").fill("修改后的午餐");
    await page.getByRole("button", { name: "保存修改" }).click();
    await expect(page.getByRole("status")).toContainText("修改已保存");
    await page.reload();
    await expect(page.getByLabel("备注")).toHaveValue("修改后的午餐");
  });

  test("03-删除时要求确认从所有关联账本删除", async ({ page }) => {
    await mockApi(page, { initialEntries: [makeEntry()] });
    await 登录(page);
    await page.getByText("午餐").click();
    page.once("dialog", (dialog) => {
      expect(dialog.message()).toContain("关联的全部账本");
      void dialog.accept();
    });
    await page.getByRole("button", { name: "从所有关联账本删除" }).click();
    await expect(page).toHaveURL(/\/$/);
    await expect(page.getByText("午餐")).not.toBeVisible();
  });
});
