@@ -10,7 +10,7 @@ import test, {
1010import nock from "nock" ;
1111import * as sinon from "sinon" ;
1212
13- import { ActionState } from "./action-common" ;
13+ import { ActionState , StateFeature } from "./action-common" ;
1414import { ActionsEnv , ActionsEnvVars , getActionVersion } from "./actions-util" ;
1515import { AnalysisKind } from "./analyses" ;
1616import * as apiClient from "./api-client" ;
@@ -189,18 +189,25 @@ export function getTestActionsEnv(): ActionsEnv {
189189 } ;
190190}
191191
192+ /** For testing purposes, we make all available state features accessible in `TestEnv`. */
193+ type AllState = [ "Logger" , "Env" , "FeatureFlags" ] ;
194+
192195/**
193196 * Wraps a function that accepts an `ActionEnv` for testing in different environments.
194197 */
195- export class TestEnv < Args extends readonly any [ ] , R > {
196- private readonly fn : ( state : ActionState , ...args : Args ) => R ;
198+ export class TestEnv <
199+ Args extends readonly any [ ] ,
200+ R ,
201+ Fs extends ReadonlyArray < AllState [ number ] > ,
202+ > {
203+ private readonly fn : ( state : ActionState < Fs > , ...args : Args ) => R ;
197204 private args ?: Args ;
198- private state : ActionState ;
205+ private state : ActionState < AllState > ;
199206
200207 constructor (
201- fn : ( state : ActionState , ...args : Args ) => R ,
208+ fn : ( state : ActionState < Fs > , ...args : Args ) => R ,
202209 args ?: Args ,
203- initialState ?: ActionState ,
210+ initialState ?: ActionState < AllState > ,
204211 ) {
205212 this . fn = fn ;
206213 this . args = args ;
@@ -211,11 +218,11 @@ export class TestEnv<Args extends readonly any[], R> {
211218 } ;
212219 }
213220
214- private clone ( ) : TestEnv < Args , R > {
221+ private clone ( ) : TestEnv < Args , R , Fs > {
215222 return new TestEnv ( this . fn , this . args , { ...this . state } ) ;
216223 }
217224
218- public getState ( ) : ActionState {
225+ public getState ( ) : ActionState < AllState > {
219226 return this . state ;
220227 }
221228
@@ -229,13 +236,13 @@ export class TestEnv<Args extends readonly any[], R> {
229236 return result ;
230237 }
231238
232- public withFeatures ( enabled : Feature [ ] ) : TestEnv < Args , R > {
239+ public withFeatures ( enabled : Feature [ ] ) : TestEnv < Args , R , Fs > {
233240 const result = this . clone ( ) ;
234241 result . state . features = createFeatures ( enabled ) ;
235242 return result ;
236243 }
237244
238- public withEnv ( env : Env ) : TestEnv < Args , R > {
245+ public withEnv ( env : Env ) : TestEnv < Args , R , Fs > {
239246 const result = this . clone ( ) ;
240247 result . state . env = env ;
241248 return result ;
@@ -245,7 +252,7 @@ export class TestEnv<Args extends readonly any[], R> {
245252 if ( ! this . args ) {
246253 throw new Error ( "Trying to call function in TestEnv without arguments." ) ;
247254 }
248- return this . fn ( this . state , ...this . args ) ;
255+ return this . fn ( this . state as ActionState < Fs > , ...this . args ) ;
249256 }
250257
251258 public passes < T > (
@@ -259,9 +266,11 @@ export class TestEnv<Args extends readonly any[], R> {
259266}
260267
261268/** Utility function to construct a `TestEnv`. */
262- export function callee < Args extends readonly any [ ] , R > (
263- fn : ( state : ActionState , ...args : Args ) => R ,
264- ) : TestEnv < Args , R > {
269+ export function callee <
270+ Args extends readonly any [ ] ,
271+ R ,
272+ Fs extends readonly StateFeature [ ] ,
273+ > ( fn : ( state : ActionState < Fs > , ...args : Args ) => R ) : TestEnv < Args , R , Fs > {
265274 return new TestEnv ( fn ) ;
266275}
267276
0 commit comments