@@ -3,6 +3,7 @@ import { createClient } from "@prover-coder-ai/docker-git-openapi"
33import type { ApiFailure } from "@prover-coder-ai/docker-git-openapi"
44import { Effect } from "effect"
55import * as fc from "fast-check"
6+ import { afterEach , vi } from "vitest"
67
78import type { JsonValue } from "../../src/shared/json-schema.js"
89import { renderDockerGitOpenApiFailure } from "../../src/web/api-http.js"
@@ -95,6 +96,11 @@ const assertOpenApiClientProperty = <PropertyArgs>(property: fc.IAsyncProperty<P
9596 try : ( ) => fc . assert ( property , { numRuns : 25 } )
9697 } )
9798
99+ afterEach ( ( ) => {
100+ vi . restoreAllMocks ( )
101+ vi . unstubAllGlobals ( )
102+ } )
103+
98104describe ( "docker-git OpenAPI Effect client" , ( ) => {
99105 it . effect ( "executes typed GET requests directly through openapi-effect" , ( ) =>
100106 Effect . gen ( function * ( _ ) {
@@ -223,4 +229,70 @@ describe("docker-git OpenAPI Effect client", () => {
223229 expect ( requests [ 0 ] ?. method ) . toBe ( "POST" )
224230 expect ( new URL ( requests [ 0 ] ?. url ?? "" ) . pathname ) . toBe ( "/projects/down-all" )
225231 } ) )
232+
233+ it . effect ( "falls back to getRandomValues when randomUUID is unavailable" , ( ) =>
234+ Effect . gen ( function * ( _ ) {
235+ const requests : Array < CapturedRequest > = [ ]
236+ const capturedIds : Array < string > = [ ]
237+ vi . stubGlobal ( "crypto" , {
238+ getRandomValues : ( values : Uint8Array ) : Uint8Array => {
239+ values . set ( [ 0x10 , 0x32 , 0x54 , 0x76 , 0x98 ] )
240+ return values
241+ }
242+ } )
243+
244+ const api = createClient ( {
245+ baseUrl : "https://docker-git.example.test" ,
246+ fetch : createMockFetch (
247+ requests ,
248+ createJsonResponse ( 200 , {
249+ cwd : "/workspace" ,
250+ ok : true ,
251+ projectsRoot : "/workspace/projects" ,
252+ revision : null
253+ } )
254+ )
255+ } )
256+ api . use ( {
257+ onRequest : ( { id } ) => {
258+ capturedIds . push ( id )
259+ }
260+ } )
261+
262+ const success = yield * _ ( api . GET ( "/health" ) )
263+
264+ expect ( success . status ) . toBe ( 200 )
265+ expect ( capturedIds ) . toEqual ( [ "103254769" ] )
266+ expect ( requests ) . toHaveLength ( 1 )
267+ } ) )
268+
269+ it . effect ( "falls back to a clock-based request id when Web Crypto is missing" , ( ) =>
270+ Effect . gen ( function * ( _ ) {
271+ const capturedIds : Array < string > = [ ]
272+ vi . stubGlobal ( "crypto" , undefined )
273+ vi . spyOn ( Date , "now" ) . mockReturnValue ( 0x1234567 )
274+
275+ const api = createClient ( {
276+ baseUrl : "https://docker-git.example.test" ,
277+ fetch : createMockFetch (
278+ [ ] ,
279+ createJsonResponse ( 200 , {
280+ cwd : "/workspace" ,
281+ ok : true ,
282+ projectsRoot : "/workspace/projects" ,
283+ revision : null
284+ } )
285+ )
286+ } )
287+ api . use ( {
288+ onRequest : ( { id } ) => {
289+ capturedIds . push ( id )
290+ }
291+ } )
292+
293+ const success = yield * _ ( api . GET ( "/health" ) )
294+
295+ expect ( success . status ) . toBe ( 200 )
296+ expect ( capturedIds ) . toEqual ( [ "123456700" ] )
297+ } ) )
226298} )
0 commit comments