@@ -110,6 +110,49 @@ describe('warnIfScopesDifferBeforeDev', () => {
110
110
} )
111
111
} )
112
112
113
+ describe ( 'getTunnelMode() if tunnelUrl is defined' , ( ) => {
114
+ const defaultOptions = {
115
+ useLocalhost : false ,
116
+ localhostPort : undefined ,
117
+ tunnelUrl : undefined ,
118
+ }
119
+
120
+ test ( 'returns AutoTunnel' , async ( ) => {
121
+ // Given
122
+ const localhostPort = 1234
123
+ vi . mocked ( getAvailableTCPPort ) . mockResolvedValue ( 1234 )
124
+
125
+ // When
126
+ const result = ( await getTunnelMode ( defaultOptions ) ) as AutoTunnel
127
+
128
+ // Then
129
+ expect ( result ) . toMatchObject ( { mode : 'auto' } )
130
+ } )
131
+ } )
132
+
133
+ describe ( 'getTunnelMode() if useLocalhost is false and tunnelUrl is a string' , ( ) => {
134
+ const defaultOptions = {
135
+ useLocalhost : false ,
136
+ localhostPort : undefined ,
137
+ tunnelUrl : 'https://my-tunnel-url.com' ,
138
+ }
139
+
140
+ test ( 'returns CustomTunnel' , async ( ) => {
141
+ // Given
142
+ const localhostPort = 1234
143
+ vi . mocked ( getAvailableTCPPort ) . mockResolvedValue ( 1234 )
144
+
145
+ // When
146
+ const result = ( await getTunnelMode ( defaultOptions ) ) as CustomTunnel
147
+
148
+ // Then
149
+ expect ( result ) . toMatchObject ( {
150
+ mode : 'custom' ,
151
+ url : defaultOptions . tunnelUrl ,
152
+ } )
153
+ } )
154
+ } )
155
+
113
156
describe ( 'getTunnelMode() if useLocalhost is true' , ( ) => {
114
157
const mockCertificate = {
115
158
keyContent : 'test-key-content' ,
@@ -147,6 +190,20 @@ describe('getTunnelMode() if useLocalhost is true', () => {
147
190
} )
148
191
} )
149
192
193
+ test ( 'throws when localhostPort is passed, but not available' , async ( ) => {
194
+ // Given
195
+ const localhostPort = 1234
196
+ vi . mocked ( getAvailableTCPPort ) . mockResolvedValue ( 4321 )
197
+
198
+ // Then
199
+ await expect (
200
+ getTunnelMode ( {
201
+ ...defaultOptions ,
202
+ localhostPort,
203
+ } ) ,
204
+ ) . rejects . toThrow ( )
205
+ } )
206
+
150
207
test ( 'returns ports.localhost when localhostPort is not passed' , async ( ) => {
151
208
// Given
152
209
vi . mocked ( getAvailableTCPPort ) . mockResolvedValue ( ports . localhost )
@@ -178,29 +235,11 @@ describe('getTunnelMode() if useLocalhost is true', () => {
178
235
expect ( mockOutput . info ( ) ) . toContain ( 'Localhost-based development is in developer preview' )
179
236
} )
180
237
181
- test ( 'Shows warning if requestedPort is not available' , async ( ) => {
182
- // Given
183
- const requestedPort = 3000
184
- const availablePort = 3001
185
- vi . mocked ( checkPortAvailability ) . mockResolvedValue ( false )
186
- vi . mocked ( getAvailableTCPPort ) . mockResolvedValue ( availablePort )
187
-
188
- // When
189
- const mockOutput = mockAndCaptureOutput ( )
190
- mockOutput . clear ( )
191
- const result = ( await getTunnelMode ( defaultOptions ) ) as NoTunnel
192
- await result . provideCertificate ( 'app-directory' )
193
-
194
- // Then
195
- expect ( result . port ) . toBe ( availablePort )
196
- expect ( mockOutput . warn ( ) ) . toContain ( 'A random port will be used for localhost' )
197
- } )
198
-
199
- test ( 'Shows warning if requestedPort is not passed and ports.localhost is not available' , async ( ) => {
238
+ test ( 'Renders warning if ports.localhost is not available' , async ( ) => {
200
239
// Given
201
- const availablePort = 3001
240
+ const availablePort = ports . localhost + 1
202
241
vi . mocked ( checkPortAvailability ) . mockResolvedValue ( false )
203
- vi . mocked ( getAvailableTCPPort ) . mockResolvedValue ( availablePort )
242
+ vi . mocked ( getAvailableTCPPort ) . mockResolvedValue ( ports . localhost + 1 )
204
243
205
244
// When
206
245
const mockOutput = mockAndCaptureOutput ( )
@@ -232,46 +271,3 @@ describe('getTunnelMode() if useLocalhost is true', () => {
232
271
} )
233
272
} )
234
273
} )
235
-
236
- describe ( 'getTunnelMode() if useLocalhost is false and tunnelUrl is a string' , ( ) => {
237
- const defaultOptions = {
238
- useLocalhost : false ,
239
- localhostPort : undefined ,
240
- tunnelUrl : 'https://my-tunnel-url.com' ,
241
- }
242
-
243
- test ( 'returns CustomTunnel' , async ( ) => {
244
- // Given
245
- const localhostPort = 1234
246
- vi . mocked ( getAvailableTCPPort ) . mockResolvedValue ( 1234 )
247
-
248
- // When
249
- const result = ( await getTunnelMode ( defaultOptions ) ) as CustomTunnel
250
-
251
- // Then
252
- expect ( result ) . toMatchObject ( {
253
- mode : 'custom' ,
254
- url : defaultOptions . tunnelUrl ,
255
- } )
256
- } )
257
- } )
258
-
259
- describe ( 'getTunnelMode() if useLocalhost is false and tunnelUrl is undefined' , ( ) => {
260
- const defaultOptions = {
261
- useLocalhost : false ,
262
- localhostPort : undefined ,
263
- tunnelUrl : undefined ,
264
- }
265
-
266
- test ( 'returns AutoTunnel' , async ( ) => {
267
- // Given
268
- const localhostPort = 1234
269
- vi . mocked ( getAvailableTCPPort ) . mockResolvedValue ( 1234 )
270
-
271
- // When
272
- const result = ( await getTunnelMode ( defaultOptions ) ) as AutoTunnel
273
-
274
- // Then
275
- expect ( result ) . toMatchObject ( { mode : 'auto' } )
276
- } )
277
- } )
0 commit comments