|
6 | 6 | import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
|
7 | 7 | import { MockWebSocket } from "@lophus/lib/testing";
|
8 | 8 | import { NostrEvent, Relay, SubscriptionId } from "@lophus/nips";
|
9 |
| -import { RelayGroup } from "./relays.ts"; |
| 9 | +import { RelayGroup, WithPool } from "./relays.ts"; |
10 | 10 |
|
11 | 11 | describe("RelayGroup", () => {
|
12 | 12 | let relays: Relay[];
|
@@ -113,3 +113,37 @@ describe("RelayGroup", () => {
|
113 | 113 | });
|
114 | 114 | });
|
115 | 115 | });
|
| 116 | + |
| 117 | +describe("WithPool", () => { |
| 118 | + let Pooled: WithPool<typeof Relay>; |
| 119 | + |
| 120 | + beforeAll(() => { |
| 121 | + globalThis.WebSocket = MockWebSocket; |
| 122 | + }); |
| 123 | + |
| 124 | + it("should accept a NIP-enabled relay as an argument", () => { |
| 125 | + Pooled = WithPool(Relay); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should have no relays in the pool initially", () => { |
| 129 | + assertEquals(Pooled.pool.size, 0); |
| 130 | + }); |
| 131 | + |
| 132 | + it("should add a relay to the pool and return it", () => { |
| 133 | + const relay = new Pooled("ws://localhost:80"); |
| 134 | + assertEquals(Pooled.pool.size, 1); |
| 135 | + assertEquals(Pooled.pool.has(relay.config.url), true); |
| 136 | + }); |
| 137 | + |
| 138 | + it("should return the pooled relay if it exists", () => { |
| 139 | + const relay = new Pooled("ws://localhost:80"); |
| 140 | + assertEquals(Pooled.pool.size, 1); |
| 141 | + assertEquals(Pooled.pool.has(relay.config.url), true); |
| 142 | + }); |
| 143 | + |
| 144 | + it("should remove a relay from the pool when it is closed", async () => { |
| 145 | + const relay = new Pooled("ws://localhost:80"); |
| 146 | + await relay.close(); |
| 147 | + assertEquals(Pooled.pool.size, 0); |
| 148 | + }); |
| 149 | +}); |
0 commit comments