Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions packages/stack/src/services/Mail.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NodeHttpClient, NodeServices } from "@effect/platform-node";
import { describe, expect, it } from "@effect/vitest";
import { Effect, Exit, FileSystem, Layer, Path } from "effect";
import { Data, Effect, Exit, FileSystem, Layer, Path, Ref, Stream } from "effect";
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
import { makeStandaloneService } from "../../tests/standalone-service.ts";
import { makeServiceRecipe } from "./Catalog.ts";
Expand Down Expand Up @@ -59,6 +59,28 @@ const sendTestEmail = (
yield* HttpClientResponse.filterStatusOk(response);
});

class MailStallError extends Data.TaggedError("MailStallError")<{ readonly message: string }> {}

/** Fails a stalled launch within the test timeout, naming its current phase and Mailpit's output. */
const guardLaunch =
(duration: `${number} seconds`, phase: Ref.Ref<string>, logs: Ref.Ref<string>) =>
<A, E, R>(effect: Effect.Effect<A, E, R>) =>
effect.pipe(
Effect.timeoutOrElse({
duration,
orElse: () =>
Effect.all([Ref.get(phase), Ref.get(logs)]).pipe(
Effect.flatMap(([stalled, output]) =>
Effect.fail(
new MailStallError({
message: `Mailpit ${stalled} did not finish within ${duration}; output:\n${output || "(none)"}`,
}),
),
),
),
}),
);

describe("service catalog", () => {
it.live("launches the real Mailpit recipe and serves its HTTP endpoint", () =>
Effect.scoped(
Expand All @@ -70,16 +92,28 @@ describe("service catalog", () => {
{ service: "mail", pin: selection.mail, config: {} },
dockerOptions(root),
);
const logs = yield* Ref.make("");
yield* Stream.fromSubscription(yield* recipe.logs).pipe(
Stream.runForEach(({ bytes }) =>
Ref.update(logs, (text) => text + new TextDecoder().decode(bytes)),
Comment thread
avallete marked this conversation as resolved.
),
Effect.forkScoped({ startImmediately: true }),
);
const instance = yield* makeStandaloneService(recipe.definition, {
id: "mail",
config: recipe.creation,
});
yield* instance.start;
yield* instance.ready;
const endpoint = yield* recipe.endpoint("http");
const response = yield* client.execute(
HttpClientRequest.get(`http://${httpHost(endpoint)}:${endpoint.port}/readyz`),
);
const phase = yield* Ref.make("start");
const response = yield* Effect.gen(function* () {
yield* instance.start;
yield* Ref.set(phase, "readiness");
yield* instance.ready;
yield* Ref.set(phase, "/readyz request");
const endpoint = yield* recipe.endpoint("http");
return yield* client.execute(
HttpClientRequest.get(`http://${httpHost(endpoint)}:${endpoint.port}/readyz`),
);
}).pipe(guardLaunch("25 seconds", phase, logs));
expect(response.status).toBe(200);
yield* instance.stop;
}),
Expand Down
Loading