@@ -129,16 +129,42 @@ export async function assertHarperHighlightBoxes(page: Page, boxes: Box[]): Prom
129129 const highlightCount = await highlights . count ( ) ;
130130 expect ( highlightCount ) . toBe ( boxes . length ) ;
131131
132+ const actualBoxes : Box [ ] = [ ] ;
133+
132134 for ( let i = 0 ; i < highlightCount ; i ++ ) {
133135 const box = await highlights . nth ( i ) . boundingBox ( ) ;
136+ expect ( box ) . not . toBeNull ( ) ;
137+ if ( box != null ) {
138+ actualBoxes . push ( box as Box ) ;
139+ }
140+ }
134141
135- console . log ( `Expected: ${ JSON . stringify ( boxes [ i ] ) } ` ) ;
136- console . log ( `Got: ${ JSON . stringify ( box ) } ` ) ;
142+ const expectedSorted = sortBoxes ( boxes ) ;
143+ const actualSorted = sortBoxes ( actualBoxes ) ;
137144
138- assertBoxesClose ( box , boxes [ i ] ) ;
145+ for ( let i = 0 ; i < expectedSorted . length ; i ++ ) {
146+ console . log ( `Expected: ${ JSON . stringify ( expectedSorted [ i ] ) } ` ) ;
147+ console . log ( `Got: ${ JSON . stringify ( actualSorted [ i ] ) } ` ) ;
148+ assertBoxesClose ( actualSorted [ i ] , expectedSorted [ i ] ) ;
139149 }
140150}
141151
152+ function sortBoxes ( values : Box [ ] ) : Box [ ] {
153+ return [ ...values ] . sort ( ( a , b ) => {
154+ if ( a == null || b == null ) return 0 ;
155+ if ( a . y !== b . y ) {
156+ return a . y - b . y ;
157+ }
158+ if ( a . x !== b . x ) {
159+ return a . x - b . x ;
160+ }
161+ if ( a . width !== b . width ) {
162+ return a . width - b . width ;
163+ }
164+ return a . height - b . height ;
165+ } ) ;
166+ }
167+
142168/** An assertion that checks to ensure that two boxes are _approximately_ equal.
143169 * Leaves wiggle room for floating point error. */
144170export function assertBoxesClose ( a : Box , b : Box ) {
@@ -148,6 +174,19 @@ export function assertBoxesClose(a: Box, b: Box) {
148174 assertClose ( a . height , b . height ) ;
149175}
150176
177+ export async function getHarperHighlightBoxes ( page : Page ) : Promise < Box [ ] > {
178+ const highlights = getHarperHighlights ( page ) ;
179+ const count = await highlights . count ( ) ;
180+ const boxes : Box [ ] = [ ] ;
181+ for ( let i = 0 ; i < count ; i ++ ) {
182+ const box = await highlights . nth ( i ) . boundingBox ( ) ;
183+ if ( box != null ) {
184+ boxes . push ( box as Box ) ;
185+ }
186+ }
187+ return boxes ;
188+ }
189+
151190function assertClose ( actual : number , expected : number ) {
152191 expect ( Math . abs ( actual - expected ) ) . toBeLessThanOrEqual ( 9 ) ;
153192}
0 commit comments