@@ -217,27 +217,35 @@ function reconstructedRecords(
217217 } ) ;
218218}
219219
220- async function exportPage (
220+ function exportResponse (
221221 env : Record < string , unknown > ,
222222 cursor : number ,
223223 sections : RecordSection [ ] = [ "items" , "commits" ] ,
224+ limit = 200 ,
224225) {
225- const response = await worker . fetch (
226- signedStateAppendRequest (
227- EXPORT_PATH ,
228- { repoSlug : REPO_SLUG , sections, cursor, limit : 200 } ,
229- SECRET ,
230- ) ,
226+ return worker . fetch (
227+ signedStateAppendRequest ( EXPORT_PATH , { repoSlug : REPO_SLUG , sections, cursor, limit } , SECRET ) ,
231228 env ,
232229 ) ;
230+ }
231+
232+ async function exportPage (
233+ env : Record < string , unknown > ,
234+ cursor : number ,
235+ sections : RecordSection [ ] = [ "items" , "commits" ] ,
236+ limit = 200 ,
237+ ) {
238+ const response = await exportResponse ( env , cursor , sections , limit ) ;
233239 assert . equal ( response . status , 200 ) ;
234240 return response . json ( ) as Promise < {
235241 records : Array < {
236242 section : RecordSection ;
237243 id : string ;
238- content : string ;
239- digest : string ;
244+ content : string | null ;
245+ digest : string | null ;
246+ revision : number ;
240247 storeRevision : number ;
248+ deleted : boolean ;
241249 } > ;
242250 nextCursor : number | null ;
243251 } > ;
@@ -295,7 +303,11 @@ test("signed record export bounds reconstruction work and source bytes while pag
295303 sourceBytes : reconstructedBytes ,
296304 reconstructionQueries : reconstructed . length ,
297305 } ) ;
298- received . push ( ...page . records ) ;
306+ for ( const record of page . records ) {
307+ assert . equal ( record . deleted , false ) ;
308+ assert . notEqual ( record . digest , null ) ;
309+ received . push ( { section : record . section , id : record . id , digest : record . digest ! } ) ;
310+ }
299311 pageSizes . push ( page . records . length ) ;
300312 if ( page . nextCursor !== null ) assert . ok ( page . nextCursor > cursor ) ;
301313 nextCursor = page . nextCursor ;
@@ -316,6 +328,88 @@ test("signed record export bounds reconstruction work and source bytes while pag
316328 behaviorProof . manifestParity = true ;
317329} ) ;
318330
331+ test ( "signed record export preserves historical backfill items and canonical tombstones" , async ( ) => {
332+ const { env, storage } = await exportHarness ( ) ;
333+ const historical = fixtureRecord ( {
334+ source : "backfill" ,
335+ section : "items" ,
336+ id : "101" ,
337+ content : "historical backfill item" ,
338+ storeRevision : 1 ,
339+ } ) ;
340+ storage . sql . exec (
341+ `INSERT INTO ${ EXACT_REVIEW_RECORD_BACKFILL_TABLE }
342+ (repo_slug, section, record_id, content, digest, byte_length, chunk_count, updated_at)
343+ VALUES (?, ?, ?, ?, ?, ?, 0, ?)` ,
344+ REPO_SLUG ,
345+ historical . section ,
346+ historical . id ,
347+ historical . content ,
348+ historical . digest ,
349+ historical . byteLength ,
350+ historical . storeRevision ,
351+ ) ;
352+ storage . sql . exec (
353+ `INSERT INTO ${ EXACT_REVIEW_RECORD_EXPORT_INDEX_TABLE }
354+ (repo_slug, section, record_id, digest, deleted, revision, store_revision, source, updated_at)
355+ VALUES (?, ?, ?, ?, 0, 0, 1, 'backfill', 1)` ,
356+ REPO_SLUG ,
357+ historical . section ,
358+ historical . id ,
359+ historical . digest ,
360+ ) ;
361+ storage . sql . exec (
362+ `INSERT INTO ${ EXACT_REVIEW_RECORD_EXPORT_INDEX_TABLE }
363+ (repo_slug, section, record_id, digest, deleted, revision, store_revision, source, updated_at)
364+ VALUES (?, 'items', '102', NULL, 1, 2, 2, 'canonical', 2)` ,
365+ REPO_SLUG ,
366+ ) ;
367+ storage . sql . exec (
368+ `UPDATE ${ EXACT_REVIEW_RECORD_EXPORT_META_TABLE }
369+ SET current_revision = 2 WHERE singleton_id = 1` ,
370+ ) ;
371+
372+ storage . sql . resetQueryHistory ( ) ;
373+ const first = await exportPage ( env , 0 , [ "items" ] , 1 ) ;
374+ assert . deepEqual ( first . records , [
375+ {
376+ section : "items" ,
377+ id : historical . id ,
378+ content : historical . content ,
379+ digest : historical . digest ,
380+ revision : 0 ,
381+ storeRevision : 1 ,
382+ updatedAt : new Date ( 1 ) . toISOString ( ) ,
383+ deleted : false ,
384+ } ,
385+ ] ) ;
386+ assert . equal ( first . nextCursor , 1 ) ;
387+ assert . equal ( storage . sql . queriesMatching ( / F R O M e x a c t _ r e v i e w _ r e c o r d _ b a c k f i l l \s / ) . length , 1 ) ;
388+ assert . equal ( storage . sql . queriesMatching ( / F R O M e x a c t _ r e v i e w _ c a n o n i c a l _ r e c o r d s \s / ) . length , 0 ) ;
389+
390+ storage . sql . resetQueryHistory ( ) ;
391+ const second = await exportPage ( env , first . nextCursor , [ "items" ] ) ;
392+ assert . deepEqual ( second . records , [
393+ {
394+ section : "items" ,
395+ id : "102" ,
396+ content : null ,
397+ digest : null ,
398+ revision : 2 ,
399+ storeRevision : 2 ,
400+ updatedAt : new Date ( 2 ) . toISOString ( ) ,
401+ deleted : true ,
402+ } ,
403+ ] ) ;
404+ assert . equal ( second . nextCursor , null ) ;
405+ assert . equal (
406+ storage . sql . queriesMatching (
407+ / F R O M (?: e x a c t _ r e v i e w _ c a n o n i c a l _ r e c o r d s | e x a c t _ r e v i e w _ r e c o r d _ b a c k f i l l ) \s / ,
408+ ) . length ,
409+ 0 ,
410+ ) ;
411+ } ) ;
412+
319413test ( "signed record export returns one oversized serialized record and advances" , async ( ) => {
320414 const { env, storage } = await exportHarness ( ) ;
321415 const fixtures = [
@@ -361,7 +455,7 @@ test("signed record export returns one oversized serialized record and advances"
361455 assert . equal ( second . nextCursor , null ) ;
362456} ) ;
363457
364- test ( "signed record export rejects missing and invalid logical byte metadata" , async ( ) => {
458+ test ( "signed record export returns sanitized 500 for missing and invalid logical byte metadata" , async ( ) => {
365459 const missingHarness = await exportHarness ( ) ;
366460 const missing = fixtureRecord ( {
367461 source : "canonical" ,
@@ -378,10 +472,12 @@ test("signed record export rejects missing and invalid logical byte metadata", a
378472 missing . section ,
379473 Number ( missing . id ) ,
380474 ) ;
381- await assert . rejects (
382- exportPage ( missingHarness . env , 0 ) ,
383- / i n v a l i d r e c o r d e x p o r t b y t e m e t a d a t a .* i t e m s \/ 1 / ,
384- ) ;
475+ const missingResponse = await exportResponse ( missingHarness . env , 0 ) ;
476+ assert . equal ( missingResponse . status , 500 ) ;
477+ assert . equal ( missingResponse . headers . get ( "content-type" ) , "application/json; charset=utf-8" ) ;
478+ const missingBody = ( await missingResponse . json ( ) ) as { error ?: unknown } ;
479+ assert . equal ( typeof missingBody . error , "string" ) ;
480+ assert . match ( String ( missingBody . error ) , / i n v a l i d r e c o r d e x p o r t b y t e m e t a d a t a .* i t e m s \/ 1 / ) ;
385481
386482 const invalidHarness = await exportHarness ( ) ;
387483 const invalid = fixtureRecord ( {
@@ -400,8 +496,13 @@ test("signed record export rejects missing and invalid logical byte metadata", a
400496 invalid . section ,
401497 invalid . id ,
402498 ) ;
403- await assert . rejects (
404- exportPage ( invalidHarness . env , 0 ) ,
405- / i n v a l i d r e c o r d e x p o r t b y t e m e t a d a t a .* c o m m i t s / ,
499+ const invalidResponse = await exportResponse ( invalidHarness . env , 0 ) ;
500+ assert . equal ( invalidResponse . status , 500 ) ;
501+ assert . equal ( invalidResponse . headers . get ( "content-type" ) , "application/json; charset=utf-8" ) ;
502+ const invalidBody = ( await invalidResponse . json ( ) ) as { error ?: unknown } ;
503+ assert . equal ( typeof invalidBody . error , "string" ) ;
504+ assert . match (
505+ String ( invalidBody . error ) ,
506+ new RegExp ( `invalid record export byte metadata.*commits/${ invalid . id } ` ) ,
406507 ) ;
407508} ) ;
0 commit comments