|
| 1 | +//go:build !integration |
| 2 | + |
| 3 | +package cli |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/github/gh-aw/pkg/workflow" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCopilotWebFetchRemovalCodemod_Metadata(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + codemod := getCopilotWebFetchRemovalCodemod() |
| 19 | + assert.Equal(t, "copilot-web-fetch-removal", codemod.ID) |
| 20 | + assert.NotEmpty(t, codemod.Name) |
| 21 | + assert.NotEmpty(t, codemod.Description) |
| 22 | + assert.Equal(t, "1.0.0", codemod.IntroducedIn) |
| 23 | + require.NotNil(t, codemod.Apply) |
| 24 | +} |
| 25 | + |
| 26 | +func TestCopilotWebFetchRemovalCodemod_Apply(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + |
| 29 | + content := `--- |
| 30 | +engine: |
| 31 | + id: copilot |
| 32 | +tools: |
| 33 | + web-fetch: true # native fetch is unavailable |
| 34 | + bash: ["git"] |
| 35 | +--- |
| 36 | +
|
| 37 | +# Keep this body intact. |
| 38 | +` |
| 39 | + frontmatter := map[string]any{ |
| 40 | + "engine": map[string]any{"id": "copilot"}, |
| 41 | + "tools": map[string]any{"web-fetch": true, "bash": []any{"git"}}, |
| 42 | + } |
| 43 | + |
| 44 | + codemod := getCopilotWebFetchRemovalCodemod() |
| 45 | + updated, applied, err := codemod.Apply(content, frontmatter) |
| 46 | + require.NoError(t, err) |
| 47 | + require.True(t, applied) |
| 48 | + assert.Equal(t, `--- |
| 49 | +engine: |
| 50 | + id: copilot |
| 51 | +tools: |
| 52 | + bash: ["git"] |
| 53 | +--- |
| 54 | +
|
| 55 | +# Keep this body intact. |
| 56 | +`, updated) |
| 57 | + |
| 58 | + updatedAgain, appliedAgain, err := codemod.Apply(updated, frontmatter) |
| 59 | + require.NoError(t, err) |
| 60 | + assert.False(t, appliedAgain) |
| 61 | + assert.Equal(t, updated, updatedAgain) |
| 62 | +} |
| 63 | + |
| 64 | +func TestCopilotWebFetchRemovalCodemod_CompatibleInputsAreUnchanged(t *testing.T) { |
| 65 | + t.Parallel() |
| 66 | + |
| 67 | + codemod := getCopilotWebFetchRemovalCodemod() |
| 68 | + tests := []struct { |
| 69 | + name string |
| 70 | + frontmatter map[string]any |
| 71 | + content string |
| 72 | + }{ |
| 73 | + { |
| 74 | + name: "Copilot SDK custom fetch", |
| 75 | + frontmatter: map[string]any{ |
| 76 | + "engine": map[string]any{"id": "copilot", "copilot-sdk": true}, |
| 77 | + "tools": map[string]any{"web-fetch": true}, |
| 78 | + }, |
| 79 | + content: "---\nengine:\n id: copilot\n copilot-sdk: true\ntools:\n web-fetch:\n---\n", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "other engine", |
| 83 | + frontmatter: map[string]any{"engine": "codex", "tools": map[string]any{"web-fetch": true}}, |
| 84 | + content: "---\nengine: codex\ntools:\n web-fetch:\n---\n", |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "explicitly disabled", |
| 88 | + frontmatter: map[string]any{"engine": "copilot", "tools": map[string]any{"web-fetch": false}}, |
| 89 | + content: "---\nengine: copilot\ntools:\n web-fetch: false\n---\n", |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "no web-fetch setting", |
| 93 | + frontmatter: map[string]any{"engine": "copilot", "tools": map[string]any{"bash": true}}, |
| 94 | + content: "---\nengine: copilot\ntools:\n bash: true\n---\n", |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + for _, tt := range tests { |
| 99 | + t.Run(tt.name, func(t *testing.T) { |
| 100 | + t.Parallel() |
| 101 | + updated, applied, err := codemod.Apply(tt.content, tt.frontmatter) |
| 102 | + require.NoError(t, err) |
| 103 | + assert.False(t, applied) |
| 104 | + assert.Equal(t, tt.content, updated) |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestCopilotWebFetchRemovalCodemod_FixesStrictCompilation(t *testing.T) { |
| 110 | + t.Parallel() |
| 111 | + |
| 112 | + content := `--- |
| 113 | +on: workflow_dispatch |
| 114 | +strict: true |
| 115 | +engine: |
| 116 | + id: copilot |
| 117 | +tools: |
| 118 | + web-fetch: |
| 119 | +--- |
| 120 | +Fetch content. |
| 121 | +` |
| 122 | + compiler := workflow.NewCompiler() |
| 123 | + _, err := compiler.ParseWorkflowString(content, "copilot-web-fetch.md") |
| 124 | + require.ErrorContains(t, err, "Copilot's native 'web-fetch' tool is unavailable") |
| 125 | + |
| 126 | + path := filepath.Join(t.TempDir(), "copilot-web-fetch.md") |
| 127 | + require.NoError(t, os.WriteFile(path, []byte(content), 0o600)) |
| 128 | + fixed, _, err := processWorkflowFileWithInfo(path, GetAllCodemods(), true, false) |
| 129 | + require.NoError(t, err) |
| 130 | + require.True(t, fixed) |
| 131 | + |
| 132 | + updatedBytes, err := os.ReadFile(path) |
| 133 | + require.NoError(t, err) |
| 134 | + updated := string(updatedBytes) |
| 135 | + compiler = workflow.NewCompiler() |
| 136 | + data, err := compiler.ParseWorkflowString(updated, "copilot-web-fetch.md") |
| 137 | + require.NoError(t, err) |
| 138 | + require.NoError(t, compiler.CompileWorkflowData(data, filepath.Join(t.TempDir(), "copilot-web-fetch.md"))) |
| 139 | +} |
0 commit comments