|
| 1 | +import { Then, When } from '@cucumber/cucumber' |
| 2 | +import { expect } from 'chai' |
| 3 | +import WebSocket from 'ws' |
| 4 | + |
| 5 | +import { createEvent, sendEvent, waitForEventCount } from '../helpers' |
| 6 | +import { ALL_RELAYS, EventKinds, EventTags } from '../../../../src/constants/base' |
| 7 | +import { Event } from '../../../../src/@types/event' |
| 8 | +import { isDraft } from '../shared' |
| 9 | + |
| 10 | +When(/^(\w+) sends a request_to_vanish event$/, async function (name: string) { |
| 11 | + const ws = this.parameters.clients[name] as WebSocket |
| 12 | + const { pubkey, privkey } = this.parameters.identities[name] |
| 13 | + |
| 14 | + const event: Event = await createEvent( |
| 15 | + { pubkey, kind: EventKinds.REQUEST_TO_VANISH, content: '', tags: [[EventTags.Relay, ALL_RELAYS]] }, |
| 16 | + privkey, |
| 17 | + ) |
| 18 | + |
| 19 | + await sendEvent(ws, event) |
| 20 | + this.parameters.events[name].push(event) |
| 21 | +}) |
| 22 | + |
| 23 | +Then( |
| 24 | + /(\w+) receives (\d+) request_to_vanish events? from (\w+) and EOSE$/, |
| 25 | + async function (name: string, count: string, author: string) { |
| 26 | + const ws = this.parameters.clients[name] as WebSocket |
| 27 | + const subscription = this.parameters.subscriptions[name][this.parameters.subscriptions[name].length - 1] |
| 28 | + const expectedCount = Number(count) |
| 29 | + const expectedPubkey = this.parameters.identities[author].pubkey |
| 30 | + const events = await waitForEventCount(ws, subscription.name, expectedCount, true) |
| 31 | + |
| 32 | + expect(events.length).to.equal(expectedCount) |
| 33 | + for (const event of events) { |
| 34 | + expect(event.kind).to.equal(EventKinds.REQUEST_TO_VANISH) |
| 35 | + expect(event.pubkey).to.equal(expectedPubkey) |
| 36 | + } |
| 37 | + }, |
| 38 | +) |
| 39 | + |
| 40 | +Then( |
| 41 | + /^(\w+) sends their last draft event unsuccessfully because "([^"]+)"$/, |
| 42 | + async function (name: string, reason: string) { |
| 43 | + const ws = this.parameters.clients[name] as WebSocket |
| 44 | + const event = this.parameters.events[name].findLast((event: Event) => event[isDraft]) |
| 45 | + |
| 46 | + if (!event) { |
| 47 | + throw new Error(`No draft event found for ${name}`) |
| 48 | + } |
| 49 | + |
| 50 | + delete event[isDraft] |
| 51 | + |
| 52 | + const command = await sendEvent(ws, event, false) |
| 53 | + expect(command[1]).to.equal(event.id) |
| 54 | + expect(command[2]).to.be.false |
| 55 | + expect(command[3]).to.equal(reason) |
| 56 | + }, |
| 57 | +) |
0 commit comments