Files
noema/internal/api/api_test.go
T
Andy Dixon 85d769b245 feat: add immutable cognitive event store, live streams and event explorer
Phase 3. Events are the append-only record of everything a mind does
(ADR-0002). pkg/events defines the closed type vocabulary, categories,
evidence references and the observation payload with explicit trust
levels. The store validates and appends inside the caller's transaction,
publishes to a bounded fan-out bus only after commit, queries by seq
cursor with type/category/source/salience/confidence/time/search
filters, and walks provenance: cause, parents, children, cited evidence,
derived events and correlation groups. Durable per-consumer cursors never
move backwards.

Server-Sent Events streams (per mind and global) subscribe before
replaying history after Last-Event-ID so nothing is missed, apply the
same filter vocabulary live, and heartbeat. Operators can inject
observations tagged with operator trust for demos and tests.

UI: live thought stream with filters and paging, forensic event page
with causal chain and evidence navigation, dashboard important events.
Personality and status changes now leave operator_action events in the
mind's own history. CLI: event inspect/list/tail, observe.
2026-06-17 20:02:22 +01:00

84 lines
3.0 KiB
Go

package api
import (
"context"
"encoding/json"
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/andydixon/noema/internal/app"
"github.com/andydixon/noema/internal/audit"
"github.com/andydixon/noema/internal/auth"
"github.com/andydixon/noema/internal/config"
evstore "github.com/andydixon/noema/internal/events"
"github.com/andydixon/noema/internal/minds"
"github.com/andydixon/noema/internal/observability"
"github.com/andydixon/noema/internal/storage"
"github.com/andydixon/noema/migrations"
)
func testServer(t *testing.T) (*httptest.Server, string) {
t.Helper()
pool := storage.TestPool(t, migrations.FS)
cfg := config.Defaults()
cfg.DatabaseURL = "postgres://test"
log := slog.New(slog.DiscardHandler)
deps := app.Deps{Config: cfg, Log: log, DB: pool, Auth: auth.NewService(pool, time.Hour, 2*time.Hour),
Audit: audit.New(pool, log), Metrics: observability.NewMetrics(), Minds: minds.NewStore(pool), Events: evstore.NewStore(pool, evstore.NewBus(50), nil), Migrations: migrations.FS, Started: time.Now()}
ctx := context.Background()
u, err := deps.Auth.CreateUser(ctx, "admin", "administrator-pass", auth.RoleAdministrator)
if err != nil {
t.Fatal(err)
}
tok, _ := deps.Auth.CreateAPIToken(ctx, u.ID, "t", nil)
srv := httptest.NewServer(New(deps, nil).Handler())
t.Cleanup(srv.Close)
return srv, tok
}
func TestStatusAuthAndErrorEnvelope(t *testing.T) {
srv, tok := testServer(t)
res, _ := http.Get(srv.URL + "/api/v1/system/status")
if res.StatusCode != 401 {
t.Fatalf("expected 401, got %d", res.StatusCode)
}
var e ErrorBody
json.NewDecoder(res.Body).Decode(&e) //nolint:errcheck
if e.Error.Code != "unauthenticated" || e.Error.RequestID == "" {
t.Fatalf("bad error envelope: %+v", e)
}
req, _ := http.NewRequest("GET", srv.URL+"/api/v1/system/status", nil)
req.Header.Set("Authorization", "Bearer "+tok)
res, _ = http.DefaultClient.Do(req)
if res.StatusCode != 200 {
t.Fatalf("expected 200, got %d", res.StatusCode)
}
var st SystemStatus
json.NewDecoder(res.Body).Decode(&st) //nolint:errcheck
if !st.Database.OK || len(st.Migrations) == 0 {
t.Fatalf("status incomplete: %+v", st)
}
if res.Header.Get("Content-Security-Policy") == "" {
t.Fatal("missing CSP header")
}
// Unknown JSON fields and body limits are rejected as 400.
req, _ = http.NewRequest("POST", srv.URL+"/api/v1/users", strings.NewReader(`{"username":"x","password":"long-enough-pass","role":"observer","bogus":1}`))
req.Header.Set("Authorization", "Bearer "+tok)
res, _ = http.DefaultClient.Do(req)
if res.StatusCode != 400 {
t.Fatalf("expected 400 for unknown field, got %d", res.StatusCode)
}
// Bearer requests are exempt from CSRF but still role-checked.
req, _ = http.NewRequest("POST", srv.URL+"/api/v1/users", strings.NewReader(`{"username":"obs","password":"long-enough-pass","role":"observer"}`))
req.Header.Set("Authorization", "Bearer "+tok)
res, _ = http.DefaultClient.Do(req)
if res.StatusCode != 201 {
t.Fatalf("expected 201, got %d", res.StatusCode)
}
}