Add InternalRelay test

This commit is contained in:
Alex Gleason 2025-02-01 11:59:38 -06:00
parent b7a1efe33c
commit 640e533dca
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 25 additions and 2 deletions

View file

@ -149,7 +149,7 @@ function connectStream(socket: WebSocket, ip: string | undefined) {
send(['EVENT', subId, msg[2]]); send(['EVENT', subId, msg[2]]);
} }
} }
} catch (_e) { } catch {
controllers.delete(subId); controllers.delete(subId);
} }
} }

View file

@ -0,0 +1,23 @@
import { assertEquals } from '@std/assert';
import { eventFixture } from '@/test.ts';
import { InternalRelay } from './InternalRelay.ts';
Deno.test('InternalRelay', async () => {
const relay = new InternalRelay();
const event1 = await eventFixture('event-1');
const promise = new Promise((resolve) => setTimeout(() => resolve(relay.event(event1)), 0));
for await (const msg of relay.req([{}])) {
if (msg[0] === 'EVENT') {
assertEquals(relay.subs.size, 1);
assertEquals(msg[2], event1);
break;
}
}
await promise;
assertEquals(relay.subs.size, 0); // cleanup
});

View file

@ -24,7 +24,7 @@ interface InternalRelayOpts {
* The pipeline should push events to it, then anything in the application can subscribe to it. * The pipeline should push events to it, then anything in the application can subscribe to it.
*/ */
export class InternalRelay implements NRelay { export class InternalRelay implements NRelay {
private subs = new Map<string, { filters: NostrFilter[]; machina: Machina<NostrEvent> }>(); readonly subs = new Map<string, { filters: NostrFilter[]; machina: Machina<NostrEvent> }>();
constructor(private opts: InternalRelayOpts = {}) {} constructor(private opts: InternalRelayOpts = {}) {}