browserTabResidencyCoordinator.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. BrowserTabResidencyCoordinator,
  4. type BrowserTabResidencyRecord,
  5. } from "../src/main/browserView/browserTabResidencyCoordinator.js";
  6. import type { BrowserTabResidencyCandidate } from "../src/main/browserView/browserTabResidencyPolicy.js";
  7. function candidate(
  8. tabId: string,
  9. overrides: Partial<BrowserTabResidencyCandidate> = {},
  10. ): BrowserTabResidencyCandidate {
  11. return {
  12. tabId,
  13. windowId: 1,
  14. sessionId: "task-a",
  15. residency: "live-background",
  16. guestAttached: true,
  17. openedAt: 1,
  18. lastActivityAt: 1,
  19. lastSelectedAt: null,
  20. preferred: false,
  21. currentTask: false,
  22. selected: false,
  23. visible: false,
  24. operationActive: false,
  25. captureActive: false,
  26. audible: false,
  27. mediaActive: false,
  28. loading: false,
  29. downloadActive: false,
  30. ...overrides,
  31. };
  32. }
  33. describe("BrowserTabResidencyCoordinator", () => {
  34. it("BTL20: detached/suspended shell 计入逻辑上限,并可作为最老 victim 关闭", async () => {
  35. const evicted: string[] = [];
  36. const coordinator = new BrowserTabResidencyCoordinator({
  37. tabLimit: 2,
  38. onEvict: async (record) => {
  39. evicted.push(record.tabId);
  40. return true;
  41. },
  42. });
  43. coordinator.upsert(
  44. candidate("browser:ghost", {
  45. guestAttached: false,
  46. residency: "suspended",
  47. openedAt: 0,
  48. lastActivityAt: 0,
  49. }),
  50. );
  51. coordinator.upsert(candidate("browser:real-old", { openedAt: 1 }));
  52. coordinator.upsert(candidate("browser:real-new", { openedAt: 2, lastActivityAt: 2 }));
  53. await coordinator.whenIdle();
  54. expect(evicted).toEqual(["browser:ghost"]);
  55. expect(coordinator.get("browser:ghost")).toBeNull();
  56. expect(coordinator.get("browser:real-new")?.residency).toBe("live-background");
  57. coordinator.dispose();
  58. });
  59. it("同一窗口、同一任务只保留最后选中的 preferred tab", async () => {
  60. let now = 100;
  61. const evicted: BrowserTabResidencyRecord[] = [];
  62. const coordinator = new BrowserTabResidencyCoordinator({
  63. now: () => now,
  64. onEvict: async (record) => {
  65. evicted.push(record);
  66. return true;
  67. },
  68. });
  69. coordinator.upsert(candidate("browser:a"));
  70. coordinator.upsert(candidate("browser:b"));
  71. coordinator.report("browser:a", { selected: true });
  72. now = 200;
  73. coordinator.report("browser:b", { selected: true });
  74. await coordinator.whenIdle();
  75. expect(coordinator.get("browser:a")?.preferred).toBe(false);
  76. expect(coordinator.get("browser:b")?.preferred).toBe(true);
  77. expect(evicted).toEqual([]);
  78. coordinator.dispose();
  79. });
  80. it("不同任务分别保留自己的 preferred tab", async () => {
  81. const coordinator = new BrowserTabResidencyCoordinator({
  82. onEvict: async () => true,
  83. });
  84. coordinator.upsert(candidate("browser:a", { sessionId: "task-a" }));
  85. coordinator.upsert(candidate("browser:b", { sessionId: "task-b" }));
  86. coordinator.report("browser:a", { selected: true });
  87. coordinator.report("browser:b", { selected: true });
  88. await coordinator.whenIdle();
  89. expect(coordinator.get("browser:a")?.preferred).toBe(true);
  90. expect(coordinator.get("browser:b")?.preferred).toBe(true);
  91. coordinator.dispose();
  92. });
  93. it("运行、捕获与媒体等保护状态开始时会刷新 LRU 活动时间", () => {
  94. let now = 100;
  95. const coordinator = new BrowserTabResidencyCoordinator({
  96. now: () => now,
  97. onEvict: async () => true,
  98. });
  99. coordinator.upsert(candidate("browser:a"));
  100. for (const patch of [
  101. { operationActive: true },
  102. { captureActive: true },
  103. { audible: true },
  104. { mediaActive: true },
  105. { loading: true },
  106. { downloadActive: true },
  107. ]) {
  108. now += 10;
  109. coordinator.report("browser:a", patch);
  110. expect(coordinator.get("browser:a")?.lastActivityAt).toBe(now);
  111. }
  112. coordinator.dispose();
  113. });
  114. it("恢复失败只回滚当前 generation,并推进 generation 拒绝迟到 attach", () => {
  115. const coordinator = new BrowserTabResidencyCoordinator({
  116. onEvict: async () => true,
  117. });
  118. coordinator.upsert(candidate("browser:a", { residency: "suspended" }));
  119. const restoring = coordinator.markRestoring("browser:a")!;
  120. expect(coordinator.failRestore("browser:a", restoring.generation - 1)).toBeNull();
  121. const failed = coordinator.failRestore("browser:a", restoring.generation)!;
  122. expect(failed.residency).toBe("suspended");
  123. expect(failed.loading).toBe(false);
  124. expect(failed.generation).toBe(restoring.generation + 1);
  125. coordinator.dispose();
  126. });
  127. it("BTL15: durable close 失败时保留 logical tab 并停止本轮重试", async () => {
  128. const evicted: string[] = [];
  129. const coordinator = new BrowserTabResidencyCoordinator({
  130. tabLimit: 0,
  131. onEvict: async (record) => {
  132. evicted.push(record.tabId);
  133. return false;
  134. },
  135. });
  136. coordinator.upsert(candidate("browser:a"));
  137. await coordinator.whenIdle();
  138. expect(evicted).toEqual(["browser:a"]);
  139. expect(coordinator.get("browser:a")?.residency).toBe("live-background");
  140. coordinator.dispose();
  141. });
  142. });