@@ -7,7 +7,9 @@ import { IrcServer } from "../../src/irc/IrcServer";
77import { MatrixClient } from "matrix-bot-sdk" ;
88import { TestIrcServer } from "matrix-org-irc" ;
99import { IrcConnectionPool } from "../../src/pool-service/IrcConnectionPool" ;
10- import dns from 'node:dns' ;
10+ import { expect } from "@jest/globals" ;
11+ import * as dns from 'node:dns' ;
12+ import { WriteStream , createWriteStream } from "node:fs" ;
1113// Needed to make tests work on GitHub actions. Node 17+ defaults
1214// to IPv6, and the homerunner domain resolves to IPv6, but the
1315// runtime doesn't actually support IPv6 🤦
@@ -24,8 +26,11 @@ interface Opts {
2426 ircNicks ?: string [ ] ;
2527 timeout ?: number ;
2628 config ?: Partial < BridgeConfig > ,
29+ traceToFile ?: boolean ,
2730}
2831
32+ const traceFilePath = '.e2e-traces' ;
33+
2934export class E2ETestMatrixClient extends MatrixClient {
3035
3136 public async waitForPowerLevel (
@@ -160,6 +165,11 @@ export class IrcBridgeE2ETest {
160165 }
161166
162167 static async createTestEnv ( opts : Opts = { } ) : Promise < IrcBridgeE2ETest > {
168+ const testName = expect . getState ( ) . currentTestName ?. replace ( / [ ^ a - z A - Z ] / g, '-' ) ;
169+ const tracePath = `${ traceFilePath } /${ testName } .log` ;
170+ console . log ( 'Opening new trace file' , tracePath ) ;
171+ const traceLog = createWriteStream ( tracePath , 'utf-8' ) ;
172+
163173 const workerID = parseInt ( process . env . JEST_WORKER_ID ?? '0' ) ;
164174 const { matrixLocalparts, config } = opts ;
165175 const ircTest = new TestIrcServer ( ) ;
@@ -280,7 +290,7 @@ export class IrcBridgeE2ETest {
280290 }
281291 } ) ,
282292 } , registration ) ;
283- return new IrcBridgeE2ETest ( homeserver , ircBridge , registration , postgresDb , ircTest , redisPool )
293+ return new IrcBridgeE2ETest ( homeserver , ircBridge , registration , postgresDb , ircTest , traceLog , redisPool )
284294 }
285295
286296 private constructor (
@@ -289,7 +299,24 @@ export class IrcBridgeE2ETest {
289299 public readonly registration : AppServiceRegistration ,
290300 readonly postgresDb : string ,
291301 public readonly ircTest : TestIrcServer ,
292- public readonly pool ?: IrcConnectionPool ) {
302+ private traceLog : WriteStream ,
303+ public readonly pool ?: IrcConnectionPool ,
304+ ) {
305+ const startTime = Date . now ( ) ;
306+ for ( const [ clientId , client ] of Object . entries ( ircTest . clients ) ) {
307+ client . on ( 'raw' , ( msg ) => {
308+ traceLog . write (
309+ `${ Date . now ( ) - startTime } ms [IRC:${ clientId } ] ${ JSON . stringify ( msg ) } \n`
310+ ) ;
311+ } )
312+ }
313+ for ( const { client, userId} of Object . values ( homeserver . users ) ) {
314+ client . on ( 'room.event' , ( roomId , eventData ) => {
315+ traceLog . write (
316+ `${ Date . now ( ) - startTime } ms [Matrix:${ userId } ] ${ roomId } ${ JSON . stringify ( eventData ) } \n`
317+ ) ;
318+ } )
319+ }
293320 }
294321
295322 public async recreateBridge ( ) {
@@ -317,6 +344,9 @@ export class IrcBridgeE2ETest {
317344 }
318345
319346 public async tearDown ( ) : Promise < void > {
347+ if ( this . traceLog ) {
348+ this . traceLog . close ( ) ;
349+ }
320350 await Promise . allSettled ( [
321351 this . ircBridge ?. kill ( ) ,
322352 this . ircTest . tearDown ( ) ,
0 commit comments