@@ -71,49 +71,72 @@ afterEach(() => {
7171} ) ;
7272
7373describe ( "storage trash restore job responsiveness" , ( ) => {
74- test ( "test-stream route is absent without OPENCODEX_CLEANUP_TEST_HOOKS" , async ( ) => {
75- delete process . env . OPENCODEX_CLEANUP_TEST_HOOKS ;
76- setRestoreTrashJobTestHooks ( { enableTestStream : true } ) ;
74+ async function withTestStreamRoute (
75+ setup : ( ) => void ,
76+ assert : ( serverUrl : string ) => Promise < void > ,
77+ ) : Promise < void > {
78+ const previousHooksEnv = process . env . OPENCODEX_CLEANUP_TEST_HOOKS ;
79+ setup ( ) ;
7780 const server = startServer ( 0 ) ;
7881 try {
79- const res = await fetch ( new URL ( "/api/storage/trash/restore/test-stream" , server . url ) ) ;
80- expect ( res . status ) . toBe ( 404 ) ;
81- expect ( res . headers . get ( "content-type" ) ?? "" ) . toContain ( "application/json" ) ;
82- expect ( await res . json ( ) ) . toEqual ( { error : "not_available" } ) ;
82+ await assert ( server . url . toString ( ) ) ;
8383 } finally {
84- await server . stop ( true ) ;
84+ try {
85+ await server . stop ( true ) ;
86+ } finally {
87+ setRestoreTrashJobTestHooks ( null ) ;
88+ if ( previousHooksEnv === undefined ) delete process . env . OPENCODEX_CLEANUP_TEST_HOOKS ;
89+ else process . env . OPENCODEX_CLEANUP_TEST_HOOKS = previousHooksEnv ;
90+ }
8591 }
92+ }
93+
94+ test ( "test-stream route is absent without OPENCODEX_CLEANUP_TEST_HOOKS" , async ( ) => {
95+ await withTestStreamRoute (
96+ ( ) => {
97+ delete process . env . OPENCODEX_CLEANUP_TEST_HOOKS ;
98+ setRestoreTrashJobTestHooks ( { enableTestStream : true } ) ;
99+ } ,
100+ async ( serverUrl ) => {
101+ const res = await fetch ( new URL ( "/api/storage/trash/restore/test-stream" , serverUrl ) ) ;
102+ expect ( res . status ) . toBe ( 404 ) ;
103+ expect ( res . headers . get ( "content-type" ) ?? "" ) . toContain ( "application/json" ) ;
104+ expect ( await res . json ( ) ) . toEqual ( { error : "not_available" } ) ;
105+ } ,
106+ ) ;
86107 } ) ;
87108
88109 test ( "test-stream route returns JSON 404 when hooks are enabled but stream is null" , async ( ) => {
89- process . env . OPENCODEX_CLEANUP_TEST_HOOKS = "1" ;
90- // enableTestStream omitted → getRestoreTrashTestStreamResponse() returns null
91- setRestoreTrashJobTestHooks ( { blockMs : 0 } ) ;
92- const server = startServer ( 0 ) ;
93- try {
94- const res = await fetch ( new URL ( "/api/storage/trash/restore/test-stream" , server . url ) ) ;
95- expect ( res . status ) . toBe ( 404 ) ;
96- expect ( res . headers . get ( "content-type" ) ?? "" ) . toContain ( "application/json" ) ;
97- expect ( await res . json ( ) ) . toEqual ( { error : "not_available" } ) ;
98- } finally {
99- await server . stop ( true ) ;
100- }
110+ await withTestStreamRoute (
111+ ( ) => {
112+ process . env . OPENCODEX_CLEANUP_TEST_HOOKS = "1" ;
113+ // enableTestStream omitted → getRestoreTrashTestStreamResponse() returns null
114+ setRestoreTrashJobTestHooks ( { blockMs : 0 } ) ;
115+ } ,
116+ async ( serverUrl ) => {
117+ const res = await fetch ( new URL ( "/api/storage/trash/restore/test-stream" , serverUrl ) ) ;
118+ expect ( res . status ) . toBe ( 404 ) ;
119+ expect ( res . headers . get ( "content-type" ) ?? "" ) . toContain ( "application/json" ) ;
120+ expect ( await res . json ( ) ) . toEqual ( { error : "not_available" } ) ;
121+ } ,
122+ ) ;
101123 } ) ;
102124
103125 test ( "test-stream route serves the enabled hook stream" , async ( ) => {
104- process . env . OPENCODEX_CLEANUP_TEST_HOOKS = "1" ;
105- setRestoreTrashJobTestHooks ( { enableTestStream : true } ) ;
106- const server = startServer ( 0 ) ;
107- try {
108- const res = await fetch ( new URL ( "/api/storage/trash/restore/test-stream" , server . url ) ) ;
109- expect ( res . status ) . toBe ( 200 ) ;
110- expect ( res . headers . get ( "content-type" ) ?? "" ) . toContain ( "text/plain" ) ;
111- const body = await res . text ( ) ;
112- expect ( body ) . toContain ( "chunk-0" ) ;
113- expect ( body ) . toContain ( "chunk-7" ) ;
114- } finally {
115- await server . stop ( true ) ;
116- }
126+ await withTestStreamRoute (
127+ ( ) => {
128+ process . env . OPENCODEX_CLEANUP_TEST_HOOKS = "1" ;
129+ setRestoreTrashJobTestHooks ( { enableTestStream : true } ) ;
130+ } ,
131+ async ( serverUrl ) => {
132+ const res = await fetch ( new URL ( "/api/storage/trash/restore/test-stream" , serverUrl ) ) ;
133+ expect ( res . status ) . toBe ( 200 ) ;
134+ expect ( res . headers . get ( "content-type" ) ?? "" ) . toContain ( "text/plain" ) ;
135+ const body = await res . text ( ) ;
136+ expect ( body ) . toContain ( "chunk-0" ) ;
137+ expect ( body ) . toContain ( "chunk-7" ) ;
138+ } ,
139+ ) ;
117140 } , { timeout : 10_000 } ) ;
118141
119142 test ( "blocked worker keeps /healthz and streaming response responsive" , async ( ) => {
0 commit comments