import { ReadableStreamDefaultReader , TransformStream , WritableStreamDefaultWriter , } from "stream/web" ; import { Log , LogLevel } from "@miniflare/shared" ; import { ExecutionContext } from "ava" ; export type LogEntry = [ level : LogLevel , message : string ] ; export class TestLog extends Log { logs : LogEntry [ ] = [ ] ; constructor ( ) { super ( LogLevel . VERBOSE ) ; } log ( message ) { this . logs . push ( [ LogLevel . NONE , message ] ) ; } logWithLevel ( level , message ) { if ( level <= this . level ) this . logs . push ( [ level , message ] ) ; } error ( message ) { throw message ; } logsAtLevel ( level ) { return this . logs . filter ( ( [ logLevel ] ) => logLevel === level ) . map ( ( [ , message ] ) => message ) ; } logsAtLevelOrBelow ( level ) { return this . logs . filter ( ( [ logLevel ] ) => logLevel <= level ) ; } } export class AsyncTestLog extends Log { #reader : ReadableStreamDefaultReader < LogEntry > ; #writer : WritableStreamDefaultWriter < LogEntry > ; constructor ( ) { super ( LogLevel . VERBOSE ) ; const { readable , writable } = new TransformStream < LogEntry , LogEntry > ( ) ; this . #reader = readable . getReader ( ) ; this . #writer = writable . getWriter ( ) ; } log ( message ) { this . logWithLevel ( LogLevel . NONE , message ) ; } logWithLevel ( level , message ) { void this . #writer . write ( [ level , message ] ) ; } async next ( ) { return ( await this . #reader . read ( ) ) . value ; } async nextAtLevel ( level ) { while ( true ) { const entry = await this . next ( ) ; if ( ! entry ) return ; if ( entry [ 0 ] === level ) return entry [ 1 ] ; } } } export function interceptConsoleLogs ( t ) { const logs = [ ] ; const originalLog = console . log ; t . teardown ( ( ) => ( console . log = originalLog ) ) ; console . log = ( ... args ) => logs . push ( args . join ( " " ) ) ; return logs ; }