storagePreparationProtocol.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { PassThrough } from "node:stream";
  2. import { mkdtemp, rm, stat } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import { expect, it } from "vitest";
  6. import { prepareProtocolStartupStorage } from "../../../apps/zcode-cli/packages/bootstrap/src/zcode-protocol/storage-startup.js";
  7. it("waits for path observation before opening storage and exits without business startup", async () => {
  8. const dir = await mkdtemp(join(tmpdir(), "zcode-storage-only-"));
  9. const input = new PassThrough();
  10. const output = new PassThrough();
  11. const frames: Array<{ method: string; params: Record<string, unknown> }> = [];
  12. output.on("data", (chunk) => {
  13. const frame = JSON.parse(String(chunk));
  14. frames.push(frame);
  15. if (frame.method === "startup/storagePath") {
  16. expect(frames).toHaveLength(1);
  17. queueMicrotask(() =>
  18. input.write(JSON.stringify({ method: "startup/storagePathReady" }) + "\n"),
  19. );
  20. }
  21. });
  22. try {
  23. await prepareProtocolStartupStorage({ dbPath: join(dir, "db.sqlite"), input, output });
  24. expect(frames.find((frame) => frame.params.phase === "ready")?.params.migration).toMatchObject({
  25. lastAppliedMigrationId: null,
  26. });
  27. expect(frames.at(-1)?.method).toBe("startup/storagePrepared");
  28. } finally {
  29. input.destroy();
  30. output.destroy();
  31. await rm(dir, { recursive: true, force: true });
  32. }
  33. });
  34. it("SILENTDB-06: Host reuse acknowledgement skips opening the already prepared storage", async () => {
  35. const dir = await mkdtemp(join(tmpdir(), "zcode-storage-reuse-"));
  36. const path = join(dir, "not-created.sqlite");
  37. const input = new PassThrough();
  38. const output = new PassThrough();
  39. const frames: string[] = [];
  40. output.on("data", (chunk) => {
  41. const frame = JSON.parse(String(chunk));
  42. frames.push(frame.method);
  43. if (frame.method === "startup/storagePath")
  44. queueMicrotask(() =>
  45. input.write(JSON.stringify({ method: "startup/storagePathReady", reuse: true }) + "\n"),
  46. );
  47. });
  48. try {
  49. await prepareProtocolStartupStorage({ dbPath: path, input, output });
  50. expect(frames).toEqual(["startup/storagePath", "startup/storagePrepared"]);
  51. await expect(stat(path)).rejects.toMatchObject({ code: "ENOENT" });
  52. } finally {
  53. input.destroy();
  54. output.destroy();
  55. await rm(dir, { recursive: true, force: true });
  56. }
  57. });