Skip to content

Commit

Permalink
use mock relays on pool tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Jan 19, 2024
1 parent 8b2b050 commit 3b81e5e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
29 changes: 18 additions & 11 deletions pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import { test, expect, afterAll } from 'bun:test'
import { finalizeEvent, type Event } from './pure.ts'
import { generateSecretKey, getPublicKey } from './pure.ts'
import { SimplePool } from './pool.ts'
import { newMockRelay } from './test-helpers.ts'

let pool = new SimplePool()

let relays = ['wss://relay.damus.io/', 'wss://relay.nostr.bg/', 'wss://nos.lol', 'wss://public.relaying.io']
let mockRelays = [newMockRelay(), newMockRelay(), newMockRelay(), newMockRelay()]
let relays = mockRelays.map(mr => mr.url)
let authors = mockRelays.flatMap(mr => mr.authors)
let ids = mockRelays.flatMap(mr => mr.ids)

afterAll(() => {
pool.close([...relays, 'wss://offchain.pub', 'wss://eden.nostr.land'])
pool.close(relays)
})

test('removing duplicates when subscribing', async () => {
Expand Down Expand Up @@ -80,8 +84,8 @@ test('query a bunch of events and cancel on eose', async () => {
let events = new Set<string>()
await new Promise<void>(resolve => {
pool.subscribeManyEose(
[...relays, 'wss://relayable.org', 'wss://relay.noswhere.com', 'wss://nothing.com'],
[{ kinds: [0, 1], limit: 40 }],
[...relays, ...relays, 'wss://relayable.org', 'wss://relay.noswhere.com', 'wss://nothing.com'],
[{ kinds: [0, 1, 2, 3, 4, 5, 6], limit: 40 }],
{
onevent(event) {
events.add(event.id)
Expand All @@ -94,11 +98,14 @@ test('query a bunch of events and cancel on eose', async () => {
})

test('querySync()', async () => {
let events = await pool.querySync([...relays.slice(2), 'wss://offchain.pub', 'wss://eden.nostr.land'], {
authors: ['3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d'],
kinds: [1],
limit: 2,
})
let events = await pool.querySync(
[...relays.slice(0, 2), ...relays.slice(0, 2), 'wss://offchain.pub', 'wss://eden.nostr.land'],
{
authors: authors.slice(0, 2),
kinds: [1],
limit: 2,
},
)

// the actual received number will be greater than 2, but there will be no duplicates
expect(events.length).toBeGreaterThan(2)
Expand All @@ -108,9 +115,9 @@ test('querySync()', async () => {

test('get()', async () => {
let event = await pool.get(relays, {
ids: ['9fa1c618fcaad6357e074417b07ed132b083ed30e13113ebb10fcda7137442fe'],
ids: [ids[0]],
})

expect(event).not.toBeNull()
expect(event).toHaveProperty('id', '9fa1c618fcaad6357e074417b07ed132b083ed30e13113ebb10fcda7137442fe')
expect(event).toHaveProperty('id', ids[0])
})
22 changes: 19 additions & 3 deletions test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@ export function buildEvent(params: Partial<Event>): Event {

let serial = 0

// the mock relay will always return 3 events before eose and then do ok with everything
export function newMockRelay(): { url: string; authors: string[] } {
// the mock relay will always return some events before eose and then be ok with everything
export function newMockRelay(): { url: string; authors: string[]; ids: string[] } {
serial++
const url = `wss://mock.relay.url/${serial}`
const relay = new Server(url)
const secretKeys = [generateSecretKey(), generateSecretKey(), generateSecretKey(), generateSecretKey()]
const preloadedEvents = secretKeys.map(sk =>
finalizeEvent(
{
kind: 1,
content: '',
created_at: Math.floor(Date.now() / 1000),
tags: [],
},
sk,
),
)

relay.on('connection', (conn: any) => {
let subs: { [subId: string]: { conn: any; filters: Filter[] } } = {}
Expand All @@ -35,6 +46,11 @@ export function newMockRelay(): { url: string; authors: string[] } {
let subId = data[1]
let filters = data.slice(2)
subs[subId] = { conn, filters }

preloadedEvents.forEach(event => {
conn.send(JSON.stringify(['EVENT', subId, event]))
})

filters.forEach((filter: Filter) => {
const kinds = filter.kinds?.length ? filter.kinds : [1]
kinds.forEach(kind => {
Expand Down Expand Up @@ -75,5 +91,5 @@ export function newMockRelay(): { url: string; authors: string[] } {
})
})

return { url, authors: secretKeys.map(getPublicKey) }
return { url, authors: secretKeys.map(getPublicKey), ids: preloadedEvents.map(evt => evt.id) }
}

0 comments on commit 3b81e5e

Please sign in to comment.