session-tabs-model.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import { describe, expect, test } from "bun:test"
  2. import {
  3. adaptiveSessionTabLayout,
  4. closeSessionTab,
  5. cycleSessionTab,
  6. moveSessionTab,
  7. moveSessionTabHistory,
  8. openSessionTab,
  9. recordClosedSessionTab,
  10. recordSessionTabHistory,
  11. reopenSessionTab,
  12. seedSessionTabMotion,
  13. sessionTabComplete,
  14. sessionTabOverflowWidth,
  15. } from "../../src/context/session-tabs-model"
  16. describe("session tabs", () => {
  17. test("moves a tab to a clamped index and returns the same tabs for no-ops", () => {
  18. const tabs = ["a", "b", "c"].map((sessionID) => ({ sessionID }))
  19. expect(moveSessionTab(tabs, "a", 2).map((tab) => tab.sessionID)).toEqual(["b", "c", "a"])
  20. expect(moveSessionTab(tabs, "c", -5).map((tab) => tab.sessionID)).toEqual(["c", "a", "b"])
  21. expect(moveSessionTab(tabs, "b", 99).map((tab) => tab.sessionID)).toEqual(["a", "c", "b"])
  22. expect(moveSessionTab(tabs, "b", 1)).toBe(tabs)
  23. expect(moveSessionTab(tabs, "missing", 0)).toBe(tabs)
  24. })
  25. test("open seeding keeps survivors and grows the new tab from zero", () => {
  26. const seeded = seedSessionTabMotion(
  27. ["a", "b"],
  28. ["a", "b", "c"],
  29. { widths: [35, 35], selections: [1, 0], activities: [0, 1] },
  30. { widths: [24, 23, 23], selections: [1, 0, 0], activities: [0, 1, 0] },
  31. )
  32. expect(seeded).toEqual({ widths: [35, 35, 0], selections: [1, 0, 0], activities: [0, 1, 0] })
  33. })
  34. test("close seeding keeps survivors at their current animated widths", () => {
  35. const seeded = seedSessionTabMotion(
  36. ["a", "b", "c"],
  37. ["a", "c"],
  38. { widths: [24, 23, 23], selections: [1, 0, 0], activities: [0, 0, 1] },
  39. { widths: [35, 35], selections: [1, 0], activities: [0, 1] },
  40. )
  41. expect(seeded).toEqual({ widths: [24, 23], selections: [1, 0], activities: [0, 1] })
  42. })
  43. test("window shifts keep retained tabs and grow revealed ones", () => {
  44. const seeded = seedSessionTabMotion(
  45. ["a", "b", "c"],
  46. ["b", "c", "d"],
  47. { widths: [22, 8, 8], selections: [1, 0, 0], activities: [0, 0, 0] },
  48. { widths: [8, 8, 22], selections: [0, 0, 1], activities: [0, 0, 0] },
  49. )
  50. expect(seeded).toEqual({ widths: [8, 8, 0], selections: [0, 0, 1], activities: [0, 0, 0] })
  51. })
  52. test("fully replaced windows jump instead of seeding", () => {
  53. const values = { widths: [22], selections: [1], activities: [0] }
  54. expect(seedSessionTabMotion(["a"], ["z"], values, values)).toBeUndefined()
  55. })
  56. test("overflow markers reserve room for a gap beside their digits", () => {
  57. expect(sessionTabOverflowWidth(5)).toBe(3)
  58. expect(sessionTabOverflowWidth(12)).toBe(4)
  59. })
  60. test("opens each session once and refreshes its title", () => {
  61. const tabs = openSessionTab([{ sessionID: "a", title: "Old" }], { sessionID: "a", title: "New" })
  62. expect(tabs).toEqual([{ sessionID: "a", title: "New" }])
  63. expect(openSessionTab(tabs, { sessionID: "b" })).toEqual([{ sessionID: "a", title: "New" }, { sessionID: "b" }])
  64. expect(openSessionTab(tabs, { sessionID: "a", title: "New" })).toBe(tabs)
  65. })
  66. test("selects the right tab then the left tab after closing", () => {
  67. expect(closeSessionTab([{ sessionID: "a" }, { sessionID: "b" }, { sessionID: "c" }], "b")).toEqual({
  68. tabs: [{ sessionID: "a" }, { sessionID: "c" }],
  69. next: "c",
  70. })
  71. expect(closeSessionTab([{ sessionID: "a" }, { sessionID: "b" }], "b").next).toBe("a")
  72. expect(closeSessionTab([{ sessionID: "a" }], "a").next).toBeUndefined()
  73. })
  74. test("closing an unknown session returns the same tabs reference", () => {
  75. const tabs = [{ sessionID: "a" }, { sessionID: "b" }]
  76. expect(closeSessionTab(tabs, "missing").tabs).toBe(tabs)
  77. })
  78. test("cycles through a filtered tab set in either direction", () => {
  79. const tabs = ["a", "c", "e"].map((sessionID) => ({ sessionID }))
  80. expect(cycleSessionTab(tabs, "c", 1)?.sessionID).toBe("e")
  81. expect(cycleSessionTab(tabs, "c", -1)?.sessionID).toBe("a")
  82. expect(cycleSessionTab(tabs, "e", 1)?.sessionID).toBe("a")
  83. expect(cycleSessionTab(tabs, "b", 1)?.sessionID).toBe("a")
  84. })
  85. test("cycles to the nearest matching tab from an unmatched active tab", () => {
  86. const tabs = ["a", "b", "c", "d", "e"].map((sessionID) => ({ sessionID }))
  87. const unread = new Set(["a", "d"])
  88. const matches = (tab: { sessionID: string }) => unread.has(tab.sessionID)
  89. expect(cycleSessionTab(tabs, "c", 1, matches)?.sessionID).toBe("d")
  90. expect(cycleSessionTab(tabs, "c", -1, matches)?.sessionID).toBe("a")
  91. expect(cycleSessionTab(tabs, "e", 1, matches)?.sessionID).toBe("a")
  92. expect(cycleSessionTab(tabs, "a", -1, matches)?.sessionID).toBe("d")
  93. })
  94. test("moves backward and forward through selection history", () => {
  95. const tabs = ["a", "b", "c", "d"].map((sessionID) => ({ sessionID }))
  96. const history = ["a", "b", "c", "d"].reduce(recordSessionTabHistory, { entries: [], index: -1 })
  97. const backToC = moveSessionTabHistory(history, tabs, "d", -1)
  98. const backToB = moveSessionTabHistory(backToC.history, tabs, "c", -1)
  99. const forwardToC = moveSessionTabHistory(backToB.history, tabs, "b", 1)
  100. const forwardToD = moveSessionTabHistory(forwardToC.history, tabs, "c", 1)
  101. expect([backToC.sessionID, backToB.sessionID, forwardToC.sessionID, forwardToD.sessionID]).toEqual([
  102. "c",
  103. "b",
  104. "c",
  105. "d",
  106. ])
  107. })
  108. test("truncates forward history after a new selection", () => {
  109. const tabs = ["a", "b", "c", "d"].map((sessionID) => ({ sessionID }))
  110. const history = ["a", "b", "c"].reduce(recordSessionTabHistory, { entries: [], index: -1 })
  111. const back = moveSessionTabHistory(history, tabs, "c", -1)
  112. const branched = recordSessionTabHistory(back.history, "d")
  113. expect(branched).toEqual({ entries: ["a", "b", "d"], index: 2 })
  114. expect(moveSessionTabHistory(branched, tabs, "d", 1).sessionID).toBeUndefined()
  115. })
  116. test("skips closed tabs and duplicate entries for the active tab", () => {
  117. const tabs = ["a", "b"].map((sessionID) => ({ sessionID }))
  118. const history = ["a", "b", "c", "b"].reduce(recordSessionTabHistory, { entries: [], index: -1 })
  119. expect(moveSessionTabHistory(history, tabs, "b", -1).sessionID).toBe("a")
  120. expect(recordSessionTabHistory(history, "b")).toBe(history)
  121. })
  122. test("drops the oldest history entries beyond the limit", () => {
  123. const sessions = Array.from({ length: 150 }, (_, index) => `session-${index}`)
  124. const history = sessions.reduce(recordSessionTabHistory, { entries: [], index: -1 })
  125. expect(history.entries.length).toBe(100)
  126. expect(history.entries[0]).toBe("session-50")
  127. expect(history.entries[history.index]).toBe("session-149")
  128. })
  129. test("returns to the latest history entry when no tab is active", () => {
  130. const tabs = ["a", "b"].map((sessionID) => ({ sessionID }))
  131. const history = ["a", "b"].reduce(recordSessionTabHistory, { entries: [], index: -1 })
  132. expect(moveSessionTabHistory(history, tabs, undefined, -1).sessionID).toBe("b")
  133. })
  134. test("returns to the previous selected open tab after closing the active tab", () => {
  135. const tabs = ["a", "b", "c"].map((sessionID) => ({ sessionID }))
  136. const history = ["a", "c"].reduce(recordSessionTabHistory, { entries: [], index: -1 })
  137. const closed = closeSessionTab(tabs, "b")
  138. const current = recordSessionTabHistory(history, "b")
  139. expect(moveSessionTabHistory(current, closed.tabs, "b", -1).sessionID).toBe("c")
  140. })
  141. test("reopens the most recently closed tab at its original position", () => {
  142. const tabs = ["a", "b", "c"].map((sessionID) => ({ sessionID }))
  143. const stack = recordClosedSessionTab([], { sessionID: "b", title: "Middle" }, 1)
  144. const reopened = reopenSessionTab(stack, [{ sessionID: "a" }, { sessionID: "c" }])
  145. expect(reopened.sessionID).toBe("b")
  146. expect(reopened.tabs).toEqual([{ sessionID: "a" }, { sessionID: "b", title: "Middle" }, { sessionID: "c" }])
  147. expect(reopened.stack).toEqual([])
  148. expect(reopenSessionTab([], tabs)).toEqual({ stack: [], tabs: undefined, sessionID: undefined })
  149. })
  150. test("skips and consumes closed entries that are already open", () => {
  151. const stack = [
  152. { tab: { sessionID: "a" }, index: 0 },
  153. { tab: { sessionID: "b" }, index: 1 },
  154. ]
  155. const reopened = reopenSessionTab(stack, [{ sessionID: "b" }])
  156. expect(reopened.sessionID).toBe("a")
  157. expect(reopened.tabs).toEqual([{ sessionID: "a" }, { sessionID: "b" }])
  158. expect(reopened.stack).toEqual([])
  159. })
  160. test("clamps restored positions and keeps one entry per session", () => {
  161. const twice = recordClosedSessionTab(recordClosedSessionTab([], { sessionID: "a" }, 5), { sessionID: "a" }, 2)
  162. expect(twice).toEqual([{ tab: { sessionID: "a" }, index: 2 }])
  163. const reopened = reopenSessionTab(twice, [{ sessionID: "b" }])
  164. expect(reopened.tabs).toEqual([{ sessionID: "b" }, { sessionID: "a" }])
  165. const overflow = Array.from({ length: 12 }, (_, index) => ({ sessionID: String(index) })).reduce(
  166. (stack, tab, index) => recordClosedSessionTab(stack, tab, index),
  167. twice,
  168. )
  169. expect(overflow).toHaveLength(10)
  170. expect(overflow.at(-1)?.tab.sessionID).toBe("11")
  171. expect(overflow[0]?.tab.sessionID).toBe("2")
  172. })
  173. test("reveals completion activity only after session work becomes idle", () => {
  174. expect(sessionTabComplete("activity", true)).toBe(false)
  175. expect(sessionTabComplete("activity", false)).toBe(true)
  176. expect(sessionTabComplete("error", false)).toBe(false)
  177. })
  178. test("expands the active tab and keeps inactive widths equal", () => {
  179. const tabs = ["a", "b", "c", "d", "e", "f", "g"].map((sessionID) => ({ sessionID }))
  180. const layout = adaptiveSessionTabLayout(tabs, "d", 76)
  181. expect(layout).toMatchObject({ before: 0, after: 0, start: 0, total: 76 })
  182. expect(layout.widths).toEqual([9, 9, 9, 22, 9, 9, 9])
  183. expect(layout.widths.reduce((total, width) => total + width, 0)).toBe(76)
  184. })
  185. test("reserves an active tab slot for the new session page", () => {
  186. const tabs = ["a", "b", "c", "d", "new"].map((sessionID) => ({ sessionID }))
  187. const layout = adaptiveSessionTabLayout(tabs, "new", 54)
  188. expect(layout.tabs).toEqual(tabs)
  189. expect(layout.widths).toEqual([8, 8, 8, 8, 22])
  190. expect(layout.widths.reduce((total, width) => total + width, 0)).toBe(layout.total)
  191. })
  192. test("keeps the visible tab window stable on the new session page", () => {
  193. const tabs = Array.from({ length: 10 }, (_, index) => ({ sessionID: String(index) }))
  194. const selected = adaptiveSessionTabLayout(tabs, "7", 70)
  195. const home = adaptiveSessionTabLayout(tabs, undefined, 70, selected.start)
  196. expect(selected.start).toBeGreaterThan(0)
  197. expect(home.start).toBe(selected.start)
  198. })
  199. test("only swaps old and new active width inside a sticky window", () => {
  200. const tabs = ["a", "b", "c", "d", "e", "f", "g"].map((sessionID) => ({ sessionID }))
  201. const before = adaptiveSessionTabLayout(tabs, "c", 76)
  202. const after = adaptiveSessionTabLayout(tabs, "d", 76, before.start)
  203. expect(before.start).toBe(after.start)
  204. expect(before.widths).toEqual([9, 9, 22, 9, 9, 9, 9])
  205. expect(after.widths).toEqual([9, 9, 9, 22, 9, 9, 9])
  206. })
  207. test("shares roomy width equally without changing widths on selection", () => {
  208. const tabs = ["a", "b", "c"].map((sessionID) => ({ sessionID }))
  209. const before = adaptiveSessionTabLayout(tabs, "a", 100)
  210. const after = adaptiveSessionTabLayout(tabs, "c", 100, before.start)
  211. expect(before.widths).toEqual([32, 32, 32])
  212. expect(after.widths).toEqual(before.widths)
  213. expect(before.total).toBe(96)
  214. })
  215. test("caps a single tab instead of stretching it across the terminal", () => {
  216. const layout = adaptiveSessionTabLayout([{ sessionID: "a" }], "a", 100)
  217. expect(layout.widths).toEqual([32])
  218. expect(layout.total).toBe(32)
  219. })
  220. test("fills roomy space equally below the maximum width", () => {
  221. const tabs = ["a", "b", "c", "d"].map((sessionID) => ({ sessionID }))
  222. expect(adaptiveSessionTabLayout(tabs, "b", 100).widths).toEqual([25, 25, 25, 25])
  223. })
  224. test("expands only the active tab under compact pressure", () => {
  225. const tabs = ["a", "b", "c", "d", "e"].map((sessionID) => ({ sessionID }))
  226. const before = adaptiveSessionTabLayout(tabs, "c", 100)
  227. const after = adaptiveSessionTabLayout(tabs, "d", 100, before.start)
  228. expect(before.widths).toEqual([19, 19, 24, 19, 19])
  229. expect(after.widths).toEqual([19, 19, 19, 24, 19])
  230. })
  231. test("moves the window only after selection crosses its edge", () => {
  232. const tabs = Array.from({ length: 10 }, (_, index) => ({ sessionID: String(index) }))
  233. const initial = adaptiveSessionTabLayout(tabs, "3", 70)
  234. const inside = adaptiveSessionTabLayout(tabs, "4", 70, initial.start)
  235. const crossed = adaptiveSessionTabLayout(tabs, "7", 70, inside.start)
  236. expect(initial.start).toBe(0)
  237. expect(inside.start).toBe(0)
  238. expect(crossed.start).toBeGreaterThan(0)
  239. expect(crossed.tabs.some((tab) => tab.sessionID === "7")).toBe(true)
  240. expect(crossed.widths.reduce((total, width) => total + width, 0)).toBe(crossed.total)
  241. })
  242. })