|
| 1 | +import { Then, When, World } from '@cucumber/cucumber' |
| 2 | +import { expect } from 'chai' |
| 3 | +import { Observable } from 'rxjs' |
| 4 | +import WebSocket from 'ws' |
| 5 | + |
| 6 | +import { CommandResult, MessageType, OutgoingMessage } from '../../../../src/@types/messages' |
| 7 | +import { createEvent, createSubscription, sendEvent, waitForEOSE, waitForNextEvent } from '../helpers' |
| 8 | +import { EventKinds, EventTags } from '../../../../src/constants/base' |
| 9 | +import { Event } from '../../../../src/@types/event' |
| 10 | +import { streams } from '../shared' |
| 11 | + |
| 12 | +When(/^(\w+) sends an encrypted_direct_message event with content "([^"]+)" to (\w+)$/, async function( |
| 13 | + name: string, |
| 14 | + content: string, |
| 15 | + recipient: string, |
| 16 | +) { |
| 17 | + const ws = this.parameters.clients[name] as WebSocket |
| 18 | + const { pubkey, privkey } = this.parameters.identities[name] |
| 19 | + const recipientPubkey = this.parameters.identities[recipient].pubkey |
| 20 | + |
| 21 | + const event: Event = await createEvent( |
| 22 | + { |
| 23 | + pubkey, |
| 24 | + kind: EventKinds.ENCRYPTED_DIRECT_MESSAGE, |
| 25 | + content, |
| 26 | + tags: [[EventTags.Pubkey, recipientPubkey]], |
| 27 | + }, |
| 28 | + privkey, |
| 29 | + ) |
| 30 | + |
| 31 | + await sendEvent(ws, event) |
| 32 | + this.parameters.events[name].push(event) |
| 33 | +}) |
| 34 | + |
| 35 | +When(/^(\w+) subscribes to tag p with (\w+) pubkey$/, async function( |
| 36 | + this: World<Record<string, any>>, |
| 37 | + name: string, |
| 38 | + target: string, |
| 39 | +) { |
| 40 | + const ws = this.parameters.clients[name] as WebSocket |
| 41 | + const targetPubkey = this.parameters.identities[target].pubkey |
| 42 | + const subscription = { name: `test-${Math.random()}`, filters: [{ '#p': [targetPubkey] }] } |
| 43 | + this.parameters.subscriptions[name].push(subscription) |
| 44 | + |
| 45 | + await createSubscription(ws, subscription.name, subscription.filters) |
| 46 | + await waitForEOSE(ws, subscription.name) |
| 47 | +}) |
| 48 | + |
| 49 | +Then(/(\w+) receives an encrypted_direct_message event from (\w+) with content "([^"]+?)" tagged for (\w+)/, async function( |
| 50 | + name: string, |
| 51 | + author: string, |
| 52 | + content: string, |
| 53 | + recipient: string, |
| 54 | +) { |
| 55 | + const ws = this.parameters.clients[name] as WebSocket |
| 56 | + const subscription = this.parameters.subscriptions[name][this.parameters.subscriptions[name].length - 1] |
| 57 | + const recipientPubkey = this.parameters.identities[recipient].pubkey |
| 58 | + const receivedEvent = await waitForNextEvent(ws, subscription.name, content) |
| 59 | + |
| 60 | + expect(receivedEvent.kind).to.equal(EventKinds.ENCRYPTED_DIRECT_MESSAGE) |
| 61 | + expect(receivedEvent.pubkey).to.equal(this.parameters.identities[author].pubkey) |
| 62 | + expect(receivedEvent.content).to.equal(content) |
| 63 | + expect(receivedEvent.tags).to.deep.include([EventTags.Pubkey, recipientPubkey]) |
| 64 | +}) |
| 65 | + |
| 66 | +When(/^(\w+) resubmits their last event$/, async function(name: string) { |
| 67 | + const ws = this.parameters.clients[name] as WebSocket |
| 68 | + const event = this.parameters.events[name][this.parameters.events[name].length - 1] as Event |
| 69 | + |
| 70 | + await new Promise<void>((resolve, reject) => { |
| 71 | + ws.send(JSON.stringify(['EVENT', event]), (err?: Error) => err ? reject(err) : resolve()) |
| 72 | + }) |
| 73 | + |
| 74 | + this.parameters.lastResubmittedEventId = this.parameters.lastResubmittedEventId ?? {} |
| 75 | + this.parameters.lastResubmittedEventId[name] = event.id |
| 76 | +}) |
| 77 | + |
| 78 | +Then(/^(\w+) receives a successful command result with message "([^"]+)"$/, async function(name: string, message: string) { |
| 79 | + const ws = this.parameters.clients[name] as WebSocket |
| 80 | + const eventId = this.parameters.lastResubmittedEventId[name] as string |
| 81 | + const observable = streams.get(ws) as Observable<OutgoingMessage> |
| 82 | + const command = await new Promise<CommandResult>((resolve, reject) => { |
| 83 | + observable.subscribe((response: OutgoingMessage) => { |
| 84 | + if ( |
| 85 | + response[0] === MessageType.OK && |
| 86 | + response[1] === eventId && |
| 87 | + response[3] === message |
| 88 | + ) { |
| 89 | + resolve(response) |
| 90 | + } else if (response[0] === MessageType.NOTICE) { |
| 91 | + reject(new Error(response[1])) |
| 92 | + } |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + expect(command[2]).to.equal(true) |
| 97 | + expect(command[3]).to.equal(message) |
| 98 | +}) |
0 commit comments