| 1 | package serve |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/agent" |
| 11 | "reasonix/internal/control" |
| 12 | "reasonix/internal/event" |
| 13 | "reasonix/internal/session" |
| 14 | "reasonix/internal/tool" |
| 15 | ) |
| 16 | |
| 17 | func TestGoalDiagnosticsHTTPExportsAuthoritativeSession(t *testing.T) { |
| 18 | service, err := session.NewService("serve", session.NewFilesystemPersistence(t.TempDir())) |
| 19 | if err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | t.Cleanup(func() { |
| 23 | if err := service.Shutdown(context.Background()); err != nil { |
| 24 | t.Errorf("shutdown session service: %v", err) |
| 25 | } |
| 26 | }) |
| 27 | runtime, err := service.Create(t.Context(), session.CreateOptions{SessionID: "diagnostic-http"}) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | exec := agent.New(nil, tool.NewRegistry(), agent.NewSession("system"), agent.Options{}, event.Discard) |
| 32 | ctrl := control.New(control.Options{Executor: exec, Sink: event.Discard, SessionService: service, SessionRuntime: runtime, ExclusiveSession: true}) |
| 33 | t.Cleanup(ctrl.Close) |
| 34 | server := &Server{ctrl: ctrl} |
| 35 | recorder := httptest.NewRecorder() |
| 36 | request := httptest.NewRequest(http.MethodGet, "/goal-diagnostics", nil) |
| 37 | server.goalDiagnostics(recorder, request) |
| 38 | if recorder.Code != 200 { |
| 39 | t.Fatalf("status = %d: %s", recorder.Code, recorder.Body.String()) |
| 40 | } |
| 41 | for _, want := range []string{session.Codec, "goal-lifecycle-v2", "activationChanges"} { |
| 42 | if !strings.Contains(recorder.Body.String(), want) { |
| 43 | t.Fatalf("diagnostic response missing %q: %s", want, recorder.Body.String()) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 |