73 lines
2.4 KiB
TypeScript
73 lines
2.4 KiB
TypeScript
/**
|
|
* Tests for the `defaultFetch` fallback path: when no `fetchFn` is
|
|
* passed and `globalThis.fetch` is missing the call rejects with
|
|
* `NOT_CONNECTED`. These tests sit in their own file so the
|
|
* globalThis.fetch monkey-patching cannot leak into the
|
|
* loop-oriented suites.
|
|
*/
|
|
|
|
import { describe, expect, it } from "@suaveplan/testing/runner";
|
|
import { HttpsTransport, HttpsTransportError } from "../src/index.js";
|
|
import { makeEnvelope } from "./_helpers.js";
|
|
|
|
const RELAY = "https://relay.test";
|
|
|
|
describe("HttpsTransport — defaultFetch fallback", () => {
|
|
it("send rejects with HttpsTransportError when globalThis.fetch is missing", {
|
|
timeout: 5000,
|
|
}, async () => {
|
|
const orig = globalThis.fetch;
|
|
// @ts-expect-error — deliberately remove
|
|
globalThis.fetch = undefined;
|
|
try {
|
|
const t = new HttpsTransport({
|
|
relayUrl: RELAY,
|
|
ourPeerId: "peer-a",
|
|
backoffBaseMs: 1,
|
|
backoffMaxMs: 5,
|
|
longPollTimeoutMs: 10,
|
|
pollIdleDelayMs: 0,
|
|
});
|
|
t.start();
|
|
await t.connect("peer-b", "x");
|
|
await expect(
|
|
t.send("peer-b", makeEnvelope())
|
|
).rejects.toBeInstanceOf(HttpsTransportError);
|
|
await t.stop();
|
|
} finally {
|
|
globalThis.fetch = orig;
|
|
}
|
|
});
|
|
|
|
it("uses globalThis.fetch when no fetchFn is provided", {
|
|
timeout: 5000,
|
|
}, async () => {
|
|
const orig = globalThis.fetch;
|
|
let called = 0;
|
|
const stub = async (_url: string | URL): Promise<Response> => {
|
|
called++;
|
|
await Promise.resolve();
|
|
return new Response(JSON.stringify({ ok: true }), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
};
|
|
globalThis.fetch = stub as typeof fetch;
|
|
try {
|
|
const t = new HttpsTransport({
|
|
relayUrl: RELAY,
|
|
ourPeerId: "peer-a",
|
|
longPollTimeoutMs: 10,
|
|
pollIdleDelayMs: 0,
|
|
});
|
|
t.start();
|
|
await t.connect("peer-b", "x");
|
|
await t.send("peer-b", makeEnvelope());
|
|
await t.stop();
|
|
expect(called).toBeGreaterThan(0);
|
|
} finally {
|
|
globalThis.fetch = orig;
|
|
}
|
|
});
|
|
});
|