|
| 1 | +import { Then, When, World } from '@cucumber/cucumber' |
| 2 | +import { expect } from 'chai' |
| 3 | +import WebSocket from 'ws' |
| 4 | + |
| 5 | +import { createEvent, createSubscription, publishEvent, waitForEventCount } from '../helpers' |
| 6 | +import { ExpiringEvent } from '../../../../src/@types/event' |
| 7 | +import { EventExpirationTimeMetadataKey, EventKinds, EventTags } from '../../../../src/constants/base' |
| 8 | +import { getMasterDbClient } from '../../../../src/database/client' |
| 9 | +import { EventRepository } from '../../../../src/repositories/event-repository' |
| 10 | + |
| 11 | +const now = (): number => Math.floor(Date.now() / 1000) |
| 12 | + |
| 13 | +const createTextNoteWithExpiration = async ( |
| 14 | + world: World<Record<string, any>>, |
| 15 | + name: string, |
| 16 | + content: string, |
| 17 | + expirationTime: number, |
| 18 | +): Promise<ExpiringEvent> => { |
| 19 | + const ws = world.parameters.clients[name] as WebSocket |
| 20 | + const { pubkey, privkey } = world.parameters.identities[name] |
| 21 | + |
| 22 | + const event = await createEvent( |
| 23 | + { |
| 24 | + pubkey, |
| 25 | + kind: EventKinds.TEXT_NOTE, |
| 26 | + content, |
| 27 | + tags: [[EventTags.Expiration, expirationTime.toString()]], |
| 28 | + }, |
| 29 | + privkey, |
| 30 | + ) as ExpiringEvent |
| 31 | + |
| 32 | + event[EventExpirationTimeMetadataKey] = expirationTime |
| 33 | + |
| 34 | + await publishEvent(ws, event) |
| 35 | + |
| 36 | + world.parameters.events[name].push(event) |
| 37 | + |
| 38 | + return event |
| 39 | +} |
| 40 | + |
| 41 | +const seedStoredTextNoteWithExpiration = async ( |
| 42 | + world: World<Record<string, any>>, |
| 43 | + name: string, |
| 44 | + content: string, |
| 45 | + expirationTime: number, |
| 46 | +): Promise<ExpiringEvent> => { |
| 47 | + const { pubkey, privkey } = world.parameters.identities[name] |
| 48 | + const dbClient = getMasterDbClient() |
| 49 | + const repository = new EventRepository(dbClient, dbClient) |
| 50 | + |
| 51 | + const event = await createEvent( |
| 52 | + { |
| 53 | + pubkey, |
| 54 | + kind: EventKinds.TEXT_NOTE, |
| 55 | + created_at: expirationTime - 30, |
| 56 | + content, |
| 57 | + tags: [[EventTags.Expiration, expirationTime.toString()]], |
| 58 | + }, |
| 59 | + privkey, |
| 60 | + ) as ExpiringEvent |
| 61 | + |
| 62 | + event[EventExpirationTimeMetadataKey] = expirationTime |
| 63 | + |
| 64 | + const inserted = await repository.create(event) |
| 65 | + expect(inserted).to.equal(1) |
| 66 | + |
| 67 | + world.parameters.events[name].push(event) |
| 68 | + |
| 69 | + return event |
| 70 | +} |
| 71 | + |
| 72 | +When(/^(\w+) sends a text_note event with content "([^"]+)" and expiration in the past$/, async function( |
| 73 | + this: World<Record<string, any>>, |
| 74 | + name: string, |
| 75 | + content: string, |
| 76 | +) { |
| 77 | + await createTextNoteWithExpiration(this, name, content, now() - 10) |
| 78 | +}) |
| 79 | + |
| 80 | +When(/^(\w+) sends a text_note event with content "([^"]+)" and expiration in the future$/, async function( |
| 81 | + this: World<Record<string, any>>, |
| 82 | + name: string, |
| 83 | + content: string, |
| 84 | +) { |
| 85 | + await createTextNoteWithExpiration(this, name, content, now() + 30) |
| 86 | +}) |
| 87 | + |
| 88 | +When(/^(\w+) has a stored text_note event with content "([^"]+)" and expiration in the past$/, async function( |
| 89 | + this: World<Record<string, any>>, |
| 90 | + name: string, |
| 91 | + content: string, |
| 92 | +) { |
| 93 | + await seedStoredTextNoteWithExpiration(this, name, content, now() - 10) |
| 94 | +}) |
| 95 | + |
| 96 | +When(/^(\w+) sends a text_note event with content "([^"]+)" and expiration in (\d+) seconds$/, async function( |
| 97 | + this: World<Record<string, any>>, |
| 98 | + name: string, |
| 99 | + content: string, |
| 100 | + durationSeconds: string, |
| 101 | +) { |
| 102 | + const expirationTime = now() + Number(durationSeconds) |
| 103 | + const event = await createTextNoteWithExpiration(this, name, content, expirationTime) |
| 104 | + const expirationTag = event.tags.find((tag) => tag[0] === EventTags.Expiration) |
| 105 | + |
| 106 | + expect(expirationTag).to.not.equal(undefined) |
| 107 | + expect(Number(expirationTag?.[1])).to.equal(expirationTime) |
| 108 | +}) |
| 109 | + |
| 110 | +When(/^(\w+) subscribes to text_note events from (\w+)$/, async function( |
| 111 | + this: World<Record<string, any>>, |
| 112 | + name: string, |
| 113 | + author: string, |
| 114 | +) { |
| 115 | + const ws = this.parameters.clients[name] as WebSocket |
| 116 | + const authorPubkey = this.parameters.identities[author].pubkey |
| 117 | + const subscription = { |
| 118 | + name: `test-${Math.random()}`, |
| 119 | + filters: [{ kinds: [EventKinds.TEXT_NOTE], authors: [authorPubkey] }], |
| 120 | + } |
| 121 | + |
| 122 | + this.parameters.subscriptions[name].push(subscription) |
| 123 | + |
| 124 | + await createSubscription(ws, subscription.name, subscription.filters) |
| 125 | +}) |
| 126 | + |
| 127 | +Then(/^(\w+) receives (\d+) text_note events and EOSE$/, async function( |
| 128 | + this: World<Record<string, any>>, |
| 129 | + name: string, |
| 130 | + count: string, |
| 131 | +) { |
| 132 | + const ws = this.parameters.clients[name] as WebSocket |
| 133 | + const subscription = this.parameters.subscriptions[name][this.parameters.subscriptions[name].length - 1] |
| 134 | + const events = await waitForEventCount(ws, subscription.name, Number(count), true) |
| 135 | + |
| 136 | + expect(events.length).to.equal(Number(count)) |
| 137 | + |
| 138 | + events.forEach((event) => { |
| 139 | + expect(event.kind).to.equal(EventKinds.TEXT_NOTE) |
| 140 | + }) |
| 141 | +}) |
0 commit comments