webRemoteControlKnownDevices.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { describe, expect, it } from "vitest";
  2. import type { WebRemoteControlDeviceGroup } from "#src/web-remote-control/index.js";
  3. import {
  4. KNOWN_DEVICES_KEY,
  5. KNOWN_DEVICES_LIMIT,
  6. readKnownDevices,
  7. rememberKnownDevices,
  8. } from "#src/web-remote-control/index.js";
  9. function memoryStorage(initial?: string) {
  10. const map = new Map<string, string>();
  11. if (initial !== undefined) map.set(KNOWN_DEVICES_KEY, initial);
  12. return {
  13. map,
  14. getItem: (key: string) => map.get(key) ?? null,
  15. setItem: (key: string, value: string) => {
  16. map.set(key, value);
  17. },
  18. };
  19. }
  20. const onlineGroup = (
  21. deviceMid: string,
  22. name?: string,
  23. platform?: string,
  24. ): WebRemoteControlDeviceGroup => ({
  25. deviceMid,
  26. name,
  27. platform,
  28. onlineConnections: [
  29. {
  30. device_sid: `${deviceMid}-sid`,
  31. device_mid: deviceMid,
  32. online: true,
  33. created_at: 1,
  34. meta: {},
  35. },
  36. ],
  37. });
  38. describe("本地记住的设备名与平台", () => {
  39. it("兼容旧 [mid, name] 二元组,接受 [mid, name, platform],忽略形状不对的条目并去掉首尾空白", () => {
  40. const storage = memoryStorage(
  41. JSON.stringify([
  42. ["mid-1", " One "],
  43. ["mid-2"],
  44. "junk",
  45. ["mid-3", " "],
  46. [3, "x"],
  47. ["", "empty"],
  48. ["mid-4", "Four", " darwin "],
  49. ["mid-5", "", "linux"],
  50. ["mid-6", "Six", 7],
  51. ["mid-7", "Seven", "win32", "extra"],
  52. ]),
  53. );
  54. expect([...readKnownDevices(storage)]).toEqual([
  55. ["mid-1", { name: "One" }],
  56. ["mid-4", { name: "Four", platform: "darwin" }],
  57. ["mid-5", { platform: "linux" }],
  58. ]);
  59. });
  60. it("缺失、非数组 JSON、损坏内容或读取抛错都按没有记住任何设备处理", () => {
  61. expect(readKnownDevices(memoryStorage()).size).toBe(0);
  62. expect(readKnownDevices(memoryStorage('{"mid-1":"One"}')).size).toBe(0);
  63. expect(readKnownDevices(memoryStorage("{not json")).size).toBe(0);
  64. expect(
  65. readKnownDevices({
  66. getItem() {
  67. throw new Error("blocked");
  68. },
  69. setItem() {},
  70. }).size,
  71. ).toBe(0);
  72. });
  73. it("只写入有在线连接且有名字或平台的设备项,再次见到的设备移到最近位置", () => {
  74. const storage = memoryStorage(
  75. JSON.stringify([
  76. ["mid-a", "A"],
  77. ["mid-b", "B", "win32"],
  78. ]),
  79. );
  80. const known = readKnownDevices(storage);
  81. rememberKnownDevices(storage, known, [
  82. onlineGroup("mid-a", "A renamed", "darwin"),
  83. onlineGroup("mid-c", "C"),
  84. onlineGroup("mid-d"),
  85. onlineGroup("mid-p", undefined, "linux"),
  86. { deviceMid: "mid-e", name: "E (offline, from cache)", onlineConnections: [] },
  87. ]);
  88. expect(JSON.parse(storage.map.get(KNOWN_DEVICES_KEY)!)).toEqual([
  89. ["mid-b", "B", "win32"],
  90. ["mid-a", "A renamed", "darwin"],
  91. ["mid-c", "C"],
  92. ["mid-p", "", "linux"],
  93. ]);
  94. });
  95. it("没有任何可写入的设备项时不触碰存储", () => {
  96. const storage = memoryStorage(JSON.stringify([["mid-a", "A"]]));
  97. const known = readKnownDevices(storage);
  98. let writes = 0;
  99. rememberKnownDevices({ ...storage, setItem: () => void writes++ }, known, [
  100. { deviceMid: "mid-a", name: "A", onlineConnections: [] },
  101. onlineGroup("mid-x"),
  102. ]);
  103. expect(writes).toBe(0);
  104. });
  105. it(`超过 ${KNOWN_DEVICES_LIMIT} 条时淘汰最久未更新的条目`, () => {
  106. const seeded = Array.from({ length: KNOWN_DEVICES_LIMIT }, (_, i) => [`mid-${i}`, `D${i}`]);
  107. const storage = memoryStorage(JSON.stringify(seeded));
  108. const known = readKnownDevices(storage);
  109. rememberKnownDevices(storage, known, [
  110. onlineGroup("mid-0", "D0"),
  111. onlineGroup("mid-new", "New"),
  112. ]);
  113. const stored = JSON.parse(storage.map.get(KNOWN_DEVICES_KEY)!) as [string, string][];
  114. expect(stored).toHaveLength(KNOWN_DEVICES_LIMIT);
  115. expect(stored[0]).toEqual(["mid-2", "D2"]);
  116. expect(stored.at(-2)).toEqual(["mid-0", "D0"]);
  117. expect(stored.at(-1)).toEqual(["mid-new", "New"]);
  118. });
  119. it("写入失败静默忽略,内存中的映射仍已更新", () => {
  120. const known = new Map();
  121. expect(() =>
  122. rememberKnownDevices(
  123. {
  124. getItem: () => null,
  125. setItem() {
  126. throw new Error("blocked");
  127. },
  128. },
  129. known,
  130. [onlineGroup("mid-a", "A", "darwin")],
  131. ),
  132. ).not.toThrow();
  133. expect([...known]).toEqual([["mid-a", { name: "A", platform: "darwin" }]]);
  134. });
  135. });