| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "the-valley/flow/internal/config" |
| 6 | "net" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "the-valley/flow/internal/run" |
| 12 | ) |
| 13 | |
| 14 | func TestWebhookFreshnessText(t *testing.T) { |
| 15 | now := time.Date(2026, 6, 28, 12, 0, 0, 0, time.UTC) |
| 16 | cases := []struct { |
| 17 | name string |
| 18 | last time.Time |
| 19 | wantText string |
| 20 | wantSev webhookSeverity |
| 21 | }{ |
| 22 | {"zero", time.Time{}, "no events yet", webhookFresh}, |
| 23 | {"10s fresh", now.Add(-10 * time.Second), "last event 10s", webhookFresh}, |
| 24 | {"just under stale", now.Add(-(webhookStaleAfter - time.Second)), "last event 29m", webhookFresh}, |
| 25 | {"at stale boundary", now.Add(-webhookStaleAfter), "last event 30m", webhookStale}, |
| 26 | {"45m stale", now.Add(-45 * time.Minute), "last event 45m", webhookStale}, |
| 27 | {"just under dead", now.Add(-(webhookDeadAfter - time.Second)), "last event 1h", webhookStale}, |
| 28 | {"at dead boundary", now.Add(-webhookDeadAfter), "last event 2h", webhookDead}, |
| 29 | {"5h dead", now.Add(-5 * time.Hour), "last event 5h", webhookDead}, |
| 30 | } |
| 31 | for _, c := range cases { |
| 32 | t.Run(c.name, func(t *testing.T) { |
| 33 | text, sev := webhookFreshnessText(c.last, now) |
| 34 | if text != c.wantText { |
| 35 | t.Errorf("text = %q, want %q", text, c.wantText) |
| 36 | } |
| 37 | if sev != c.wantSev { |
| 38 | t.Errorf("severity = %d, want %d", sev, c.wantSev) |
| 39 | } |
| 40 | }) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestHumanizeDuration(t *testing.T) { |
| 45 | cases := []struct { |
| 46 | d time.Duration |
| 47 | want string |
| 48 | }{ |
| 49 | {-5 * time.Second, "0s"}, |
| 50 | {0, "0s"}, |
| 51 | {12 * time.Second, "12s"}, |
| 52 | {59 * time.Second, "59s"}, |
| 53 | {time.Minute, "1m"}, |
| 54 | {5 * time.Minute, "5m"}, |
| 55 | {59 * time.Minute, "59m"}, |
| 56 | {time.Hour, "1h"}, |
| 57 | {3 * time.Hour, "3h"}, |
| 58 | {23 * time.Hour, "23h"}, |
| 59 | {24 * time.Hour, "1d"}, |
| 60 | {50 * time.Hour, "2d"}, |
| 61 | } |
| 62 | for _, c := range cases { |
| 63 | if got := humanizeDuration(c.d); got != c.want { |
| 64 | t.Errorf("humanizeDuration(%v) = %q, want %q", c.d, got, c.want) |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // TestSelectedPRURLForOpenHandler verifies the path the 'o' key uses: |
| 70 | // selectedPR yields the row whose state carries the URL to open in the |
| 71 | // browser. |
| 72 | func TestSelectedPRURLForOpenHandler(t *testing.T) { |
| 73 | states := []*run.State{ |
| 74 | {ID: "a", Prompt: "p", TargetRepo: strPtr("r"), PRURL: strPtr("https://github.com/o/r/pull/1"), CreatedAt: "2026-01-01T00:00:00Z"}, |
| 75 | {ID: "b", Prompt: "p", TargetRepo: strPtr("r"), PRURL: strPtr("https://github.com/o/r/pull/2"), CreatedAt: "2026-01-01T00:01:00Z"}, |
| 76 | } |
| 77 | // Rows are most recent first, so cursor 1 is the older PR, #1. |
| 78 | m := dashboardModel{states: states, tmuxDeps: testDashboardTmuxDeps(), cursor: 1} |
| 79 | |
| 80 | r, ok := m.selectedPR("open") |
| 81 | if !ok || r.state().PRURL == nil { |
| 82 | t.Fatal("selected row has no PRURL") |
| 83 | } |
| 84 | if got := *r.state().PRURL; got != "https://github.com/o/r/pull/1" { |
| 85 | t.Errorf("selected PR URL = %q, want pull/1", got) |
| 86 | } |
| 87 | |
| 88 | // A bare agent has no PR to open: a hint is noted instead. |
| 89 | m = dashboardModel{states: []*run.State{{ID: "20260101-0000-beef", Prompt: "p", CreatedAt: "2026-01-01T00:00:00Z"}}, tmuxDeps: testDashboardTmuxDeps()} |
| 90 | if _, ok := m.selectedPR("open"); ok { |
| 91 | t.Error("selectedPR on a bare agent row should report no PR") |
| 92 | } |
| 93 | if len(m.recentErrors) != 1 || !strings.Contains(m.recentErrors[0].Message, "agent:beef has no PR") { |
| 94 | t.Errorf("recentErrors = %v, want a no-PR hint", m.recentErrors) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestSetupWebhookPortHeldFallsBackToPolling(t *testing.T) { |
| 99 | ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 100 | if err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | defer ln.Close() |
| 104 | port := ln.Addr().(*net.TCPAddr).Port |
| 105 | |
| 106 | m := newDashboardModel(nil, config.Config{}, nil) |
| 107 | srv, err := setupWebhook(&m, config.Config{Webhook: &config.WebhookConfig{Port: port}}) |
| 108 | if err != nil { |
| 109 | t.Fatalf("setupWebhook: %v", err) |
| 110 | } |
| 111 | if srv != nil || m.useWebhook || !m.pollEnabled { |
| 112 | t.Errorf("srv=%v useWebhook=%v pollEnabled=%v, want polling only", srv, m.useWebhook, m.pollEnabled) |
| 113 | } |
| 114 | if line := m.renderSourceLine(); !strings.Contains(line, "polling: 30s") || !strings.Contains(line, fmt.Sprintf("port %d", port)) { |
| 115 | t.Errorf("source line = %q", line) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestSetupWebhookOtherListenErrorFails(t *testing.T) { |
| 120 | m := newDashboardModel(nil, config.Config{}, nil) |
| 121 | if _, err := setupWebhook(&m, config.Config{Webhook: &config.WebhookConfig{Port: -1}}); err == nil { |
| 122 | t.Fatal("want an error for an unusable port") |
| 123 | } |
| 124 | } |