Initial Commit
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import { describe, expect, it } from "@suaveplan/testing/runner";
|
||||
|
||||
describe("react-network-libp2p-demo dep-graph", () => {
|
||||
it("loads index and exposes every public surface", async () => {
|
||||
const mod = await import("../src/index.js");
|
||||
// Errors
|
||||
expect(mod.REACT_LIBP2P_DEMO_CODES).toBeDefined();
|
||||
expect(mod.ReactLibp2pDemoError).toBeDefined();
|
||||
// Hook + schemas + helpers
|
||||
expect(mod.useLibp2pDemo).toBeDefined();
|
||||
expect(mod.UseLibp2pDemoOptionsSchema).toBeDefined();
|
||||
expect(mod.wirePeerEvents).toBeDefined();
|
||||
// Components
|
||||
expect(mod.Libp2pDemo).toBeDefined();
|
||||
expect(mod.LogPanel).toBeDefined();
|
||||
expect(mod.NodeControls).toBeDefined();
|
||||
expect(mod.NodeStatusPanel).toBeDefined();
|
||||
expect(mod.PeerInteractionPanel).toBeDefined();
|
||||
expect(mod.PeerList).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,126 @@
|
||||
import type {
|
||||
DemoPeerEvent,
|
||||
DemoSubscription,
|
||||
Libp2pDemoNode,
|
||||
Libp2pDemoNodeConfig,
|
||||
Libp2pDemoNodeFactory,
|
||||
} from "../../src/hooks/use-libp2p-demo/use-libp2p-demo.types.js";
|
||||
|
||||
/**
|
||||
* Deterministic fake `Libp2pDemoNode` used across the unit test suite.
|
||||
* No mocks, no spies — just a real implementation of the structural
|
||||
* contract that records calls and exposes typed event emitters.
|
||||
*/
|
||||
export interface FakeNodeOptions {
|
||||
readonly peerId?: string;
|
||||
readonly listenAddresses?: readonly string[];
|
||||
readonly dialResult?: string;
|
||||
readonly pingResult?: number;
|
||||
readonly echoResult?: string;
|
||||
readonly throwOnStart?: Error;
|
||||
readonly throwOnStop?: Error;
|
||||
readonly throwOnDial?: Error;
|
||||
readonly throwOnPing?: Error;
|
||||
readonly throwOnEcho?: Error;
|
||||
}
|
||||
|
||||
export interface FakeNode extends Libp2pDemoNode {
|
||||
readonly emitDiscovery: (evt: DemoPeerEvent) => void;
|
||||
readonly emitConnect: (evt: DemoPeerEvent) => void;
|
||||
readonly emitDisconnect: (evt: DemoPeerEvent) => void;
|
||||
readonly startedCount: () => number;
|
||||
readonly stoppedCount: () => number;
|
||||
readonly lastConfig: () => Libp2pDemoNodeConfig | null;
|
||||
}
|
||||
|
||||
export function createFakeNode(options: FakeNodeOptions = {}): FakeNode {
|
||||
const discoveryHandlers = new Set<(evt: DemoPeerEvent) => void>();
|
||||
const connectHandlers = new Set<(evt: DemoPeerEvent) => void>();
|
||||
const disconnectHandlers = new Set<(evt: DemoPeerEvent) => void>();
|
||||
let starts = 0;
|
||||
let stops = 0;
|
||||
let lastConfig: Libp2pDemoNodeConfig | null = null;
|
||||
|
||||
const sub =
|
||||
(set: Set<(evt: DemoPeerEvent) => void>) =>
|
||||
(handler: (evt: DemoPeerEvent) => void): DemoSubscription => {
|
||||
set.add(handler);
|
||||
return {
|
||||
unsubscribe: () => {
|
||||
set.delete(handler);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const node: FakeNode = {
|
||||
peerId: options.peerId ?? "12D3KooWFakeNode",
|
||||
listenAddresses: options.listenAddresses ?? ["/ip4/127.0.0.1/tcp/0"],
|
||||
start: async () => {
|
||||
starts += 1;
|
||||
if (options.throwOnStart) throw options.throwOnStart;
|
||||
},
|
||||
stop: async () => {
|
||||
stops += 1;
|
||||
if (options.throwOnStop) throw options.throwOnStop;
|
||||
},
|
||||
dial: async (_addr, opts) => {
|
||||
if (opts?.signal?.aborted)
|
||||
throw opts.signal.reason ?? new Error("aborted");
|
||||
if (options.throwOnDial) throw options.throwOnDial;
|
||||
return options.dialResult ?? "12D3KooWFakeRemote";
|
||||
},
|
||||
ping: async (_target, opts) => {
|
||||
if (opts?.signal?.aborted)
|
||||
throw opts.signal.reason ?? new Error("aborted");
|
||||
if (options.throwOnPing) throw options.throwOnPing;
|
||||
return options.pingResult ?? 42;
|
||||
},
|
||||
echo: async (_target, message, opts) => {
|
||||
if (opts?.signal?.aborted)
|
||||
throw opts.signal.reason ?? new Error("aborted");
|
||||
if (options.throwOnEcho) throw options.throwOnEcho;
|
||||
return options.echoResult ?? message;
|
||||
},
|
||||
onPeerDiscovery: sub(discoveryHandlers),
|
||||
onPeerConnect: sub(connectHandlers),
|
||||
onPeerDisconnect: sub(disconnectHandlers),
|
||||
emitDiscovery: (evt) => {
|
||||
for (const h of discoveryHandlers) h(evt);
|
||||
},
|
||||
emitConnect: (evt) => {
|
||||
for (const h of connectHandlers) h(evt);
|
||||
},
|
||||
emitDisconnect: (evt) => {
|
||||
for (const h of disconnectHandlers) h(evt);
|
||||
},
|
||||
startedCount: () => starts,
|
||||
stoppedCount: () => stops,
|
||||
lastConfig: () => lastConfig,
|
||||
};
|
||||
|
||||
// Expose a setter via a separate factory so the holder for lastConfig
|
||||
// is captured by the factory closure below.
|
||||
(
|
||||
node as { _setLastConfig?: (c: Libp2pDemoNodeConfig) => void }
|
||||
)._setLastConfig = (c: Libp2pDemoNodeConfig) => {
|
||||
lastConfig = c;
|
||||
};
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
export function fakeNodeFactory(
|
||||
nodeOrOptions: FakeNode | FakeNodeOptions = {}
|
||||
): Libp2pDemoNodeFactory {
|
||||
return async (config) => {
|
||||
const node: FakeNode =
|
||||
"emitDiscovery" in (nodeOrOptions as object)
|
||||
? (nodeOrOptions as FakeNode)
|
||||
: createFakeNode(nodeOrOptions as FakeNodeOptions);
|
||||
const setter = (
|
||||
node as { _setLastConfig?: (c: Libp2pDemoNodeConfig) => void }
|
||||
)._setLastConfig;
|
||||
setter?.(config);
|
||||
return node;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// React-specific polyfills. The base webcrypto polyfill loads via
|
||||
// `@suaveplan/testing/polyfills`. This sets the React 18+
|
||||
// `IS_REACT_ACT_ENVIRONMENT` flag so DOM assertions outside `act(...)`
|
||||
// don't generate runtime warnings.
|
||||
interface ReactActEnvWindow {
|
||||
IS_REACT_ACT_ENVIRONMENT?: boolean;
|
||||
}
|
||||
const win = globalThis as unknown as ReactActEnvWindow;
|
||||
if (win.IS_REACT_ACT_ENVIRONMENT === undefined) {
|
||||
win.IS_REACT_ACT_ENVIRONMENT = true;
|
||||
}
|
||||
Reference in New Issue
Block a user