browserTabResidencyPolicy.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. BROWSER_TAB_LIMIT,
  4. selectBrowserTabLimitVictim,
  5. type BrowserTabResidencyCandidate,
  6. } 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("Browser tab residency policy", () => {
  34. it("BTL04: 第 33 个逻辑 tab 出现时忽略 task/preferred 身份并选择最老 victim", () => {
  35. const tabs = Array.from({ length: BROWSER_TAB_LIMIT + 1 }, (_, index) =>
  36. candidate(`tab-${String(index).padStart(2, "0")}`, {
  37. openedAt: index + 2,
  38. lastActivityAt: index + 2,
  39. }),
  40. );
  41. tabs[0] = candidate("oldest-preferred-within-former-protection-window", {
  42. lastActivityAt: 0,
  43. lastSelectedAt: 19_999,
  44. preferred: true,
  45. });
  46. tabs[1] = candidate("current-task-secondary", {
  47. currentTask: true,
  48. lastActivityAt: 1,
  49. });
  50. expect(
  51. selectBrowserTabLimitVictim(tabs, {
  52. windowId: 1,
  53. })?.tabId,
  54. ).toBe("oldest-preferred-within-former-protection-window");
  55. });
  56. it("BTL04: preferred tab 即使刚选择过也不再获得常驻保护", () => {
  57. const tabs = Array.from({ length: BROWSER_TAB_LIMIT + 1 }, (_, index) =>
  58. candidate(`tab-${index}`, {
  59. openedAt: index,
  60. lastActivityAt: index,
  61. lastSelectedAt: 20_000 + index,
  62. preferred: true,
  63. }),
  64. );
  65. expect(
  66. selectBrowserTabLimitVictim(tabs, {
  67. windowId: 1,
  68. })?.tabId,
  69. ).toBe("tab-0");
  70. });
  71. it("BTL05: 所有候选都处于运行态保护时允许暂时超额", () => {
  72. const protectedTabs = Array.from({ length: BROWSER_TAB_LIMIT + 1 }, (_, index) =>
  73. candidate(`protected-${index}`, { operationActive: true }),
  74. );
  75. expect(
  76. selectBrowserTabLimitVictim(protectedTabs, {
  77. windowId: 1,
  78. }),
  79. ).toBeNull();
  80. });
  81. it("保护可见、选中、操作、捕获、声音、媒体、加载和下载中的 tab", () => {
  82. const protectedVariants: Array<Partial<BrowserTabResidencyCandidate>> = [
  83. { visible: true, residency: "live-visible" },
  84. { selected: true },
  85. { operationActive: true },
  86. { captureActive: true },
  87. { audible: true },
  88. { mediaActive: true },
  89. { loading: true },
  90. { downloadActive: true },
  91. { residency: "restoring" },
  92. ];
  93. const tabs = [
  94. ...protectedVariants.map((overrides, index) => candidate(`protected-${index}`, overrides)),
  95. ...Array.from({ length: BROWSER_TAB_LIMIT + 1 }, (_, index) =>
  96. candidate(`eligible-${index}`, { lastActivityAt: index }),
  97. ),
  98. ];
  99. expect(
  100. selectBrowserTabLimitVictim(tabs, {
  101. windowId: 1,
  102. })?.tabId,
  103. ).toBe("eligible-0");
  104. });
  105. it("每个 BrowserWindow 独立计数,visible tab 也占逻辑 tab 上限", () => {
  106. const firstWindow = Array.from({ length: BROWSER_TAB_LIMIT - 1 }, (_, index) =>
  107. candidate(`first-${index}`),
  108. );
  109. const secondWindow = Array.from({ length: BROWSER_TAB_LIMIT }, (_, index) =>
  110. candidate(`second-${index}`, { windowId: 2 }),
  111. );
  112. const visible = candidate("visible", {
  113. residency: "live-visible",
  114. selected: true,
  115. visible: true,
  116. });
  117. expect(
  118. selectBrowserTabLimitVictim([...firstWindow, ...secondWindow, visible], {
  119. windowId: 1,
  120. }),
  121. ).toBeNull();
  122. expect(
  123. selectBrowserTabLimitVictim(
  124. [...firstWindow, ...secondWindow, { ...visible, tabId: "visible-2", windowId: 2 }],
  125. {
  126. windowId: 2,
  127. },
  128. )?.tabId,
  129. ).toBe("second-0");
  130. });
  131. it("BTL20: suspended 或 detached logical shell 仍计入上限并可成为最老 victim", () => {
  132. const tabs = [
  133. candidate("suspended-oldest", {
  134. residency: "suspended",
  135. guestAttached: false,
  136. openedAt: 0,
  137. lastActivityAt: 0,
  138. }),
  139. candidate("live-a", { openedAt: 1, lastActivityAt: 1 }),
  140. candidate("live-b", { openedAt: 2, lastActivityAt: 2 }),
  141. ];
  142. expect(
  143. selectBrowserTabLimitVictim(tabs, {
  144. tabLimit: 2,
  145. windowId: 1,
  146. })?.tabId,
  147. ).toBe("suspended-oldest");
  148. });
  149. });