knowledge-base-context-menu.spec.ts 3.05 KB
import { test, expect } from "@playwright/test";

/**
 * 缺陷 1008905 - 知识库右键菜单第一项显示 "KnowledgeBase.open" 而非 "打开"
 *
 * 验收标准:
 * 1. 右键文件夹,第一个菜单项显示「打开」,不显示翻译 key 字符串
 * 2. 右键文件,第一个菜单项显示「编辑」
 * 3. 菜单包含「重命名」「下载」「删除」
 */
test.describe("知识库右键菜单文字修复 (#1008905)", () => {
  test.beforeEach(async ({ page }) => {
    await page.goto("/#/app/knowledge-base");
    await page.waitForLoadState("domcontentloaded");
  });

  test("右键菜单第一项不应显示翻译 key 字符串", async ({ page }) => {
    // 等待文件列表加载
    const fileList = page.locator(".el-table__body-wrapper, .file-list-table");
    await fileList.waitFor({ timeout: 15000 }).catch(() => {});

    // 查找任意一行(文件夹或文件均可)
    const firstRow = page.locator(".el-table__row").first();
    const rowCount = await page.locator(".el-table__row").count();
    if (rowCount === 0) {
      test.skip(true, "没有文件行,跳过右键菜单测试");
      return;
    }

    // 右键触发上下文菜单
    await firstRow.click({ button: "right" });
    const contextMenu = page.locator(".context-menu");
    await contextMenu.waitFor({ timeout: 5000 });

    // 核心断言:第一项文本不能包含 "KnowledgeBase."(翻译 key 泄露)
    const firstItem = contextMenu.locator(".context-menu-item").first();
    const firstItemText = await firstItem.innerText();
    expect(firstItemText).not.toContain("KnowledgeBase.");
    expect(firstItemText.trim()).not.toBe("");

    // 第一项应为「打开」或「编辑」(中文)
    const validLabels = ["打开", "编辑", "Open", "Edit"];
    const isValid = validLabels.some((label) => firstItemText.includes(label));
    expect(isValid, `菜单第一项应为「打开」或「编辑」,实际为:「${firstItemText.trim()}」`).toBe(true);
  });

  test("右键文件夹菜单包含完整4个选项", async ({ page }) => {
    const rowCount = await page.locator(".el-table__row").count();
    if (rowCount === 0) {
      test.skip(true, "没有文件行,跳过");
      return;
    }

    // 找文件夹行右键
    const folderRow = page.locator(".el-table__row").filter({
      has: page.locator("td").filter({ hasText: "folder" }),
    }).first();

    const folderCount = await folderRow.count();
    if (folderCount === 0) {
      test.skip(true, "没有文件夹行,跳过");
      return;
    }

    await folderRow.click({ button: "right" });
    const contextMenu = page.locator(".context-menu");
    await contextMenu.waitFor({ timeout: 5000 });

    const items = contextMenu.locator(".context-menu-item");
    const count = await items.count();
    expect(count).toBe(4);

    const texts = await items.allInnerTexts();
    const allText = texts.join(" ");
    expect(allText).toContain("打开");
    expect(allText).toContain("重命名");
    expect(allText).toContain("下载");
    expect(allText).toContain("删除");
  });
});