1- import { isDockerfile } from "@cloudflare/containers-shared" ;
1+ import { mkdirSync , writeFileSync } from "fs" ;
2+ import { getCloudflareContainerRegistry } from "@cloudflare/containers-shared" ;
23import { vi } from "vitest" ;
34import { getNormalizedContainerOptions } from "../../containers/config" ;
45import { UserError } from "../../errors" ;
6+ import { mockAccountId , mockApiToken } from "../helpers/mock-account-id" ;
7+ import { runInTempDir } from "../helpers/run-in-tmp" ;
58import type { Config } from "../../config" ;
69
7- // Mock dependencies using vi.hoisted
8- vi . mock ( "@cloudflare/containers-shared" ) ;
9-
10- const mockIsDockerfile = vi . mocked ( isDockerfile ) ;
11-
1210describe ( "getNormalizedContainerOptions" , ( ) => {
11+ runInTempDir ( ) ;
1312 beforeEach ( ( ) => {
1413 vi . clearAllMocks ( ) ;
1514 } ) ;
@@ -99,18 +98,19 @@ describe("getNormalizedContainerOptions", () => {
9998 await expect ( getNormalizedContainerOptions ( config ) ) . rejects . toThrow (
10099 UserError
101100 ) ;
102- await expect ( getNormalizedContainerOptions ( config ) ) . rejects . toThrow (
103- "The container class_name TestContainer does not match any durable object class_name defined in your Wrangler config file"
101+ await expect (
102+ getNormalizedContainerOptions ( config )
103+ ) . rejects . toThrowErrorMatchingInlineSnapshot (
104+ `[Error: The container test-container is referencing the durable object TestContainer, which appears to be defined on the other-script Worker instead (via the 'script_name' field). You cannot configure a container on a Durable Object that is defined in another Worker.]`
104105 ) ;
105106 } ) ;
106107
107108 it ( "should normalize and set defaults for container with dockerfile" , async ( ) => {
108- mockIsDockerfile . mockReturnValue ( true ) ;
109+ writeFileSync ( "Dockerfile" , "FROM scratch" ) ;
109110
110111 const config : Config = {
111112 name : "test-worker" ,
112- configPath : "/test/wrangler.toml" ,
113- userConfigPath : "/test/wrangler.toml" ,
113+ configPath : "./wrangler.toml" ,
114114 topLevelName : "test-worker" ,
115115 containers : [
116116 {
@@ -132,24 +132,22 @@ describe("getNormalizedContainerOptions", () => {
132132 const result = await getNormalizedContainerOptions ( config ) ;
133133
134134 expect ( result ) . toHaveLength ( 1 ) ;
135- expect ( result [ 0 ] ) . toEqual ( {
135+ expect ( result [ 0 ] ) . toMatchObject ( {
136136 name : "test-container" ,
137137 class_name : "TestContainer" ,
138138 max_instances : 0 ,
139139 scheduling_policy : "default" ,
140140 rollout_step_percentage : 25 ,
141141 rollout_kind : "full_auto" ,
142142 instance_type : "dev" ,
143- dockerfile : "/test/ Dockerfile" ,
144- image_build_context : "/test" ,
143+ dockerfile : expect . stringMatching ( / [ / \\ ] D o c k e r f i l e $ / ) ,
144+ image_build_context : expect . stringMatching ( / [ / \\ ] [ ^ / \\ ] * $ / ) ,
145145 image_vars : undefined ,
146146 constraints : undefined ,
147147 } ) ;
148148 } ) ;
149149
150150 it ( "should normalize and set defaults for container with registry image" , async ( ) => {
151- mockIsDockerfile . mockReturnValue ( false ) ;
152-
153151 const config : Config = {
154152 name : "test-worker" ,
155153 configPath : "/test/wrangler.toml" ,
@@ -174,22 +172,20 @@ describe("getNormalizedContainerOptions", () => {
174172 const result = await getNormalizedContainerOptions ( config ) ;
175173
176174 expect ( result ) . toHaveLength ( 1 ) ;
177- expect ( result [ 0 ] ) . toEqual ( {
175+ expect ( result [ 0 ] ) . toMatchObject ( {
178176 name : "test-container" ,
179177 class_name : "TestContainer" ,
180178 max_instances : 0 ,
181179 scheduling_policy : "default" ,
182180 rollout_step_percentage : 25 ,
183181 rollout_kind : "full_auto" ,
184182 instance_type : "dev" ,
185- registry_link : "registry.example.com/test:latest" ,
186183 constraints : undefined ,
184+ image_uri : "registry.cloudflare.com/some-account-id/test:latest" ,
187185 } ) ;
188186 } ) ;
189187
190188 it ( "should handle custom limit configuration" , async ( ) => {
191- mockIsDockerfile . mockReturnValue ( false ) ;
192-
193189 const config : Config = {
194190 name : "test-worker" ,
195191 configPath : "/test/wrangler.toml" ,
@@ -219,7 +215,7 @@ describe("getNormalizedContainerOptions", () => {
219215
220216 const result = await getNormalizedContainerOptions ( config ) ;
221217
222- expect ( result [ 0 ] ) . toEqual ( {
218+ expect ( result [ 0 ] ) . toMatchObject ( {
223219 name : "test-container" ,
224220 class_name : "TestContainer" ,
225221 max_instances : 0 ,
@@ -229,14 +225,12 @@ describe("getNormalizedContainerOptions", () => {
229225 disk_bytes : 5_000_000_000 , // 5000 MB in bytes
230226 memory_mib : 1024 ,
231227 vcpu : 2 ,
232- registry_link : "registry.example.com/test:latest" ,
233228 constraints : undefined ,
229+ image_uri : "registry.example.com/test:latest" ,
234230 } ) ;
235231 } ) ;
236232
237233 it ( "should handle instance type configuration" , async ( ) => {
238- mockIsDockerfile . mockReturnValue ( false ) ;
239-
240234 const config : Config = {
241235 name : "test-worker" ,
242236 configPath : "/test/wrangler.toml" ,
@@ -262,22 +256,20 @@ describe("getNormalizedContainerOptions", () => {
262256
263257 const result = await getNormalizedContainerOptions ( config ) ;
264258
265- expect ( result [ 0 ] ) . toEqual ( {
259+ expect ( result [ 0 ] ) . toMatchObject ( {
266260 name : "test-container" ,
267261 class_name : "TestContainer" ,
268262 max_instances : 0 ,
269263 scheduling_policy : "default" ,
270264 rollout_step_percentage : 25 ,
271265 rollout_kind : "full_auto" ,
272266 instance_type : "standard" ,
273- registry_link : "registry.example.com/test:latest" ,
274267 constraints : undefined ,
268+ image_uri : "registry.example.com/test:latest" ,
275269 } ) ;
276270 } ) ;
277271
278272 it ( "should handle all custom configuration options" , async ( ) => {
279- mockIsDockerfile . mockReturnValue ( false ) ;
280-
281273 const config : Config = {
282274 name : "test-worker" ,
283275 configPath : "/test/wrangler.toml" ,
@@ -320,7 +312,7 @@ describe("getNormalizedContainerOptions", () => {
320312 rollout_step_percentage : 50 ,
321313 rollout_kind : "full_manual" ,
322314 instance_type : "basic" ,
323- registry_link : "registry.example.com/test:latest" ,
315+ image_uri : "registry.example.com/test:latest" ,
324316 constraints : {
325317 regions : [ "us-east-1" , "us-west-2" ] ,
326318 cities : [ "NYC" , "SF" ] ,
@@ -330,17 +322,17 @@ describe("getNormalizedContainerOptions", () => {
330322 } ) ;
331323
332324 it ( "should handle dockerfile with default build context" , async ( ) => {
333- mockIsDockerfile . mockReturnValue ( true ) ;
325+ mkdirSync ( "nested" , { recursive : true } ) ;
326+ writeFileSync ( "nested/Dockerfile" , "FROM scratch" ) ;
334327
335328 const config : Config = {
336329 name : "test-worker" ,
337- configPath : "/test/wrangler.toml" ,
338- userConfigPath : "/test/wrangler.toml" ,
330+ configPath : "./wrangler.toml" ,
339331 topLevelName : "test-worker" ,
340332 containers : [
341333 {
342334 class_name : "TestContainer" ,
343- image : "./path/to /Dockerfile" ,
335+ image : "./nested /Dockerfile" ,
344336 name : "test-container" ,
345337 } ,
346338 ] ,
@@ -356,24 +348,22 @@ describe("getNormalizedContainerOptions", () => {
356348
357349 const result = await getNormalizedContainerOptions ( config ) ;
358350
359- expect ( result [ 0 ] ) . toEqual ( {
351+ expect ( result [ 0 ] ) . toMatchObject ( {
360352 name : "test-container" ,
361353 class_name : "TestContainer" ,
362354 max_instances : 0 ,
363355 scheduling_policy : "default" ,
364356 rollout_step_percentage : 25 ,
365357 rollout_kind : "full_auto" ,
366358 instance_type : "dev" ,
367- dockerfile : "/test/path/to/ Dockerfile" ,
368- image_build_context : "/test/path/to" ,
359+ dockerfile : expect . stringMatching ( / [ / \\ ] n e s t e d [ / \\ ] D o c k e r f i l e $ / ) ,
360+ image_build_context : expect . stringMatching ( / [ / \\ ] n e s t e d $ / ) ,
369361 image_vars : undefined ,
370362 constraints : undefined ,
371363 } ) ;
372364 } ) ;
373365
374366 it ( "should handle multiple containers" , async ( ) => {
375- mockIsDockerfile . mockReturnValue ( false ) ;
376-
377367 const config : Config = {
378368 name : "test-worker" ,
379369 configPath : "/test/wrangler.toml" ,
@@ -413,8 +403,7 @@ describe("getNormalizedContainerOptions", () => {
413403 } ) ;
414404
415405 it ( "should handle config with no configPath" , async ( ) => {
416- mockIsDockerfile . mockReturnValue ( true ) ;
417-
406+ writeFileSync ( "Dockerfile" , "FROM scratch" ) ;
418407 const config : Config = {
419408 name : "test-worker" ,
420409 configPath : undefined ,
@@ -439,9 +428,9 @@ describe("getNormalizedContainerOptions", () => {
439428
440429 const result = await getNormalizedContainerOptions ( config ) ;
441430
442- // Check that it has dockerfile properties (not registry_link )
431+ // Check that it has dockerfile properties (not image_uri )
443432 expect ( result [ 0 ] ) . toHaveProperty ( "dockerfile" ) ;
444433 expect ( result [ 0 ] ) . toHaveProperty ( "image_build_context" ) ;
445- expect ( result [ 0 ] ) . not . toHaveProperty ( "registry_link " ) ;
434+ expect ( result [ 0 ] ) . not . toHaveProperty ( "image_uri " ) ;
446435 } ) ;
447436} ) ;
0 commit comments