11import { TestBed } from '@angular/core/testing' ;
22import type { DevframeRpcClient } from 'devframe/client' ;
3- import { afterEach , describe , expect , it } from 'vitest' ;
3+ import { afterEach , describe , expect , it , vi } from 'vitest' ;
44import { ComponentTree } from '../pages/component-tree' ;
55
6- function client ( pages : Record < string , unknown > ) : DevframeRpcClient {
6+ function client (
7+ pages : Record < string , unknown > ,
8+ call : ( ...args : unknown [ ] ) => Promise < unknown > = ( ) => Promise . resolve ( [ ] ) ,
9+ ) : DevframeRpcClient {
710 const rpc = {
8- call : ( ) => Promise . resolve ( [ ] ) ,
11+ call,
912 callEvent : ( ) => Promise . resolve ( ) ,
1013 sharedState : ( name : string ) =>
1114 Promise . resolve ( {
@@ -19,6 +22,7 @@ function client(pages: Record<string, unknown>): DevframeRpcClient {
1922describe ( 'defer blocks' , ( ) => {
2023 afterEach ( ( ) => {
2124 document . body . innerHTML = '' ;
25+ vi . useRealTimers ( ) ;
2226 } ) ;
2327
2428 it ( 'style the state badge apart from the empty-state box' , async ( ) => {
@@ -120,4 +124,111 @@ describe('defer blocks', () => {
120124 expect ( await pill ( page ( ) ) ) . toBe ( '2' ) ;
121125 expect ( await pill ( page ( true ) ) ) . toBe ( '2+' ) ;
122126 } ) ;
127+
128+ it ( 'shows the notes the agent tool prints, the @error block and the block times' , async ( ) => {
129+ const hour = 3_600_000 ;
130+ // Fix the panel clock so the ages stay exact; timers stay real for the waits.
131+ vi . useFakeTimers ( { toFake : [ 'Date' ] } ) ;
132+ vi . setSystemTime ( 1_700_000_000_000 ) ;
133+ // The page's clock runs an hour ahead of the panel's.
134+ const pageNow = Date . now ( ) + hour ;
135+ const block = ( id : string , extra : Record < string , unknown > ) => ( {
136+ id,
137+ owner : { id : 'c1' , name : 'App' , tag : 'app-root' } ,
138+ state : 'complete' ,
139+ hydration : 'not-configured' ,
140+ triggers : [ 'on idle' ] ,
141+ rootIds : [ ] ,
142+ since : pageNow ,
143+ hasErrorBlock : false ,
144+ ...extra ,
145+ } ) ;
146+ const page = {
147+ pageId : 'p1' ,
148+ roots : [ { id : 'c1' , name : 'App' , tag : 'app-root' , children : [ ] } ] ,
149+ count : 1 ,
150+ detail : null ,
151+ reportedAt : 1000 ,
152+ clockOffset : - hour ,
153+ deferBlocks : [
154+ block ( 'd1' , { state : 'error' , hasErrorBlock : true } ) ,
155+ block ( 'd2' , { state : 'placeholder' , since : pageNow - 30_000 } ) ,
156+ block ( 'd3' , { state : 'placeholder' , since : pageNow - 2_000 } ) ,
157+ block ( 'd4' , { hydration : 'dehydrated' , triggers : [ 'hydrate on viewport' ] } ) ,
158+ block ( 'd5' , {
159+ state : 'loading' ,
160+ loading : { minimumTime : 1000 , afterTime : 100 } ,
161+ placeholder : { minimumTime : 500 } ,
162+ } ) ,
163+ ] ,
164+ } ;
165+ const fixture = TestBed . createComponent ( ComponentTree ) ;
166+ fixture . componentRef . setInput ( 'rpc' , client ( { p1 : page } ) ) ;
167+ for ( let i = 0 ; i < 3 ; i ++ ) {
168+ await new Promise ( ( resolve ) => setTimeout ( resolve ) ) ;
169+ await fixture . whenStable ( ) ;
170+ }
171+ const rows = Array . from (
172+ ( fixture . nativeElement as HTMLElement ) . querySelectorAll < HTMLElement > ( '.defer-row' ) ,
173+ ) ;
174+ const flags = ( row : HTMLElement ) =>
175+ Array . from ( row . querySelectorAll ( '.flag' ) ) . map ( ( el ) => el . textContent ?. trim ( ) ) ;
176+ expect ( flags ( rows [ 0 ] ! ) ) . toEqual ( [ 'failed' , '@error block' ] ) ;
177+ expect ( rows [ 0 ] ! . querySelector ( '.note-failed' ) ?. getAttribute ( 'title' ) ) . toBe ( 'failed to load' ) ;
178+ // 30 s on the page's clock, not an hour and 30 s.
179+ expect ( flags ( rows [ 1 ] ! ) ) . toEqual ( [ 'stuck 30s' ] ) ;
180+ expect ( rows [ 1 ] ! . querySelector ( '.note-stuck' ) ?. getAttribute ( 'title' ) ) . toBe (
181+ 'on its placeholder for 30s' ,
182+ ) ;
183+ expect ( flags ( rows [ 2 ] ! ) ) . toEqual ( [ ] ) ;
184+ expect ( flags ( rows [ 3 ] ! ) ) . toEqual ( [ 'dehydrated' ] ) ;
185+ expect ( rows [ 4 ] ! . querySelector ( '.timings' ) ?. textContent ?. trim ( ) ) . toBe (
186+ 'placeholder min 500ms · loading after 100ms, min 1s' ,
187+ ) ;
188+ expect ( rows [ 0 ] ! . querySelector ( '.timings' ) ) . toBeNull ( ) ;
189+ fixture . destroy ( ) ;
190+ } ) ;
191+
192+ it ( 'highlights every root element of a block in one request' , async ( ) => {
193+ const call = vi . fn ( ( ..._args : unknown [ ] ) => Promise . resolve ( [ ] as unknown ) ) ;
194+ const block = ( id : string , rootIds : string [ ] ) => ( {
195+ id,
196+ owner : { id : 'c1' , name : 'App' , tag : 'app-root' } ,
197+ state : 'complete' ,
198+ hydration : 'not-configured' ,
199+ triggers : [ ] ,
200+ rootIds,
201+ } ) ;
202+ const page = {
203+ pageId : 'p1' ,
204+ roots : [ { id : 'c1' , name : 'App' , tag : 'app-root' , children : [ ] } ] ,
205+ count : 1 ,
206+ detail : null ,
207+ reportedAt : 1000 ,
208+ deferBlocks : [ block ( 'd1' , [ 'e1' , 'e2' ] ) , block ( 'd2' , [ 'e3' ] ) , block ( 'd3' , [ ] ) ] ,
209+ } ;
210+ const fixture = TestBed . createComponent ( ComponentTree ) ;
211+ fixture . componentRef . setInput ( 'rpc' , client ( { p1 : page } , call ) ) ;
212+ for ( let i = 0 ; i < 3 ; i ++ ) {
213+ await new Promise ( ( resolve ) => setTimeout ( resolve ) ) ;
214+ await fixture . whenStable ( ) ;
215+ }
216+ const rows = ( fixture . nativeElement as HTMLElement ) . querySelectorAll ( '.defer-row' ) ;
217+ const highlights = ( ) =>
218+ call . mock . calls . filter ( ( args ) => args [ 0 ] === 'request-page-highlight' ) . map ( ( args ) => args [ 1 ] ) ;
219+ call . mockClear ( ) ;
220+ for ( const row of Array . from ( rows ) ) {
221+ row . dispatchEvent ( new Event ( 'mouseenter' ) ) ;
222+ row . dispatchEvent ( new Event ( 'mouseleave' ) ) ;
223+ }
224+ expect ( highlights ( ) ) . toEqual ( [
225+ { pageId : 'p1' , ids : [ 'e1' , 'e2' ] } ,
226+ null ,
227+ { pageId : 'p1' , id : 'e3' } ,
228+ null ,
229+ { pageId : 'p1' , id : 'c1' } ,
230+ null ,
231+ ] ) ;
232+ fixture . destroy ( ) ;
233+ } ) ;
123234} ) ;
0 commit comments