@@ -3,19 +3,13 @@ import { expect } from 'chai'
33import WebSocket from 'ws'
44
55import { createEvent , createSubscription , publishEvent , waitForEventCount } from '../helpers'
6- import { Event , ExpiringEvent } from '../../../../src/@types/event'
6+ import { ExpiringEvent } from '../../../../src/@types/event'
77import { EventExpirationTimeMetadataKey , EventKinds , EventTags } from '../../../../src/constants/base'
8+ import { getMasterDbClient } from '../../../../src/database/client'
9+ import { EventRepository } from '../../../../src/repositories/event-repository'
810
911const 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-
1913const 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+
4772When ( / ^ ( \w + ) s e n d s a t e x t _ n o t e e v e n t w i t h c o n t e n t " ( [ ^ " ] + ) " a n d e x p i r a t i o n i n t h e p a s t $ / , 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 + ) h a s a s t o r e d t e x t _ n o t e e v e n t w i t h c o n t e n t " ( [ ^ " ] + ) " a n d e x p i r a t i o n i n t h e p a s t $ / , 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+
6396When ( / ^ ( \w + ) s e n d s a t e x t _ n o t e e v e n t w i t h c o n t e n t " ( [ ^ " ] + ) " a n d e x p i r a t i o n i n ( \d + ) s e c o n d s $ / , 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 + ) w a i t s u n t i l ( \w + ) ' s l a s t t e x t _ n o t e e v e n t e x p i r e s $ / , 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-
93110When ( / ^ ( \w + ) s u b s c r i b e s t o t e x t _ n o t e e v e n t s f r o m ( \w + ) $ / , async function (
94111 this : World < Record < string , any > > ,
95112 name : string ,
0 commit comments