globalNoticeStore.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. createGlobalNoticeStore,
  4. type GlobalNoticeEnvelope,
  5. type GlobalNoticeStorage,
  6. } from "@/global-notice/globalNoticeStore.js";
  7. function memoryStorage(): GlobalNoticeStorage {
  8. const values = new Map<string, string>();
  9. return {
  10. getItem: (key) => values.get(key) ?? null,
  11. setItem: (key, value) => values.set(key, value),
  12. removeItem: (key) => values.delete(key),
  13. };
  14. }
  15. function shareNotice(now: number): GlobalNoticeEnvelope {
  16. return {
  17. noticeId: "notice-1",
  18. dedupeKey: "highspeed-share:latest",
  19. type: "highspeed-share",
  20. schemaVersion: 1,
  21. scope: "user",
  22. priority: "normal",
  23. interruptPolicy: "passive",
  24. createdAt: now,
  25. expiresAt: now + 60_000,
  26. payload: { cardId: "hsc-1", tokenUsage: 100, durationMs: 1_000, savedDurationMs: 500 },
  27. actions: [],
  28. dismissPolicy: { kind: "cooldown", cooldownMs: 24 * 60 * 60_000 },
  29. persistencePolicy: "latest-only",
  30. source: "highspeed",
  31. revision: 1,
  32. };
  33. }
  34. function quotaExceeded(): Error {
  35. const error = new Error("The quota has been exceeded.");
  36. error.name = "QuotaExceededError";
  37. return error;
  38. }
  39. describe("global notice store", () => {
  40. it("drops a persisted legacy Highspeed mock notice during hydration", () => {
  41. const storage = memoryStorage();
  42. storage.setItem(
  43. "zcode:global-notice:v1",
  44. JSON.stringify({
  45. version: 1,
  46. latest: {
  47. noticeId: "highspeed-share:share_hsc_mock_1:1000",
  48. dedupeKey: "highspeed-share:latest",
  49. type: "highspeed-share",
  50. schemaVersion: 1,
  51. scope: "user",
  52. priority: "normal",
  53. interruptPolicy: "passive",
  54. createdAt: 1000,
  55. expiresAt: 2000,
  56. payload: {
  57. cardId: "hsc_mock_1",
  58. shareUrl: "https://mock.z.ai/highspeed/share/hsc_mock_1",
  59. },
  60. actions: [],
  61. dismissPolicy: { kind: "cooldown", cooldownMs: 24 * 60 * 60_000 },
  62. persistencePolicy: "latest-only",
  63. source: "highspeed",
  64. revision: 1000,
  65. },
  66. dismissedUntilByKey: {},
  67. requiresPublishAfterDismissalByKey: { "highspeed-share:latest": false },
  68. }),
  69. );
  70. const store = createGlobalNoticeStore({ storage, now: () => 1500 });
  71. expect(store.getVisible()).toBeNull();
  72. expect(store.getSnapshot().latest).toBeNull();
  73. expect(JSON.parse(storage.getItem("zcode:global-notice:v1") ?? "null").latest).toBeNull();
  74. });
  75. it("persists latest-only Highspeed notice and enforces a user-level 24h dismissal", () => {
  76. let now = 1000;
  77. const storage = memoryStorage();
  78. const store = createGlobalNoticeStore({ storage, now: () => now });
  79. store.publish({
  80. noticeId: "notice-1",
  81. dedupeKey: "highspeed-share:latest",
  82. type: "highspeed-share",
  83. schemaVersion: 1,
  84. scope: "user",
  85. priority: "normal",
  86. interruptPolicy: "passive",
  87. createdAt: now,
  88. expiresAt: now + 60_000,
  89. payload: { cardId: "hsc-1", shareUrl: "https://example.test/s/1" },
  90. actions: [],
  91. dismissPolicy: { kind: "cooldown", cooldownMs: 24 * 60 * 60_000 },
  92. persistencePolicy: "latest-only",
  93. source: "highspeed",
  94. revision: 1,
  95. });
  96. expect(store.getVisible()?.noticeId).toBe("notice-1");
  97. store.dismiss("notice-1");
  98. expect(store.getVisible()).toBeNull();
  99. store.publish({
  100. ...store.getSnapshot().latest!,
  101. noticeId: "notice-2",
  102. createdAt: now + 1,
  103. revision: 2,
  104. });
  105. expect(store.getVisible()).toBeNull();
  106. now += 24 * 60 * 60_000 + 1;
  107. expect(store.getVisible()).toBeNull();
  108. store.publish({
  109. ...store.getSnapshot().latest!,
  110. noticeId: "notice-3",
  111. createdAt: now,
  112. expiresAt: now + 60_000,
  113. revision: 3,
  114. });
  115. expect(store.getVisible()?.noticeId).toBe("notice-3");
  116. });
  117. it("keeps publish/dismiss working and notifies subscribers when storage writes fail", () => {
  118. // CR-03 回归:setItem 抛 QuotaExceeded 曾让 publish 抛错、订阅者收不到内存态更新;
  119. // SessionPane 已先 claim 自动分享再 publish,异常会留下不可重试的 claim,分享卡永远不出现。
  120. const backing = memoryStorage();
  121. let failWrites = false;
  122. const flaky: GlobalNoticeStorage = {
  123. ...backing,
  124. setItem: (key, value) => {
  125. if (failWrites) throw quotaExceeded();
  126. backing.setItem(key, value);
  127. },
  128. };
  129. const failures: unknown[] = [];
  130. const store = createGlobalNoticeStore({
  131. storage: flaky,
  132. now: () => 1000,
  133. onPersistError: (error) => failures.push(error),
  134. });
  135. const listener = vi.fn();
  136. store.subscribe(listener);
  137. failWrites = true;
  138. expect(() => store.publish(shareNotice(1000))).not.toThrow();
  139. expect(store.getVisible()?.noticeId).toBe("notice-1");
  140. expect(listener).toHaveBeenCalledTimes(1);
  141. expect(failures).toHaveLength(1);
  142. expect((failures[0] as Error).name).toBe("QuotaExceededError");
  143. expect(() => store.dismiss("notice-1")).not.toThrow();
  144. expect(store.getVisible()).toBeNull();
  145. expect(listener).toHaveBeenCalledTimes(2);
  146. // 存储恢复后下一次写入照常落盘,内存态与持久态重新对齐。
  147. failWrites = false;
  148. store.publish({ ...shareNotice(2000), noticeId: "notice-2", revision: 2 });
  149. expect(JSON.parse(backing.getItem("zcode:global-notice:v1") ?? "null").latest.noticeId).toBe(
  150. "notice-2",
  151. );
  152. });
  153. it("drops the legacy mock notice even when the cleanup write fails", () => {
  154. const backing = memoryStorage();
  155. backing.setItem(
  156. "zcode:global-notice:v1",
  157. JSON.stringify({
  158. version: 1,
  159. latest: { ...shareNotice(1000), payload: { cardId: "hsc_mock_1" } },
  160. dismissedUntilByKey: { "highspeed-share:latest": 5000 },
  161. requiresPublishAfterDismissalByKey: {},
  162. }),
  163. );
  164. const readOnly: GlobalNoticeStorage = {
  165. ...backing,
  166. setItem: () => {
  167. throw quotaExceeded();
  168. },
  169. };
  170. const failures: unknown[] = [];
  171. const store = createGlobalNoticeStore({
  172. storage: readOnly,
  173. now: () => 1500,
  174. onPersistError: (error) => failures.push(error),
  175. });
  176. expect(store.getSnapshot().latest).toBeNull();
  177. // 清理写失败不能把整份状态打回空:已有的 24h 静默必须保留。
  178. expect(store.getSnapshot().dismissedUntilByKey["highspeed-share:latest"]).toBe(5000);
  179. expect(failures).toHaveLength(1);
  180. });
  181. });