Files
orchestra/internal/admin/admin_test.go
T

53 lines
1.4 KiB
Go

package admin
import (
"context"
"encoding/json"
"net/http/httptest"
"orchestra/internal/authz"
"orchestra/internal/domain"
"orchestra/internal/store"
"strings"
"testing"
)
func TestDiagnosticsRequiresFullControl(t *testing.T) {
s, err := store.Open(t.TempDir())
if err != nil {
t.Fatal(err)
}
h := (&Server{Store: s}).Diagnostics
r := httptest.NewRequest("GET", "/v1/admin/diagnostics", nil)
r.Header.Set("X-Orchestra-Surface", string(authz.MCP))
w := httptest.NewRecorder()
h(w, r)
if w.Code != 403 || !strings.Contains(w.Body.String(), `"code":"forbidden"`) {
t.Fatalf("status=%d body=%s", w.Code, w.Body)
}
}
func TestSubscribeEmitsCursorAndEvent(t *testing.T) {
s, err := store.Open(t.TempDir())
if err != nil {
t.Fatal(err)
}
p, _ := json.Marshal(map[string]any{"source": "test", "external_id": "1", "project": "p"})
if err := s.Append(domain.Event{ID: "e", TaskID: "t", Type: "TaskCreated", Version: 1, Payload: p}); err != nil {
t.Fatal(err)
}
h := (&Server{Store: s}).Subscribe
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r := httptest.NewRequest("GET", "/v1/events/subscribe?since=0", nil).WithContext(ctx)
w := httptest.NewRecorder()
done := make(chan struct{})
go func() { h(w, r); close(done) }()
for !strings.Contains(w.Body.String(), "id: 1") {
}
cancel()
<-done
if !strings.Contains(w.Body.String(), "id: 1") {
t.Fatalf("body=%s", w.Body)
}
}