From d99a5b1dca9468a52943403b2d1da19868434695 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 8 Feb 2026 00:33:37 +0000 Subject: [PATCH] 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 <53631862+PR0M3TH3AN@users.noreply.github.com> --- tests/nostr/defaultClient.test.mjs | 72 ++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/nostr/defaultClient.test.mjs diff --git a/tests/nostr/defaultClient.test.mjs b/tests/nostr/defaultClient.test.mjs new file mode 100644 index 00000000..ae752adb --- /dev/null +++ b/tests/nostr/defaultClient.test.mjs @@ -0,0 +1,72 @@ +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); + }); +});