desktopTraySupport.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. createCloseToTrayCapabilityMonitor,
  4. detectDesktopCloseToTrayCapability,
  5. isGnomeLikeDesktop,
  6. parseStatusNotifierOwnerOutput,
  7. resolveCloseToTrayBootstrapValue,
  8. resolveCloseToTrayRuntimeValue,
  9. resolveCloseToTraySupported,
  10. type TraySupportCommandExecutor,
  11. } from "../src/main/desktopTraySupport.js";
  12. describe("parseStatusNotifierOwnerOutput", () => {
  13. it("parses gdbus / busctl / qdbus outputs", () => {
  14. expect(parseStatusNotifierOwnerOutput("(true,)")).toBe(true);
  15. expect(parseStatusNotifierOwnerOutput("(false,)")).toBe(false);
  16. expect(parseStatusNotifierOwnerOutput('b "true"')).toBe(true);
  17. expect(parseStatusNotifierOwnerOutput('b "false"')).toBe(false);
  18. // CR-01:busctl 的真实布尔输出不带引号(字符串类型才带引号);带引号变体一并接受。
  19. expect(parseStatusNotifierOwnerOutput("b true")).toBe(true);
  20. expect(parseStatusNotifierOwnerOutput("b false")).toBe(false);
  21. expect(parseStatusNotifierOwnerOutput("true")).toBe(true);
  22. expect(parseStatusNotifierOwnerOutput("false")).toBe(false);
  23. expect(parseStatusNotifierOwnerOutput("true\n")).toBe(true);
  24. });
  25. it("returns null for unrecognized output", () => {
  26. expect(parseStatusNotifierOwnerOutput("")).toBeNull();
  27. expect(parseStatusNotifierOwnerOutput("Error: unknown")).toBeNull();
  28. });
  29. });
  30. describe("isGnomeLikeDesktop", () => {
  31. it("detects gnome variants case-insensitively", () => {
  32. expect(isGnomeLikeDesktop({ XDG_CURRENT_DESKTOP: "GNOME" })).toBe(true);
  33. expect(isGnomeLikeDesktop({ XDG_CURRENT_DESKTOP: "ubuntu:GNOME" })).toBe(true);
  34. expect(isGnomeLikeDesktop({ XDG_CURRENT_DESKTOP: "KDE" })).toBe(false);
  35. expect(isGnomeLikeDesktop({ XDG_SESSION_DESKTOP: "gnome-xorg" })).toBe(false);
  36. expect(isGnomeLikeDesktop({})).toBe(false);
  37. });
  38. });
  39. describe("detectDesktopCloseToTrayCapability", () => {
  40. it("short-circuits to supported on non-linux platforms", async () => {
  41. const execute = vi.fn<TraySupportCommandExecutor>();
  42. await expect(
  43. detectDesktopCloseToTrayCapability({ platform: "win32", execute }),
  44. ).resolves.toEqual({ supported: true, gnomeLikeWithoutTray: false });
  45. expect(execute).not.toHaveBeenCalled();
  46. });
  47. it("returns supported when gdbus reports watcher owner", async () => {
  48. const execute = vi.fn<TraySupportCommandExecutor>().mockResolvedValue({ stdout: "(true,)" });
  49. await expect(
  50. detectDesktopCloseToTrayCapability({ platform: "linux", execute }),
  51. ).resolves.toEqual({ supported: true, gnomeLikeWithoutTray: false });
  52. expect(execute).toHaveBeenCalledTimes(1);
  53. expect(execute.mock.calls[0]?.[0]).toBe("gdbus");
  54. });
  55. it("falls back to busctl when gdbus is missing", async () => {
  56. const execute = vi
  57. .fn<TraySupportCommandExecutor>()
  58. .mockRejectedValueOnce(new Error("not found"))
  59. .mockResolvedValueOnce({ stdout: 'b "true"' });
  60. await expect(
  61. detectDesktopCloseToTrayCapability({ platform: "linux", execute }),
  62. ).resolves.toEqual({ supported: true, gnomeLikeWithoutTray: false });
  63. expect(execute).toHaveBeenCalledTimes(2);
  64. expect(execute.mock.calls[1]?.[0]).toBe("busctl");
  65. });
  66. it("falls back to qdbus and parses plain output", async () => {
  67. const execute = vi
  68. .fn<TraySupportCommandExecutor>()
  69. .mockRejectedValueOnce(new Error("not found"))
  70. .mockResolvedValueOnce({ stdout: "garbage" })
  71. .mockResolvedValueOnce({ stdout: "false" });
  72. await expect(
  73. detectDesktopCloseToTrayCapability({ platform: "linux", execute }),
  74. ).resolves.toEqual({ supported: false, gnomeLikeWithoutTray: false });
  75. expect(execute).toHaveBeenCalledTimes(3);
  76. expect(execute.mock.calls[2]?.[0]).toBe("qdbus");
  77. });
  78. it("marks gnomeLikeWithoutTray on GNOME without watcher", async () => {
  79. const execute = vi.fn<TraySupportCommandExecutor>().mockResolvedValue({ stdout: "(false,)" });
  80. await expect(
  81. detectDesktopCloseToTrayCapability({
  82. platform: "linux",
  83. env: { XDG_CURRENT_DESKTOP: "ubuntu:GNOME" },
  84. execute,
  85. }),
  86. ).resolves.toEqual({ supported: false, gnomeLikeWithoutTray: true });
  87. });
  88. it("treats all-probe-failure as unsupported (safe default)", async () => {
  89. const execute = vi.fn<TraySupportCommandExecutor>().mockRejectedValue(new Error("missing"));
  90. await expect(
  91. detectDesktopCloseToTrayCapability({
  92. platform: "linux",
  93. env: { XDG_CURRENT_DESKTOP: "KDE" },
  94. execute,
  95. }),
  96. ).resolves.toEqual({ supported: false, gnomeLikeWithoutTray: false });
  97. expect(execute).toHaveBeenCalledTimes(3);
  98. });
  99. it("passes timeout to the executor", async () => {
  100. const execute = vi.fn<TraySupportCommandExecutor>().mockResolvedValue({ stdout: "(true,)" });
  101. await detectDesktopCloseToTrayCapability({
  102. platform: "linux",
  103. execute,
  104. commandTimeoutMs: 250,
  105. });
  106. expect(execute.mock.calls[0]?.[2]).toBe(250);
  107. });
  108. });
  109. describe("createCloseToTrayCapabilityMonitor", () => {
  110. it("returns null before first refresh completes", () => {
  111. const monitor = createCloseToTrayCapabilityMonitor({ platform: "linux" });
  112. expect(monitor.getSync()).toBeNull();
  113. });
  114. it("caches refresh result for getSync", async () => {
  115. let sequence = 0;
  116. const monitor = createCloseToTrayCapabilityMonitor({
  117. platform: "linux",
  118. execute: vi.fn<TraySupportCommandExecutor>().mockImplementation(async () => {
  119. sequence += 1;
  120. return { stdout: sequence === 1 ? "(true,)" : "(false,)" };
  121. }),
  122. });
  123. await monitor.refresh(true);
  124. expect(monitor.getSync()).toEqual({ supported: true, gnomeLikeWithoutTray: false });
  125. });
  126. it("serves cached result within stale window and re-probes after it", async () => {
  127. let clock = 0;
  128. let sequence = 0;
  129. const execute = vi.fn<TraySupportCommandExecutor>().mockImplementation(async () => {
  130. sequence += 1;
  131. return { stdout: sequence === 1 ? "(true,)" : "(false,)" };
  132. });
  133. const monitor = createCloseToTrayCapabilityMonitor({
  134. platform: "linux",
  135. execute,
  136. now: () => clock,
  137. staleMs: 10_000,
  138. });
  139. await monitor.refresh(true);
  140. expect(execute).toHaveBeenCalledTimes(1);
  141. clock += 5_000;
  142. await monitor.refresh();
  143. expect(execute).toHaveBeenCalledTimes(1);
  144. clock += 6_000;
  145. await monitor.refresh();
  146. expect(execute).toHaveBeenCalledTimes(2);
  147. expect(monitor.getSync()).toEqual({ supported: false, gnomeLikeWithoutTray: false });
  148. });
  149. it("concurrent refreshes share one probe", async () => {
  150. let resolveProbe: ((value: { stdout: string }) => void) | null = null;
  151. const execute = vi.fn<TraySupportCommandExecutor>().mockImplementation(
  152. () =>
  153. new Promise((resolve) => {
  154. resolveProbe = resolve;
  155. }),
  156. );
  157. const monitor = createCloseToTrayCapabilityMonitor({ platform: "linux", execute });
  158. const first = monitor.refresh(true);
  159. const second = monitor.refresh(true);
  160. resolveProbe?.({ stdout: "(true,)" });
  161. const [a, b] = await Promise.all([first, second]);
  162. expect(a).toEqual(b);
  163. expect(execute).toHaveBeenCalledTimes(1);
  164. });
  165. });
  166. describe("resolveCloseToTrayBootstrapValue", () => {
  167. it("keeps windows fallback default true and never migrates", () => {
  168. expect(
  169. resolveCloseToTrayBootstrapValue({
  170. platform: "win32",
  171. stored: undefined,
  172. linuxMigrationInitialized: undefined,
  173. }),
  174. ).toEqual({ value: true, needsLinuxMigration: false });
  175. expect(
  176. resolveCloseToTrayBootstrapValue({
  177. platform: "win32",
  178. stored: false,
  179. linuxMigrationInitialized: undefined,
  180. }),
  181. ).toEqual({ value: false, needsLinuxMigration: false });
  182. });
  183. it("forces linux default off on first launch and requests migration", () => {
  184. expect(
  185. resolveCloseToTrayBootstrapValue({
  186. platform: "linux",
  187. stored: true,
  188. linuxMigrationInitialized: undefined,
  189. }),
  190. ).toEqual({ value: false, needsLinuxMigration: true });
  191. });
  192. it("respects explicit linux choice after migration initialized", () => {
  193. expect(
  194. resolveCloseToTrayBootstrapValue({
  195. platform: "linux",
  196. stored: true,
  197. linuxMigrationInitialized: true,
  198. }),
  199. ).toEqual({ value: true, needsLinuxMigration: false });
  200. expect(
  201. resolveCloseToTrayBootstrapValue({
  202. platform: "linux",
  203. stored: false,
  204. linuxMigrationInitialized: true,
  205. }),
  206. ).toEqual({ value: false, needsLinuxMigration: false });
  207. expect(
  208. resolveCloseToTrayBootstrapValue({
  209. platform: "linux",
  210. stored: undefined,
  211. linuxMigrationInitialized: true,
  212. }),
  213. ).toEqual({ value: false, needsLinuxMigration: false });
  214. });
  215. });
  216. describe("resolveCloseToTraySupported", () => {
  217. it("win32 只依赖托盘实例创建结果", () => {
  218. expect(
  219. resolveCloseToTraySupported({
  220. platform: "win32",
  221. desktopTrayReady: true,
  222. capabilitySupported: undefined,
  223. }),
  224. ).toBe(true);
  225. // CR-01:Windows 托盘创建失败时同样不得隐藏(入口不存在即失联),即使设置开启。
  226. expect(
  227. resolveCloseToTraySupported({
  228. platform: "win32",
  229. desktopTrayReady: false,
  230. capabilitySupported: true,
  231. }),
  232. ).toBe(false);
  233. });
  234. it("linux 要求托盘实例与 DBus 能力同时成立", () => {
  235. expect(
  236. resolveCloseToTraySupported({
  237. platform: "linux",
  238. desktopTrayReady: true,
  239. capabilitySupported: true,
  240. }),
  241. ).toBe(true);
  242. // DBus 可用但 Tray 创建失败(图标缺失/会话未就绪)——不得隐藏。
  243. expect(
  244. resolveCloseToTraySupported({
  245. platform: "linux",
  246. desktopTrayReady: false,
  247. capabilitySupported: true,
  248. }),
  249. ).toBe(false);
  250. // 托盘在但 watcher 掉线/缓存为空——按不可用安全默认。
  251. expect(
  252. resolveCloseToTraySupported({
  253. platform: "linux",
  254. desktopTrayReady: true,
  255. capabilitySupported: false,
  256. }),
  257. ).toBe(false);
  258. expect(
  259. resolveCloseToTraySupported({
  260. platform: "linux",
  261. desktopTrayReady: true,
  262. capabilitySupported: undefined,
  263. }),
  264. ).toBe(false);
  265. });
  266. it("其它平台一律不可隐藏", () => {
  267. expect(
  268. resolveCloseToTraySupported({
  269. platform: "darwin",
  270. desktopTrayReady: true,
  271. capabilitySupported: true,
  272. }),
  273. ).toBe(false);
  274. });
  275. });
  276. describe("resolveCloseToTrayRuntimeValue", () => {
  277. it("迁移持久化成功时采用归位值 false", () => {
  278. expect(
  279. resolveCloseToTrayRuntimeValue({
  280. platform: "linux",
  281. stored: true,
  282. linuxMigrationInitialized: undefined,
  283. migrationPersisted: true,
  284. }),
  285. ).toBe(false);
  286. });
  287. it("迁移持久化失败时回退文件值,维持文件为唯一可信来源(CR-02)", () => {
  288. expect(
  289. resolveCloseToTrayRuntimeValue({
  290. platform: "linux",
  291. stored: true,
  292. linuxMigrationInitialized: undefined,
  293. migrationPersisted: false,
  294. }),
  295. ).toBe(true);
  296. expect(
  297. resolveCloseToTrayRuntimeValue({
  298. platform: "linux",
  299. stored: undefined,
  300. linuxMigrationInitialized: undefined,
  301. migrationPersisted: false,
  302. }),
  303. ).toBe(true);
  304. });
  305. it("无迁移需求的路径不受持久化标志影响", () => {
  306. expect(
  307. resolveCloseToTrayRuntimeValue({
  308. platform: "linux",
  309. stored: false,
  310. linuxMigrationInitialized: true,
  311. migrationPersisted: false,
  312. }),
  313. ).toBe(false);
  314. expect(
  315. resolveCloseToTrayRuntimeValue({
  316. platform: "win32",
  317. stored: undefined,
  318. linuxMigrationInitialized: undefined,
  319. migrationPersisted: false,
  320. }),
  321. ).toBe(true);
  322. });
  323. });