|
| 1 | +import { After, Before, Given, Then, When } from '@cucumber/cucumber' |
| 2 | +import { assocPath, pipe } from 'ramda' |
| 3 | + |
| 4 | +import { CommandResult, MessageType } from '../../../../src/@types/messages' |
| 5 | +import { createEvent, sendEvent } from '../helpers' |
| 6 | + |
| 7 | +import { Event } from '../../../../src/@types/event' |
| 8 | +import { expect } from 'chai' |
| 9 | +import { isDraft } from '../shared' |
| 10 | +import { SettingsStatic } from '../../../../src/utils/settings' |
| 11 | +import WebSocket from 'ws' |
| 12 | + |
| 13 | +const previousSettingsSnapshot = Symbol('nip22PreviousSettingsSnapshot') |
| 14 | +const draftOffsetSeconds = Symbol('nip22DraftOffsetSeconds') |
| 15 | + |
| 16 | +const setCreatedAtLimits = (maxPositiveDelta: number, maxNegativeDelta: number) => { |
| 17 | + const settings = SettingsStatic._settings ?? SettingsStatic.createSettings() |
| 18 | + |
| 19 | + SettingsStatic._settings = pipe( |
| 20 | + assocPath(['limits', 'event', 'createdAt', 'maxPositiveDelta'], maxPositiveDelta), |
| 21 | + assocPath(['limits', 'event', 'createdAt', 'maxNegativeDelta'], maxNegativeDelta), |
| 22 | + )(settings) as any |
| 23 | +} |
| 24 | + |
| 25 | +Before({ tags: '@nip-22' }, function(this: any) { |
| 26 | + this[previousSettingsSnapshot] = SettingsStatic._settings |
| 27 | +}) |
| 28 | + |
| 29 | +After({ tags: '@nip-22' }, function(this: any) { |
| 30 | + SettingsStatic._settings = this[previousSettingsSnapshot] |
| 31 | + delete this[previousSettingsSnapshot] |
| 32 | +}) |
| 33 | + |
| 34 | +Given(/^created_at limits are set to maxPositiveDelta (\d+) and maxNegativeDelta (\d+)$/, function( |
| 35 | + maxPositiveDelta: string, |
| 36 | + maxNegativeDelta: string, |
| 37 | +) { |
| 38 | + setCreatedAtLimits(Number(maxPositiveDelta), Number(maxNegativeDelta)) |
| 39 | +}) |
| 40 | + |
| 41 | +When(/^(\w+) drafts a text_note event with content "([^"]+)" and created_at (-?\d+) seconds from now$/, async function( |
| 42 | + name: string, |
| 43 | + content: string, |
| 44 | + offsetSeconds: string, |
| 45 | +) { |
| 46 | + const { pubkey, privkey } = this.parameters.identities[name] |
| 47 | + const createdAt = Math.floor(Date.now() / 1000) + Number(offsetSeconds) |
| 48 | + |
| 49 | + const event: Event = await createEvent( |
| 50 | + { |
| 51 | + pubkey, |
| 52 | + kind: 1, |
| 53 | + content, |
| 54 | + created_at: createdAt, |
| 55 | + }, |
| 56 | + privkey, |
| 57 | + ) |
| 58 | + |
| 59 | + const draftEvent = event as any |
| 60 | + draftEvent[isDraft] = true |
| 61 | + draftEvent[draftOffsetSeconds] = Number(offsetSeconds) |
| 62 | + |
| 63 | + this.parameters.events[name].push(event) |
| 64 | +}) |
| 65 | + |
| 66 | +Then(/^(\w+) sends their last draft event unsuccessfully with reason containing "([^"]+)"$/, async function( |
| 67 | + name: string, |
| 68 | + expectedReason: string, |
| 69 | +) { |
| 70 | + const ws = this.parameters.clients[name] as WebSocket |
| 71 | + |
| 72 | + const event = this.parameters.events[name].findLast((lastEvent: Event) => (lastEvent as any)[isDraft]) |
| 73 | + if (!event) { |
| 74 | + throw new Error(`No draft event found for ${name}`) |
| 75 | + } |
| 76 | + |
| 77 | + const draftEvent = event as any |
| 78 | + const offsetSeconds = draftEvent[draftOffsetSeconds] |
| 79 | + |
| 80 | + let eventToSend = event |
| 81 | + if (typeof offsetSeconds === 'number') { |
| 82 | + const { pubkey, privkey } = this.parameters.identities[name] |
| 83 | + const createdAt = Math.floor(Date.now() / 1000) + offsetSeconds |
| 84 | + |
| 85 | + eventToSend = await createEvent( |
| 86 | + { |
| 87 | + pubkey, |
| 88 | + kind: event.kind, |
| 89 | + content: event.content, |
| 90 | + created_at: createdAt, |
| 91 | + }, |
| 92 | + privkey, |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + delete draftEvent[isDraft] |
| 97 | + delete draftEvent[draftOffsetSeconds] |
| 98 | + |
| 99 | + const command = await sendEvent(ws, eventToSend, false) as CommandResult |
| 100 | + |
| 101 | + expect(command[0]).to.equal(MessageType.OK) |
| 102 | + expect(command[2]).to.equal(false) |
| 103 | + expect(command[3].toLowerCase()).to.contain(expectedReason.toLowerCase()) |
| 104 | +}) |
0 commit comments