t3-code-android-nightly/.repos/alchemy-effect/packages/alchemy/test/Cloudflare/Utils/Http.test.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

101 lines
3.5 KiB
TypeScript

// Pins expectUrlContains's time bounds against hostile servers: the total
// timeout must fire even when the underlying fetch never settles. A wedged
// bun fetch (keep-alive reuse against a closed connection) once pinned the
// timeout's interruption and turned a 300s assertion bound into a
// suite-timeout hang — these three shapes keep that class of bug dead.
import { expect, test } from "alchemy-test";
import * as Effect from "effect/Effect";
import * as Result from "effect/Result";
import { createServer } from "node:http";
import { expectUrlContains } from "./Http.ts";
const timed = <A, E>(eff: Effect.Effect<A, E>) =>
Effect.gen(function* () {
const start = Date.now();
const result = yield* Effect.result(eff);
return { ms: Date.now() - start, ok: Result.isSuccess(result) };
});
test(
"total timeout fires against a connect-blackhole",
async () =>
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const { ms, ok } = yield* timed(
expectUrlContains("https://192.0.2.1/", "nope", {
timeout: "250 millis",
label: "blackhole",
}),
);
expect(ok).toBe(false);
expect(ms).toBeLessThan(3_000);
}),
),
),
{ tags: ["unit", "provider:cloudflare", "local"], timeout: 5_000 },
);
test(
"total timeout fires against an accept-but-never-respond server",
async () =>
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const server = yield* Effect.acquireRelease(
Effect.callback<ReturnType<typeof createServer>>((resume) => {
const s = createServer(() => {
/* accept request, never respond */
});
s.listen(0, "127.0.0.1", () => resume(Effect.succeed(s)));
}),
(s) => Effect.sync(() => void s.close()),
);
const port = (server.address() as { port: number }).port;
const { ms, ok } = yield* timed(
expectUrlContains(`http://127.0.0.1:${port}/`, "nope", {
timeout: "250 millis",
label: "black-hole-response",
}),
);
expect(ok).toBe(false);
expect(ms).toBeLessThan(3_000);
}),
),
),
{ tags: ["unit", "provider:cloudflare", "local"], timeout: 5_000 },
);
test(
"total timeout fires against a fast keep-alive 502 server",
async () =>
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const server = yield* Effect.acquireRelease(
Effect.callback<ReturnType<typeof createServer>>((resume) => {
const s = createServer((_req, res) => {
res.writeHead(502, {
"content-type": "text/html",
connection: "keep-alive",
});
res.end("<html>Bad gateway</html>");
});
s.listen(0, "127.0.0.1", () => resume(Effect.succeed(s)));
}),
(s) => Effect.sync(() => void s.close()),
);
const port = (server.address() as { port: number }).port;
const { ms, ok } = yield* timed(
expectUrlContains(`http://127.0.0.1:${port}/`, "nope", {
timeout: "250 millis",
label: "fast-502",
}),
);
expect(ok).toBe(false);
expect(ms).toBeLessThan(3_000);
}),
),
),
{ tags: ["unit", "provider:cloudflare", "local"], timeout: 5_000 },
);