|
3 | 3 | import { describe, expect, it, vi } from "vitest"; |
4 | 4 | import type { |
5 | 5 | SessionCatalogSession, |
| 6 | + SessionCatalogTranscriptItem, |
6 | 7 | SessionsCatalogListResult, |
7 | 8 | SessionsCatalogReadResult, |
8 | 9 | TaskSuggestion, |
@@ -37,7 +38,18 @@ type TestChatPane = HTMLElement & { |
37 | 38 | onPaneSessionChange?: (paneId: string, sessionKey: string) => void; |
38 | 39 | sessionKey: string; |
39 | 40 | catalogSession: SessionCatalogSession | null; |
40 | | - loadCatalogSession: (key: CatalogSessionKey, older: boolean) => Promise<void>; |
| 41 | + catalogItemMessage: ( |
| 42 | + item: SessionCatalogTranscriptItem, |
| 43 | + index: number, |
| 44 | + ) => Record<string, unknown> | null; |
| 45 | + handleTranscriptScroll: (event: Event) => void; |
| 46 | + historyAutoLoadBlocked: boolean; |
| 47 | + syncHistoryObserver: () => void; |
| 48 | + loadCatalogSession: (key: CatalogSessionKey, older: boolean) => Promise<boolean>; |
| 49 | + loadOlderMessages: () => Promise<void>; |
| 50 | + hasOlderMessages: () => boolean; |
| 51 | + catalogCursor: string | undefined; |
| 52 | + olderCursorsSeen: Set<string>; |
41 | 53 | }; |
42 | 54 |
|
43 | 55 | const suggestion: TaskSuggestion = { |
@@ -115,6 +127,10 @@ function createTestChatPane(params: { client: GatewayBrowserClient; sessions: Se |
115 | 127 | sessionsLoading: false, |
116 | 128 | sidebarContent: null, |
117 | 129 | sidebarOpen: false, |
| 130 | + // Minimal scroll host so scheduleChatScroll is a no-op instead of throwing. |
| 131 | + chatScrollGeneration: 0, |
| 132 | + chatScrollCommitCleanup: null, |
| 133 | + renderLifecycle: { afterCommit: () => () => {}, invalidate: () => {} }, |
118 | 134 | } as unknown as ChatPageHost; |
119 | 135 | pane.context = createSessionContext(params.client, params.sessions); |
120 | 136 | pane.state = state; |
@@ -348,6 +364,173 @@ describe("chat pane catalog session lifecycle", () => { |
348 | 364 | }); |
349 | 365 | expect(pane.catalogSession).toEqual(selectedSession); |
350 | 366 | }); |
| 367 | + |
| 368 | + it.each([ |
| 369 | + { |
| 370 | + name: "uses a raw command for an empty tool call", |
| 371 | + item: { type: "toolCall", raw: { command: "git status --short" } }, |
| 372 | + expected: "Tool call\n\ngit status --short", |
| 373 | + }, |
| 374 | + { |
| 375 | + name: "uses aggregated output for an empty tool result", |
| 376 | + item: { type: "toolResult", raw: { aggregatedOutput: "working tree clean" } }, |
| 377 | + expected: "Tool result\n\nworking tree clean", |
| 378 | + }, |
| 379 | + { |
| 380 | + name: "renders an empty reasoning item as its label alone", |
| 381 | + item: { type: "reasoning" }, |
| 382 | + expected: "Thinking", |
| 383 | + }, |
| 384 | + ])("$name", ({ item, expected }) => { |
| 385 | + const client = { request: vi.fn() } as unknown as GatewayBrowserClient; |
| 386 | + const { pane } = createTestChatPane({ client, sessions: {} as SessionCapability }); |
| 387 | + |
| 388 | + const message = pane.catalogItemMessage(item as SessionCatalogTranscriptItem, 0) as { |
| 389 | + content: Array<{ text: string }>; |
| 390 | + }; |
| 391 | + |
| 392 | + expect(message.content[0]?.text).toBe(expected); |
| 393 | + expect(message.content[0]?.text).not.toContain("Unsupported external session item"); |
| 394 | + }); |
| 395 | + |
| 396 | + it("clamps oversized aggregated tool output before rendering", () => { |
| 397 | + const client = { request: vi.fn() } as unknown as GatewayBrowserClient; |
| 398 | + const { pane } = createTestChatPane({ client, sessions: {} as SessionCapability }); |
| 399 | + |
| 400 | + const message = pane.catalogItemMessage( |
| 401 | + { |
| 402 | + type: "toolResult", |
| 403 | + raw: { aggregatedOutput: "x".repeat(5000) }, |
| 404 | + } as SessionCatalogTranscriptItem, |
| 405 | + 0, |
| 406 | + ) as { content: Array<{ text: string }> }; |
| 407 | + |
| 408 | + // The 500-char preview cap keeps a single huge tool result from injecting |
| 409 | + // megabytes into one chat message; the "Tool result\n\n" prefix adds a bit. |
| 410 | + expect(message.content[0]?.text.length).toBeLessThan(600); |
| 411 | + expect(message.content[0]?.text.startsWith("Tool result")).toBe(true); |
| 412 | + }); |
| 413 | + |
| 414 | + it("skips an empty unknown catalog item", () => { |
| 415 | + const client = { request: vi.fn() } as unknown as GatewayBrowserClient; |
| 416 | + const { pane } = createTestChatPane({ client, sessions: {} as SessionCapability }); |
| 417 | + |
| 418 | + expect(pane.catalogItemMessage({ type: "other" }, 0)).toBeNull(); |
| 419 | + }); |
| 420 | + |
| 421 | + it("exhausts pagination when an older read does not advance the cursor", async () => { |
| 422 | + const readPage: SessionsCatalogReadResult = { |
| 423 | + hostId: "gateway:local", |
| 424 | + threadId: "thread-1", |
| 425 | + items: [{ id: "u1", type: "userMessage", text: "hi" }], |
| 426 | + // Same cursor the request was made with: a stale provider that would loop. |
| 427 | + nextCursor: "cursor-1", |
| 428 | + }; |
| 429 | + const client = { |
| 430 | + request: vi.fn(async () => readPage), |
| 431 | + } as unknown as GatewayBrowserClient; |
| 432 | + const { pane, state } = createTestChatPane({ client, sessions: {} as SessionCapability }); |
| 433 | + const key = "catalog:claude:gateway%3Alocal:thread-1"; |
| 434 | + state.sessionKey = key; |
| 435 | + pane.sessionKey = key; |
| 436 | + pane.catalogCursor = "cursor-1"; |
| 437 | + |
| 438 | + const progressed = await pane.loadCatalogSession( |
| 439 | + { catalogId: "claude", hostId: "gateway:local", threadId: "thread-1" }, |
| 440 | + true, |
| 441 | + ); |
| 442 | + |
| 443 | + expect(progressed).toBe(false); |
| 444 | + // Cursor cleared → hasOlderMessages() is false, so the observer will not refire. |
| 445 | + expect(pane.catalogCursor).toBeUndefined(); |
| 446 | + }); |
| 447 | + |
| 448 | + it("keeps paging when an advancing older page renders nothing new", async () => { |
| 449 | + const readPage: SessionsCatalogReadResult = { |
| 450 | + hostId: "gateway:local", |
| 451 | + threadId: "thread-1", |
| 452 | + // A page of only unsupported/empty items renders nothing but still advances |
| 453 | + // the cursor: older renderable history may sit behind it, so paging continues. |
| 454 | + items: [{ id: "x1", type: "other" }], |
| 455 | + nextCursor: "cursor-2", |
| 456 | + }; |
| 457 | + const client = { |
| 458 | + request: vi.fn(async () => readPage), |
| 459 | + } as unknown as GatewayBrowserClient; |
| 460 | + const { pane, state } = createTestChatPane({ client, sessions: {} as SessionCapability }); |
| 461 | + const key = "catalog:claude:gateway%3Alocal:thread-1"; |
| 462 | + state.sessionKey = key; |
| 463 | + pane.sessionKey = key; |
| 464 | + pane.catalogCursor = "cursor-1"; |
| 465 | + |
| 466 | + const progressed = await pane.loadCatalogSession( |
| 467 | + { catalogId: "claude", hostId: "gateway:local", threadId: "thread-1" }, |
| 468 | + true, |
| 469 | + ); |
| 470 | + |
| 471 | + expect(progressed).toBe(true); |
| 472 | + expect(pane.catalogCursor).toBe("cursor-2"); |
| 473 | + }); |
| 474 | + |
| 475 | + it("exhausts pagination when an older read cycles back to a visited cursor", async () => { |
| 476 | + const readPage: SessionsCatalogReadResult = { |
| 477 | + hostId: "gateway:local", |
| 478 | + threadId: "thread-1", |
| 479 | + items: [{ id: "x1", type: "other" }], |
| 480 | + // Cursor points back to one already visited this session: a c1 -> c2 -> c1 |
| 481 | + // cycle that would otherwise loop forever on empty pages. |
| 482 | + nextCursor: "cursor-1", |
| 483 | + }; |
| 484 | + const client = { |
| 485 | + request: vi.fn(async () => readPage), |
| 486 | + } as unknown as GatewayBrowserClient; |
| 487 | + const { pane, state } = createTestChatPane({ client, sessions: {} as SessionCapability }); |
| 488 | + const key = "catalog:claude:gateway%3Alocal:thread-1"; |
| 489 | + state.sessionKey = key; |
| 490 | + pane.sessionKey = key; |
| 491 | + pane.catalogCursor = "cursor-2"; |
| 492 | + pane.olderCursorsSeen.add("cursor-1"); |
| 493 | + |
| 494 | + const progressed = await pane.loadCatalogSession( |
| 495 | + { catalogId: "claude", hostId: "gateway:local", threadId: "thread-1" }, |
| 496 | + true, |
| 497 | + ); |
| 498 | + |
| 499 | + expect(progressed).toBe(false); |
| 500 | + expect(pane.catalogCursor).toBeUndefined(); |
| 501 | + }); |
| 502 | + |
| 503 | + it("re-arms a failed older-page load only after another user scroll", () => { |
| 504 | + const client = { request: vi.fn() } as unknown as GatewayBrowserClient; |
| 505 | + const { pane, state } = createTestChatPane({ client, sessions: {} as SessionCapability }); |
| 506 | + state.handleChatScroll = vi.fn(); |
| 507 | + pane.historyAutoLoadBlocked = true; |
| 508 | + pane.syncHistoryObserver = vi.fn(); |
| 509 | + const event = new Event("scroll"); |
| 510 | + |
| 511 | + pane.handleTranscriptScroll(event); |
| 512 | + |
| 513 | + expect(pane.historyAutoLoadBlocked).toBe(false); |
| 514 | + expect(pane.syncHistoryObserver).toHaveBeenCalledOnce(); |
| 515 | + expect(state.handleChatScroll).toHaveBeenCalledWith(event); |
| 516 | + }); |
| 517 | + |
| 518 | + it("re-arms a blocked auto-load when the manual fallback is clicked", async () => { |
| 519 | + const client = { request: vi.fn() } as unknown as GatewayBrowserClient; |
| 520 | + const { pane, state } = createTestChatPane({ client, sessions: {} as SessionCapability }); |
| 521 | + // A short (non-scrollable) thread cannot emit a scroll event, so a blocked |
| 522 | + // auto-load must be recoverable through the fallback's loadOlderMessages call. |
| 523 | + state.sessionKey = "catalog:claude:gateway%3Alocal:thread-1"; |
| 524 | + pane.historyAutoLoadBlocked = true; |
| 525 | + pane.loadCatalogSession = vi.fn(async () => false); |
| 526 | + pane.hasOlderMessages = vi.fn(() => true); |
| 527 | + |
| 528 | + await pane.loadOlderMessages(); |
| 529 | + |
| 530 | + // loadOlderMessages clears the block on entry, so the retry is not stranded. |
| 531 | + expect(pane.loadCatalogSession).toHaveBeenCalledOnce(); |
| 532 | + expect(pane.historyAutoLoadBlocked).toBe(true); |
| 533 | + }); |
351 | 534 | }); |
352 | 535 |
|
353 | 536 | describe("chat pane task suggestion lifecycle", () => { |
|
0 commit comments