Skip to content

Commit 73bc06b

Browse files
test: avoid real-time waits in NIP-40 integration
1 parent b6f3e10 commit 73bc06b

2 files changed

Lines changed: 44 additions & 28 deletions

File tree

test/integration/features/nip-40/nip-40.feature

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ Feature: NIP-40 Event expiration for standalone events
1616
And Bob subscribes to text_note events from Alice
1717
Then Bob receives a text_note event from Alice with content "not yet expired"
1818

19-
Scenario: Stored event is not returned to new subscribers after expiration time passes
19+
Scenario: Stored expired event is not returned to new subscribers
2020
Given someone called Alice
2121
And someone called Bob
22-
When Alice sends a text_note event with content "short lived" and expiration in 2 seconds
23-
And Bob waits until Alice's last text_note event expires
22+
When Alice has a stored text_note event with content "short lived" and expiration in the past
2423
And Bob subscribes to text_note events from Alice
2524
Then Bob receives 0 text_note events and EOSE

test/integration/features/nip-40/nip-40.feature.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@ import { expect } from 'chai'
33
import WebSocket from 'ws'
44

55
import { createEvent, createSubscription, publishEvent, waitForEventCount } from '../helpers'
6-
import { Event, ExpiringEvent } from '../../../../src/@types/event'
6+
import { ExpiringEvent } from '../../../../src/@types/event'
77
import { EventExpirationTimeMetadataKey, EventKinds, EventTags } from '../../../../src/constants/base'
8+
import { getMasterDbClient } from '../../../../src/database/client'
9+
import { EventRepository } from '../../../../src/repositories/event-repository'
810

911
const now = (): number => Math.floor(Date.now() / 1000)
1012

11-
const wait = async (ms: number): Promise<void> => {
12-
if (ms <= 0) {
13-
return
14-
}
15-
16-
await new Promise<void>((resolve) => setTimeout(resolve, ms))
17-
}
18-
1913
const createTextNoteWithExpiration = async (
2014
world: World<Record<string, any>>,
2115
name: string,
@@ -44,6 +38,37 @@ const createTextNoteWithExpiration = async (
4438
return event
4539
}
4640

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+
4772
When(/^(\w+) sends a text_note event with content "([^"]+)" and expiration in the past$/, async function(
4873
this: World<Record<string, any>>,
4974
name: string,
@@ -60,6 +85,14 @@ When(/^(\w+) sends a text_note event with content "([^"]+)" and expiration in th
6085
await createTextNoteWithExpiration(this, name, content, now() + 30)
6186
})
6287

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+
6396
When(/^(\w+) sends a text_note event with content "([^"]+)" and expiration in (\d+) seconds$/, async function(
6497
this: World<Record<string, any>>,
6598
name: string,
@@ -74,22 +107,6 @@ When(/^(\w+) sends a text_note event with content "([^"]+)" and expiration in (\
74107
expect(Number(expirationTag?.[1])).to.equal(expirationTime)
75108
})
76109

77-
When(/^(\w+) waits until (\w+)'s last text_note event expires$/, async function(
78-
this: World<Record<string, any>>,
79-
_name: string,
80-
author: string,
81-
) {
82-
const events = this.parameters.events[author] as Event[]
83-
const event = events[events.length - 1] as ExpiringEvent
84-
const expirationTime = event[EventExpirationTimeMetadataKey]
85-
86-
expect(expirationTime).to.be.a('number')
87-
88-
const millisecondsUntilExpired = (Number(expirationTime) - now() + 1) * 1000
89-
90-
await wait(millisecondsUntilExpired)
91-
})
92-
93110
When(/^(\w+) subscribes to text_note events from (\w+)$/, async function(
94111
this: World<Record<string, any>>,
95112
name: string,

0 commit comments

Comments
 (0)