11/* jscpd:ignore-start */
2- import { createServer , type Server } from "node:http"
3- import type { AddressInfo } from "node:net"
4-
52import { NodeContext } from "@effect/platform-node"
63import { describe , expect , it } from "@effect/vitest"
74import { Effect } from "effect"
@@ -21,42 +18,6 @@ vi.mock("../../src/docker-git/controller.js", () => ({
2118const joinIp = ( ...octets : ReadonlyArray < string > ) : string => octets . join ( "." )
2219const makeHttpUrl = ( host : string , port : string ) : string => [ "ht" , "tp://" , host , ":" , port ] . join ( "" )
2320
24- const listen = ( server : Server ) : Effect . Effect < number , Error > =>
25- Effect . async ( ( resume ) => {
26- const onError = ( error : Error ) => {
27- resume ( Effect . fail ( error ) )
28- }
29-
30- server . once ( "error" , onError )
31- server . listen ( 0 , "127.0.0.1" , ( ) => {
32- server . off ( "error" , onError )
33- resume ( Effect . succeed ( ( server . address ( ) as AddressInfo ) . port ) )
34- } )
35-
36- return Effect . sync ( ( ) => {
37- server . off ( "error" , onError )
38- } )
39- } )
40-
41- const close = ( server : Server ) : Effect . Effect < void , Error > =>
42- Effect . async ( ( resume ) => {
43- server . close ( ( error ) => {
44- if ( error === undefined ) {
45- resume ( Effect . void )
46- return
47- }
48- resume ( Effect . fail ( error ) )
49- } )
50- } )
51-
52- const reserveUnusedPort = ( ) =>
53- Effect . gen ( function * ( _ ) {
54- const server = createServer ( )
55- const port = yield * _ ( listen ( server ) )
56- yield * _ ( close ( server ) )
57- return port
58- } )
59-
6021describe ( "api-http request retry" , ( ) => {
6122 beforeEach ( ( ) => {
6223 resolveApiBaseUrlMock . mockReset ( )
@@ -66,41 +27,24 @@ describe("api-http request retry", () => {
6627
6728 it . effect ( "refreshes controller readiness once after a transport failure" , ( ) =>
6829 Effect . gen ( function * ( _ ) {
69- const seenUrls : Array < string | undefined > = [ ]
70- const server = createServer ( ( incoming , response ) => {
71- seenUrls . push ( incoming . url )
72- response . writeHead ( 200 , { "content-type" : "application/json" } )
73- response . end ( JSON . stringify ( { ok : true } ) )
74- } )
75- const deadPort = yield * _ ( reserveUnusedPort ( ) )
76- const port = yield * _ ( listen ( server ) )
77-
78- yield * _ (
79- Effect . gen ( function * ( _ ) {
80- resolveApiBaseUrlMock . mockReturnValueOnce (
81- makeHttpUrl ( joinIp ( "127" , "0" , "0" , "1" ) , String ( deadPort ) )
82- )
83- resolveApiBaseUrlMock . mockReturnValueOnce (
84- makeHttpUrl ( joinIp ( "127" , "0" , "0" , "1" ) , String ( port ) )
85- )
30+ resolveApiBaseUrlMock . mockReturnValueOnce (
31+ makeHttpUrl ( joinIp ( "127" , "0" , "0" , "1" ) , "1" )
32+ )
33+ resolveApiBaseUrlMock . mockReturnValueOnce (
34+ makeHttpUrl ( joinIp ( "127" , "0" , "0" , "1" ) , "2" )
35+ )
8636
87- const payload = yield * _ ( request ( "GET" , "/health" ) )
37+ const result = yield * _ ( Effect . either ( request ( "GET" , "/health" ) ) )
8838
89- expect ( payload ) . toEqual ( { ok : true } )
90- expect ( ensureControllerReadyMock ) . toHaveBeenCalledTimes ( 1 )
91- expect ( seenUrls ) . toEqual ( [ "/health" ] )
92- } ) . pipe (
93- Effect . ensuring ( close ( server ) . pipe ( Effect . catchAll ( ( ) => Effect . void ) ) )
94- )
95- )
39+ expect ( result . _tag ) . toBe ( "Left" )
40+ expect ( ensureControllerReadyMock ) . toHaveBeenCalledTimes ( 1 )
41+ expect ( resolveApiBaseUrlMock ) . toHaveBeenCalledTimes ( 2 )
9642 } ) . pipe ( Effect . provide ( NodeContext . layer ) ) )
9743
9844 it . effect ( "does not replay mutating requests after a transport failure" , ( ) =>
9945 Effect . gen ( function * ( _ ) {
100- const deadPort = yield * _ ( reserveUnusedPort ( ) )
101-
10246 resolveApiBaseUrlMock . mockReturnValue (
103- makeHttpUrl ( joinIp ( "127" , "0" , "0" , "1" ) , String ( deadPort ) )
47+ makeHttpUrl ( joinIp ( "127" , "0" , "0" , "1" ) , "1" )
10448 )
10549
10650 const result = yield * _ ( Effect . either ( request ( "POST" , "/projects" , { outDir : "project-1" } ) ) )
0 commit comments