globalDatabaseStartupLoading.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // @vitest-environment jsdom
  2. import { createRoot } from "react-dom/client";
  3. import { act, createElement } from "react";
  4. import { renderToStaticMarkup } from "react-dom/server";
  5. import { expect, it, vi } from "vitest";
  6. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  7. import { GlobalDatabaseStartupLoading } from "@/root/GlobalDatabaseStartupLoading.js";
  8. it("explains FULL, provides manual recovery, and never claims all changes rolled back", () => {
  9. const html = renderToStaticMarkup(
  10. createElement(
  11. ZCodeIntlProvider,
  12. { initialLocale: "zh-CN" },
  13. createElement(GlobalDatabaseStartupLoading, {
  14. state: {
  15. schemaVersion: 1,
  16. startupId: "startup",
  17. attemptId: "diagnostic-1",
  18. sequence: 2,
  19. phase: "failed",
  20. failedPhase: "preparing_session_storage",
  21. errorCode: "storage_full",
  22. startedAt: 1,
  23. updatedAt: 2,
  24. disk: [],
  25. },
  26. onRetry: () => {},
  27. onCopy: async () => {},
  28. onExit: () => {},
  29. }),
  30. ),
  31. );
  32. expect(html).toContain("存储空间不足或已达到容量限制");
  33. expect(html).toContain("系统临时目录");
  34. expect(html).toContain("diagnostic-1");
  35. expect(html).toContain("重试");
  36. expect(html).not.toContain("原数据库已保留");
  37. });
  38. function renderStartup(
  39. state: import("@zcode/shared").DatabaseStartupState | null,
  40. locale: "zh-CN" | "en-US" = "zh-CN",
  41. ) {
  42. return renderToStaticMarkup(
  43. createElement(
  44. ZCodeIntlProvider,
  45. { initialLocale: locale },
  46. createElement(GlobalDatabaseStartupLoading, {
  47. state,
  48. onRetry() {},
  49. onCopy: async () => {},
  50. onExit() {},
  51. }),
  52. ),
  53. );
  54. }
  55. const baseState: import("@zcode/shared").DatabaseStartupState = {
  56. schemaVersion: 1,
  57. startupId: "silent",
  58. attemptId: "silent",
  59. sequence: 1,
  60. phase: "preparing_host_storage",
  61. startedAt: 1,
  62. updatedAt: 2,
  63. disk: [],
  64. };
  65. it("SILENTDB-01/08: every normal preparation phase has no visible text or buttons", () => {
  66. for (const locale of ["zh-CN", "en-US"] as const) {
  67. for (const databasePhase of [
  68. "checking",
  69. "waiting_for_lock",
  70. "committing",
  71. "maintaining",
  72. "ready",
  73. ] as const) {
  74. const html = renderStartup({ ...baseState, databasePhase }, locale);
  75. expect(html).toContain('data-testid="root-startup-loading"');
  76. expect(html).not.toContain('data-testid="database-startup-status"');
  77. expect(html).not.toMatch(/<(h1|p|button)(\s|>)/);
  78. }
  79. expect(renderStartup(null, locale)).not.toMatch(/<(h1|p|button)(\s|>)/);
  80. expect(renderStartup({ ...baseState, phase: "starting_services" }, locale)).not.toMatch(
  81. /<(h1|p|button)(\s|>)/,
  82. );
  83. }
  84. });
  85. it("SILENTDB-02/05: migration facts control initialization, upgrade, saving and finishing text", () => {
  86. const migration = { kind: "upgrade" as const, executedCount: 1, committedCount: 0 };
  87. expect(
  88. renderStartup({
  89. ...baseState,
  90. databasePhase: "migrating",
  91. migration,
  92. currentMigration: migration,
  93. }),
  94. ).toContain("正在升级本地数据");
  95. expect(
  96. renderStartup({
  97. ...baseState,
  98. databasePhase: "committing",
  99. finalDatabase: true,
  100. migration,
  101. currentMigration: migration,
  102. }),
  103. ).toContain("正在保存更新");
  104. expect(
  105. renderStartup({
  106. ...baseState,
  107. phase: "starting_services",
  108. migration: { ...migration, committedCount: 1 },
  109. }),
  110. ).toContain("正在完成启动");
  111. expect(
  112. renderStartup({
  113. ...baseState,
  114. databasePhase: "checking",
  115. migration: { ...migration, committedCount: 1 },
  116. }),
  117. ).not.toContain("正在完成启动");
  118. expect(
  119. renderStartup({
  120. ...baseState,
  121. databasePhase: "migrating",
  122. migration: { ...migration, kind: "initialize" },
  123. currentMigration: { ...migration, kind: "initialize" },
  124. }),
  125. ).toContain("正在初始化本地数据");
  126. expect(
  127. renderStartup({
  128. ...baseState,
  129. databasePhase: "waiting_for_lock",
  130. migration,
  131. currentMigration: migration,
  132. }),
  133. ).toContain("正在等待数据库准备");
  134. });
  135. it("SILENTDB-01: silent UI mounts no timer and migration-to-failure clears the progress timer", async () => {
  136. vi.useFakeTimers();
  137. const interval = vi.spyOn(globalThis, "setInterval");
  138. const container = document.createElement("div");
  139. const root = createRoot(container);
  140. const show = async (state: import("@zcode/shared").DatabaseStartupState) => {
  141. await act(async () =>
  142. root.render(
  143. createElement(
  144. ZCodeIntlProvider,
  145. { initialLocale: "zh-CN" },
  146. createElement(GlobalDatabaseStartupLoading, {
  147. state,
  148. onRetry() {},
  149. onCopy: async () => {},
  150. onExit() {},
  151. }),
  152. ),
  153. ),
  154. );
  155. };
  156. try {
  157. await show(baseState);
  158. expect(container.textContent).toBe("");
  159. expect(interval).not.toHaveBeenCalled();
  160. const migration = { kind: "upgrade" as const, executedCount: 0, committedCount: 0 };
  161. await show({
  162. ...baseState,
  163. databasePhase: "migrating",
  164. migration,
  165. currentMigration: migration,
  166. });
  167. expect(container.textContent).toContain("正在升级本地数据");
  168. expect(interval).toHaveBeenCalledTimes(1);
  169. await show({ ...baseState, phase: "failed", errorCode: "storage_full", migration });
  170. expect(container.textContent).toContain("存储空间不足");
  171. expect(vi.getTimerCount()).toBe(0);
  172. await show({ ...baseState, attemptId: "retry" });
  173. expect(container.textContent).toBe("");
  174. expect(interval).toHaveBeenCalledTimes(1);
  175. } finally {
  176. await act(async () => root.unmount());
  177. interval.mockRestore();
  178. vi.useRealTimers();
  179. }
  180. });
  181. it("SILENTDB-05: intermediate database commits and maintenance never announce global saving or finishing", () => {
  182. const migration = { kind: "upgrade" as const, executedCount: 1, committedCount: 1 };
  183. for (const databasePhase of ["checking", "committing", "maintaining", "ready"] as const) {
  184. const html = renderStartup({
  185. ...baseState,
  186. databasePhase,
  187. migration,
  188. currentMigration: migration,
  189. finalDatabase: false,
  190. });
  191. expect(html).toContain("正在升级本地数据");
  192. expect(html).not.toContain("正在保存更新");
  193. expect(html).not.toContain("正在完成启动");
  194. }
  195. expect(
  196. renderStartup({
  197. ...baseState,
  198. databasePhase: "ready",
  199. migration,
  200. currentMigration: migration,
  201. finalDatabase: true,
  202. }),
  203. ).toContain("正在完成启动");
  204. });