taskNotificationPreferences.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. const localStorageState = new Map<string, string>();
  3. Object.defineProperty(globalThis, "localStorage", {
  4. configurable: true,
  5. value: {
  6. getItem: (key: string) => localStorageState.get(key) ?? null,
  7. setItem: (key: string, value: string) => {
  8. localStorageState.set(key, value);
  9. },
  10. removeItem: (key: string) => {
  11. localStorageState.delete(key);
  12. },
  13. clear: () => {
  14. localStorageState.clear();
  15. },
  16. },
  17. });
  18. describe("task notification preferences", () => {
  19. beforeEach(() => {
  20. localStorageState.clear();
  21. vi.resetModules();
  22. });
  23. it("treats notification sound as disabled when task notifications are off", async () => {
  24. localStorageState.set("zcode-notification-enabled", "false");
  25. localStorageState.set("zcode-notification-sound-enabled", "true");
  26. const { isTaskNotificationSoundEnabled } = await import(
  27. "../src/lib/taskNotificationPreferences.js"
  28. );
  29. expect(isTaskNotificationSoundEnabled()).toBe(false);
  30. });
  31. it("skips audio playback when the notification sound preference is disabled", async () => {
  32. const audioInstance = {
  33. pause: vi.fn(),
  34. currentTime: 7,
  35. play: vi.fn(() => Promise.resolve()),
  36. preload: "",
  37. };
  38. const audioConstructor = vi.fn(function AudioMock() {
  39. return audioInstance;
  40. });
  41. Object.defineProperty(globalThis, "Audio", {
  42. configurable: true,
  43. value: audioConstructor,
  44. });
  45. localStorageState.set("zcode-notification-enabled", "true");
  46. localStorageState.set("zcode-notification-sound-enabled", "false");
  47. const { playTaskNotificationSound } = await import(
  48. "../src/lib/taskNotificationSound.js"
  49. );
  50. await playTaskNotificationSound();
  51. expect(audioConstructor).not.toHaveBeenCalled();
  52. });
  53. it("plays the sound only when notifications and notification sound are both enabled", async () => {
  54. const pause = vi.fn();
  55. const play = vi.fn(() => Promise.resolve());
  56. const audio = {
  57. pause,
  58. currentTime: 12,
  59. play,
  60. preload: "",
  61. };
  62. const audioConstructor = vi.fn(function AudioMock() {
  63. return audio;
  64. });
  65. Object.defineProperty(globalThis, "Audio", {
  66. configurable: true,
  67. value: audioConstructor,
  68. });
  69. localStorageState.set("zcode-notification-enabled", "true");
  70. localStorageState.set("zcode-notification-sound-enabled", "true");
  71. const { playTaskNotificationSound } = await import(
  72. "../src/lib/taskNotificationSound.js"
  73. );
  74. await playTaskNotificationSound();
  75. expect(audioConstructor).toHaveBeenCalledTimes(1);
  76. expect(pause).toHaveBeenCalledTimes(1);
  77. expect(audio.currentTime).toBe(0);
  78. expect(play).toHaveBeenCalledTimes(1);
  79. });
  80. });