core/plan/command_test.go
core/plan/command_test.goBrowse 1970 files
787 tokens
2,999 bytes
Token encoding: o200k_base
Snapshot 21a254f
← Back to SKILL.md
1package plan2 3import (4 "encoding/json"5 "testing"6 7 "github.com/stretchr/testify/require"8)9 10func TestCommandMarshalUnmarshal(t *testing.T) {11 tests := []struct {12 name string13 command Command14 expectedJSON string15 unmarshalString string16 }{17 // Exec18 {19 name: "exec command without custom name",20 command: NewExecShellCommand("echo hello", ExecOptions{CustomName: "echo hello"}),21 expectedJSON: `{"cmd":"sh -c 'echo hello'","customName":"echo hello"}`,22 unmarshalString: "echo hello",23 },24 {25 name: "exec command with custom name",26 command: NewExecShellCommand("echo hello", ExecOptions{CustomName: "Say Hello"}),27 expectedJSON: `{"cmd":"sh -c 'echo hello'","customName":"Say Hello"}`,28 unmarshalString: "RUN#Say Hello:echo hello",29 },30 31 // Path32 {33 name: "path command",34 command: NewPathCommand("/usr/local/bin"),35 expectedJSON: `{"path":"/usr/local/bin"}`,36 unmarshalString: "PATH:/usr/local/bin",37 },38 39 // Copy40 {41 name: "copy command",42 command: NewCopyCommand("src.txt", "dst.txt"),43 expectedJSON: `{"src":"src.txt","dest":"dst.txt"}`,44 unmarshalString: "COPY:src.txt dst.txt",45 },46 47 // File48 {49 name: "file command without custom name",50 command: NewFileCommand("/etc/conf", "config.yaml"),51 expectedJSON: `{"path":"/etc/conf","name":"config.yaml"}`,52 unmarshalString: "FILE:/etc/conf config.yaml",53 },54 {55 name: "file command with custom name",56 command: NewFileCommand("/etc/conf", "config.yaml", FileOptions{CustomName: "Config File"}),57 expectedJSON: `{"path":"/etc/conf","name":"config.yaml","customName":"Config File"}`,58 unmarshalString: "FILE#Config File:/etc/conf config.yaml",59 },60 }61 62 for _, tt := range tests {63 t.Run(tt.name, func(t *testing.T) {64 // Test marshalling to JSON object65 data, err := json.Marshal(tt.command)66 require.NoError(t, err, "failed to marshal command")67 require.Equal(t, string(data), tt.expectedJSON, "marshal result")68 69 // Test unmarshalling from JSON object70 cmd, err := UnmarshalCommand([]byte(tt.expectedJSON))71 require.NoError(t, err, "failed to unmarshal JSON command")72 73 // Marshal again to verify it produces the same result74 roundTrip, err := json.Marshal(cmd)75 require.NoError(t, err, "failed to marshal unmarshalled command")76 require.Equal(t, string(roundTrip), tt.expectedJSON, "round-trip JSON result")77 78 // Test unmarshalling from string format79 if tt.unmarshalString != "" {80 cmd, err = UnmarshalCommand([]byte(tt.unmarshalString))81 require.NoError(t, err, "failed to unmarshal string command")82 83 // Marshal to JSON to verify it produces the same object84 roundTrip, err = json.Marshal(cmd)85 require.NoError(t, err, "failed to marshal string-unmarshalled command")86 require.Equal(t, string(roundTrip), tt.expectedJSON, "string unmarshal to JSON result")87 88 }89 })90 }91}92