plugin-extension-start-draft.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // PLM-EXT-H2:扩展经 workbench.startDraft 开预填草稿(docs/plugins/extension-host.md §18.1 H2、§18.4 GitLab 第 3 步)。
  2. // 隔离 HOME:纯 JS fixture 扩展 e2e-ext-draft(test/e2e/fixtures/fs/plugins/plugin-extension-start-draft/)经
  3. // plugins.dirs 加载。composer.status 条目 draft 点击执行命令,命令调 ctx.workbench.startDraft。
  4. // 断言:新草稿的输入框预填了文字、没有发送(没有用户消息、文字仍在输入框里)、任务列表没有新任务;
  5. // 第二次显式传工作区也一样;超长与空白文字报 invalid-input。不需要模型。
  6. // 运行时证据:Main 日志([extensions] / [extension-slots] / fixture 的 E2E_START_DRAFT*)、输入框与任务列表 DOM。
  7. import { cp, mkdir, writeFile } from "node:fs/promises";
  8. import { join, resolve } from "node:path";
  9. import { DEFAULT_WORKSPACE, clearAppData, getE2EAppDataPaths } from "#e2e/helpers/desktop-app";
  10. import { restartIntoWorkspacePreservingProfile } from "#e2e/helpers/model-provider-restart-runtime";
  11. import {
  12. clickSlot,
  13. createExtensionLog,
  14. waitForSlot,
  15. writePluginDirsConfig,
  16. } from "#e2e/helpers/extension-host-views";
  17. import {
  18. getV4ComposerText,
  19. getV4Messages,
  20. prepareV4ConversationE2E,
  21. setV4ComposerText,
  22. } from "#e2e/helpers/v4-conversation";
  23. import {
  24. capturePluginManualReview,
  25. requirePluginManualReview,
  26. setPluginPresentation,
  27. } from "#e2e/helpers/plugin-management-lifecycle";
  28. const CASE_ID = "PLM-EXT-H2";
  29. // WDIO 以 packages/desktop 为 cwd;按 cwd 解析,晋级移动 spec 后路径不变。
  30. const FIXTURE_ROOT = resolve(
  31. process.cwd(),
  32. "test/e2e/fixtures/fs/plugins/plugin-extension-start-draft",
  33. );
  34. const PLUGIN = "e2e-ext-draft";
  35. const PLUGIN_ID = `${PLUGIN}@inline`;
  36. const SLOT_ITEM = `${PLUGIN_ID}/draft`;
  37. const DRAFT_TEXT =
  38. "E2E_START_DRAFT review https://gitlab.example.com/group/project/-/merge_requests/7";
  39. const evidence: Record<string, unknown> = {};
  40. const log = createExtensionLog();
  41. async function seedFixture(): Promise<void> {
  42. const target = join(getE2EAppDataPaths().homeDir, "fixtures", "extension-start-draft");
  43. await mkdir(target, { recursive: true });
  44. await cp(join(FIXTURE_ROOT, PLUGIN), join(target, PLUGIN), { recursive: true, force: true });
  45. await writePluginDirsConfig([join(target, PLUGIN)], [PLUGIN_ID]);
  46. }
  47. /** 任务列表里的任务行数(草稿不在列表里,发送后才有任务)。 */
  48. async function countTaskItems(): Promise<number> {
  49. return browser.execute(() => document.querySelectorAll('[data-testid^="task-item-"]').length);
  50. }
  51. async function waitForComposerText(expected: string, message: string): Promise<void> {
  52. let latest: string | null = null;
  53. await browser.waitUntil(
  54. async () => {
  55. latest = await getV4ComposerText();
  56. return latest === expected;
  57. },
  58. { timeout: 30000, interval: 300, timeoutMsg: `${message}: ${JSON.stringify(latest)}` },
  59. );
  60. }
  61. describe("PLM-EXT-H2 workbench.startDraft 开预填草稿", () => {
  62. after(async () => {
  63. await writeFile(
  64. join(process.env.ZCODE_E2E_ARTIFACT_DIR ?? process.cwd(), "plm-ext-h2-evidence.json"),
  65. JSON.stringify(evidence, null, 2),
  66. ).catch(() => undefined);
  67. await clearAppData();
  68. });
  69. it("命令开出预填草稿:不发送、不新建任务;入参不合规时报 invalid-input", async function () {
  70. this.timeout(600000);
  71. requirePluginManualReview();
  72. await seedFixture();
  73. await restartIntoWorkspacePreservingProfile(DEFAULT_WORKSPACE, {
  74. afterElectronProcessExit: () => log.mark(),
  75. });
  76. await prepareV4ConversationE2E();
  77. await setPluginPresentation({ locale: "zh-CN", theme: "zai-light", width: 1400, height: 900 });
  78. await waitForSlot(SLOT_ITEM, (slot) => slot.text.includes("E2E draft"), "draft slot");
  79. await setV4ComposerText("");
  80. const tasksBefore = await countTaskItems();
  81. evidence.tasksBefore = tasksBefore;
  82. // (a) 当前工作区:输入框预填文字,不发送,任务列表不变。
  83. const linesBeforeFirst = (await log.lines()).length;
  84. await clickSlot(SLOT_ITEM);
  85. evidence.firstLog = await log.waitFor(
  86. (line) => line.includes("E2E_START_DRAFT ") && line.includes("click=1"),
  87. "the first startDraft call should finish",
  88. );
  89. expect(String(evidence.firstLog)).toContain("E2E_START_DRAFT ok click=1 workspace=current");
  90. await waitForComposerText(`${DRAFT_TEXT} (click 1)`, "the new draft should be prefilled");
  91. evidence.firstWindowLogs = (await log.lines())
  92. .slice(linesBeforeFirst)
  93. .filter((line) => line.includes("workbench.startDraft"));
  94. expect(JSON.stringify(evidence.firstWindowLogs)).toContain(
  95. `workbench.startDraft acked plugin=${PLUGIN_ID} ok=true`,
  96. );
  97. // 日志不带文字本身。
  98. expect(JSON.stringify(evidence.firstWindowLogs)).not.toContain("E2E_START_DRAFT review");
  99. expect(await getV4Messages("user")).toEqual([]);
  100. expect(await countTaskItems()).toBe(tasksBefore);
  101. await capturePluginManualReview(CASE_ID, "draft-prefilled-zh-CN-zai-light");
  102. // (b) 入参不合规:超长、空白都在扩展进程内报 invalid-input,输入框不变。
  103. evidence.tooLongLog = await log.waitFor(
  104. (line) => line.includes("E2E_START_DRAFT_INVALID too-long"),
  105. "the too-long text should be rejected",
  106. );
  107. expect(String(evidence.tooLongLog)).toContain("code=invalid-input");
  108. evidence.blankLog = await log.waitFor(
  109. (line) => line.includes("E2E_START_DRAFT_INVALID blank"),
  110. "the blank text should be rejected",
  111. );
  112. expect(String(evidence.blankLog)).toContain("code=invalid-input");
  113. expect(await getV4ComposerText()).toBe(`${DRAFT_TEXT} (click 1)`);
  114. // (c) 显式传工作区(命令作用域的工作区):同样预填、不发送。
  115. await setV4ComposerText("");
  116. await clickSlot(SLOT_ITEM);
  117. evidence.secondLog = await log.waitFor(
  118. (line) => line.includes("E2E_START_DRAFT ") && line.includes("click=2"),
  119. "the second startDraft call should finish",
  120. );
  121. expect(String(evidence.secondLog)).toContain("E2E_START_DRAFT ok click=2 workspace=explicit");
  122. await waitForComposerText(
  123. `${DRAFT_TEXT} (click 2)`,
  124. "the explicit-workspace draft should be prefilled",
  125. );
  126. expect(await getV4Messages("user")).toEqual([]);
  127. expect(await countTaskItems()).toBe(tasksBefore);
  128. evidence.tasksAfter = await countTaskItems();
  129. await setPluginPresentation({ locale: "en-US", theme: "zai-dark", width: 1400, height: 900 });
  130. await capturePluginManualReview(CASE_ID, "draft-prefilled-en-US-zai-dark");
  131. });
  132. });