Files
bitvid/tests/nostr/defaultClient.test.mjs
T
google-labs-jules[bot]andPR0M3TH3AN d99a5b1dca test: verify default client initialization and configuration
Add `tests/nostr/defaultClient.test.mjs` to ensure that `nostrClient` is correctly exported as a singleton, initialized with `DEFAULT_RELAY_URLS`, and registered in the client registry.
This test handles global mocks (`WebSocket`, `localStorage`) via dynamic imports to verify the module's top-level execution.

Co-authored-by: PR0M3TH3AN <[email protected]>
2026-02-08 00:33:37 +00:00

73 lines
2.2 KiB
JavaScript

import { describe, it, after } from "node:test";
import assert from "node:assert/strict";
import "../test-setup.mjs";
// Mock dependencies before importing the module under test
if (!globalThis.WebSocket) {
globalThis.WebSocket = class MockWebSocket {
send() {}
close() {}
};
}
if (!globalThis.localStorage) {
globalThis.localStorage = {
getItem: () => null,
setItem: () => {},
removeItem: () => {},
clear: () => {},
};
}
// Set dev mode override
globalThis.__BITVID_DEV_MODE_OVERRIDE__ = true;
// Mock console to suppress logs during tests
const originalConsole = { ...console };
globalThis.console = {
...console,
log: () => {},
info: () => {},
warn: () => {},
error: () => {},
debug: () => {},
};
// Import modules dynamically after setting up mocks
const { nostrClient } = await import("../../js/nostr/defaultClient.js");
const { DEFAULT_RELAY_URLS } = await import("../../js/nostr/toolkit.js");
const { getRegisteredNostrClient } = await import("../../js/nostrClientRegistry.js");
const { NostrClient } = await import("../../js/nostr/client.js");
describe("Default NostrClient", () => {
after(() => {
// Restore console
globalThis.console = originalConsole;
// Clean up globals if desired, though process isolation makes this optional
delete globalThis.__BITVID_DEV_MODE_OVERRIDE__;
});
it("should be an instance of NostrClient", () => {
assert.ok(nostrClient instanceof NostrClient);
});
it("should be configured with default relays", () => {
const expectedRelays = Array.from(DEFAULT_RELAY_URLS);
assert.deepEqual(nostrClient.relays, expectedRelays);
});
it("should be initialized with default read relays", () => {
const expectedRelays = Array.from(DEFAULT_RELAY_URLS);
assert.deepEqual(nostrClient.readRelays, expectedRelays);
});
it("should be initialized with default write relays", () => {
const expectedRelays = Array.from(DEFAULT_RELAY_URLS);
assert.deepEqual(nostrClient.writeRelays, expectedRelays);
});
it("should be registered as the default client", () => {
const registered = getRegisteredNostrClient();
assert.strictEqual(registered, nostrClient);
});
});