|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/inspect" |
| 14 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/state" |
| 15 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/state/sqlite" |
| 16 | +) |
| 17 | + |
| 18 | +func TestInspect_web_flagsAndAPI(t *testing.T) { |
| 19 | + ctx := context.Background() |
| 20 | + db := filepath.Join(t.TempDir(), "web.db") |
| 21 | + root := runProjRoot(t) |
| 22 | + |
| 23 | + // Create state via a workflow run. |
| 24 | + ResetGlobalsForTest() |
| 25 | + runCmd := NewRootCmd() |
| 26 | + runCmd.SetOut(io.Discard) |
| 27 | + runCmd.SetErr(io.Discard) |
| 28 | + runCmd.SetArgs([]string{ |
| 29 | + "run", "workflow/demo", |
| 30 | + "--project", root, |
| 31 | + "-e", "staging", |
| 32 | + "--state", db, |
| 33 | + "--input", "topic=inspect-web", |
| 34 | + }) |
| 35 | + if err := runCmd.Execute(); err != nil { |
| 36 | + t.Fatal(err) |
| 37 | + } |
| 38 | + |
| 39 | + st, err := sqlite.OpenReadOnly(ctx, db) |
| 40 | + if err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + t.Cleanup(func() { _ = st.Close() }) |
| 44 | + |
| 45 | + runs, err := st.ListRecentRuns(ctx, 5) |
| 46 | + if err != nil || len(runs) == 0 { |
| 47 | + t.Fatalf("runs=%v err=%v", runs, err) |
| 48 | + } |
| 49 | + |
| 50 | + srv, err := inspect.NewServer(st, inspect.Config{ |
| 51 | + StatePath: db, |
| 52 | + Env: "staging", |
| 53 | + Port: 0, // not used when served via httptest pattern below |
| 54 | + }) |
| 55 | + if err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + |
| 59 | + ts := httptest.NewServer(srv.Handler()) |
| 60 | + t.Cleanup(ts.Close) |
| 61 | + res, err := http.Get(ts.URL + "/api/runs") |
| 62 | + if err != nil { |
| 63 | + t.Fatal(err) |
| 64 | + } |
| 65 | + defer res.Body.Close() |
| 66 | + if res.StatusCode != http.StatusOK { |
| 67 | + t.Fatalf("status=%d", res.StatusCode) |
| 68 | + } |
| 69 | + b, _ := io.ReadAll(res.Body) |
| 70 | + if !strings.Contains(string(b), runs[0].RunID) { |
| 71 | + t.Fatalf("body missing run id: %s", b) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestInspect_web_requiresNoArgs(t *testing.T) { |
| 76 | + db := filepath.Join(t.TempDir(), "missing.db") |
| 77 | + root := runProjRoot(t) |
| 78 | + |
| 79 | + ResetGlobalsForTest() |
| 80 | + cmd := NewRootCmd() |
| 81 | + cmd.SetOut(io.Discard) |
| 82 | + cmd.SetErr(io.Discard) |
| 83 | + cmd.SetArgs([]string{"inspect", "--web", "--project", root, "--state", db, "Workflow/demo"}) |
| 84 | + err := cmd.Execute() |
| 85 | + if err == nil { |
| 86 | + t.Fatal("expected error") |
| 87 | + } |
| 88 | + if ExitCodeOf(err) != ExitValidationError { |
| 89 | + t.Fatalf("exit=%d err=%v", ExitCodeOf(err), err) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestInspect_web_invalidTraceUI_exit2(t *testing.T) { |
| 94 | + root := runProjRoot(t) |
| 95 | + db := filepath.Join(t.TempDir(), "ui.db") |
| 96 | + |
| 97 | + ResetGlobalsForTest() |
| 98 | + runCmd := NewRootCmd() |
| 99 | + runCmd.SetOut(io.Discard) |
| 100 | + runCmd.SetErr(io.Discard) |
| 101 | + runCmd.SetArgs([]string{"run", "workflow/demo", "--project", root, "--state", db, "--input", "topic=trace-ui-test"}) |
| 102 | + if err := runCmd.Execute(); err != nil { |
| 103 | + t.Fatal(err) |
| 104 | + } |
| 105 | + |
| 106 | + ResetGlobalsForTest() |
| 107 | + cmd := NewRootCmd() |
| 108 | + cmd.SetOut(io.Discard) |
| 109 | + cmd.SetErr(io.Discard) |
| 110 | + cmd.SetArgs([]string{"inspect", "--web", "--project", root, "--state", db, "--trace-ui", "javascript:alert(1)"}) |
| 111 | + err := cmd.Execute() |
| 112 | + if err == nil { |
| 113 | + t.Fatal("expected error") |
| 114 | + } |
| 115 | + if ExitCodeOf(err) != ExitValidationError { |
| 116 | + t.Fatalf("exit=%d err=%v", ExitCodeOf(err), err) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestInspect_web_invalidPort_exit2(t *testing.T) { |
| 121 | + root := runProjRoot(t) |
| 122 | + db := filepath.Join(t.TempDir(), "port.db") |
| 123 | + |
| 124 | + ResetGlobalsForTest() |
| 125 | + cmd := NewRootCmd() |
| 126 | + cmd.SetOut(io.Discard) |
| 127 | + cmd.SetErr(io.Discard) |
| 128 | + cmd.SetArgs([]string{"inspect", "--web", "--project", root, "--state", db, "--port", "99999"}) |
| 129 | + err := cmd.Execute() |
| 130 | + if err == nil { |
| 131 | + t.Fatal("expected error") |
| 132 | + } |
| 133 | + if ExitCodeOf(err) != ExitValidationError { |
| 134 | + t.Fatalf("exit=%d err=%v", ExitCodeOf(err), err) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestInspect_web_missingDB_exit2(t *testing.T) { |
| 139 | + db := filepath.Join(t.TempDir(), "no.db") |
| 140 | + root := runProjRoot(t) |
| 141 | + |
| 142 | + ResetGlobalsForTest() |
| 143 | + cmd := NewRootCmd() |
| 144 | + cmd.SetOut(io.Discard) |
| 145 | + cmd.SetErr(io.Discard) |
| 146 | + cmd.SetArgs([]string{"inspect", "--web", "--project", root, "--state", db}) |
| 147 | + err := cmd.Execute() |
| 148 | + if err == nil { |
| 149 | + t.Fatal("expected error") |
| 150 | + } |
| 151 | + if ExitCodeOf(err) != ExitValidationError { |
| 152 | + t.Fatalf("exit=%d err=%v", ExitCodeOf(err), err) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestInspect_web_readOnlyOpen(t *testing.T) { |
| 157 | + ctx := context.Background() |
| 158 | + db := filepath.Join(t.TempDir(), "ro-cli.db") |
| 159 | + st, err := sqlite.Open(ctx, db) |
| 160 | + if err != nil { |
| 161 | + t.Fatal(err) |
| 162 | + } |
| 163 | + if err := st.StartRun(ctx, state.Run{ |
| 164 | + RunID: "r", WorkflowName: "w", Env: "local", Status: state.RunStatusRunning, |
| 165 | + StartedAt: time.Now().UTC(), InputJSON: `{}`, |
| 166 | + }); err != nil { |
| 167 | + t.Fatal(err) |
| 168 | + } |
| 169 | + _ = st.Close() |
| 170 | + |
| 171 | + ro, err := sqlite.OpenReadOnly(ctx, db) |
| 172 | + if err != nil { |
| 173 | + t.Fatal(err) |
| 174 | + } |
| 175 | + _ = ro.Close() |
| 176 | +} |
0 commit comments