interfaceMode.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // @vitest-environment jsdom
  2. import { beforeEach, describe, expect, it, vi } from "vitest";
  3. import type { IBroadcastService } from "@zcode/services";
  4. import { INTERFACE_MODE_STORAGE_KEY, normalizeInterfaceMode } from "@/lib/interfaceMode.js";
  5. import { createZCodeStore } from "@/store/index.js";
  6. describe("界面模式偏好", () => {
  7. beforeEach(() => localStorage.clear());
  8. it.each([
  9. [null, "coding"],
  10. [undefined, "coding"],
  11. ["invalid", "coding"],
  12. ["office", "office"],
  13. ["coding", "coding"],
  14. // CR-02:改名 office 前的旧名,存量 localStorage 值必须映射到 office 而不是重置成 coding。
  15. ["general", "office"],
  16. ["concise", "office"],
  17. ["professional", "coding"],
  18. ])("归一化 %s 为 %s", (value, expected) => {
  19. expect(normalizeInterfaceMode(value)).toBe(expected);
  20. });
  21. it("保存模式、跨窗口同步且不回播,不改写代码换行偏好", () => {
  22. let receive: Parameters<IBroadcastService["onMessage"]>[0] | undefined;
  23. const send = vi.fn(async () => {});
  24. const service: IBroadcastService = {
  25. send,
  26. acquireClaim: async () => ({ status: "unavailable" }),
  27. commitClaim: async () => {},
  28. releaseClaim: async () => {},
  29. tryClaim: async () => false,
  30. onMessage: (listener) => {
  31. receive = listener;
  32. return { dispose: () => {} };
  33. },
  34. };
  35. localStorage.setItem(INTERFACE_MODE_STORAGE_KEY, "office");
  36. const store = createZCodeStore(service);
  37. expect(store.getState().interfaceMode).toBe("office");
  38. store.getState().setCodePreviewSettings({ wrapLongLines: false });
  39. store.getState().setInterfaceMode("coding");
  40. expect(localStorage.getItem(INTERFACE_MODE_STORAGE_KEY)).toBe("coding");
  41. expect(send).toHaveBeenCalledWith({ channel: "state:interfaceMode", payload: "coding" });
  42. send.mockClear();
  43. receive?.({ channel: "state:interfaceMode", payload: "office" });
  44. expect(store.getState().interfaceMode).toBe("office");
  45. expect(localStorage.getItem(INTERFACE_MODE_STORAGE_KEY)).toBe("office");
  46. expect(store.getState().codePreviewSettings.wrapLongLines).toBe(false);
  47. expect(send).not.toHaveBeenCalled();
  48. receive?.({ channel: "state:interfaceMode", payload: "unexpected" });
  49. expect(store.getState().interfaceMode).toBe("office");
  50. });
  51. });