Skip to content

Commit 2d0a7c4

Browse files
test: harden duplicate command assertion in NIP-04 flow
1 parent 036fe7c commit 2d0a7c4

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

test/integration/features/nip-04/nip-04.feature.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Then, When, World } from '@cucumber/cucumber'
22
import { expect } from 'chai'
3+
import { Observable } from 'rxjs'
34
import WebSocket from 'ws'
45

6+
import { CommandResult, MessageType, OutgoingMessage } from '../../../../src/@types/messages'
57
import { createEvent, createSubscription, sendEvent, waitForEOSE, waitForNextEvent } from '../helpers'
68
import { EventKinds, EventTags } from '../../../../src/constants/base'
7-
import { CommandResult } from '../../../../src/@types/messages'
89
import { Event } from '../../../../src/@types/event'
10+
import { streams } from '../shared'
911

1012
When(/^(\w+) sends an encrypted_direct_message event with content "([^"]+)" to (\w+)$/, async function(
1113
name: string,
@@ -64,13 +66,32 @@ Then(/(\w+) receives an encrypted_direct_message event from (\w+) with content "
6466
When(/^(\w+) resubmits their last event$/, async function(name: string) {
6567
const ws = this.parameters.clients[name] as WebSocket
6668
const event = this.parameters.events[name][this.parameters.events[name].length - 1] as Event
67-
const command = await sendEvent(ws, event) as CommandResult
68-
this.parameters.commands = this.parameters.commands ?? {}
69-
this.parameters.commands[name] = command
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
7076
})
7177

72-
Then(/^(\w+) receives a successful command result with message "([^"]+)"$/, function(name: string, message: string) {
73-
const command = this.parameters.commands[name] as CommandResult
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+
})
7495

7596
expect(command[2]).to.equal(true)
7697
expect(command[3]).to.equal(message)

0 commit comments

Comments
 (0)