|
| 1 | +import { ServerStopHookError } from "@t3tools/contracts"; |
| 2 | +import { assert, it } from "@effect/vitest"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Layer from "effect/Layer"; |
| 5 | +import * as Schema from "effect/Schema"; |
| 6 | +import * as HttpClient from "effect/unstable/http/HttpClient"; |
| 7 | +import * as HttpClientResponse from "effect/unstable/http/HttpClientResponse"; |
| 8 | + |
| 9 | +import * as InstanceHooks from "./instanceHooks.ts"; |
| 10 | +import * as ServerSettings from "./serverSettings.ts"; |
| 11 | + |
| 12 | +interface RecordedHookRequest { |
| 13 | + readonly method: string; |
| 14 | + readonly url: string; |
| 15 | +} |
| 16 | + |
| 17 | +const makeHookEndpointLayer = (requests: Array<RecordedHookRequest>, status: number) => |
| 18 | + Layer.succeed( |
| 19 | + HttpClient.HttpClient, |
| 20 | + HttpClient.make((request) => |
| 21 | + Effect.sync(() => { |
| 22 | + requests.push({ method: request.method, url: request.url }); |
| 23 | + return HttpClientResponse.fromWeb(request, new Response(null, { status })); |
| 24 | + }), |
| 25 | + ), |
| 26 | + ); |
| 27 | + |
| 28 | +const isStopHookError = Schema.is(ServerStopHookError); |
| 29 | + |
| 30 | +it.effect("DELETEs the stop hook and reports the instance as stopping on 204", () => |
| 31 | + Effect.gen(function* () { |
| 32 | + const requests: Array<RecordedHookRequest> = []; |
| 33 | + const result = yield* InstanceHooks.runStopHook.pipe( |
| 34 | + Effect.provide( |
| 35 | + Layer.mergeAll( |
| 36 | + makeHookEndpointLayer(requests, 204), |
| 37 | + ServerSettings.layerTest({ stopHookUrl: "https://mgmt.example.test/instances/1/stop" }), |
| 38 | + ), |
| 39 | + ), |
| 40 | + ); |
| 41 | + assert.deepEqual(result, { outcome: "stopped" }); |
| 42 | + assert.deepEqual(requests, [ |
| 43 | + { method: "DELETE", url: "https://mgmt.example.test/instances/1/stop" }, |
| 44 | + ]); |
| 45 | + }), |
| 46 | +); |
| 47 | + |
| 48 | +it.effect("clears the stop hook setting when the endpoint is gone", () => |
| 49 | + Effect.gen(function* () { |
| 50 | + const requests: Array<RecordedHookRequest> = []; |
| 51 | + const settingsLayer = ServerSettings.layerTest({ |
| 52 | + stopHookUrl: "https://mgmt.example.test/instances/1/stop", |
| 53 | + }); |
| 54 | + const result = yield* Effect.gen(function* () { |
| 55 | + const outcome = yield* InstanceHooks.runStopHook.pipe( |
| 56 | + Effect.provide(makeHookEndpointLayer(requests, 404)), |
| 57 | + ); |
| 58 | + const settings = yield* (yield* ServerSettings.ServerSettingsService).getSettings; |
| 59 | + return { outcome, stopHookUrl: settings.stopHookUrl }; |
| 60 | + }).pipe(Effect.provide(settingsLayer)); |
| 61 | + assert.deepEqual(result.outcome, { outcome: "gone" }); |
| 62 | + assert.equal(result.stopHookUrl, null); |
| 63 | + }), |
| 64 | +); |
| 65 | + |
| 66 | +it.effect("fails when no stop hook is configured", () => |
| 67 | + Effect.gen(function* () { |
| 68 | + const requests: Array<RecordedHookRequest> = []; |
| 69 | + const failure = yield* InstanceHooks.runStopHook.pipe( |
| 70 | + Effect.provide( |
| 71 | + Layer.mergeAll(makeHookEndpointLayer(requests, 204), ServerSettings.layerTest()), |
| 72 | + ), |
| 73 | + Effect.flip, |
| 74 | + ); |
| 75 | + assert.isTrue(isStopHookError(failure)); |
| 76 | + assert.equal(isStopHookError(failure) ? failure.reason : null, "not-configured"); |
| 77 | + assert.deepEqual(requests, []); |
| 78 | + }), |
| 79 | +); |
| 80 | + |
| 81 | +it.effect("surfaces unexpected statuses without clearing the hook", () => |
| 82 | + Effect.gen(function* () { |
| 83 | + const requests: Array<RecordedHookRequest> = []; |
| 84 | + const settingsLayer = ServerSettings.layerTest({ |
| 85 | + stopHookUrl: "https://mgmt.example.test/instances/1/stop", |
| 86 | + }); |
| 87 | + const result = yield* Effect.gen(function* () { |
| 88 | + const failure = yield* InstanceHooks.runStopHook.pipe( |
| 89 | + Effect.provide(makeHookEndpointLayer(requests, 500)), |
| 90 | + Effect.flip, |
| 91 | + ); |
| 92 | + const settings = yield* (yield* ServerSettings.ServerSettingsService).getSettings; |
| 93 | + return { failure, stopHookUrl: settings.stopHookUrl }; |
| 94 | + }).pipe(Effect.provide(settingsLayer)); |
| 95 | + assert.isTrue(isStopHookError(result.failure)); |
| 96 | + assert.equal( |
| 97 | + isStopHookError(result.failure) ? result.failure.reason : null, |
| 98 | + "unexpected-status", |
| 99 | + ); |
| 100 | + assert.equal(result.stopHookUrl, "https://mgmt.example.test/instances/1/stop"); |
| 101 | + }), |
| 102 | +); |
0 commit comments