@@ -1230,3 +1230,69 @@ This rule is from a remote directory.
12301230 t .Errorf ("task not found in stdout. Output:\n %s" , output )
12311231 }
12321232}
1233+
1234+ // TestSingleExpansion verifies that content is expanded only once in the full flow
1235+ func TestSingleExpansion (t * testing.T ) {
1236+ dirs := setupTestDirs (t )
1237+
1238+ // Create a task that uses a parameter with expansion syntax
1239+ taskFile := filepath .Join (dirs .tasksDir , "test-expand.md" )
1240+ taskContent := `Task with parameter: ${param1}
1241+
1242+ And a value that looks like expansion syntax but should not be expanded: ${"nested"}`
1243+ if err := os .WriteFile (taskFile , []byte (taskContent ), 0644 ); err != nil {
1244+ t .Fatalf ("failed to create task file: %v" , err )
1245+ }
1246+
1247+ // Run with param1 set to a value that contains expansion syntax
1248+ output := runTool (t , "-C" , dirs .tmpDir , "-p" , "param1=!`echo hello`" , "test-expand" )
1249+
1250+ // The param1 should be replaced with the literal string "!`echo hello`"
1251+ // It should NOT be expanded again (that would execute the command)
1252+ if ! strings .Contains (output , "!`echo hello`" ) {
1253+ t .Errorf ("Expected param1 to be replaced with literal value, got: %s" , output )
1254+ }
1255+
1256+ // Verify "hello" is not in output (which would indicate the command was executed)
1257+ // Note: there may be other "hello" strings, so check for the specific context
1258+ if strings .Contains (output , "Task with parameter: hello" ) {
1259+ t .Errorf ("Parameter value was re-expanded (command was executed), got: %s" , output )
1260+ }
1261+ }
1262+
1263+ // TestCommandExpansionOnce verifies that command files are expanded only once
1264+ func TestCommandExpansionOnce (t * testing.T ) {
1265+ dirs := setupTestDirs (t )
1266+ commandsDir := filepath .Join (dirs .tmpDir , ".agents" , "commands" )
1267+ if err := os .MkdirAll (commandsDir , 0o755 ); err != nil {
1268+ t .Fatalf ("failed to create commands dir: %v" , err )
1269+ }
1270+
1271+ // Create a command file with a parameter
1272+ commandFile := filepath .Join (commandsDir , "test-cmd.md" )
1273+ commandContent := `Command param: ${cmd_param}`
1274+ if err := os .WriteFile (commandFile , []byte (commandContent ), 0644 ); err != nil {
1275+ t .Fatalf ("failed to create command file: %v" , err )
1276+ }
1277+
1278+ // Create a task that calls the command with a param containing expansion syntax
1279+ taskFile := filepath .Join (dirs .tasksDir , "test-cmd-task.md" )
1280+ taskContent := `/test-cmd cmd_param="!` + "`echo injected`" + `"`
1281+ if err := os .WriteFile (taskFile , []byte (taskContent ), 0644 ); err != nil {
1282+ t .Fatalf ("failed to create task file: %v" , err )
1283+ }
1284+
1285+ // Run the task
1286+ output := runTool (t , "-C" , dirs .tmpDir , "test-cmd-task" )
1287+
1288+ // The command parameter should be replaced with the literal string "!`echo injected`"
1289+ // It should NOT be expanded again (that would execute the command)
1290+ if ! strings .Contains (output , "!`echo injected`" ) {
1291+ t .Errorf ("Expected command param to be replaced with literal value, got: %s" , output )
1292+ }
1293+
1294+ // Verify "injected" is not in output (which would indicate the command was executed)
1295+ if strings .Contains (output , "Command param: injected" ) {
1296+ t .Errorf ("Command parameter value was re-expanded (command was executed), got: %s" , output )
1297+ }
1298+ }
0 commit comments