chatSessionScrollMemory.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { beforeEach, describe, expect, it } from "vitest";
  2. import {
  3. CHAT_SESSION_SCROLL_MEMORY_MAX_ENTRIES,
  4. buildChatSessionScrollMemoryKey,
  5. clearChatSessionScrollMemoryForTest,
  6. migrateTaskScrollMemoryToSession,
  7. readChatSessionScrollMemoryState,
  8. resolveChatSessionScrollRestoreTop,
  9. saveChatSessionScrollMemoryState,
  10. } from "../src/lib/chatSessionScrollMemory.js";
  11. describe("chat session scroll memory", () => {
  12. beforeEach(() => {
  13. clearChatSessionScrollMemoryForTest();
  14. });
  15. it("keys V4 scroll memory by workspace identity, pane, and session", () => {
  16. expect(
  17. buildChatSessionScrollMemoryKey({
  18. workspacePath: "/repo/demo",
  19. workspaceIdentity: "ssh://demo",
  20. paneId: "pane-1",
  21. sessionId: "session-1",
  22. taskId: "task-1",
  23. }),
  24. ).toBe("ssh://demo::pane:pane-1::session:session-1");
  25. });
  26. it("keeps legacy keys compatible when pane id is absent", () => {
  27. expect(
  28. buildChatSessionScrollMemoryKey({
  29. workspacePath: "/repo/demo",
  30. sessionId: "session-1",
  31. taskId: "task-1",
  32. }),
  33. ).toBe("/repo/demo::session:session-1");
  34. });
  35. it("isolates stored positions by workspace identity, pane, and session", () => {
  36. const scopes = [
  37. { workspaceIdentity: "ssh://one", paneId: "pane-1", sessionId: "session-1" },
  38. { workspaceIdentity: "ssh://one", paneId: "pane-2", sessionId: "session-1" },
  39. { workspaceIdentity: "ssh://one", paneId: "pane-1", sessionId: "session-2" },
  40. { workspaceIdentity: "ssh://two", paneId: "pane-1", sessionId: "session-1" },
  41. ];
  42. const keys = scopes.map((scope) =>
  43. buildChatSessionScrollMemoryKey({ workspacePath: "/same/path", ...scope }),
  44. );
  45. keys.forEach((key, index) => {
  46. saveChatSessionScrollMemoryState(key, {
  47. scrollTop: 100 + index,
  48. scrollHeight: 1000,
  49. clientHeight: 300,
  50. wasPinnedToBottom: false,
  51. updatedAt: index,
  52. });
  53. });
  54. expect(new Set(keys).size).toBe(scopes.length);
  55. expect(keys.map((key) => readChatSessionScrollMemoryState(key)?.scrollTop)).toEqual([
  56. 100, 101, 102, 103,
  57. ]);
  58. });
  59. it("uses a task fallback until the ZCode Agent session id is restored", () => {
  60. expect(
  61. buildChatSessionScrollMemoryKey({
  62. workspacePath: "/repo/demo",
  63. sessionId: null,
  64. taskId: "task-1",
  65. }),
  66. ).toBe("/repo/demo::task:task-1");
  67. });
  68. it("migrates the temporary task bucket into the real session bucket", () => {
  69. const taskKey = buildChatSessionScrollMemoryKey({
  70. workspacePath: "/repo/demo",
  71. sessionId: null,
  72. taskId: "task-1",
  73. });
  74. saveChatSessionScrollMemoryState(taskKey, {
  75. scrollTop: 320,
  76. scrollHeight: 1000,
  77. clientHeight: 400,
  78. updatedAt: 1,
  79. });
  80. const sessionKey = migrateTaskScrollMemoryToSession({
  81. workspacePath: "/repo/demo",
  82. sessionId: "session-1",
  83. taskId: "task-1",
  84. });
  85. expect(sessionKey).toBe("/repo/demo::session:session-1");
  86. expect(readChatSessionScrollMemoryState(taskKey)).toBeNull();
  87. expect(readChatSessionScrollMemoryState(sessionKey)?.scrollTop).toBe(320);
  88. });
  89. it("migrates task fallback memory inside the same pane", () => {
  90. const taskKey = buildChatSessionScrollMemoryKey({
  91. workspacePath: "/repo/demo",
  92. paneId: "pane-2",
  93. sessionId: null,
  94. taskId: "task-1",
  95. });
  96. saveChatSessionScrollMemoryState(taskKey, {
  97. scrollTop: 240,
  98. scrollHeight: 900,
  99. clientHeight: 300,
  100. wasPinnedToBottom: false,
  101. updatedAt: 1,
  102. });
  103. const sessionKey = migrateTaskScrollMemoryToSession({
  104. workspacePath: "/repo/demo",
  105. paneId: "pane-2",
  106. sessionId: "session-1",
  107. taskId: "task-1",
  108. });
  109. expect(sessionKey).toBe("/repo/demo::pane:pane-2::session:session-1");
  110. expect(readChatSessionScrollMemoryState(taskKey)).toBeNull();
  111. expect(readChatSessionScrollMemoryState(sessionKey)?.scrollTop).toBe(240);
  112. });
  113. it("keeps bottom-lock intent when migrating scroll memory", () => {
  114. const taskKey = buildChatSessionScrollMemoryKey({
  115. workspacePath: "/repo/demo",
  116. sessionId: null,
  117. taskId: "task-1",
  118. });
  119. saveChatSessionScrollMemoryState(taskKey, {
  120. scrollTop: 700,
  121. scrollHeight: 1000,
  122. clientHeight: 300,
  123. wasPinnedToBottom: true,
  124. updatedAt: 1,
  125. });
  126. const sessionKey = migrateTaskScrollMemoryToSession({
  127. workspacePath: "/repo/demo",
  128. sessionId: "session-1",
  129. taskId: "task-1",
  130. });
  131. expect(readChatSessionScrollMemoryState(sessionKey)?.wasPinnedToBottom).toBe(true);
  132. });
  133. it("restores saved scroll positions and clamps them to the current range", () => {
  134. expect(
  135. resolveChatSessionScrollRestoreTop(
  136. {
  137. scrollTop: 50,
  138. },
  139. {
  140. scrollHeight: 1200,
  141. clientHeight: 300,
  142. },
  143. ),
  144. ).toBe(50);
  145. expect(
  146. resolveChatSessionScrollRestoreTop(
  147. {
  148. scrollTop: 1200,
  149. },
  150. {
  151. scrollHeight: 1000,
  152. clientHeight: 250,
  153. },
  154. ),
  155. ).toBe(750);
  156. });
  157. it("prunes old entries with an LRU cap", () => {
  158. for (let index = 0; index <= CHAT_SESSION_SCROLL_MEMORY_MAX_ENTRIES; index += 1) {
  159. saveChatSessionScrollMemoryState(`workspace::session:${index}`, {
  160. scrollTop: index,
  161. scrollHeight: 1000,
  162. clientHeight: 300,
  163. updatedAt: index,
  164. });
  165. }
  166. expect(readChatSessionScrollMemoryState("workspace::session:0")).toBeNull();
  167. expect(
  168. readChatSessionScrollMemoryState(
  169. `workspace::session:${CHAT_SESSION_SCROLL_MEMORY_MAX_ENTRIES}`,
  170. )?.scrollTop,
  171. ).toBe(CHAT_SESSION_SCROLL_MEMORY_MAX_ENTRIES);
  172. });
  173. });