11import { describe , expect , it } from "bun:test" ;
22import {
3+ execContainer ,
34 executeScriptInContainer ,
5+ findResourceInstance ,
6+ runContainer ,
47 runTerraformApply ,
58 runTerraformInit ,
69 testRequiredVariables ,
7- writeCoder ,
8- execContainer ,
10+ type TerraformState ,
911} from "../test" ;
1012
13+ /**
14+ * Helper function that executes the script in a container after setting up
15+ * the necessary environment for testing.
16+ */
17+ const executeScriptInContainerWithMockEnv = async (
18+ state : TerraformState ,
19+ image = "alpine" ,
20+ shell = "bash" ,
21+ extraSetup ?: string
22+ ) : Promise < {
23+ exitCode : number ;
24+ stdout : string [ ] ;
25+ stderr : string [ ] ;
26+ } > => {
27+ const instance = findResourceInstance ( state , "coder_script" ) ;
28+ const id = await runContainer ( image ) ;
29+
30+ // Set up the container with necessary tools and mocks
31+ const setupCommands = `
32+ # Install bash and other necessary packages
33+ apk add --no-cache bash
34+
35+ # Set up environment
36+ export HOME=/home/coder
37+
38+ # Create necessary directories
39+ mkdir -p /home/coder/bin
40+ mkdir -p /home/coder/.config
41+ mkdir -p /usr/bin
42+
43+ # Mock commands we need for the aider script
44+ echo '#!/bin/bash
45+ echo "Linux"' > /usr/bin/uname
46+ chmod +x /usr/bin/uname
47+
48+ # Mock command for checking if command exists
49+ echo '#!/bin/bash
50+ true' > /usr/bin/command
51+ chmod +x /usr/bin/command
52+
53+ # Mock sudo to just run the command without sudo
54+ echo '#!/bin/bash
55+ shift
56+ "$@"' > /usr/bin/sudo
57+ chmod +x /usr/bin/sudo
58+
59+ # Set up apt-get mock
60+ echo '#!/bin/bash
61+ echo "apt-get $@"' > /usr/bin/apt-get
62+ chmod +x /usr/bin/apt-get
63+
64+ # Set up dnf mock
65+ echo '#!/bin/bash
66+ echo "dnf $@"' > /usr/bin/dnf
67+ chmod +x /usr/bin/dnf
68+
69+ # Set up aider mock
70+ echo '#!/bin/bash
71+ echo "Aider mock started"' > /home/coder/bin/aider
72+ chmod +x /home/coder/bin/aider
73+ export PATH=/home/coder/bin:$PATH
74+
75+ # Set up screen mock
76+ echo '#!/bin/bash
77+ echo "screen mock $@"' > /usr/bin/screen
78+ chmod +x /usr/bin/screen
79+
80+ # Set up curl mock
81+ echo '#!/bin/bash
82+ echo "curl mock $@"' > /usr/bin/curl
83+ chmod +x /usr/bin/curl
84+
85+ # Set up base64 mock
86+ echo '#!/bin/bash
87+ if [ "$1" = "-d" ]; then
88+ cat
89+ else
90+ echo "base64 $@"
91+ fi' > /usr/bin/base64
92+ chmod +x /usr/bin/base64
93+
94+ # Create empty aider log file
95+ touch /home/coder/.aider.log
96+
97+ # Run any extra setup commands if provided
98+ ${ extraSetup || "" }
99+ ` ;
100+
101+ await execContainer ( id , [ "sh" , "-c" , setupCommands ] ) ;
102+
103+ // Now run the actual script
104+ const resp = await execContainer ( id , [ shell , "-c" , instance . script ] ) ;
105+
106+ const stdout = resp . stdout . trim ( ) . split ( "\n" ) ;
107+ const stderr = resp . stderr . trim ( ) . split ( "\n" ) ;
108+
109+ return {
110+ exitCode : resp . exitCode ,
111+ stdout,
112+ stderr,
113+ } ;
114+ } ;
115+
11116describe ( "aider" , async ( ) => {
12117 await runTerraformInit ( import . meta. dir ) ;
13118
@@ -20,17 +125,7 @@ describe("aider", async () => {
20125 agent_id : "foo" ,
21126 } ) ;
22127
23- // Execute the script with mock setup
24- const output = await executeScriptInContainer (
25- state ,
26- "alpine" ,
27- "sh" ,
28- // Setup needed mocks to make the script run without errors
29- "mkdir -p /home/coder/bin && echo '#!/bin/sh\necho \"Aider mock started\"' > /home/coder/bin/aider && chmod +x /home/coder/bin/aider && " +
30- "mkdir -p /usr/bin && echo '#!/bin/sh\necho \"Linux\"' > /usr/bin/uname && chmod +x /usr/bin/uname && " +
31- "mkdir -p /usr/bin && echo '#!/bin/sh\necho \"screen mock $@\"' > /usr/bin/screen && chmod +x /usr/bin/screen && " +
32- "touch /home/coder/.aider.log"
33- ) ;
128+ const output = await executeScriptInContainerWithMockEnv ( state ) ;
34129
35130 // Verify expected outputs
36131 expect ( output . exitCode ) . toBe ( 0 ) ;
@@ -47,16 +142,16 @@ describe("aider", async () => {
47142 use_screen : false ,
48143 } ) ;
49144
50- // Execute the script with mock setup
51- const output = await executeScriptInContainer (
145+ const output = await executeScriptInContainerWithMockEnv (
52146 state ,
53147 "alpine" ,
54- "sh" ,
55- // Setup needed mocks to make the script run without errors
56- "mkdir -p /home/coder/bin && echo '#!/bin/sh\necho \"Aider mock started\"' > /home/coder/bin/aider && chmod +x /home/coder/bin/aider && " +
57- "mkdir -p /usr/bin && echo '#!/bin/sh\necho \"Linux\"' > /usr/bin/uname && chmod +x /usr/bin/uname && " +
58- "mkdir -p /usr/bin && echo '#!/bin/sh\necho \"tmux mock $@\"' > /usr/bin/tmux && chmod +x /usr/bin/tmux && " +
59- "touch /home/coder/.aider.log"
148+ "bash" ,
149+ `
150+ # Set up tmux mock instead of screen
151+ echo '#!/bin/bash
152+ echo "tmux mock $@"' > /usr/bin/tmux
153+ chmod +x /usr/bin/tmux
154+ `
60155 ) ;
61156
62157 // Verify expected outputs
@@ -72,17 +167,16 @@ describe("aider", async () => {
72167 experiment_report_tasks : true ,
73168 } ) ;
74169
75- // Execute the script with mock setup
76- const output = await executeScriptInContainer (
170+ const output = await executeScriptInContainerWithMockEnv (
77171 state ,
78172 "alpine" ,
79- "sh " ,
80- // Setup needed mocks to make the script run without errors
81- "mkdir -p /usr/bin && echo '#!/bin/sh\necho \"coder mock $@\"' > /usr/bin/ coder && chmod +x /usr/bin/coder && " +
82- "mkdir -p /home/coder/bin && echo '#!/bin/sh\necho \"Aider mock started\"' > /home/coder/bin/aider && chmod +x /home/coder/bin/aider && " +
83- "mkdir -p /usr/bin && echo '#!/bin/sh\necho \"Linux\" ' > /usr/bin/uname && chmod +x /usr/bin/uname && " +
84- "mkdir -p /usr/bin && echo '#!/bin/sh\necho \"screen mock $@\"' > /usr/bin/screen && chmod +x /usr/bin/screen && " +
85- "touch /home/coder/.aider.log"
173+ "bash " ,
174+ `
175+ # Set up coder mock
176+ echo '#!/bin/bash
177+ echo "coder mock $@" ' > /usr/bin/coder
178+ chmod +x /usr/bin/coder
179+ `
86180 ) ;
87181
88182 // Verify expected outputs
@@ -99,17 +193,7 @@ describe("aider", async () => {
99193 experiment_post_install_script : "echo 'Post-install script executed'" ,
100194 } ) ;
101195
102- // Execute the script with mock setup
103- const output = await executeScriptInContainer (
104- state ,
105- "alpine" ,
106- "sh" ,
107- // Setup needed mocks to make the script run without errors
108- "mkdir -p /home/coder/bin && echo '#!/bin/sh\necho \"Aider mock started\"' > /home/coder/bin/aider && chmod +x /home/coder/bin/aider && " +
109- "mkdir -p /usr/bin && echo '#!/bin/sh\necho \"Linux\"' > /usr/bin/uname && chmod +x /usr/bin/uname && " +
110- "mkdir -p /usr/bin && echo '#!/bin/sh\necho \"screen mock $@\"' > /usr/bin/screen && chmod +x /usr/bin/screen && " +
111- "touch /home/coder/.aider.log"
112- ) ;
196+ const output = await executeScriptInContainerWithMockEnv ( state ) ;
113197
114198 // Verify expected outputs
115199 expect ( output . exitCode ) . toBe ( 0 ) ;
0 commit comments