1
1
import { Emulators } from "./types" ;
2
2
import { EmulatorRegistry } from "./registry" ;
3
3
import { expect } from "chai" ;
4
- import * as sinon from "sinon" ;
5
-
6
- import * as controller from "./controller" ;
7
- import * as commandUtils from "./commandUtils" ;
8
- import * as functionsConfig from "../functions/projectConfig" ;
9
- import * as functionsHelper from "../deploy/functions/functionsDeployHelper" ;
10
- import * as projectUtils from "../projectUtils" ;
11
- import * as portUtils from "./portUtils" ;
12
4
import { FakeEmulator } from "./testing/fakeEmulator" ;
13
- import { FunctionsEmulator } from "./functionsEmulator" ;
14
- import { Runtime } from "../deploy/functions/runtimes/supported" ;
15
- import { Options } from "../options" ;
16
- import { Config } from "../config" ;
17
5
18
6
describe ( "EmulatorController" , ( ) => {
19
- const sandbox = sinon . createSandbox ( ) ;
20
-
21
- let functionsEmulatorConstructorStub : sinon . SinonStub ;
22
- let normalizeAndValidateStub : sinon . SinonStub ;
23
- let getEndpointFiltersStub : sinon . SinonStub ;
24
- let shouldStartStub : sinon . SinonStub ;
25
- let filterEmulatorTargetsStub : sinon . SinonStub ;
26
- let resolveHostAndAssignPortsStub : sinon . SinonStub ;
27
- let getProjectIdStub : sinon . SinonStub ;
28
- let startEmulatorStub : sinon . SinonStub ;
29
- let checkJavaMajorVersionStub : sinon . SinonStub ;
30
-
31
- beforeEach ( ( ) => {
32
- // Stub dependencies
33
- functionsEmulatorConstructorStub = sandbox . stub ( FunctionsEmulator . prototype , "start" ) ;
34
- normalizeAndValidateStub = sandbox . stub ( functionsConfig , "normalizeAndValidate" ) ;
35
- getEndpointFiltersStub = sandbox . stub ( functionsHelper , "getEndpointFilters" ) ;
36
- shouldStartStub = sandbox . stub ( controller , "shouldStart" ) ;
37
- filterEmulatorTargetsStub = sandbox . stub ( controller , "filterEmulatorTargets" ) ;
38
- resolveHostAndAssignPortsStub = sandbox . stub ( portUtils , "resolveHostAndAssignPorts" ) ;
39
- getProjectIdStub = sandbox . stub ( projectUtils , "getProjectId" ) ;
40
- startEmulatorStub = sandbox . stub ( EmulatorRegistry , "start" ) ;
41
- checkJavaMajorVersionStub = sandbox . stub ( commandUtils , "checkJavaMajorVersion" ) . resolves ( 11 ) ; // Assume valid Java version
42
-
43
- // Default stub behaviors
44
- getProjectIdStub . returns ( "test-project" ) ;
45
- shouldStartStub . returns ( false ) ; // Default to not starting emulators unless specified
46
- filterEmulatorTargetsStub . returns ( [ ] ) ; // Default to no targets unless specified
47
- resolveHostAndAssignPortsStub . resolves ( { } ) ; // Default to empty assigned ports
48
- } ) ;
49
-
50
7
afterEach ( async ( ) => {
51
- sandbox . restore ( ) ;
52
8
await EmulatorRegistry . stopAll ( ) ;
53
9
} ) ;
54
10
@@ -63,84 +19,4 @@ describe("EmulatorController", () => {
63
19
expect ( EmulatorRegistry . isRunning ( name ) ) . to . be . true ;
64
20
expect ( EmulatorRegistry . getInfo ( name ) ! . port ) . to . eql ( fake . getInfo ( ) . port ) ;
65
21
} ) ;
66
-
67
- describe ( "startAll" , ( ) => {
68
- const mockOptions = ( ) : Options => {
69
- return {
70
- project : "test-project" ,
71
- only : "" ,
72
- config : new Config ( { } ) , // Provide a mock Config object
73
- // Add other necessary default options
74
- } as unknown as Options ; // Use unknown assertion carefully
75
- } ;
76
-
77
- const mockFunctionsConfig : functionsConfig . ValidatedConfig = [
78
- { source : "functions- A" , codebase : "default" , runtime : "nodejs18" } ,
79
- { source : "functions-B" , codebase : "api" , runtime : "nodejs18" } ,
80
- { source : "functions-C" , codebase : "worker" , runtime : "nodejs18" } ,
81
- ] ;
82
-
83
- it ( "should call getEndpointFilters with strict=true when starting Functions emulator" , async ( ) => {
84
- const options = mockOptions ( ) ;
85
- options . only = "functions" ; // Enable functions emulator generally
86
- options . config = new Config ( { functions : { source : "functions" } } ) ; // Basic functions config
87
-
88
- // Setup stubs for this test
89
- shouldStartStub . withArgs ( options , Emulators . FUNCTIONS ) . returns ( true ) ;
90
- filterEmulatorTargetsStub . withArgs ( options ) . returns ( [ Emulators . FUNCTIONS ] ) ;
91
- normalizeAndValidateStub . returns ( mockFunctionsConfig . slice ( 0 , 1 ) ) ; // Return only default codebase config
92
- resolveHostAndAssignPortsStub . resolves ( {
93
- [ Emulators . FUNCTIONS ] : [ { address : "localhost" , port : 5001 , family : "IPv4" } ] ,
94
- [ Emulators . EVENTARC ] : [ { address : "localhost" , port : 9200 , family : "IPv4" } ] ,
95
- [ Emulators . TASKS ] : [ { address : "localhost" , port : 9300 , family : "IPv4" } ] ,
96
- } ) ;
97
- getEndpointFiltersStub . returns ( undefined ) ; // No specific filters from --only
98
-
99
- await controller . startAll ( options ) ;
100
-
101
- // Assert getEndpointFilters was called with strict = true
102
- expect ( getEndpointFiltersStub . calledOnce ) . to . be . true ;
103
- expect ( getEndpointFiltersStub . getCall ( 0 ) . args [ 1 ] ) . to . equal ( true ) ; // strict = true
104
- } ) ;
105
-
106
- it ( "should filter function codebases based on --only flag using strict parsing" , async ( ) => {
107
- const options = mockOptions ( ) ;
108
- options . only = "functions:api" ; // Only start 'api' codebase functions
109
- options . config = new Config ( {
110
- functions : [
111
- { source : "functions-A" , codebase : "default" } ,
112
- { source : "functions-B" , codebase : "api" } ,
113
- { source : "functions-C" , codebase : "worker" } ,
114
- ] ,
115
- } ) ;
116
-
117
- // Setup stubs for this test
118
- shouldStartStub . withArgs ( options , Emulators . FUNCTIONS ) . returns ( true ) ;
119
- filterEmulatorTargetsStub . withArgs ( options ) . returns ( [ Emulators . FUNCTIONS ] ) ;
120
- normalizeAndValidateStub . returns ( mockFunctionsConfig ) ; // Return all codebases initially
121
- resolveHostAndAssignPortsStub . resolves ( {
122
- [ Emulators . FUNCTIONS ] : [ { address : "localhost" , port : 5001 , family : "IPv4" } ] ,
123
- [ Emulators . EVENTARC ] : [ { address : "localhost" , port : 9200 , family : "IPv4" } ] ,
124
- [ Emulators . TASKS ] : [ { address : "localhost" , port : 9300 , family : "IPv4" } ] ,
125
- } ) ;
126
-
127
- // Simulate getEndpointFilters returning the filter for 'api' codebase in strict mode
128
- getEndpointFiltersStub . withArgs ( options , true ) . returns ( [ { codebase : "api" } ] ) ;
129
-
130
- // Spy on the FunctionsEmulator constructor to capture arguments
131
- const functionsEmulatorSpy = sandbox . spy (
132
- require ( "../emulator/functionsEmulator" ) , // Use require to spy on the class itself
133
- "FunctionsEmulator" ,
134
- ) ;
135
-
136
- await controller . startAll ( options ) ;
137
-
138
- // Assert FunctionsEmulator was called with filtered backends
139
- expect ( functionsEmulatorSpy . calledOnce ) . to . be . true ;
140
- const constructorArgs = functionsEmulatorSpy . getCall ( 0 ) . args [ 0 ] ;
141
- expect ( constructorArgs . emulatableBackends ) . to . be . an ( "array" ) . with . lengthOf ( 1 ) ;
142
- expect ( constructorArgs . emulatableBackends [ 0 ] . codebase ) . to . equal ( "api" ) ;
143
- expect ( constructorArgs . emulatableBackends [ 0 ] . source ) . to . equal ( "functions-B" ) ;
144
- } ) ;
145
- } ) ;
146
- } ) . timeout ( 10000 ) ; // Increase timeout for potentially slower setup/teardown
22
+ } ) . timeout ( 2000 ) ;
0 commit comments