1
0

session-tabs-model.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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("cycles through a filtered tab set in either direction", () => {
  75. const tabs = ["a", "c", "e"].map((sessionID) => ({ sessionID }))
  76. expect(cycleSessionTab(tabs, "c", 1)?.sessionID).toBe("e")
  77. expect(cycleSessionTab(tabs, "c", -1)?.sessionID).toBe("a")
  78. expect(cycleSessionTab(tabs, "e", 1)?.sessionID).toBe("a")
  79. expect(cycleSessionTab(tabs, "b", 1)?.sessionID).toBe("a")
  80. })
  81. test("moves backward and forward through selection history", () => {
  82. const tabs = ["a", "b", "c", "d"].map((sessionID) => ({ sessionID }))
  83. const history = ["a", "b", "c", "d"].reduce(recordSessionTabHistory, { entries: [], index: -1 })
  84. const backToC = moveSessionTabHistory(history, tabs, "d", -1)
  85. const backToB = moveSessionTabHistory(backToC.history, tabs, "c", -1)
  86. const forwardToC = moveSessionTabHistory(backToB.history, tabs, "b", 1)
  87. const forwardToD = moveSessionTabHistory(forwardToC.history, tabs, "c", 1)
  88. expect([backToC.sessionID, backToB.sessionID, forwardToC.sessionID, forwardToD.sessionID]).toEqual([
  89. "c",
  90. "b",
  91. "c",
  92. "d",
  93. ])
  94. })
  95. test("truncates forward history after a new selection", () => {
  96. const tabs = ["a", "b", "c", "d"].map((sessionID) => ({ sessionID }))
  97. const history = ["a", "b", "c"].reduce(recordSessionTabHistory, { entries: [], index: -1 })
  98. const back = moveSessionTabHistory(history, tabs, "c", -1)
  99. const branched = recordSessionTabHistory(back.history, "d")
  100. expect(branched).toEqual({ entries: ["a", "b", "d"], index: 2 })
  101. expect(moveSessionTabHistory(branched, tabs, "d", 1).sessionID).toBeUndefined()
  102. })
  103. test("skips closed tabs and duplicate entries for the active tab", () => {
  104. const tabs = ["a", "b"].map((sessionID) => ({ sessionID }))
  105. const history = ["a", "b", "c", "b"].reduce(recordSessionTabHistory, { entries: [], index: -1 })
  106. expect(moveSessionTabHistory(history, tabs, "b", -1).sessionID).toBe("a")
  107. expect(recordSessionTabHistory(history, "b")).toBe(history)
  108. })
  109. test("returns to the latest history entry when no tab is active", () => {
  110. const tabs = ["a", "b"].map((sessionID) => ({ sessionID }))
  111. const history = ["a", "b"].reduce(recordSessionTabHistory, { entries: [], index: -1 })
  112. expect(moveSessionTabHistory(history, tabs, undefined, -1).sessionID).toBe("b")
  113. })
  114. test("returns to the previous selected open tab after closing the active tab", () => {
  115. const tabs = ["a", "b", "c"].map((sessionID) => ({ sessionID }))
  116. const history = ["a", "c"].reduce(recordSessionTabHistory, { entries: [], index: -1 })
  117. const closed = closeSessionTab(tabs, "b")
  118. const current = recordSessionTabHistory(history, "b")
  119. expect(moveSessionTabHistory(current, closed.tabs, "b", -1).sessionID).toBe("c")
  120. })
  121. test("reopens the most recently closed tab at its original position", () => {
  122. const tabs = ["a", "b", "c"].map((sessionID) => ({ sessionID }))
  123. const stack = recordClosedSessionTab([], { sessionID: "b", title: "Middle" }, 1)
  124. const reopened = reopenSessionTab(stack, [{ sessionID: "a" }, { sessionID: "c" }])
  125. expect(reopened.sessionID).toBe("b")
  126. expect(reopened.tabs).toEqual([{ sessionID: "a" }, { sessionID: "b", title: "Middle" }, { sessionID: "c" }])
  127. expect(reopened.stack).toEqual([])
  128. expect(reopenSessionTab([], tabs)).toEqual({ stack: [], tabs: undefined, sessionID: undefined })
  129. })
  130. test("skips and consumes closed entries that are already open", () => {
  131. const stack = [
  132. { tab: { sessionID: "a" }, index: 0 },
  133. { tab: { sessionID: "b" }, index: 1 },
  134. ]
  135. const reopened = reopenSessionTab(stack, [{ sessionID: "b" }])
  136. expect(reopened.sessionID).toBe("a")
  137. expect(reopened.tabs).toEqual([{ sessionID: "a" }, { sessionID: "b" }])
  138. expect(reopened.stack).toEqual([])
  139. })
  140. test("clamps restored positions and keeps one entry per session", () => {
  141. const twice = recordClosedSessionTab(recordClosedSessionTab([], { sessionID: "a" }, 5), { sessionID: "a" }, 2)
  142. expect(twice).toEqual([{ tab: { sessionID: "a" }, index: 2 }])
  143. const reopened = reopenSessionTab(twice, [{ sessionID: "b" }])
  144. expect(reopened.tabs).toEqual([{ sessionID: "b" }, { sessionID: "a" }])
  145. const overflow = Array.from({ length: 12 }, (_, index) => ({ sessionID: String(index) })).reduce(
  146. (stack, tab, index) => recordClosedSessionTab(stack, tab, index),
  147. twice,
  148. )
  149. expect(overflow).toHaveLength(10)
  150. expect(overflow.at(-1)?.tab.sessionID).toBe("11")
  151. expect(overflow[0]?.tab.sessionID).toBe("2")
  152. })
  153. test("reveals completion activity only after session work becomes idle", () => {
  154. expect(sessionTabComplete("activity", true)).toBe(false)
  155. expect(sessionTabComplete("activity", false)).toBe(true)
  156. expect(sessionTabComplete("error", false)).toBe(false)
  157. })
  158. test("expands the active tab and keeps inactive widths equal", () => {
  159. const tabs = ["a", "b", "c", "d", "e", "f", "g"].map((sessionID) => ({ sessionID }))
  160. const layout = adaptiveSessionTabLayout(tabs, "d", 76)
  161. expect(layout).toMatchObject({ before: 0, after: 0, start: 0, total: 76 })
  162. expect(layout.widths).toEqual([9, 9, 9, 22, 9, 9, 9])
  163. expect(layout.widths.reduce((total, width) => total + width, 0)).toBe(76)
  164. })
  165. test("reserves an active tab slot for the new session page", () => {
  166. const tabs = ["a", "b", "c", "d", "new"].map((sessionID) => ({ sessionID }))
  167. const layout = adaptiveSessionTabLayout(tabs, "new", 54)
  168. expect(layout.tabs).toEqual(tabs)
  169. expect(layout.widths).toEqual([8, 8, 8, 8, 22])
  170. expect(layout.widths.reduce((total, width) => total + width, 0)).toBe(layout.total)
  171. })
  172. test("keeps the visible tab window stable on the new session page", () => {
  173. const tabs = Array.from({ length: 10 }, (_, index) => ({ sessionID: String(index) }))
  174. const selected = adaptiveSessionTabLayout(tabs, "7", 70)
  175. const home = adaptiveSessionTabLayout(tabs, undefined, 70, selected.start)
  176. expect(selected.start).toBeGreaterThan(0)
  177. expect(home.start).toBe(selected.start)
  178. })
  179. test("only swaps old and new active width inside a sticky window", () => {
  180. const tabs = ["a", "b", "c", "d", "e", "f", "g"].map((sessionID) => ({ sessionID }))
  181. const before = adaptiveSessionTabLayout(tabs, "c", 76)
  182. const after = adaptiveSessionTabLayout(tabs, "d", 76, before.start)
  183. expect(before.start).toBe(after.start)
  184. expect(before.widths).toEqual([9, 9, 22, 9, 9, 9, 9])
  185. expect(after.widths).toEqual([9, 9, 9, 22, 9, 9, 9])
  186. })
  187. test("shares roomy width equally without changing widths on selection", () => {
  188. const tabs = ["a", "b", "c"].map((sessionID) => ({ sessionID }))
  189. const before = adaptiveSessionTabLayout(tabs, "a", 100)
  190. const after = adaptiveSessionTabLayout(tabs, "c", 100, before.start)
  191. expect(before.widths).toEqual([32, 32, 32])
  192. expect(after.widths).toEqual(before.widths)
  193. expect(before.total).toBe(96)
  194. })
  195. test("caps a single tab instead of stretching it across the terminal", () => {
  196. const layout = adaptiveSessionTabLayout([{ sessionID: "a" }], "a", 100)
  197. expect(layout.widths).toEqual([32])
  198. expect(layout.total).toBe(32)
  199. })
  200. test("fills roomy space equally below the maximum width", () => {
  201. const tabs = ["a", "b", "c", "d"].map((sessionID) => ({ sessionID }))
  202. expect(adaptiveSessionTabLayout(tabs, "b", 100).widths).toEqual([25, 25, 25, 25])
  203. })
  204. test("expands only the active tab under compact pressure", () => {
  205. const tabs = ["a", "b", "c", "d", "e"].map((sessionID) => ({ sessionID }))
  206. const before = adaptiveSessionTabLayout(tabs, "c", 100)
  207. const after = adaptiveSessionTabLayout(tabs, "d", 100, before.start)
  208. expect(before.widths).toEqual([19, 19, 24, 19, 19])
  209. expect(after.widths).toEqual([19, 19, 19, 24, 19])
  210. })
  211. test("moves the window only after selection crosses its edge", () => {
  212. const tabs = Array.from({ length: 10 }, (_, index) => ({ sessionID: String(index) }))
  213. const initial = adaptiveSessionTabLayout(tabs, "3", 70)
  214. const inside = adaptiveSessionTabLayout(tabs, "4", 70, initial.start)
  215. const crossed = adaptiveSessionTabLayout(tabs, "7", 70, inside.start)
  216. expect(initial.start).toBe(0)
  217. expect(inside.start).toBe(0)
  218. expect(crossed.start).toBeGreaterThan(0)
  219. expect(crossed.tabs.some((tab) => tab.sessionID === "7")).toBe(true)
  220. expect(crossed.widths.reduce((total, width) => total + width, 0)).toBe(crossed.total)
  221. })
  222. })