@@ -42,7 +42,8 @@ describe('drillRecordOrigin (#36554)', () => {
4242 expect ( object ) . toBe ( 'deal' ) ;
4343 expect ( params ) . toEqual ( {
4444 $filter : QUERY . $filter ,
45- $orderby : QUERY . $orderby ,
45+ // The list's sort, with the record id as the last tiebreaker.
46+ $orderby : [ ...QUERY . $orderby , { field : 'id' , order : 'asc' } ] ,
4647 $search : 'acme' ,
4748 $searchFields : [ 'name' ] ,
4849 $select : [ 'id' ] ,
@@ -78,6 +79,57 @@ describe('drillRecordOrigin (#36554)', () => {
7879 expect ( ds . find . mock . calls . map ( ( [ , params ] ) => ( params as { $top ?: number } ) . $top ) ) . toEqual ( [ 250 , 250 ] ) ;
7980 } ) ;
8081
82+ it ( 'reads a sort with ties in a stable order: every record once, across windows' , async ( ) => {
83+ // 150 deals that all tie on the list's sort, so the result spans two
84+ // windows. Like a database, the source answers ties in no fixed order
85+ // unless the sort settles them: here a later window sees the ties reversed.
86+ const rows = Array . from ( { length : 150 } , ( _ , i ) => ( { id : `d${ String ( i + 1 ) . padStart ( 3 , '0' ) } ` , amount : 1 } ) ) ;
87+ const ds = {
88+ find : vi . fn ( async ( _object : string , params : { $top : number ; $skip ?: number ; $orderby ?: Array < { field : string } > } ) => {
89+ const skip = params . $skip ?? 0 ;
90+ const settled = params . $orderby ?. [ params . $orderby . length - 1 ] ?. field === 'id' ;
91+ const ordered = settled || skip === 0 ? rows : [ ...rows ] . reverse ( ) ;
92+ return { data : ordered . slice ( skip , skip + params . $top ) . map ( ( { id } ) => ( { id } ) ) , total : rows . length } ;
93+ } ) ,
94+ } ;
95+ const origin = await drillRecordOrigin ( {
96+ dataSource : ds , objectName : 'deal' , recordId : 'd002' , drawn : [ 'd001' , 'd002' ] , total : 150 , findParams : QUERY ,
97+ } ) ;
98+ expect ( ds . find ) . toHaveBeenCalledTimes ( 2 ) ;
99+ for ( const [ , params ] of ds . find . mock . calls ) {
100+ expect ( params . $orderby ) . toEqual ( [ { field : 'amount' , order : 'desc' } , { field : 'id' , order : 'asc' } ] ) ;
101+ }
102+ expect ( origin . siblings ) . toEqual ( rows . map ( ( { id } ) => id ) ) ;
103+ } ) ;
104+
105+ it ( 'sorts by the record id alone when the list has no sort, and keeps an id sort as written' , async ( ) => {
106+ const ds = source ( 40 ) ;
107+ const { $orderby : _unsorted , ...noSort } = QUERY ;
108+ await drillRecordOrigin ( { dataSource : ds , objectName : 'deal' , recordId : 'd1' , drawn : [ 'd1' , 'd2' ] , total : 40 , findParams : noSort } ) ;
109+ expect ( ds . find . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( { $orderby : [ { field : 'id' , order : 'asc' } ] } ) ;
110+
111+ const byId = [ { field : 'id' , order : 'desc' } ] ;
112+ await drillRecordOrigin ( {
113+ dataSource : ds , objectName : 'deal' , recordId : 'd1' , drawn : [ 'd1' , 'd2' ] , total : 40 , findParams : { ...QUERY , $orderby : byId } ,
114+ } ) ;
115+ expect ( ds . find . mock . calls [ 1 ] [ 1 ] ) . toMatchObject ( { $orderby : byId } ) ;
116+ } ) ;
117+
118+ it ( 'counts a record once when two windows both answer it' , async ( ) => {
119+ // A source whose second window starts one row early: the boundary row comes back twice.
120+ const ids = Array . from ( { length : 150 } , ( _ , i ) => `d${ i + 1 } ` ) ;
121+ const ds = {
122+ find : vi . fn ( async ( _object : string , params : { $top : number ; $skip ?: number } ) => {
123+ const skip = Math . max ( 0 , ( params . $skip ?? 0 ) - 1 ) ;
124+ return { data : ids . slice ( skip , skip + params . $top ) . map ( ( id ) => ( { id } ) ) , total : ids . length } ;
125+ } ) ,
126+ } ;
127+ const origin = await drillRecordOrigin ( {
128+ dataSource : ds , objectName : 'deal' , recordId : 'd1' , drawn : [ 'd1' , 'd2' ] , total : 150 , findParams : QUERY ,
129+ } ) ;
130+ expect ( origin . siblings ) . toEqual ( ids ) ;
131+ } ) ;
132+
81133 it ( 'control: a result all drawn reads nothing and hands the rows drawn with the count, at once' , ( ) => {
82134 const ds = source ( 3 ) ;
83135 const origin = drillRecordOrigin ( {
@@ -102,6 +154,7 @@ describe('drillRecordOrigin (#36554)', () => {
102154 } ) ;
103155
104156 it ( 'keeps the rows drawn when the read fails or no longer holds the record opened' , async ( ) => {
157+ const warn = vi . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } ) ;
105158 const failing = { find : vi . fn ( async ( ) => { throw new Error ( 'offline' ) ; } ) } ;
106159 expect ( await drillRecordOrigin ( {
107160 dataSource : failing , objectName : 'deal' , recordId : 'd1' , drawn : [ 'd1' , 'd2' ] , total : 40 , findParams : QUERY ,
@@ -111,5 +164,11 @@ describe('drillRecordOrigin (#36554)', () => {
111164 expect ( await drillRecordOrigin ( {
112165 dataSource : moved , objectName : 'deal' , recordId : 'gone' , drawn : [ 'gone' , 'd2' ] , total : 40 , findParams : QUERY ,
113166 } ) ) . toEqual ( { siblings : [ 'gone' , 'd2' ] , total : 40 } ) ;
167+ // The failure is said once, not on every click.
168+ await drillRecordOrigin ( {
169+ dataSource : failing , objectName : 'deal' , recordId : 'd1' , drawn : [ 'd1' , 'd2' ] , total : 40 , findParams : QUERY ,
170+ } ) ;
171+ expect ( warn ) . toHaveBeenCalledTimes ( 1 ) ;
172+ warn . mockRestore ( ) ;
114173 } ) ;
115174} ) ;
0 commit comments