snort/packages/nostr/test/simple-communication.ts

86 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-03-02 22:47:49 +00:00
import { Nostr } from "../src/client"
import { EventKind, SignedEvent } from "../src/event"
2023-03-06 21:47:14 +00:00
import { PrivateKey } from "../src/crypto"
2023-02-25 11:07:01 +00:00
import assert from "assert"
2023-03-02 22:47:49 +00:00
import { EventParams } from "../src/client/emitter"
2023-02-25 11:07:01 +00:00
// TODO Switch out the relay implementation and see if the issue persists
2023-03-02 22:47:49 +00:00
// TODO Do on("error", done) for all of these
2023-02-25 11:07:01 +00:00
2023-03-02 22:47:49 +00:00
describe("simple communication", function () {
const secret = new PrivateKey(
"nsec1xlu55y6fqfgrq448xslt6a8j2rh7lj08hyhgs94ryq04yf6surwsjl0kzh"
)
const pubkey = secret.pubkey
const timestamp = new Date()
const note = "hello world"
const url = new URL("ws://localhost:12648")
2023-02-25 11:07:01 +00:00
2023-03-02 22:47:49 +00:00
const publisher = new Nostr()
const subscriber = new Nostr()
2023-02-25 11:07:01 +00:00
2023-03-02 22:47:49 +00:00
beforeEach(() => {
publisher.open(url)
subscriber.open(url)
})
2023-02-25 11:07:01 +00:00
2023-03-02 22:47:49 +00:00
afterEach(() => {
publisher.close()
subscriber.close()
})
it("publish and receive", function (done) {
function listener({ signed: { event } }: EventParams, nostr: Nostr) {
assert.equal(nostr, subscriber)
2023-02-25 11:07:01 +00:00
assert.equal(event.kind, EventKind.TextNote)
2023-03-06 21:47:14 +00:00
assert.equal(event.pubkey.toHex(), pubkey.toHex())
2023-02-25 11:07:01 +00:00
assert.equal(event.createdAt.toString(), timestamp.toString())
if (event.kind === EventKind.TextNote) {
2023-02-28 20:22:15 +00:00
assert.equal(event.content, note)
2023-02-25 11:07:01 +00:00
}
// There is a bug with the nostr relay used for testing where if the publish and
// subscribe happen at the same time, the same event might end up being broadcast twice.
// To prevent reacting to the same event and calling done() twice, remove the callback
// for future events.
2023-03-02 20:13:57 +00:00
subscriber.off("event", listener)
2023-02-25 11:07:01 +00:00
done()
2023-03-02 20:13:57 +00:00
}
subscriber.on("event", listener)
2023-02-28 13:38:40 +00:00
subscriber.subscribe([])
2023-02-25 11:07:01 +00:00
publisher.publish(
{
kind: EventKind.TextNote,
createdAt: timestamp,
2023-02-28 20:22:15 +00:00
content: note,
2023-02-25 11:07:01 +00:00
pubkey,
},
secret
)
})
2023-03-02 22:47:49 +00:00
it("publish and ok", function (done) {
SignedEvent.sign(
{
kind: EventKind.TextNote,
createdAt: timestamp,
content: note,
pubkey,
},
secret
).then((event) => {
publisher.on("ok", (params, nostr) => {
assert.equal(nostr, publisher)
2023-03-06 21:47:14 +00:00
assert.equal(params.eventId.toHex(), event.eventId.toHex())
2023-03-02 22:47:49 +00:00
assert.equal(params.relay.toString(), url.toString())
assert.equal(params.ok, true)
done()
})
publisher.on("error", done)
publisher.publish(event)
})
})
2023-02-25 11:07:01 +00:00
})