|
| 1 | +//go:build !integration |
| 2 | + |
| 3 | +package cli |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// TestWorkflowRunPathFieldUnmarshal verifies that the "path" key returned by |
| 16 | +// "gh run list --json" is correctly bridged to WorkflowRun.WorkflowPath during |
| 17 | +// unmarshaling. The gh CLI uses "path" but WorkflowRun serialises the field as |
| 18 | +// "workflowPath" for backward compatibility, so a helper struct is used at the |
| 19 | +// unmarshal site. |
| 20 | +// |
| 21 | +// Regression: commit 61cc2d7ac (Jan 4 2026) silently dropped "path" from the |
| 22 | +// gh run list --json field list, causing WorkflowPath to always be "" and the |
| 23 | +// .lock.yml filter in fetchWorkflowRuns to discard every run. |
| 24 | +func TestWorkflowRunPathFieldUnmarshal(t *testing.T) { |
| 25 | + // Simulate a single row of "gh run list --json path,workflowName,..." |
| 26 | + rawJSON := `[ |
| 27 | + { |
| 28 | + "databaseId": 42, |
| 29 | + "workflowName": "My Workflow", |
| 30 | + "path": ".github/workflows/my-workflow.lock.yml", |
| 31 | + "status": "completed", |
| 32 | + "conclusion": "success", |
| 33 | + "createdAt": "2026-01-01T00:00:00Z", |
| 34 | + "startedAt": "2026-01-01T00:00:01Z", |
| 35 | + "updatedAt": "2026-01-01T00:01:00Z" |
| 36 | + } |
| 37 | + ]` |
| 38 | + |
| 39 | + var rawRuns []struct { |
| 40 | + WorkflowRun |
| 41 | + Path string `json:"path"` |
| 42 | + } |
| 43 | + require.NoError(t, json.Unmarshal([]byte(rawJSON), &rawRuns), "unmarshal should succeed") |
| 44 | + require.Len(t, rawRuns, 1) |
| 45 | + |
| 46 | + run := rawRuns[0].WorkflowRun |
| 47 | + run.WorkflowPath = rawRuns[0].Path |
| 48 | + |
| 49 | + assert.Equal(t, ".github/workflows/my-workflow.lock.yml", run.WorkflowPath, |
| 50 | + "WorkflowPath should be populated from the 'path' JSON key") |
| 51 | + assert.Equal(t, int64(42), run.DatabaseID) |
| 52 | + assert.Equal(t, "My Workflow", run.WorkflowName) |
| 53 | +} |
| 54 | + |
| 55 | +// TestFetchWorkflowRunsLockYMLFilter verifies the .lock.yml suffix filter used |
| 56 | +// in fetchWorkflowRuns. Only runs whose WorkflowPath ends in ".lock.yml" must |
| 57 | +// be retained; runs with a plain ".yml" path (regular Actions workflows) must |
| 58 | +// be excluded. |
| 59 | +func TestFetchWorkflowRunsLockYMLFilter(t *testing.T) { |
| 60 | + runs := []WorkflowRun{ |
| 61 | + { |
| 62 | + DatabaseID: 1, |
| 63 | + WorkflowName: "Agentic Workflow", |
| 64 | + WorkflowPath: ".github/workflows/agentic-workflow.lock.yml", |
| 65 | + StartedAt: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC), |
| 66 | + UpdatedAt: time.Date(2026, 1, 1, 0, 5, 0, 0, time.UTC), |
| 67 | + }, |
| 68 | + { |
| 69 | + DatabaseID: 2, |
| 70 | + WorkflowName: "Regular CI", |
| 71 | + WorkflowPath: ".github/workflows/ci.yml", |
| 72 | + }, |
| 73 | + { |
| 74 | + DatabaseID: 3, |
| 75 | + WorkflowName: "Another Agentic", |
| 76 | + WorkflowPath: ".github/workflows/another.lock.yml", |
| 77 | + StartedAt: time.Date(2026, 1, 2, 0, 0, 0, 0, time.UTC), |
| 78 | + UpdatedAt: time.Date(2026, 1, 2, 0, 3, 0, 0, time.UTC), |
| 79 | + }, |
| 80 | + { |
| 81 | + // Run with empty WorkflowPath — must be excluded (mimics the pre-fix state |
| 82 | + // where "path" was absent from the JSON query). |
| 83 | + DatabaseID: 4, |
| 84 | + WorkflowName: "Agentic But No Path", |
| 85 | + WorkflowPath: "", |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + var filtered []WorkflowRun |
| 90 | + for _, run := range runs { |
| 91 | + if strings.HasSuffix(run.WorkflowPath, ".lock.yml") { |
| 92 | + if run.Duration == 0 && !run.StartedAt.IsZero() && !run.UpdatedAt.IsZero() { |
| 93 | + run.Duration = run.UpdatedAt.Sub(run.StartedAt) |
| 94 | + } |
| 95 | + filtered = append(filtered, run) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + require.Len(t, filtered, 2, "only .lock.yml runs should pass the filter") |
| 100 | + |
| 101 | + assert.Equal(t, int64(1), filtered[0].DatabaseID) |
| 102 | + assert.Equal(t, 5*time.Minute, filtered[0].Duration, "duration should be calculated from StartedAt/UpdatedAt") |
| 103 | + |
| 104 | + assert.Equal(t, int64(3), filtered[1].DatabaseID) |
| 105 | + assert.Equal(t, 3*time.Minute, filtered[1].Duration) |
| 106 | +} |
0 commit comments