@@ -28,7 +28,7 @@ import {
2828 type ReactElement ,
2929} from "react" ;
3030import * as DateTime from "effect/DateTime" ;
31- import { Atom } from "effect/reactivity" ;
31+ import { Atom , AsyncResult } from "effect/reactivity" ;
3232import { describe , expect , it , onTestFinished , vi } from "vite-plus/test" ;
3333
3434import "~/index.css" ;
@@ -37,6 +37,7 @@ import { environmentThreadDetails } from "../../state/threads";
3737import { act , fakeTimers , render , stubGlobal } from "../../../test/render" ;
3838import { ChangedFilesCard } from "./ChangedFilesTree" ;
3939import { MessagesTimeline } from "./MessagesTimeline" ;
40+ import { resolveTimelineIsAtEnd } from "./MessagesTimeline.logic" ;
4041import { makeStreamingTimelineFixture } from "../../test-fixtures" ;
4142import {
4243 type PendingApproval ,
@@ -56,7 +57,7 @@ import {
5657import { collectComposerContextReferences } from "@t3tools/shared/composerContextReferences" ;
5758
5859const environmentQuery = vi . hoisted ( ( ) => ( {
59- error : "Pull request not found" ,
60+ error : "Pull request not found" as string | null ,
6061 data : null as unknown ,
6162} ) ) ;
6263vi . mock ( import ( "~/state/query" ) , async ( original ) => ( {
@@ -212,6 +213,7 @@ async function click(element: HTMLElement) {
212213}
213214
214215function setup ( { scheduledTasks = [ ] as ReadonlyArray < { id : string ; title : string } > } = { } ) {
216+ environmentQuery . error = "Pull request not found" ;
215217 environmentQuery . data = { tasks : scheduledTasks } ;
216218}
217219
@@ -773,6 +775,220 @@ describe("MessagesTimeline messages", () => {
773775 await click ( screen . button ( "Safe details" ) ) ;
774776 expect ( ( globalThis as { __t3Xss ?: number } ) . __t3Xss ) . toBeUndefined ( ) ;
775777 } ) ;
778+
779+ const revealModes = [ "inline" , "at-end" , "delayed" , "grouped-delayed" , "grouped-end" ] ;
780+ it . each ( revealModes ) ( "reveals %s output" , async ( mode ) => {
781+ const inline = mode === "inline" || mode === "at-end" ;
782+ setup ( ) ;
783+ environmentQuery . data =
784+ mode === "grouped-end"
785+ ? {
786+ output : { text : `${ "output line\n" . repeat ( 10 ) } TOOL_INPUT_END` , truncated : false } ,
787+ running : false ,
788+ }
789+ : null ;
790+ environmentQuery . error = null ;
791+ const props = baseProps ( ) ;
792+ const composerOverlayHeight = mode === "grouped-end" ? 140 : 200 ;
793+ const tallEntries = Array . from ( { length : 12 } , ( _ , index ) =>
794+ message (
795+ `Message ${ index } \n\n${ "Long paragraph. " . repeat ( 80 ) } ` ,
796+ { role : "assistant" } ,
797+ `tall-${ index } ` ,
798+ ) ,
799+ ) ;
800+ const projectedItem = makeStreamingTimelineFixture ( ) . visibleTurnItems [ 1 ] ! ;
801+ const source = {
802+ detail : ( ) => Atom . make ( AsyncResult . initial ( ) ) ,
803+ commandOutput : ( ) => Atom . make ( AsyncResult . initial ( ) ) ,
804+ } ;
805+ let outerGrowth = false ;
806+ let routeThreadKey = props . routeThreadKey ;
807+ const timeline = ( ) => (
808+ < MessagesTimeline
809+ { ...props }
810+ routeThreadKey = { routeThreadKey }
811+ liveFollowEnabled = { false }
812+ { ...( inline
813+ ? { }
814+ : {
815+ turnItemSource : {
816+ detail : ( ) => source . detail ( ) ,
817+ commandOutput : ( ) => source . commandOutput ( ) ,
818+ } ,
819+ } ) }
820+ contentInsetEndAdjustment = { composerOverlayHeight }
821+ timelineEntries = { [
822+ ...tallEntries . slice ( 0 , 6 ) ,
823+ {
824+ id : "tool-near-composer" ,
825+ kind : "work" ,
826+ createdAt : CREATED_AT ,
827+ entry : {
828+ id : "tool-near-composer" ,
829+ createdAt : CREATED_AT ,
830+ label : "Example tool" ,
831+ toolTitle : "Example tool" ,
832+ tone : "tool" ,
833+ itemType : "dynamic_tool" ,
834+ toolLifecycleStatus : "completed" ,
835+ toolData : { input : { query : `${ "x" . repeat ( 1200 ) } TOOL_INPUT_END` } } ,
836+ ...( inline
837+ ? { }
838+ : {
839+ itemType : "command_execution" ,
840+ projectedItem,
841+ structuredPayload : projectedItem . item ,
842+ } ) ,
843+ } ,
844+ } ,
845+ ...( mode . startsWith ( "grouped-" )
846+ ? [
847+ {
848+ id : "another-tool" ,
849+ kind : "work" as const ,
850+ createdAt : CREATED_AT ,
851+ entry : {
852+ id : "another-tool" ,
853+ createdAt : CREATED_AT ,
854+ label : "Other tool" ,
855+ tone : "tool" as const ,
856+ itemType : "dynamic_tool" as const ,
857+ toolLifecycleStatus : "completed" as const ,
858+ toolData : { input : { query : "other" } } ,
859+ } ,
860+ } ,
861+ ]
862+ : [ ] ) ,
863+ ...( mode === "grouped-end"
864+ ? [ message ( "Completed." , { role : "assistant" } , "final-message" ) ]
865+ : mode === "at-end"
866+ ? [ message ( "Done." , { role : "assistant" } , "final-message" ) ]
867+ : tallEntries . slice ( 6 ) ) ,
868+ ...( outerGrowth
869+ ? [ message ( "Later output\n\n" . repeat ( 20 ) , { role : "assistant" } , "outer-growth" ) ]
870+ : [ ] ) ,
871+ ] }
872+ />
873+ ) ;
874+ await using screen = await renderElement (
875+ mode === "grouped-delayed" ? (
876+ < MessagesTimeline { ...props } timelineEntries = { [ message ( "Previous thread" ) ] } />
877+ ) : (
878+ timeline ( )
879+ ) ,
880+ ) ;
881+ if ( mode === "grouped-end" ) screen . container . style . height = "648px" ;
882+ await vi . waitFor ( ( ) => expect ( props . listRef . current ?. getScrollableNode ( ) ) . toBeTruthy ( ) ) ;
883+ const viewport = props . listRef . current ! . getScrollableNode ( ) ! ;
884+ if ( mode === "grouped-delayed" ) {
885+ routeThreadKey = `${ routeThreadKey } -switched` ;
886+ await screen . rerender ( timeline ( ) ) ;
887+ await vi . waitFor ( ( ) => expect ( viewport . scrollHeight ) . toBeGreaterThan ( 3000 ) ) ;
888+ await act ( ( ) => viewport . dispatchEvent ( new WheelEvent ( "wheel" , { deltaY : - 1 } ) ) ) ;
889+ }
890+ if ( mode . startsWith ( "grouped-" ) ) {
891+ await act ( ( ) => {
892+ void props . listRef . current ! . scrollToIndex ( { index : 6 , animated : false } ) ;
893+ } ) ;
894+ await vi . waitFor ( ( ) =>
895+ expect (
896+ screen . container . querySelector ( '[data-timeline-row-kind="work-toggle"] button' ) ,
897+ ) . not . toBeNull ( ) ,
898+ ) ;
899+ const group = ( ) =>
900+ screen . container . querySelector < HTMLElement > (
901+ '[data-timeline-row-kind="work-toggle"] button' ,
902+ ) ! ;
903+ await click ( group ( ) ) ;
904+ await vi . waitFor ( ( ) =>
905+ expect (
906+ screen . container . querySelector ( '[data-work-entry-id="tool-near-composer"]' ) ,
907+ ) . not . toBeNull ( ) ,
908+ ) ;
909+ if ( mode === "grouped-delayed" ) {
910+ await click (
911+ screen . container . querySelector < HTMLElement > ( '[data-work-entry-id="tool-near-composer"]' ) ! ,
912+ ) ;
913+ await vi . waitFor ( ( ) => expect ( screen . text ( ) ) . toContain ( "Loading output" ) ) ;
914+ await click ( group ( ) ) ;
915+ }
916+ }
917+ const header = ( ) => {
918+ if ( mode === "grouped-end" )
919+ return screen . container . querySelector < HTMLElement > (
920+ '[data-work-entry-id="tool-near-composer"]' ,
921+ ) ! ;
922+ if ( mode === "grouped-delayed" )
923+ return screen . container . querySelector < HTMLElement > (
924+ '[data-timeline-row-kind="work-toggle"] button' ,
925+ ) ! ;
926+ const rows = screen . container . querySelectorAll < HTMLElement > (
927+ '[data-timeline-row-kind="work"] [role="button"]' ,
928+ ) ;
929+ if ( ! rows [ 0 ] ) throw new Error ( screen . text ( ) ) ;
930+ return rows [ 0 ] ;
931+ } ;
932+ const visibleBottom = ( ) => viewport . getBoundingClientRect ( ) . bottom - composerOverlayHeight ;
933+ await act ( ( ) => {
934+ void props . listRef . current ! . scrollToIndex ( { index : 6 , animated : false } ) ;
935+ } ) ;
936+ if ( mode === "at-end" ) {
937+ await act ( ( ) => {
938+ void props . listRef . current ! . scrollToEnd ( { animated : false } ) ;
939+ } ) ;
940+ await vi . waitFor ( ( ) =>
941+ expect ( resolveTimelineIsAtEnd ( props . listRef . current ! . getState ( ) ) ) . toBe ( true ) ,
942+ ) ;
943+ } else {
944+ await vi . waitFor ( async ( ) => {
945+ await act ( ( ) => {
946+ viewport . scrollTop += header ( ) . getBoundingClientRect ( ) . bottom - ( visibleBottom ( ) - 8 ) ;
947+ viewport . dispatchEvent ( new Event ( "scroll" ) ) ;
948+ } ) ;
949+ expect ( resolveTimelineIsAtEnd ( props . listRef . current ! . getState ( ) ) ) . toBe ( false ) ;
950+ expect ( header ( ) . getBoundingClientRect ( ) . bottom ) . toBeLessThanOrEqual ( visibleBottom ( ) ) ;
951+ expect ( header ( ) . getBoundingClientRect ( ) . top ) . toBeGreaterThan (
952+ viewport . getBoundingClientRect ( ) . top ,
953+ ) ;
954+ } ) ;
955+ }
956+
957+ const collapsedScrollTop = viewport . scrollTop ;
958+ using scrollBy = vi . spyOn ( viewport , "scrollBy" ) ;
959+ // The nested row can measure before the outer scroller gains its new range.
960+ if ( mode === "grouped-delayed" ) scrollBy . mockImplementationOnce ( ( ) => { } ) ;
961+ await click ( header ( ) ) ;
962+ if ( mode === "grouped-delayed" ) {
963+ await vi . waitFor ( ( ) => expect ( scrollBy ) . toHaveBeenCalled ( ) ) ;
964+ outerGrowth = true ;
965+ await screen . rerender ( timeline ( ) ) ;
966+ await vi . waitFor ( ( ) => expect ( viewport . scrollTop ) . toBeGreaterThan ( collapsedScrollTop ) ) ;
967+ }
968+ if ( ! inline && mode !== "grouped-end" ) {
969+ await vi . waitFor ( ( ) =>
970+ expect ( header ( ) . getBoundingClientRect ( ) . bottom ) . toBeLessThanOrEqual ( visibleBottom ( ) + 1 ) ,
971+ ) ;
972+ await vi . waitFor ( ( ) => expect ( screen . text ( ) ) . toContain ( "Loading output" ) ) ;
973+ environmentQuery . data = {
974+ output : { text : `${ "output line\n" . repeat ( 20 ) } TOOL_INPUT_END` , truncated : false } ,
975+ running : false ,
976+ } ;
977+ await screen . rerender ( timeline ( ) ) ;
978+ }
979+ const inputEnd = ( ) =>
980+ Array . from ( screen . container . querySelectorAll < HTMLElement > ( "*" ) ) . findLast ( ( element ) =>
981+ element . textContent ?. includes ( "TOOL_INPUT_END" ) ,
982+ ) ! ;
983+ await vi . waitFor ( ( ) =>
984+ expect ( inputEnd ( ) . getBoundingClientRect ( ) . bottom ) . toBeLessThanOrEqual ( visibleBottom ( ) + 1 ) ,
985+ ) ;
986+ await vi . waitFor ( ( ) =>
987+ expect ( header ( ) . getBoundingClientRect ( ) . top ) . toBeGreaterThanOrEqual (
988+ viewport . getBoundingClientRect ( ) . top ,
989+ ) ,
990+ ) ;
991+ } ) ;
776992} ) ;
777993
778994function usageLimitEntries ( resetAt : string ) {
0 commit comments