storageWorkerDiagnostics.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { execFile } from "node:child_process";
  2. import { mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import { promisify } from "node:util";
  6. import { expect, it } from "vitest";
  7. it("E2E observer records stderr, exit code and final frame without changing Worker outcome", async () => {
  8. const dir = await mkdtemp(join(tmpdir(), "storage-observer-"));
  9. const entry = join(dir, "fixture.cjs");
  10. const artifacts = join(dir, "artifacts");
  11. try {
  12. await writeFile(
  13. entry,
  14. `process.stderr.write("fixture startup failure\\n", () => {
  15. process.stdout.write(JSON.stringify({method:"startup/storagePath",params:{path:"/fixture/db.sqlite"}})+"\\n", () => process.exit(23));
  16. });`,
  17. );
  18. const probe = `const { Worker } = await import('node:worker_threads');
  19. const { once } = await import('node:events');
  20. const worker = new Worker(${JSON.stringify(entry)}, { argv:['--prepare-storage'], stdout:true, stderr:true });
  21. worker.stdout.resume(); worker.stderr.resume();
  22. const [code] = await once(worker, 'exit');
  23. if (code !== 23) throw new Error('observer changed exit code');`;
  24. const probePath = join(dir, "probe.mjs");
  25. await writeFile(probePath, probe);
  26. await promisify(execFile)(
  27. process.execPath,
  28. [
  29. "--import",
  30. new URL("./e2e/helpers/storage-worker-diagnostics.mjs", import.meta.url).href,
  31. probePath,
  32. ],
  33. { env: { ...process.env, ZCODE_E2E_STORAGE_WORKER_DIAGNOSTICS_DIR: artifacts } },
  34. );
  35. const files = await readdir(artifacts);
  36. expect(files).toHaveLength(1);
  37. expect(JSON.parse(await readFile(join(artifacts, files[0]!), "utf8"))).toMatchObject({
  38. exitCode: 23,
  39. stderr: "fixture startup failure\n",
  40. workerError: null,
  41. lastFrame: { method: "startup/storagePath", params: { path: "/fixture/db.sqlite" } },
  42. });
  43. } finally {
  44. await rm(dir, { recursive: true, force: true });
  45. }
  46. });