|
| 1 | +package skills |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func skipOnWindows(t *testing.T) { |
| 15 | + t.Helper() |
| 16 | + if runtime.GOOS == "windows" { |
| 17 | + t.Skip("skipping on windows") |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func TestExpandCommands(t *testing.T) { |
| 22 | + skipOnWindows(t) |
| 23 | + |
| 24 | + tests := []struct { |
| 25 | + name string |
| 26 | + content string |
| 27 | + want string |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "no patterns", |
| 31 | + content: "# My Skill\n\nJust regular markdown content.", |
| 32 | + want: "# My Skill\n\nJust regular markdown content.", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "simple echo", |
| 36 | + content: "Hello !`echo world`!", |
| 37 | + want: "Hello world!", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "multiple commands", |
| 41 | + content: "Name: !`echo alice`, Age: !`echo 30`", |
| 42 | + want: "Name: alice, Age: 30", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "multiline output", |
| 46 | + content: "Files:\n!`printf 'a.go\nb.go\nc.go\n'`\nEnd.", |
| 47 | + want: "Files:\na.go\nb.go\nc.go\nEnd.", |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "empty output", |
| 51 | + content: "Before !`true` after", |
| 52 | + want: "Before after", |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "pipes", |
| 56 | + content: "Count: !`printf 'a\nb\nc\n' | wc -l | tr -d ' '`", |
| 57 | + want: "Count: 3", |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "preserves regular backticks", |
| 61 | + content: "Use `echo hello` to print.\n\nCode: ```go\nfmt.Println()\n```", |
| 62 | + want: "Use `echo hello` to print.\n\nCode: ```go\nfmt.Println()\n```", |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + result := ExpandCommands(t.Context(), tt.content, t.TempDir()) |
| 69 | + assert.Equal(t, tt.want, result) |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestExpandCommands_WorkingDirectory(t *testing.T) { |
| 75 | + skipOnWindows(t) |
| 76 | + |
| 77 | + tmpDir := t.TempDir() |
| 78 | + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "test.txt"), []byte("hello"), 0o644)) |
| 79 | + |
| 80 | + result := ExpandCommands(t.Context(), "Content: !`cat test.txt`", tmpDir) |
| 81 | + assert.Equal(t, "Content: hello", result) |
| 82 | +} |
| 83 | + |
| 84 | +func TestExpandCommands_ScriptExecution(t *testing.T) { |
| 85 | + skipOnWindows(t) |
| 86 | + |
| 87 | + tmpDir := t.TempDir() |
| 88 | + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "info.sh"), []byte("#!/bin/sh\necho from-script"), 0o755)) |
| 89 | + |
| 90 | + result := ExpandCommands(t.Context(), "Output: !`./info.sh`", tmpDir) |
| 91 | + assert.Equal(t, "Output: from-script", result) |
| 92 | +} |
| 93 | + |
| 94 | +func TestExpandCommands_FailedCommand(t *testing.T) { |
| 95 | + skipOnWindows(t) |
| 96 | + |
| 97 | + result := ExpandCommands(t.Context(), "Before !`nonexistent_command_12345` after", t.TempDir()) |
| 98 | + assert.Contains(t, result, "Before ") |
| 99 | + assert.Contains(t, result, "[error executing `nonexistent_command_12345`:") |
| 100 | + assert.Contains(t, result, " after") |
| 101 | +} |
| 102 | + |
| 103 | +func TestExpandCommands_CancelledContext(t *testing.T) { |
| 104 | + skipOnWindows(t) |
| 105 | + |
| 106 | + ctx, cancel := context.WithCancel(t.Context()) |
| 107 | + cancel() |
| 108 | + |
| 109 | + result := ExpandCommands(ctx, "Result: !`echo hello`", t.TempDir()) |
| 110 | + assert.Contains(t, result, "[error executing `echo hello`:") |
| 111 | +} |
0 commit comments