test: nip-09 deleted events

Signed-off-by: Ricardo Arturo Cabral Mejía <me@ricardocabral.io>
This commit is contained in:
Ricardo Arturo Cabral Mejía 2023-01-10 21:06:57 -05:00
parent 273dcc6530
commit 3fa30724d7
6 changed files with 60 additions and 5 deletions

View File

@ -32,7 +32,7 @@ export interface DBEvent {
event_delegator?: Buffer | null
event_deduplication?: string | null
first_seen: Date
deleted_at: Date
deleted_at?: Date
}
export interface CanonicalEvent {

View File

@ -2,12 +2,12 @@ import { anyPass, equals, map, uniqWith } from 'ramda'
import { pipeline } from 'stream/promises'
import { createEndOfStoredEventsNoticeMessage, createNoticeMessage, createOutgoingEventMessage } from '../utils/messages'
import { DBEvent, Event } from '../@types/event'
import { IAbortable, IMessageHandler } from '../@types/message-handlers'
import { isEventMatchingFilter, toNostrEvent } from '../utils/event'
import { streamEach, streamEnd, streamFilter, streamMap } from '../utils/stream'
import { SubscriptionFilter, SubscriptionId } from '../@types/subscription'
import { createLogger } from '../factories/logger-factory'
import { Event } from '../@types/event'
import { IEventRepository } from '../@types/repositories'
import { ISettings } from '../@types/settings'
import { IWebSocketAdapter } from '../@types/adapters'
@ -57,9 +57,12 @@ export class SubscribeMessageHandler implements IMessageHandler, IAbortable {
const findEvents = this.eventRepository.findByFilters(filters).stream()
const isNotDeleted = (row: DBEvent) => { console.log(row); return true }
try {
await pipeline(
findEvents,
streamFilter(isNotDeleted),
streamMap(toNostrEvent),
streamFilter(isSubscribedToEvent),
streamEach(sendEvent),

View File

@ -232,9 +232,7 @@ export class EventRepository implements IEventRepository {
public deleteByPubkeyAndIds(pubkey: string, ids: EventId[]): Promise<number> {
debug('deleting events from %s: %o', pubkey, ids)
return this.dbClient('events')
.where({
event_pubkey: toBuffer(pubkey),
})
.where('event_pubkey', toBuffer(pubkey))
.whereIn('event_id', map(toBuffer)(ids))
.whereNull('deleted_at')
.update({

View File

@ -17,6 +17,7 @@ services:
REDIS_USER: default
REDIS_PASSWORD: nostr_ts_relay_test
NOSTR_CONFIG_DIR: /code
DEBUG: knex:query,primary:event-repository
volumes:
- ../../package.json:/code/package.json
- ../../settings.sample.json:/code/settings.sample.json
@ -47,6 +48,8 @@ services:
POSTGRES_DB: nostr_ts_relay_test
networks:
- nostream-test
ports:
- 25432:5432
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
timeout: 5s

View File

@ -0,0 +1,14 @@
@Test
Feature: NIP-09
Scenario: Charlie deletes an event
Given someone called Charlie
And someone called Bob
And Charlie sends a text_note event with content "Twitter > Nostr"
And Charlie subscribes to author Charlie
And Charlie receives a text_note event from Charlie with content "Twitter > Nostr"
And Charlie unsubscribes from author Charlie
When Charlie sends a delete event for their last event
And Charlie subscribes to author Charlie
And Charlie receives 1 delete event from Charlie and EOSE
Then Bob subscribes to author Charlie
Then Bob receives 1 delete event from Charlie and EOSE

View File

@ -0,0 +1,37 @@
import { Then, When } from '@cucumber/cucumber'
import { expect } from 'chai'
import WebSocket from 'ws'
import { createEvent, sendEvent, waitForEventCount } from '../helpers'
import { Event } from '../../../../src/@types/event'
import { EventTags } from '../../../../src/constants/base'
import { Tag } from '../../../../src/@types/base'
When(/^(\w+) sends a delete event for their last event$/, async function(
name: string,
) {
const ws = this.parameters.clients[name] as WebSocket
const { pubkey, privkey } = this.parameters.identities[name]
const tags: Tag[] = [
[EventTags.Event, this.parameters.events[name][this.parameters.events[name].length - 1].id],
]
const event: Event = await createEvent({ pubkey, kind: 5, content: '', tags }, privkey)
console.log('event', event)
await sendEvent(ws, event)
this.parameters.events[name].push(event)
})
Then(
/(\w+) receives (\d+) delete events? from (\w+) and EOSE/,
async function(name: string, count: string, author: string) {
const ws = this.parameters.clients[name] as WebSocket
const subscription = this.parameters.subscriptions[name][this.parameters.subscriptions[name].length - 1]
const [event] = await waitForEventCount(ws, subscription.name, Number(count), true)
expect(event.kind).to.equal(5)
expect(event.pubkey).to.equal(this.parameters.identities[author].pubkey)
})