11import fs from "node:fs" ;
2+ import http from "node:http" ;
3+ import type { AddressInfo } from "node:net" ;
24import os from "node:os" ;
35import path from "node:path" ;
46import type { OpenKeyedStoreOptions } from "openclaw/plugin-sdk/plugin-state-runtime" ;
@@ -13,8 +15,11 @@ import { generateIdentity } from "../protocol/index.js";
1315import { runReefChannelLifecycle } from "./channel-lifecycle.js" ;
1416import { reefPlugin } from "./channel.js" ;
1517import { resolveReefConfig } from "./config-schema.js" ;
18+ import { reefKeys } from "./flow.test-helpers.js" ;
19+ import { ReefFriendManager } from "./friends.js" ;
1620import { resolveReefInboundDispatchContent } from "./inbound.js" ;
1721import { setReefRuntime } from "./runtime.js" ;
22+ import { ReefTransportClient } from "./transport.js" ;
1823import { openReefTrustStore } from "./trust-store.js" ;
1924
2025function deferred ( ) {
@@ -350,6 +355,69 @@ describe("Reef channel lifecycle", () => {
350355 expect ( inbox . seen ) . toHaveLength ( 0 ) ;
351356 } ) ;
352357
358+ it . each ( [
359+ { phase : "friend reconciliation" , completedRequests : 0 } ,
360+ { phase : "pairing-candidate surfacing" , completedRequests : 1 } ,
361+ ] ) ( "promptly aborts a stalled $phase request" , async ( { completedRequests } ) => {
362+ const requestStarted = deferred ( ) ;
363+ let requests = 0 ;
364+ const server = http . createServer ( ( _request , response ) => {
365+ if ( requests ++ < completedRequests ) {
366+ response . writeHead ( 200 , { "content-type" : "application/json" } ) ;
367+ response . end ( JSON . stringify ( { friendships : [ ] } ) ) ;
368+ return ;
369+ }
370+ requestStarted . resolve ( ) ;
371+ } ) ;
372+ await new Promise < void > ( ( resolve , reject ) => {
373+ server . once ( "error" , reject ) ;
374+ server . listen ( 0 , "127.0.0.1" , resolve ) ;
375+ } ) ;
376+
377+ const parent = new AbortController ( ) ;
378+ const inbox = hangingInbox ( ) ;
379+ const relayUrl = `http://127.0.0.1:${ ( server . address ( ) as AddressInfo ) . port } ` ;
380+ const transport = new ReefTransportClient (
381+ relayUrl ,
382+ "alice" ,
383+ reefKeys ( ) ,
384+ fetch ,
385+ ( ) => 1_752_300_000 ,
386+ 1_000 ,
387+ ) ;
388+ const friends = new ReefFriendManager (
389+ transport ,
390+ { } as ConstructorParameters < typeof ReefFriendManager > [ 1 ] ,
391+ { list : async ( ) => [ ] , remove : async ( ) => false } ,
392+ ) ;
393+ const lifecycle = runReefChannelLifecycle ( {
394+ parentSignal : parent . signal ,
395+ startInbox : inbox . startInbox ,
396+ reconcile : async ( signal ) => {
397+ await friends . reconcile ( signal ) ;
398+ await friends . surfacePairingCandidates ( async ( ) => { } , signal ) ;
399+ } ,
400+ onReconcileError : ( ) => { } ,
401+ } ) ;
402+
403+ try {
404+ await requestStarted . promise ;
405+ const abortedAt = performance . now ( ) ;
406+ parent . abort ( ) ;
407+ await lifecycle ;
408+
409+ expect ( performance . now ( ) - abortedAt ) . toBeLessThan ( 500 ) ;
410+ expect ( inbox . seen ) . toHaveLength ( 0 ) ;
411+ } finally {
412+ parent . abort ( ) ;
413+ server . closeAllConnections ( ) ;
414+ await new Promise < void > ( ( resolve ) => {
415+ server . close ( ( ) => resolve ( ) ) ;
416+ } ) ;
417+ await lifecycle ;
418+ }
419+ } ) ;
420+
353421 it ( "does not start the inbox when the parent aborts during activation" , async ( ) => {
354422 const parent = new AbortController ( ) ;
355423 const inbox = hangingInbox ( ) ;
0 commit comments