conversation-session-guarded-batch-delete.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // DCA-V1-19/20/23/24:仅覆盖 POSIX/Git Bash 桌面拒绝,Windows 原生语义另有 oracle。
  2. import { execFile } from "node:child_process";
  3. import { access, mkdir, readFile, rm, writeFile } from "node:fs/promises";
  4. import { join } from "node:path";
  5. import { DatabaseSync } from "node:sqlite";
  6. import { promisify } from "node:util";
  7. import { clearAppData, getE2EAppDataPaths } from "#e2e/helpers/desktop-app";
  8. import { resolveE2ERuntimePath } from "#e2e/helpers/e2e-runtime-paths";
  9. import { skipOccupationOnboardingIfPresent } from "#e2e/helpers/occupation-onboarding";
  10. import { getLatestUpstreamToolResultByToolCallId } from "#e2e/helpers/conversation-session-network";
  11. import {
  12. clickV4Stop,
  13. denyV4PermissionWithFeedback,
  14. getV4PaneSnapshot,
  15. prepareV4ConversationE2E,
  16. sendV4Prompt,
  17. switchV4Mode,
  18. waitForV4PermissionDialogInComposerDock,
  19. waitForV4TimelineContaining,
  20. } from "#e2e/helpers/v4-conversation";
  21. const CASE = "conversation-session-guarded-batch-delete";
  22. const ROOT = resolveE2ERuntimePath(CASE);
  23. const MARKERS = [
  24. "E2E_GUARDED_DELETE_RECURSIVE",
  25. "E2E_GUARDED_DELETE_BATCH",
  26. "E2E_GUARDED_DELETE_GLOB",
  27. "E2E_GUARDED_DELETE_FIND",
  28. "E2E_GUARDED_DELETE_CLEAN",
  29. ] as const;
  30. const exists = (path: string) =>
  31. access(path).then(
  32. () => true,
  33. () => false,
  34. );
  35. describe("Autonomous batch deletion approval (pending human review)", () => {
  36. let originalRules = "";
  37. before(async () => {
  38. await mkdir(ROOT, { recursive: true });
  39. await skipOccupationOnboardingIfPresent();
  40. });
  41. beforeEach(async () => {
  42. originalRules = await readProjectRules();
  43. });
  44. afterEach(async () => {
  45. expect(await readProjectRules()).toBe(originalRules);
  46. if ((await getV4PaneSnapshot()).canStop) await clickV4Stop();
  47. });
  48. after(async () => {
  49. await browser.electron.restoreAllMocks();
  50. await clearAppData();
  51. await rm(ROOT, { recursive: true, force: true });
  52. });
  53. for (const marker of MARKERS) {
  54. it(`${marker} refuses the new deletion match and returns the real denial`, async () => {
  55. const directory = join(ROOT, marker);
  56. await mkdir(join(directory, "target"), { recursive: true });
  57. for (const name of ["one.txt", "two.txt"]) {
  58. await writeFile(join(directory, name), "preserved");
  59. await writeFile(join(directory, "target", name), "preserved");
  60. }
  61. // clean 必须使用独立仓库,即使匹配回归也不能清理真实工作区。
  62. await promisify(execFile)("git", ["init", "-q", directory]);
  63. await prepareV4ConversationE2E();
  64. await switchV4Mode("guarded");
  65. await sendV4Prompt(`${marker} Execute the fixture command once.`);
  66. await waitForV4PermissionDialogInComposerDock();
  67. expect(await $('[data-permission-option-kind="allowAlways"]').isExisting()).toBe(false);
  68. expect(await exists(join(directory, "executed"))).toBe(false);
  69. expect(await denyV4PermissionWithFeedback("Preserve batch deletion fixture")).toBe(true);
  70. await waitForV4TimelineContaining(`${marker}_DONE`, 60000);
  71. const result = await getLatestUpstreamToolResultByToolCallId(`toolu_${marker}`);
  72. expect(result?.isError).toBe(true);
  73. expect(result?.content).toContain("Preserve batch deletion fixture");
  74. expect(await exists(join(directory, "executed"))).toBe(false);
  75. for (const name of ["one.txt", "two.txt", "target/one.txt", "target/two.txt"])
  76. expect(await readFile(join(directory, name), "utf8")).toBe("preserved");
  77. await $('[data-permission-option-kind="deny"]').waitForExist({ reverse: true });
  78. });
  79. }
  80. });
  81. async function readProjectRules(): Promise<string> {
  82. const path = join(getE2EAppDataPaths().storageRoot, "cli", "db", "db.sqlite");
  83. if (!(await exists(path))) return "[]";
  84. const database = new DatabaseSync(path, { readOnly: true });
  85. try {
  86. return JSON.stringify(
  87. database
  88. .prepare(
  89. "select scope, scope_id, value from local_setting where namespace = 'permission' and key = 'ruleset' order by scope, scope_id",
  90. )
  91. .all(),
  92. );
  93. } finally {
  94. database.close();
  95. }
  96. }