unreadBadge.test.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. parseWindowUnreadCount,
  4. sumWindowUnreadCounts,
  5. supportsAppUnreadBadge,
  6. syncAppUnreadBadge,
  7. } from "../src/main/unreadBadge.js";
  8. describe("unread badge", () => {
  9. it("只接受非负整数的窗口未读数上报", () => {
  10. expect(parseWindowUnreadCount(0)).toBe(0);
  11. expect(parseWindowUnreadCount(3)).toBe(3);
  12. expect(parseWindowUnreadCount(-1)).toBeNull();
  13. expect(parseWindowUnreadCount(1.5)).toBeNull();
  14. expect(parseWindowUnreadCount("2")).toBeNull();
  15. });
  16. it("会把所有窗口的未读数聚合成应用级总数", () => {
  17. expect(
  18. sumWindowUnreadCounts(
  19. new Map([
  20. [1, 2],
  21. [2, 3],
  22. [3, 0],
  23. ]),
  24. ),
  25. ).toBe(5);
  26. });
  27. it("只在 macOS / Linux 启用应用级 badge", () => {
  28. expect(supportsAppUnreadBadge("darwin")).toBe(true);
  29. expect(supportsAppUnreadBadge("linux")).toBe(true);
  30. expect(supportsAppUnreadBadge("win32")).toBe(false);
  31. });
  32. it("支持的平台会把聚合结果写回宿主 badge", () => {
  33. const setBadgeCount = vi.fn();
  34. syncAppUnreadBadge({
  35. platform: "darwin",
  36. totalUnreadCount: 4,
  37. setBadgeCount,
  38. });
  39. expect(setBadgeCount).toHaveBeenCalledWith(4);
  40. });
  41. it("Windows 一期保持 no-op,不提前引入窗口级 overlay 语义", () => {
  42. const setBadgeCount = vi.fn();
  43. syncAppUnreadBadge({
  44. platform: "win32",
  45. totalUnreadCount: 4,
  46. setBadgeCount,
  47. });
  48. expect(setBadgeCount).not.toHaveBeenCalled();
  49. });
  50. });