databaseStartup.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { describe, expect, it } from "vitest";
  2. import { classifyDatabaseStartupError } from "@zcode/shared";
  3. import { DatabaseStartupCoordinator } from "../src/host/databaseStartupCoordinator.js";
  4. describe("global database startup", () => {
  5. it.each([
  6. [{ code: "ENOSPC" }, "storage_full"],
  7. [{ code: "EDQUOT" }, "storage_full"],
  8. [{ code: "ERR_SQLITE_ERROR", errcode: 13 + 256 }, "storage_full"],
  9. [{ cause: { errcode: 10 + 256 } }, "io_error"],
  10. [{ errcode: 14 }, "open_failed"],
  11. [{ errcode: 7 }, "out_of_memory"],
  12. [{ code: "EACCES" }, "permission_denied"],
  13. [{ errcode: 11 }, "corrupt"],
  14. [new Error("disk full out of memory"), "sql_failed"],
  15. ])("classifies structured causes without guessing from text", (error, expected) => {
  16. expect(classifyDatabaseStartupError(error)).toBe(expected);
  17. });
  18. it("retains failure until manual retry; ignores stale and concurrent retries", async () => {
  19. let runs = 0;
  20. let release!: () => void;
  21. const states: string[] = [];
  22. const coordinator = new DatabaseStartupCoordinator({
  23. prepare: async (report) => {
  24. runs++;
  25. report("preparing_host_storage");
  26. if (runs === 1) throw Object.assign(new Error("full"), { errcode: 13 });
  27. await new Promise<void>((resolve) => {
  28. release = resolve;
  29. });
  30. report("starting_services");
  31. },
  32. publish: (state) => states.push(state.phase),
  33. });
  34. await coordinator.start();
  35. const failed = coordinator.snapshot;
  36. expect(failed.phase).toBe("failed");
  37. expect(failed.errorCode).toBe("storage_full");
  38. await coordinator.retry("stale");
  39. expect(runs).toBe(1);
  40. const retry = coordinator.retry(failed.attemptId);
  41. await coordinator.retry(failed.attemptId);
  42. expect(runs).toBe(2);
  43. expect(coordinator.snapshot.phase).toBe("preparing_host_storage");
  44. release();
  45. await retry;
  46. expect(coordinator.snapshot.phase).toBe("ready");
  47. expect(states.filter((phase) => phase === "ready")).toHaveLength(1);
  48. });
  49. it("does not publish ready before service assembly and survives telemetry failure", async () => {
  50. const coordinator = new DatabaseStartupCoordinator({
  51. prepare: async (report) => {
  52. report("starting_services");
  53. throw new Error("service assembly failed");
  54. },
  55. publish: () => {
  56. throw new Error("telemetry unavailable");
  57. },
  58. });
  59. await coordinator.start();
  60. expect(coordinator.snapshot.phase).toBe("failed");
  61. expect(coordinator.snapshot.failedPhase).toBe("starting_services");
  62. });
  63. });
  64. it("preserves structured failure details through the startup snapshot", async () => {
  65. const coordinator = new DatabaseStartupCoordinator({
  66. prepare: async () => {
  67. throw Object.assign(
  68. new Error("wrapper", { cause: { errcode: 778, code: "ERR_SQLITE_ERROR" } }),
  69. { migrationId: "0003_official_glm_selection" },
  70. );
  71. },
  72. publish: () => {},
  73. });
  74. await coordinator.start();
  75. expect(coordinator.snapshot).toMatchObject({
  76. errorCode: "io_error",
  77. sqliteCode: 778,
  78. migrationId: "0003_official_glm_selection",
  79. });
  80. });
  81. it("requires reopening the app after partial service assembly instead of creating duplicate services", async () => {
  82. let runs = 0;
  83. const coordinator = new DatabaseStartupCoordinator({
  84. prepare: async (report) => {
  85. runs++;
  86. report("starting_services");
  87. throw new Error("assembly failed");
  88. },
  89. publish: () => {},
  90. });
  91. await coordinator.start();
  92. await coordinator.retry(coordinator.snapshot.attemptId);
  93. expect(runs).toBe(1);
  94. });
  95. it("SILENTDB-05: Host keeps migration facts across databases, failure and retries without double counting", async () => {
  96. let run = 0;
  97. const coordinator = new DatabaseStartupCoordinator({
  98. prepare: async (report) => {
  99. run++;
  100. if (run === 2) {
  101. report("starting_services");
  102. return;
  103. }
  104. const facts = { kind: "upgrade" as const, executedCount: 2, committedCount: 2 };
  105. report("preparing_host_storage", "maintaining", { databaseId: "tasks", migration: facts });
  106. report("preparing_host_storage", "maintaining", { databaseId: "tasks", migration: facts });
  107. report("preparing_session_storage", "checking", {
  108. databaseId: "session",
  109. migration: { kind: "none", executedCount: 0, committedCount: 0 },
  110. });
  111. throw Object.assign(new Error("full"), { code: "ENOSPC" });
  112. },
  113. publish() {},
  114. });
  115. await coordinator.start();
  116. expect(coordinator.snapshot.migration).toEqual({
  117. kind: "upgrade",
  118. executedCount: 2,
  119. committedCount: 2,
  120. });
  121. await coordinator.retry(coordinator.snapshot.attemptId);
  122. expect(coordinator.snapshot.migration).toBeUndefined();
  123. expect(coordinator.snapshot.phase).toBe("ready");
  124. });
  125. it("SILENTDB-04: consumes terminal failure migration facts without pretending the transaction committed", async () => {
  126. const coordinator = new DatabaseStartupCoordinator({
  127. prepare: async (report) => {
  128. report("preparing_session_storage", "migrating", {
  129. databaseId: "session",
  130. migration: { kind: "upgrade", executedCount: 1, committedCount: 0 },
  131. });
  132. throw Object.assign(new Error("full"), {
  133. code: "ENOSPC",
  134. migrationUpdate: {
  135. databaseId: "session",
  136. migration: { kind: "upgrade", executedCount: 2, committedCount: 0 },
  137. },
  138. });
  139. },
  140. publish() {},
  141. });
  142. await coordinator.start();
  143. expect(coordinator.snapshot).toMatchObject({
  144. phase: "failed",
  145. errorCode: "storage_full",
  146. migration: { kind: "upgrade", executedCount: 2, committedCount: 0 },
  147. });
  148. });
  149. it("preserves per-database locked baselines through failure and clears them for retry", async () => {
  150. let run = 0;
  151. const coordinator = new DatabaseStartupCoordinator({
  152. publish: () => {},
  153. prepare: async (report) => {
  154. run++;
  155. report("preparing_host_storage", "checking", { databaseId: "tasks-index" });
  156. expect(coordinator.snapshot.migrationBaselines).toEqual([
  157. {
  158. databaseId: "tasks-index",
  159. databaseKind: "tasks-index",
  160. lastAppliedMigrationId: undefined,
  161. },
  162. ]);
  163. if (run > 1) return;
  164. const migration = {
  165. kind: "upgrade" as const,
  166. executedCount: 0,
  167. committedCount: 0,
  168. lastAppliedMigrationId: "0002_provider_selection",
  169. };
  170. report("preparing_host_storage", "migrating", { databaseId: "tasks-index", migration });
  171. report("preparing_session_storage", "checking", { databaseId: "session-hash" });
  172. report("preparing_session_storage", "committing", {
  173. databaseId: "session-hash",
  174. migration: { ...migration, lastAppliedMigrationId: null },
  175. });
  176. throw Object.assign(new Error("full"), { errcode: 13 });
  177. },
  178. });
  179. await coordinator.start();
  180. expect(coordinator.snapshot.migrationBaselines).toEqual([
  181. {
  182. databaseId: "tasks-index",
  183. databaseKind: "tasks-index",
  184. lastAppliedMigrationId: "0002_provider_selection",
  185. },
  186. { databaseId: "session-hash", databaseKind: "session", lastAppliedMigrationId: null },
  187. ]);
  188. await coordinator.retry(coordinator.snapshot.attemptId);
  189. expect(coordinator.snapshot.migrationBaselines).toHaveLength(1);
  190. });