55 lines
1.6 KiB
Go
55 lines
1.6 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"tutor/internal/config"
|
|
"tutor/internal/interview"
|
|
"tutor/internal/learnermemory"
|
|
"tutor/internal/ontology"
|
|
"tutor/internal/progression"
|
|
"tutor/internal/workflows"
|
|
)
|
|
|
|
func TestOntologyHTTPFlow(t *testing.T) {
|
|
memory := learnermemory.NewService(learnermemory.NewMemoryStore())
|
|
service := interview.NewService(interview.NewMemoryStore(), workflows.NewStubRunner(), memory)
|
|
progress := progression.NewService(memory)
|
|
onto := ontology.NewService(ontology.NewMemoryStore())
|
|
handler := NewHandler(config.Config{Environment: "test"}, service, memory, progress, onto)
|
|
routes := handler.Routes()
|
|
|
|
body := bytes.NewBufferString(`{
|
|
"title":"Backend interview notes",
|
|
"source_type":"markdown",
|
|
"body":"Idempotent API retries need transactions. Cache invalidation uses TTL tradeoffs."
|
|
}`)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/materials", body)
|
|
rec := httptest.NewRecorder()
|
|
routes.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusCreated {
|
|
t.Fatalf("ingest status = %d, body = %s", rec.Code, rec.Body.String())
|
|
}
|
|
|
|
var result ontology.IngestResult
|
|
if err := json.NewDecoder(rec.Body).Decode(&result); err != nil {
|
|
t.Fatalf("decode ingest response: %v", err)
|
|
}
|
|
if len(result.Snapshot.Concepts) == 0 {
|
|
t.Fatal("expected ontology concepts")
|
|
}
|
|
|
|
getReq := httptest.NewRequest(http.MethodGet, "/api/v1/ontology", nil)
|
|
getRec := httptest.NewRecorder()
|
|
routes.ServeHTTP(getRec, getReq)
|
|
|
|
if getRec.Code != http.StatusOK {
|
|
t.Fatalf("ontology status = %d, body = %s", getRec.Code, getRec.Body.String())
|
|
}
|
|
}
|