import { describe, expect, it } from "@suaveplan/testing/runner"; import { Libp2pServer } from "../src/server/server.js"; import { FakeLibp2pHub, FakeLibp2pServerNode, makeFakeServerFactory, } from "./setup/fake-libp2p-server.js"; async function fetchJson( url: string ): Promise<{ readonly status: number; readonly body: unknown }> { const res = await fetch(url); const body = await res.json(); return { status: res.status, body }; } describe("integration: real HTTP boot + health probes", () => { it("health/ready/live respond with 200 while running", { timeout: 10_000, }, async () => { const hub = new FakeLibp2pHub(); const { factory } = makeFakeServerFactory(hub); const server = new Libp2pServer({ libp2pFactory: factory, listen: ["/ip4/0.0.0.0/tcp/0"], healthPort: 0, healthHost: "127.0.0.1", }); const { healthAddress, healthPort } = await server.start(); const base = `http://${ healthAddress === "::" || healthAddress === "0.0.0.0" ? "127.0.0.1" : healthAddress }:${healthPort}`; try { const health = await fetchJson(`${base}/health`); expect(health.status).toBe(200); expect((health.body as { status: string }).status).toBe("ok"); const ready = await fetchJson(`${base}/ready`); expect(ready.status).toBe(200); const live = await fetchJson(`${base}/live`); expect(live.status).toBe(200); // Non-health route falls through to the plaintext marker. const fall = await fetch(`${base}/something-else`); expect(fall.status).toBe(200); expect(await fall.text()).toContain("libp2p"); } finally { await server.stop(); } }); it("a client dial is recorded as peer-connected end-to-end", { timeout: 10_000, }, async () => { const hub = new FakeLibp2pHub(); const { factory, instances } = makeFakeServerFactory(hub); const server = new Libp2pServer({ libp2pFactory: factory, listen: ["/ip4/0.0.0.0/tcp/0"], healthPort: 0, healthHost: "127.0.0.1", }); await server.start(); try { const serverNode = instances[0]; if (!(serverNode instanceof FakeLibp2pServerNode)) { throw new Error("expected fake node"); } // A second fake node in the same hub simulates a browser-tier // client dialling the server. The hub wires both ends' peer // events deterministically. const client = new FakeLibp2pServerNode( hub, { listen: ["/ip4/0.0.0.0/tcp/0"], bootstrap: [], autoRelay: { enabled: false, maxConnections: 100 }, maxConnections: 100, protocolId: "/suaveplan/network-server/1.0.0", }, "client-1" ); hub.connectPair(serverNode, client); expect(server.peers()).toContain("client-1"); expect(server.activePeers).toBe(1); } finally { await server.stop(); } }); it("server relays a stream observation across two clients", { timeout: 10_000, }, async () => { const hub = new FakeLibp2pHub(); const { factory, instances } = makeFakeServerFactory(hub); const server = new Libp2pServer({ libp2pFactory: factory, listen: ["/ip4/0.0.0.0/tcp/0"], healthPort: 0, healthHost: "127.0.0.1", autoRelay: { enabled: true, maxConnections: 50 }, }); await server.start(); try { const serverNode = instances[0]; if (!(serverNode instanceof FakeLibp2pServerNode)) { throw new Error("expected fake node"); } // Two clients sit behind NAT; both connect to the server. const a = new FakeLibp2pServerNode( hub, { listen: ["/ip4/0.0.0.0/tcp/0"], bootstrap: [], autoRelay: { enabled: false, maxConnections: 100 }, maxConnections: 100, protocolId: "/suaveplan/network-server/1.0.0", }, "client-A" ); const b = new FakeLibp2pServerNode( hub, { listen: ["/ip4/0.0.0.0/tcp/0"], bootstrap: [], autoRelay: { enabled: false, maxConnections: 100 }, maxConnections: 100, protocolId: "/suaveplan/network-server/1.0.0", }, "client-B" ); hub.connectPair(serverNode, a); hub.connectPair(serverNode, b); expect(server.peers()).toContain("client-A"); expect(server.peers()).toContain("client-B"); // Daemon observes a relayed stream between the two clients. server.observeRelayedStream("client-A->client-B"); } finally { await server.stop(); } }); it("graceful stop closes the inline health listener", { timeout: 10_000, }, async () => { const hub = new FakeLibp2pHub(); const { factory } = makeFakeServerFactory(hub); const server = new Libp2pServer({ libp2pFactory: factory, listen: ["/ip4/0.0.0.0/tcp/0"], healthPort: 0, healthHost: "127.0.0.1", }); const { healthAddress, healthPort } = await server.start(); const base = `http://${ healthAddress === "::" || healthAddress === "0.0.0.0" ? "127.0.0.1" : healthAddress }:${healthPort}`; await server.stop(); await expect(fetch(`${base}/health`)).rejects.toBeDefined(); }); });