codeCommandRecorder.test.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { execFile } from "node:child_process";
  2. import { createHash } from "node:crypto";
  3. import { mkdtemp, readFile, rm } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { join } from "node:path";
  6. import { promisify } from "node:util";
  7. import { afterEach, describe, expect, it } from "vitest";
  8. import { createCodeCommandRecorder } from "./e2e/helpers/code-command-recorder.js";
  9. const directories: string[] = [];
  10. const run = promisify(execFile);
  11. afterEach(async () => {
  12. await Promise.all(
  13. directories.splice(0).map((path) => rm(path, { recursive: true, force: true })),
  14. );
  15. });
  16. describe.skipIf(process.platform === "win32")("原生编辑器 OS 边界录制夹具", () => {
  17. it("实际执行命令并准确记录参数摘要,不把 authority 或参数明文写进文件", async () => {
  18. const root = await mkdtemp(join(tmpdir(), "code recorder's "));
  19. directories.push(root);
  20. const recorder = await createCodeCommandRecorder(root);
  21. const args = [
  22. "--file-uri",
  23. "vscode-remote://ssh-remote+private-authority/tmp/%E4%B8%AD%20x.md",
  24. ];
  25. await run(recorder.commandPath, args);
  26. expect(await recorder.readCalls()).toEqual([
  27. {
  28. argumentCount: 2,
  29. argumentHashes: args.map((arg) => createHash("sha256").update(arg).digest("hex")),
  30. },
  31. ]);
  32. expect(await readFile(recorder.logPath, "utf8")).not.toContain("private-authority");
  33. });
  34. it("相同命令两次执行留下两条记录,不覆盖而掩盖重复调用", async () => {
  35. const root = await mkdtemp(join(tmpdir(), "code-recorder-"));
  36. directories.push(root);
  37. const recorder = await createCodeCommandRecorder(root);
  38. expect(await recorder.readCalls()).toEqual([]);
  39. await run(recorder.commandPath, ["--file-uri", "vscode-remote://ssh-remote+host/a.md"]);
  40. await run(recorder.commandPath, ["--file-uri", "vscode-remote://ssh-remote+host/a.md"]);
  41. expect(await recorder.readCalls()).toHaveLength(2);
  42. });
  43. });