@@ -81,6 +81,8 @@ let createResult: any = [{ id: "mem-new" }]
8181let workspaces : any [ ] = [ { id : 42 , name : "acme" , memory_enabled : true } ]
8282/** When set, GET /list fails -- used to prove a failed lookup never creates. */
8383let listFails = false
84+ /** When set, a PATCH to this record id fails. */
85+ let failPatchOf : string | undefined
8486
8587function stubFetch ( ) {
8688 globalThis . fetch = ( async ( input : any , init ?: any ) => {
@@ -90,6 +92,9 @@ function stubFetch() {
9092 if ( listFails && url . includes ( "/datamates/memory/list" ) ) {
9193 return new Response ( JSON . stringify ( { detail : "boom" } ) , { status : 500 } )
9294 }
95+ if ( failPatchOf && method === "PATCH" && url . endsWith ( `/datamates/memory/${ failPatchOf } ` ) ) {
96+ return new Response ( JSON . stringify ( { detail : "boom" } ) , { status : 500 } )
97+ }
9398 const payload = ( ( ) => {
9499 // Server-side binding lookup, used when no local cache entry exists.
95100 if ( url . includes ( "/datamate-project-bindings/by-" ) ) {
@@ -164,6 +169,7 @@ beforeEach(() => {
164169 captured = [ ]
165170 listResponse = [ ]
166171 listFails = false
172+ failPatchOf = undefined
167173 createResult = [ { id : "mem-new" } ]
168174 workspaces = [ { id : 42 , name : "acme" , memory_enabled : true } ]
169175 serverBinding = null
@@ -2326,6 +2332,51 @@ describe("two live records of one block", () => {
23262332 expect ( callsTo ( "/datamates/memory/" , "POST" ) . length ) . toBe ( 0 )
23272333 } )
23282334
2335+ test ( "a copy whose archive failed is archived by the next save of the same content" , async ( ) => {
2336+ boundTo ( { repoRemote : HTTPS } )
2337+ listResponse = [
2338+ copy ( "mem-retry-ssh" , "copies-retry" , { repo_remote : SSH , block_updated : OLDER } ) ,
2339+ copy ( "mem-retry-https" , "copies-retry" , { repo_remote : HTTPS , block_updated : NEWER } ) ,
2340+ ]
2341+ failPatchOf = "mem-retry-ssh"
2342+ const b = edited ( "copies-retry" )
2343+ await mirrorBlock ( b ) . catch ( ( ) => undefined )
2344+ expect ( callsTo ( "/datamates/memory/mem-retry-ssh" , "PATCH" ) . length ) . toBe ( 1 )
2345+
2346+ failPatchOf = undefined
2347+ captured = [ ]
2348+ await mirrorBlock ( b )
2349+ const archived = callsTo ( "/datamates/memory/mem-retry-ssh" , "PATCH" )
2350+ expect ( archived . length ) . toBe ( 1 )
2351+ expect ( archived [ 0 ] . body . metadata . archived ) . toBe ( "true" )
2352+ } )
2353+
2354+ test ( "a split create's extra whose archive failed is archived by the next save" , async ( ) => {
2355+ boundTo ( { repoRemote : HTTPS } )
2356+ createResult = [ { id : "mem-split-primary" } , { id : "mem-split-extra" } ]
2357+ failPatchOf = "mem-split-extra"
2358+ const b = edited ( "copies-split" )
2359+ await mirrorBlock ( b ) . catch ( ( ) => undefined )
2360+ expect ( callsTo ( "/datamates/memory/mem-split-extra" , "PATCH" ) . length ) . toBe ( 1 )
2361+ // What the service now holds: both records under the metadata the create
2362+ // sent, the primary's rewritten by its repair update.
2363+ const sent = callsTo ( "/datamates/memory/" , "POST" ) [ 0 ] . body . memory_options . metadata
2364+ const repair = callsTo ( "/datamates/memory/mem-split-primary" , "PATCH" ) [ 0 ] . body
2365+ listResponse = [
2366+ { id : "mem-split-primary" , memory : repair . memory , metadata : repair . metadata } ,
2367+ { id : "mem-split-extra" , memory : "rewritten by the extractor" , metadata : sent } ,
2368+ ]
2369+
2370+ failPatchOf = undefined
2371+ captured = [ ]
2372+ await mirrorBlock ( b )
2373+ expect ( callsTo ( "/datamates/memory/" , "POST" ) . length ) . toBe ( 0 )
2374+ const patched = [ "mem-split-primary" , "mem-split-extra" ] . map (
2375+ ( id ) => callsTo ( `/datamates/memory/${ id } ` , "PATCH" ) [ 0 ] ?. body . metadata . archived ,
2376+ )
2377+ expect ( patched . sort ( ) ) . toEqual ( [ "true" , undefined ] . sort ( ) )
2378+ } )
2379+
23292380 test ( "a delete archives every live copy" , async ( ) => {
23302381 boundTo ( { repoRemote : HTTPS } )
23312382 listResponse = [
@@ -2346,6 +2397,20 @@ describe("two live records of one block", () => {
23462397 expect ( callsTo ( "/datamates/memory/mem-del-other" , "PATCH" ) . length ) . toBe ( 0 )
23472398 } )
23482399
2400+ test ( "a delete on a truncated listing archives the copies in reach" , async ( ) => {
2401+ boundTo ( { repoRemote : HTTPS } )
2402+ listResponse = [
2403+ copy ( "mem-trunc-ssh" , "copies-trunc" , { repo_remote : SSH , block_updated : OLDER } ) ,
2404+ ...Array . from ( { length : LIST_LIMIT - 1 } , ( _ , i ) => ( {
2405+ id : `trunc-filler-${ i } ` ,
2406+ memory : "x" ,
2407+ metadata : { source : MIRROR_SOURCE , block_id : `filler/${ i } ` , block_scope : "global" } ,
2408+ } ) ) ,
2409+ ]
2410+ await archiveBlock ( "project" , "copies-trunc" )
2411+ expect ( callsTo ( "/datamates/memory/mem-trunc-ssh" , "PATCH" ) [ 0 ] . body . metadata . archived ) . toBe ( "true" )
2412+ } )
2413+
23492414 test ( "a delete whose indexed record is already archived still archives a live copy" , async ( ) => {
23502415 boundTo ( { repoRemote : HTTPS } )
23512416 await recordIndexEntry (
0 commit comments