Skip to content

Commit

Permalink
unify mock-socket interface into a single implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Jan 19, 2024
1 parent d4090da commit 8b2b050
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 69 deletions.
Binary file modified bun.lockb
Binary file not shown.
78 changes: 11 additions & 67 deletions relay.test.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,44 @@
import { expect, test } from 'bun:test'
import { Server } from 'mock-socket'

import { finalizeEvent, generateSecretKey, getPublicKey } from './pure.ts'
import { Relay } from './relay.ts'
import { newMockRelay } from './test-helpers.ts'

test('connectivity', async () => {
const mockRelayURL = 'wss://mock.relay.url'
const mockRelay = new Server(mockRelayURL)

const relay = new Relay(mockRelayURL)
const { url } = newMockRelay()
const relay = new Relay(url)
await relay.connect()

expect(relay.connected).toBeTrue()

relay.close()
mockRelay.stop()
})

test('connectivity, with Relay.connect()', async () => {
const mockRelayURL = 'wss://mock.relay.url'
const mockRelay = new Server(mockRelayURL)

const relay = await Relay.connect(mockRelayURL)

const { url } = newMockRelay()
const relay = await Relay.connect(url)
expect(relay.connected).toBeTrue()

relay.close()
mockRelay.stop()
})

test('querying', async done => {
const sk = generateSecretKey()
const pk = getPublicKey(sk)
const { url, authors } = newMockRelay()
const kind = 0

const mockRelayURL = 'wss://mock.relay.url'
const mockRelay = new Server(mockRelayURL)

mockRelay.on('connection', socket => {
socket.on('message', message => {
const data = JSON.parse(message as string)

const event = finalizeEvent(
{
kind,
content: '',
created_at: Math.floor(Date.now() / 1000),
tags: [],
},
sk,
)

socket.send(JSON.stringify(['EVENT', data[1], event]))
})
})

const relay = new Relay(mockRelayURL)
const relay = new Relay(url)
await relay.connect()

relay.subscribe(
[
{
authors: [pk],
authors: authors,
kinds: [kind],
},
],
{
onevent(event) {
expect(event).toHaveProperty('pubkey', pk)
expect(authors).toContain(event.pubkey)
expect(event).toHaveProperty('kind', kind)

relay.close()
mockRelay.stop()

done()
},
},
Expand All @@ -84,27 +50,8 @@ test('listening and publishing and closing', async done => {
const pk = getPublicKey(sk)
const kind = 23571

const mockRelayURL = 'wss://mock.relay.url'
const mockRelay = new Server(mockRelayURL)

mockRelay.on('connection', socket => {
let subId: string | null = null

socket.on('message', message => {
const data = JSON.parse(message as string)

if (data[0] === 'REQ') {
subId = data[1]
socket.send(JSON.stringify(['EOSE', data[1]]))
} else if (data[0] === 'EVENT') {
socket.send(JSON.stringify(['OK', data[1].id, 'true']))

socket.send(JSON.stringify(['EVENT', subId, data[1]]))
}
})
})

const relay = new Relay(mockRelayURL)
const { url } = newMockRelay()
const relay = new Relay(url)
await relay.connect()

let sub = relay.subscribe(
Expand All @@ -119,14 +66,11 @@ test('listening and publishing and closing', async done => {
expect(event).toHaveProperty('pubkey', pk)
expect(event).toHaveProperty('kind', kind)
expect(event).toHaveProperty('content', 'content')

sub.close()
},
oneose() {},
onclose() {
relay.close()
mockRelay.stop()

done()
},
},
Expand Down
68 changes: 66 additions & 2 deletions test-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { Event } from './pure.ts'
import { Server } from 'mock-socket'

import { finalizeEvent, type Event, getPublicKey, generateSecretKey } from './pure.ts'
import { matchFilters, type Filter } from './filter.ts'

/** Build an event for testing purposes. */
export function buildEvent(params: Partial<Event>): Event {
return {
id: '',
Expand All @@ -13,3 +15,65 @@ export function buildEvent(params: Partial<Event>): Event {
...params,
}
}

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[] } {
serial++
const url = `wss://mock.relay.url/${serial}`
const relay = new Server(url)
const secretKeys = [generateSecretKey(), generateSecretKey(), generateSecretKey(), generateSecretKey()]

relay.on('connection', (conn: any) => {
let subs: { [subId: string]: { conn: any; filters: Filter[] } } = {}

conn.on('message', (message: string) => {
const data = JSON.parse(message)
switch (data[0]) {
case 'REQ': {
let subId = data[1]
let filters = data.slice(2)
subs[subId] = { conn, filters }
filters.forEach((filter: Filter) => {
const kinds = filter.kinds?.length ? filter.kinds : [1]
kinds.forEach(kind => {
secretKeys.forEach(sk => {
const event = finalizeEvent(
{
kind,
content: '',
created_at: Math.floor(Date.now() / 1000),
tags: [],
},
sk,
)
conn.send(JSON.stringify(['EVENT', subId, event]))
})
})
})
conn.send(JSON.stringify(['EOSE', subId]))
break
}
case 'CLOSE': {
let subId = data[1]
delete subs[subId]
break
}
case 'EVENT': {
let event = data[1]
conn.send(JSON.stringify(['OK', event.id, 'true']))
for (let subId in subs) {
const { filters, conn: listener } = subs[subId]
if (matchFilters(filters, event)) {
listener.send(JSON.stringify(['EVENT', subId, event]))
}
}
break
}
}
})
})

return { url, authors: secretKeys.map(getPublicKey) }
}

0 comments on commit 8b2b050

Please sign in to comment.