-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathcopilot_centralization_optimizer_workflow_test.go
More file actions
134 lines (111 loc) · 3.86 KB
/
Copy pathcopilot_centralization_optimizer_workflow_test.go
File metadata and controls
134 lines (111 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//go:build !integration
package workflow
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/goccy/go-yaml"
)
type workflowFrontmatter struct {
Steps []struct {
Name string `yaml:"name"`
Run string `yaml:"run"`
} `yaml:"steps"`
}
func readCopilotCentralizationOptimizerSource(t *testing.T) string {
t.Helper()
sourceContent, err := os.ReadFile("../../.github/workflows/copilot-centralization-optimizer.md")
if err != nil {
t.Fatalf("failed to read workflow source: %v", err)
}
return string(sourceContent)
}
func assertContainsAll(t *testing.T, text string, fragments []string) {
t.Helper()
for _, fragment := range fragments {
if !strings.Contains(text, fragment) {
t.Fatalf("expected content to contain %q", fragment)
}
}
}
func extractCollectAgentTaskDataRunScript(t *testing.T, sourceText string) string {
t.Helper()
parts := strings.SplitN(sourceText, "\n---\n", 2)
if len(parts) != 2 {
t.Fatal("expected workflow source to contain frontmatter separator")
}
var frontmatter workflowFrontmatter
if err := yaml.Unmarshal([]byte(parts[0]), &frontmatter); err != nil {
t.Fatalf("failed to parse workflow frontmatter: %v", err)
}
for _, step := range frontmatter.Steps {
if step.Name == "Collect agent task data" {
return step.Run
}
}
t.Fatal("failed to find Collect agent task data step")
return ""
}
func TestCopilotCentralizationOptimizerWorkflowHandlesTaskFetchFailures(t *testing.T) {
sourceText := readCopilotCentralizationOptimizerSource(t)
assertContainsAll(t, sourceText, []string{
`_task_ids=$(mktemp)`,
`if gh api --paginate \`,
`done < "$_task_ids" \`,
`echo "Agent tasks API request failed; proceeding with empty dataset" >&2`,
`jq -s '.' /tmp/gh-aw/data/task-summaries.jsonl > /tmp/gh-aw/data/task-summaries.json`,
})
}
func TestCopilotCentralizationOptimizerCompiledWorkflowHandlesTaskFetchFailures(t *testing.T) {
lockContent, err := os.ReadFile("../../.github/workflows/copilot-centralization-optimizer.lock.yml")
if err != nil {
t.Fatalf("failed to read compiled workflow: %v", err)
}
lockText := string(lockContent)
assertContainsAll(t, lockText, []string{
`_task_ids=$(mktemp)`,
`if gh api --paginate \\\n`,
`done < \"$_task_ids\" \\\n`,
`echo \"Agent tasks API request failed; proceeding with empty dataset\" >&2`,
`jq -s '.' /tmp/gh-aw/data/task-summaries.jsonl > /tmp/gh-aw/data/task-summaries.json`,
})
}
func TestCopilotCentralizationOptimizerTaskFetchFailureStillWritesJSON(t *testing.T) {
sourceText := readCopilotCentralizationOptimizerSource(t)
script := extractCollectAgentTaskDataRunScript(t, sourceText)
tempDir := t.TempDir()
dataDir := filepath.Join(tempDir, "data")
script = strings.ReplaceAll(script, "/tmp/gh-aw/data", dataDir)
fakeBinDir := filepath.Join(tempDir, "bin")
if err := os.MkdirAll(fakeBinDir, 0o755); err != nil {
t.Fatalf("failed to create fake bin dir: %v", err)
}
fakeGHPath := filepath.Join(fakeBinDir, "gh")
if err := os.WriteFile(fakeGHPath, []byte("#!/bin/sh\nexit 1\n"), 0o755); err != nil {
t.Fatalf("failed to write fake gh: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "bash", "-c", script)
cmd.Env = append(os.Environ(),
"PATH="+fakeBinDir+string(os.PathListSeparator)+os.Getenv("PATH"),
"REPO=github/gh-aw",
"TASK_LOOKBACK_DAYS=30",
"TASK_LIMIT=300",
)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("expected script to tolerate gh failures, got %v:\n%s", err, output)
}
taskSummariesJSON, err := os.ReadFile(filepath.Join(dataDir, "task-summaries.json"))
if err != nil {
t.Fatalf("expected task-summaries.json to be written: %v", err)
}
if strings.TrimSpace(string(taskSummariesJSON)) != "[]" {
t.Fatalf("expected task-summaries.json to contain an empty JSON array, got %q", taskSummariesJSON)
}
}