1- import { After , AfterAll , Before , BeforeAll , Given , Then , When , World } from '@cucumber/cucumber'
1+ import { After , AfterAll , Before , BeforeAll , Given , Then , When , World , setDefaultTimeout } from '@cucumber/cucumber'
22import { assocPath , pipe } from 'ramda'
33import { fromEvent , map , Observable , ReplaySubject , Subject , takeUntil } from 'rxjs'
44import WebSocket , { MessageEvent } from 'ws'
@@ -15,19 +15,32 @@ import { workerFactory } from '../../../src/factories/worker-factory'
1515export const isDraft = Symbol ( 'draft' )
1616
1717let worker : AppWorker
18-
1918let dbClient : DatabaseClient
2019let rrDbClient : DatabaseClient
2120
2221export const streams = new WeakMap < WebSocket , Observable < unknown > > ( )
2322
24- BeforeAll ( { timeout : 1000 } , async function ( ) {
23+ setDefaultTimeout ( 30000 )
24+
25+ BeforeAll ( async function ( ) {
2526 process . env . RELAY_PORT = '18808'
2627 process . env . SECRET = Math . random ( ) . toString ( ) . repeat ( 6 )
28+
29+ process . env . DB_HOST ??= 'localhost'
30+ process . env . DB_PORT ??= '5432'
31+ process . env . DB_USER ??= 'postgres'
32+ process . env . DB_PASSWORD ??= 'postgres'
33+ process . env . DB_NAME ??= 'nostr_ts_relay_test'
34+ process . env . DB_MIN_POOL_SIZE ??= '1'
35+ process . env . DB_MAX_POOL_SIZE ??= '2'
36+ process . env . DB_ACQUIRE_CONNECTION_TIMEOUT ??= '10000'
37+
2738 dbClient = getMasterDbClient ( )
2839 rrDbClient = getReadReplicaDbClient ( )
40+
2941 await dbClient . raw ( 'SELECT 1=1' )
3042 await rrDbClient . raw ( 'SELECT 1=1' )
43+
3144 Sinon . stub ( SettingsStatic , 'watchSettings' )
3245 const settings = SettingsStatic . createSettings ( )
3346
@@ -45,9 +58,16 @@ BeforeAll({ timeout: 1000 }, async function () {
4558} )
4659
4760AfterAll ( { timeout : 30000 } , async function ( ) {
61+ const clients = [ ...new Set ( [ dbClient , rrDbClient ] . filter ( Boolean ) ) ]
62+
4863 await new Promise < void > ( ( resolve ) => {
64+ if ( ! worker ) {
65+ void Promise . all ( clients . map ( ( client ) => client . destroy ( ) ) ) . then ( ( ) => resolve ( ) )
66+ return
67+ }
68+
4969 worker . close ( async ( ) => {
50- await Promise . all ( [ dbClient . destroy ( ) , rrDbClient . destroy ( ) ] )
70+ await Promise . all ( clients . map ( ( client ) => client . destroy ( ) ) )
5171 resolve ( )
5272 } )
5373 } )
@@ -63,11 +83,13 @@ Before(function () {
6383After ( async function ( ) {
6484 this . parameters . events = { }
6585 this . parameters . subscriptions = { }
86+
6687 for ( const ws of Object . values ( this . parameters . clients as Record < string , WebSocket > ) ) {
6788 if ( ws && ws . readyState === WebSocket . OPEN ) {
6889 ws . close ( )
6990 }
7091 }
92+
7193 this . parameters . clients = { }
7294
7395 await dbClient ( 'events' )
@@ -78,20 +100,22 @@ After(async function () {
78100 ) ,
79101 )
80102 . delete ( )
103+
81104 this . parameters . identities = { }
82105} )
83106
84107Given ( / s o m e o n e c a l l e d ( \w + ) / , async function ( name : string ) {
85108 const connection = await connect ( name )
109+
86110 this . parameters . identities [ name ] = this . parameters . identities [ name ] ?? createIdentity ( name )
87111 this . parameters . clients [ name ] = connection
88112 this . parameters . subscriptions [ name ] = [ ]
89113 this . parameters . events [ name ] = [ ]
114+
90115 const close = new Subject ( )
91116 connection . once ( 'close' , close . next . bind ( close ) )
92117
93118 const projection = ( raw : MessageEvent ) => JSON . parse ( raw . data . toString ( 'utf8' ) )
94-
95119 const replaySubject = new ReplaySubject ( 2 , 1000 )
96120
97121 fromEvent ( connection , 'message' )
@@ -104,7 +128,12 @@ Given(/someone called (\w+)/, async function (name: string) {
104128When ( / ( \w + ) s u b s c r i b e s t o a u t h o r ( \w + ) $ / , async function ( this : World < Record < string , any > > , from : string , to : string ) {
105129 const ws = this . parameters . clients [ from ] as WebSocket
106130 const pubkey = this . parameters . identities [ to ] . pubkey
107- const subscription = { name : `test-${ Math . random ( ) } ` , filters : [ { authors : [ pubkey ] } ] }
131+
132+ const subscription = {
133+ name : `test-${ Math . random ( ) } ` ,
134+ filters : [ { authors : [ pubkey ] } ] ,
135+ }
136+
108137 this . parameters . subscriptions [ from ] . push ( subscription )
109138
110139 await createSubscription ( ws , subscription . name , subscription . filters )
@@ -113,6 +142,7 @@ When(/(\w+) subscribes to author (\w+)$/, async function (this: World<Record<str
113142Then ( / ( \w + ) u n s u b s c r i b e s f r o m a u t h o r \w + / , async function ( from : string ) {
114143 const ws = this . parameters . clients [ from ] as WebSocket
115144 const subscription = this . parameters . subscriptions [ from ] . pop ( )
145+
116146 return new Promise < void > ( ( resolve , reject ) => {
117147 ws . send ( JSON . stringify ( [ 'CLOSE' , subscription . name ] ) , ( err ) => ( err ? reject ( err ) : resolve ( ) ) )
118148 } )
0 commit comments