|
| 1 | +import { After, 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 setCreatedAtLimits = (maxPositiveDelta: number, maxNegativeDelta: number) => { |
| 14 | + const settings = SettingsStatic._settings ?? SettingsStatic.createSettings() |
| 15 | + |
| 16 | + SettingsStatic._settings = pipe( |
| 17 | + assocPath(['limits', 'event', 'createdAt', 'maxPositiveDelta'], maxPositiveDelta), |
| 18 | + assocPath(['limits', 'event', 'createdAt', 'maxNegativeDelta'], maxNegativeDelta), |
| 19 | + )(settings) as any |
| 20 | +} |
| 21 | + |
| 22 | +After({ tags: '@nip-22' }, function() { |
| 23 | + setCreatedAtLimits(900, 0) |
| 24 | +}) |
| 25 | + |
| 26 | +Given(/^created_at limits are set to maxPositiveDelta (\d+) and maxNegativeDelta (\d+)$/, function( |
| 27 | + maxPositiveDelta: string, |
| 28 | + maxNegativeDelta: string, |
| 29 | +) { |
| 30 | + setCreatedAtLimits(Number(maxPositiveDelta), Number(maxNegativeDelta)) |
| 31 | +}) |
| 32 | + |
| 33 | +When(/^(\w+) drafts a text_note event with content "([^"]+)" and created_at (-?\d+) seconds from now$/, async function( |
| 34 | + name: string, |
| 35 | + content: string, |
| 36 | + offsetSeconds: string, |
| 37 | +) { |
| 38 | + const { pubkey, privkey } = this.parameters.identities[name] |
| 39 | + const createdAt = Math.floor(Date.now() / 1000) + Number(offsetSeconds) |
| 40 | + |
| 41 | + const event: Event = await createEvent( |
| 42 | + { |
| 43 | + pubkey, |
| 44 | + kind: 1, |
| 45 | + content, |
| 46 | + created_at: createdAt, |
| 47 | + }, |
| 48 | + privkey, |
| 49 | + ) |
| 50 | + |
| 51 | + const draftEvent = event as any |
| 52 | + draftEvent[isDraft] = true |
| 53 | + |
| 54 | + this.parameters.events[name].push(event) |
| 55 | +}) |
| 56 | + |
| 57 | +Then(/^(\w+) sends their last draft event unsuccessfully with reason containing "([^"]+)"$/, async function( |
| 58 | + name: string, |
| 59 | + expectedReason: string, |
| 60 | +) { |
| 61 | + const ws = this.parameters.clients[name] as WebSocket |
| 62 | + |
| 63 | + const event = this.parameters.events[name].findLast((lastEvent: Event) => (lastEvent as any)[isDraft]) |
| 64 | + if (!event) { |
| 65 | + throw new Error(`No draft event found for ${name}`) |
| 66 | + } |
| 67 | + |
| 68 | + delete (event as any)[isDraft] |
| 69 | + |
| 70 | + const command = await sendEvent(ws, event, false) as CommandResult |
| 71 | + |
| 72 | + expect(command[0]).to.equal(MessageType.OK) |
| 73 | + expect(command[2]).to.equal(false) |
| 74 | + expect(command[3].toLowerCase()).to.contain(expectedReason.toLowerCase()) |
| 75 | +}) |
0 commit comments