|
| 1 | +// Nostr tests cover outbound relay failover behavior. |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { startNostrBus } from "./nostr-bus.js"; |
| 4 | + |
| 5 | +const BAD_RELAY = "wss://bad-relay.example"; |
| 6 | +const GOOD_RELAY = "wss://good-relay.example"; |
| 7 | +const RECIPIENT_PUBKEY = "b".repeat(64); |
| 8 | + |
| 9 | +const mocks = vi.hoisted(() => ({ |
| 10 | + poolPublish: vi.fn(), |
| 11 | + close: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("nostr-tools", () => { |
| 15 | + class MockSimplePool { |
| 16 | + subscribeMany() { |
| 17 | + return { close: vi.fn() }; |
| 18 | + } |
| 19 | + |
| 20 | + publish(relays: string[]) { |
| 21 | + return mocks.poolPublish(relays); |
| 22 | + } |
| 23 | + |
| 24 | + close(relays: string[]) { |
| 25 | + mocks.close(relays); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return { |
| 30 | + SimplePool: MockSimplePool, |
| 31 | + finalizeEvent: vi.fn((event: unknown) => event), |
| 32 | + getPublicKey: vi.fn(() => "a".repeat(64)), |
| 33 | + verifyEvent: vi.fn(() => true), |
| 34 | + nip19: { |
| 35 | + decode: vi.fn(), |
| 36 | + npubEncode: vi.fn(), |
| 37 | + }, |
| 38 | + }; |
| 39 | +}); |
| 40 | + |
| 41 | +vi.mock("nostr-tools/nip04", () => ({ |
| 42 | + decrypt: vi.fn(), |
| 43 | + encrypt: vi.fn(() => "ciphertext"), |
| 44 | +})); |
| 45 | + |
| 46 | +vi.mock("./nostr-state-store.js", () => ({ |
| 47 | + readNostrBusState: vi.fn(async () => null), |
| 48 | + writeNostrBusState: vi.fn(async () => {}), |
| 49 | + computeSinceTimestamp: vi.fn(() => 0), |
| 50 | + readNostrProfileState: vi.fn(async () => null), |
| 51 | + writeNostrProfileState: vi.fn(async () => {}), |
| 52 | +})); |
| 53 | + |
| 54 | +vi.mock("./nostr-profile.js", () => ({ |
| 55 | + publishProfile: vi.fn(), |
| 56 | +})); |
| 57 | + |
| 58 | +describe("Nostr outbound relay failover", () => { |
| 59 | + beforeEach(() => { |
| 60 | + mocks.poolPublish |
| 61 | + .mockReset() |
| 62 | + .mockImplementation((relays: string[]) => [ |
| 63 | + Promise.resolve( |
| 64 | + relays[0] === BAD_RELAY ? "connection failure: connection failed" : "saved", |
| 65 | + ), |
| 66 | + ]); |
| 67 | + mocks.close.mockReset(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("tries the next relay when the first relay cannot connect", async () => { |
| 71 | + const bus = await startNostrBus({ |
| 72 | + privateKey: "1".repeat(64), |
| 73 | + relays: [BAD_RELAY, GOOD_RELAY], |
| 74 | + onMessage: vi.fn(async () => {}), |
| 75 | + onMetric: () => {}, |
| 76 | + }); |
| 77 | + |
| 78 | + await bus.sendDm(RECIPIENT_PUBKEY, "hello"); |
| 79 | + |
| 80 | + expect(mocks.poolPublish.mock.calls.map(([relays]) => relays)).toEqual([ |
| 81 | + [BAD_RELAY], |
| 82 | + [GOOD_RELAY], |
| 83 | + ]); |
| 84 | + bus.close(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("preserves string connection failures when every relay fails", async () => { |
| 88 | + const onError = vi.fn(); |
| 89 | + const bus = await startNostrBus({ |
| 90 | + privateKey: "1".repeat(64), |
| 91 | + relays: [BAD_RELAY], |
| 92 | + onMessage: vi.fn(async () => {}), |
| 93 | + onError, |
| 94 | + onMetric: () => {}, |
| 95 | + }); |
| 96 | + |
| 97 | + await expect(bus.sendDm(RECIPIENT_PUBKEY, "hello")).rejects.toThrow( |
| 98 | + "Failed to publish to any relay: connection failed", |
| 99 | + ); |
| 100 | + expect(onError).toHaveBeenCalledWith(expect.any(Error), `publish to ${BAD_RELAY}`); |
| 101 | + |
| 102 | + bus.close(); |
| 103 | + }); |
| 104 | +}); |
0 commit comments