conversation-session-v4-bash-command-prefix-permission.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // BPR01/BPR02:Bash 项目始终允许使用 AST command prefix,而不是完整命令字符串。
  2. import { mkdir, rm, writeFile } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import { clearAppData } from "#e2e/helpers/desktop-app";
  5. import { resolveE2ERuntimePath } from "#e2e/helpers/e2e-runtime-paths";
  6. import {
  7. approveV4Permission,
  8. approveV4PermissionAlways,
  9. getV4PaneSnapshot,
  10. getV4PermissionRuleScopes,
  11. hasV4PermissionDialog,
  12. prepareV4ConversationE2E,
  13. sendV4Prompt,
  14. waitForV4PermissionDialog,
  15. waitForV4TimelineContaining,
  16. } from "#e2e/helpers/v4-conversation";
  17. const BPR_PROJECT_DIR = resolveE2ERuntimePath(
  18. "conversation-session-v4-bash-command-prefix-permission",
  19. );
  20. describe("v4 Bash command prefix 项目权限", () => {
  21. before(async () => {
  22. // beforeSession 已在 case 目录安装 pnpm shim;这里只补 package fixture,不能整目录删除。
  23. await mkdir(BPR_PROJECT_DIR, { recursive: true });
  24. await writeFile(
  25. join(BPR_PROJECT_DIR, "package.json"),
  26. JSON.stringify({
  27. private: true,
  28. scripts: {
  29. lint: "node -e \"console.log('lint-ok')\"",
  30. test: "node -e \"console.log('test-ok')\"",
  31. },
  32. }),
  33. );
  34. });
  35. after(async () => {
  36. await browser.electron.restoreAllMocks();
  37. await clearAppData();
  38. await rm(BPR_PROJECT_DIR, { recursive: true, force: true });
  39. });
  40. it("展示并持久化 script prefix;同 script 复用,sibling script 继续询问", async () => {
  41. await prepareV4ConversationE2E();
  42. await sendV4Prompt("E2E_BPR_PREFIX_FIRST 执行 lint 脚本");
  43. await waitForV4PermissionDialog();
  44. const firstScopes = await getV4PermissionRuleScopes();
  45. expect(firstScopes).toHaveLength(1);
  46. expect(firstScopes[0]?.kind).toBe("prefix");
  47. expect(firstScopes[0]?.text).toContain("pnpm run lint …");
  48. expect(await approveV4PermissionAlways()).toBe(true);
  49. await waitForV4TimelineContaining("E2E_BPR_PREFIX_FIRST_DONE", 60000);
  50. let sameScriptPrompted = false;
  51. await sendV4Prompt("E2E_BPR_PREFIX_SAME 同一脚本使用不同参数");
  52. await browser.waitUntil(
  53. async () => {
  54. sameScriptPrompted ||= await hasV4PermissionDialog();
  55. return (await getV4PaneSnapshot()).timelineText.includes("E2E_BPR_PREFIX_SAME_DONE");
  56. },
  57. { timeout: 60000, timeoutMsg: "同 script 不同参数没有自动执行并收口" },
  58. );
  59. expect(sameScriptPrompted).toBe(false);
  60. await sendV4Prompt("E2E_BPR_PREFIX_SIBLING 执行 sibling test 脚本");
  61. await waitForV4PermissionDialog();
  62. const siblingScopes = await getV4PermissionRuleScopes();
  63. expect(siblingScopes[0]?.kind).toBe("prefix");
  64. expect(siblingScopes[0]?.text).toContain("pnpm run test …");
  65. expect(await approveV4Permission()).toBe(true);
  66. await waitForV4TimelineContaining("E2E_BPR_PREFIX_SIBLING_DONE", 60000);
  67. });
  68. });