1
1
import { randomUUID } from 'crypto' ;
2
+ import { status as grpcStatus } from '@grpc/grpc-js' ;
2
3
import { ErrorConstructor , ExecutionContext , TestFn } from 'ava' ;
3
4
import {
5
+ isGrpcServiceError ,
4
6
WorkflowFailedError ,
5
7
WorkflowHandle ,
6
8
WorkflowStartOptions ,
@@ -12,6 +14,7 @@ import {
12
14
} from '@temporalio/testing' ;
13
15
import {
14
16
DefaultLogger ,
17
+ LogEntry ,
15
18
LogLevel ,
16
19
Runtime ,
17
20
WorkerOptions ,
@@ -50,6 +53,7 @@ export function makeTestFunction(opts: {
50
53
workflowsPath : string ;
51
54
workflowEnvironmentOpts ?: LocalTestWorkflowEnvironmentOptions ;
52
55
workflowInterceptorModules ?: string [ ] ;
56
+ recordedLogs ?: { [ workflowId : string ] : LogEntry [ ] } ;
53
57
} ) : TestFn < Context > {
54
58
const test = anyTest as TestFn < Context > ;
55
59
test . before ( async ( t ) => {
@@ -59,9 +63,15 @@ export function makeTestFunction(opts: {
59
63
workflowsPath : opts . workflowsPath ,
60
64
logger : new DefaultLogger ( 'WARN' ) ,
61
65
} ) ;
62
- // Ignore invalid log levels
66
+ const logger = opts . recordedLogs
67
+ ? new DefaultLogger ( 'DEBUG' , ( entry ) => {
68
+ const workflowId = ( entry . meta as any ) ?. workflowInfo ?. workflowId ?? ( entry . meta as any ) ?. workflowId ;
69
+ opts . recordedLogs ! [ workflowId ] ??= [ ] ;
70
+ opts . recordedLogs ! [ workflowId ] . push ( entry ) ;
71
+ } )
72
+ : new DefaultLogger ( ( process . env . TEST_LOG_LEVEL || 'DEBUG' ) . toUpperCase ( ) as LogLevel ) ;
63
73
Runtime . install ( {
64
- logger : new DefaultLogger ( ( process . env . TEST_LOG_LEVEL || 'DEBUG' ) . toUpperCase ( ) as LogLevel ) ,
74
+ logger,
65
75
telemetryOptions : {
66
76
logging : {
67
77
filter : makeTelemetryFilterString ( {
@@ -107,6 +117,7 @@ export interface Helpers {
107
117
) : Promise < WorkflowHandle < T > > ;
108
118
assertWorkflowUpdateFailed ( p : Promise < any > , causeConstructor : ErrorConstructor , message ?: string ) : Promise < void > ;
109
119
assertWorkflowFailedError ( p : Promise < any > , causeConstructor : ErrorConstructor , message ?: string ) : Promise < void > ;
120
+ updateHasBeenAdmitted ( handle : WorkflowHandle < workflow . Workflow > , updateId : string ) : Promise < boolean > ;
110
121
}
111
122
112
123
export function helpers ( t : ExecutionContext < Context > , testEnv : TestWorkflowEnvironment = t . context . env ) : Helpers {
@@ -172,5 +183,22 @@ export function helpers(t: ExecutionContext<Context>, testEnv: TestWorkflowEnvir
172
183
t . is ( err . cause ?. message , message ) ;
173
184
}
174
185
} ,
186
+ async updateHasBeenAdmitted ( handle : WorkflowHandle < workflow . Workflow > , updateId : string ) : Promise < boolean > {
187
+ try {
188
+ await testEnv . client . workflowService . pollWorkflowExecutionUpdate ( {
189
+ namespace : testEnv . client . options . namespace ,
190
+ updateRef : {
191
+ workflowExecution : { workflowId : handle . workflowId } ,
192
+ updateId,
193
+ } ,
194
+ } ) ;
195
+ return true ;
196
+ } catch ( err ) {
197
+ if ( isGrpcServiceError ( err ) && err . code === grpcStatus . NOT_FOUND ) {
198
+ return false ;
199
+ }
200
+ throw err ;
201
+ }
202
+ } ,
175
203
} ;
176
204
}
0 commit comments