|
1 | 1 | import { Then, When, World } from '@cucumber/cucumber' |
2 | 2 | import { expect } from 'chai' |
| 3 | +import { Observable } from 'rxjs' |
3 | 4 | import WebSocket from 'ws' |
4 | 5 |
|
| 6 | +import { CommandResult, MessageType, OutgoingMessage } from '../../../../src/@types/messages' |
5 | 7 | import { createEvent, createSubscription, sendEvent, waitForEOSE, waitForNextEvent } from '../helpers' |
6 | 8 | import { EventKinds, EventTags } from '../../../../src/constants/base' |
7 | | -import { CommandResult } from '../../../../src/@types/messages' |
8 | 9 | import { Event } from '../../../../src/@types/event' |
| 10 | +import { streams } from '../shared' |
9 | 11 |
|
10 | 12 | When(/^(\w+) sends an encrypted_direct_message event with content "([^"]+)" to (\w+)$/, async function( |
11 | 13 | name: string, |
@@ -64,13 +66,32 @@ Then(/(\w+) receives an encrypted_direct_message event from (\w+) with content " |
64 | 66 | When(/^(\w+) resubmits their last event$/, async function(name: string) { |
65 | 67 | const ws = this.parameters.clients[name] as WebSocket |
66 | 68 | 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 |
70 | 76 | }) |
71 | 77 |
|
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 | + }) |
74 | 95 |
|
75 | 96 | expect(command[2]).to.equal(true) |
76 | 97 | expect(command[3]).to.equal(message) |
|
0 commit comments