1- import { After , AfterAll , Before , BeforeAll , Given , Then , When , World , setDefaultTimeout } from '@cucumber/cucumber'
1+ import { After , AfterAll , Before , BeforeAll , Given , Then , When , World } from '@cucumber/cucumber'
22import { assocPath , pipe } from 'ramda'
33import { fromEvent , map , Observable , ReplaySubject , Subject , takeUntil } from 'rxjs'
44import WebSocket , { MessageEvent } from 'ws'
@@ -15,32 +15,19 @@ import { workerFactory } from '../../../src/factories/worker-factory'
1515export const isDraft = Symbol ( 'draft' )
1616
1717let worker : AppWorker
18+
1819let dbClient : DatabaseClient
1920let rrDbClient : DatabaseClient
2021
2122export const streams = new WeakMap < WebSocket , Observable < unknown > > ( )
2223
23- setDefaultTimeout ( 30000 )
24-
25- BeforeAll ( async function ( ) {
24+ BeforeAll ( { timeout : 1000 } , async function ( ) {
2625 process . env . RELAY_PORT = '18808'
2726 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-
3827 dbClient = getMasterDbClient ( )
3928 rrDbClient = getReadReplicaDbClient ( )
40-
4129 await dbClient . raw ( 'SELECT 1=1' )
4230 await rrDbClient . raw ( 'SELECT 1=1' )
43-
4431 Sinon . stub ( SettingsStatic , 'watchSettings' )
4532 const settings = SettingsStatic . createSettings ( )
4633
@@ -58,16 +45,9 @@ BeforeAll(async function () {
5845} )
5946
6047AfterAll ( { timeout : 30000 } , async function ( ) {
61- const clients = [ ...new Set ( [ dbClient , rrDbClient ] . filter ( Boolean ) ) ]
62-
6348 await new Promise < void > ( ( resolve ) => {
64- if ( ! worker ) {
65- void Promise . all ( clients . map ( ( client ) => client . destroy ( ) ) ) . then ( ( ) => resolve ( ) )
66- return
67- }
68-
6949 worker . close ( async ( ) => {
70- await Promise . all ( clients . map ( ( client ) => client . destroy ( ) ) )
50+ await Promise . all ( [ dbClient . destroy ( ) , rrDbClient . destroy ( ) ] )
7151 resolve ( )
7252 } )
7353 } )
@@ -83,13 +63,11 @@ Before(function () {
8363After ( async function ( ) {
8464 this . parameters . events = { }
8565 this . parameters . subscriptions = { }
86-
8766 for ( const ws of Object . values ( this . parameters . clients as Record < string , WebSocket > ) ) {
8867 if ( ws && ws . readyState === WebSocket . OPEN ) {
8968 ws . close ( )
9069 }
9170 }
92-
9371 this . parameters . clients = { }
9472
9573 await dbClient ( 'events' )
@@ -100,22 +78,20 @@ After(async function () {
10078 ) ,
10179 )
10280 . delete ( )
103-
10481 this . parameters . identities = { }
10582} )
10683
10784Given ( / s o m e o n e c a l l e d ( \w + ) / , async function ( name : string ) {
10885 const connection = await connect ( name )
109-
11086 this . parameters . identities [ name ] = this . parameters . identities [ name ] ?? createIdentity ( name )
11187 this . parameters . clients [ name ] = connection
11288 this . parameters . subscriptions [ name ] = [ ]
11389 this . parameters . events [ name ] = [ ]
114-
11590 const close = new Subject ( )
11691 connection . once ( 'close' , close . next . bind ( close ) )
11792
11893 const projection = ( raw : MessageEvent ) => JSON . parse ( raw . data . toString ( 'utf8' ) )
94+
11995 const replaySubject = new ReplaySubject ( 2 , 1000 )
12096
12197 fromEvent ( connection , 'message' )
@@ -128,12 +104,7 @@ Given(/someone called (\w+)/, async function (name: string) {
128104When ( / ( \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 ) {
129105 const ws = this . parameters . clients [ from ] as WebSocket
130106 const pubkey = this . parameters . identities [ to ] . pubkey
131-
132- const subscription = {
133- name : `test-${ Math . random ( ) } ` ,
134- filters : [ { authors : [ pubkey ] } ] ,
135- }
136-
107+ const subscription = { name : `test-${ Math . random ( ) } ` , filters : [ { authors : [ pubkey ] } ] }
137108 this . parameters . subscriptions [ from ] . push ( subscription )
138109
139110 await createSubscription ( ws , subscription . name , subscription . filters )
@@ -142,7 +113,6 @@ When(/(\w+) subscribes to author (\w+)$/, async function (this: World<Record<str
142113Then ( / ( \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 ) {
143114 const ws = this . parameters . clients [ from ] as WebSocket
144115 const subscription = this . parameters . subscriptions [ from ] . pop ( )
145-
146116 return new Promise < void > ( ( resolve , reject ) => {
147117 ws . send ( JSON . stringify ( [ 'CLOSE' , subscription . name ] ) , ( err ) => ( err ? reject ( err ) : resolve ( ) ) )
148118 } )
0 commit comments