generate-large-task-fixture.test.mjs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { mkdir, mkdtemp, readFile, rm } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { dirname, join } from "node:path";
  4. import { DatabaseSync } from "node:sqlite";
  5. import { afterEach, expect, it, vi } from "vitest";
  6. const fixture = vi.hoisted(() => ({ home: "" }));
  7. vi.mock("node:os", async (importOriginal) => ({
  8. ...(await importOriginal()),
  9. homedir: () => fixture.home,
  10. }));
  11. const originalArgv = process.argv;
  12. afterEach(async () => {
  13. process.argv = originalArgv;
  14. vi.restoreAllMocks();
  15. if (fixture.home) await rm(fixture.home, { recursive: true, force: true });
  16. });
  17. it("生成的本地 fixture 使用路径归属,不把目录哈希写成显式身份", async () => {
  18. fixture.home = await mkdtemp(join(tmpdir(), "large-task-fixture-"));
  19. const workspace = join(fixture.home, "workspace");
  20. await mkdir(workspace);
  21. const cliPath = join(fixture.home, ".zcode/cli/db/db.sqlite");
  22. const indexPath = join(fixture.home, ".zcode/v2/tasks-index.sqlite");
  23. for (const [path, schema] of [
  24. [cliPath, "apps/zcode-cli/packages/adapters/src/storage/session-store/schema.baseline.sql"],
  25. [indexPath, "packages/services/src/project-storage/schema.baseline.sql"],
  26. ]) {
  27. await mkdir(dirname(path), { recursive: true });
  28. const db = new DatabaseSync(path);
  29. try {
  30. db.exec(await readFile(new URL(`../../../../${schema}`, import.meta.url), "utf8"));
  31. } finally {
  32. db.close();
  33. }
  34. }
  35. const output = vi.spyOn(console, "log").mockImplementation(() => {});
  36. process.argv = [
  37. process.execPath,
  38. "generate-large-task-fixture.mjs",
  39. "--workspace",
  40. workspace,
  41. "--turns",
  42. "1",
  43. "--no-backup",
  44. ];
  45. await import("./generate-large-task-fixture.mjs");
  46. const manifest = JSON.parse(output.mock.calls.at(-1)[0]);
  47. const cli = new DatabaseSync(cliPath, { readOnly: true });
  48. const index = new DatabaseSync(indexPath, { readOnly: true });
  49. try {
  50. expect(
  51. cli
  52. .prepare("select workspace_id, directory, path from session where id = ?")
  53. .get(manifest.sessionId),
  54. ).toEqual({
  55. workspace_id: null,
  56. directory: workspace,
  57. path: workspace,
  58. });
  59. expect(
  60. index
  61. .prepare(
  62. "select workspace_identity, workspace_key, workspace_path from tasks where task_id = ?",
  63. )
  64. .get(manifest.sessionId),
  65. ).toEqual({
  66. workspace_identity: null,
  67. workspace_key: workspace,
  68. workspace_path: workspace,
  69. });
  70. expect(
  71. cli.prepare("select count(*) count from message where session_id = ?").get(manifest.sessionId)
  72. .count,
  73. ).toBe(2);
  74. expect(
  75. cli.prepare("select count(*) count from part where session_id = ?").get(manifest.sessionId)
  76. .count,
  77. ).toBe(manifest.partCount);
  78. } finally {
  79. cli.close();
  80. index.close();
  81. }
  82. });