Files
supply-intelligence/internal/httpapi/server_integration_test.go
2026-05-07 10:16:46 +08:00

150 lines
6.4 KiB
Go

package httpapi_test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"supply-intelligence/internal/app"
"supply-intelligence/internal/domain"
"supply-intelligence/internal/probe"
)
func TestApplicationServerRoutes(t *testing.T) {
application := app.New()
req := httptest.NewRequest(http.MethodPost, "/internal/supply-intelligence/probe/evaluate", bytes.NewBufferString(`{"account_id":7,"platform":"openai","current_status":"active","status_code":401}`))
rr := httptest.NewRecorder()
application.Server.Routes().ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("unexpected status: %d body=%s", rr.Code, rr.Body.String())
}
var result probe.EvaluateOutput
if err := json.NewDecoder(rr.Body).Decode(&result); err != nil {
t.Fatalf("decode error: %v", err)
}
if result.RoutingState.AccountID != 7 || result.RoutingState.AccountStatus != "suspended" {
t.Fatalf("unexpected state: %+v", result.RoutingState)
}
getReq := httptest.NewRequest(http.MethodGet, "/internal/supply-intelligence/accounts/7/routing-state", nil)
getRR := httptest.NewRecorder()
application.Server.Routes().ServeHTTP(getRR, getReq)
if getRR.Code != http.StatusOK {
t.Fatalf("unexpected get status: %d body=%s", getRR.Code, getRR.Body.String())
}
}
func TestPublishConsumeOnceListAppliedIntegration(t *testing.T) {
application := app.New()
publishReq := httptest.NewRequest(http.MethodPost, "/internal/supply-intelligence/publish/package-event", bytes.NewBufferString(`{"event_id":"evt-integration-1","package_id":501,"platform":"openai","model":"gpt-4.1-mini","version":9,"occurred_at":"2026-05-06T20:30:00Z"}`))
publishRR := httptest.NewRecorder()
application.Server.Routes().ServeHTTP(publishRR, publishReq)
if publishRR.Code != http.StatusOK {
t.Fatalf("unexpected publish status: %d body=%s", publishRR.Code, publishRR.Body.String())
}
consumeReq := httptest.NewRequest(http.MethodPost, "/internal/supply-intelligence/gateway/consume-once", bytes.NewBufferString(`{"consumer":"gateway"}`))
consumeRR := httptest.NewRecorder()
application.Server.Routes().ServeHTTP(consumeRR, consumeReq)
if consumeRR.Code != http.StatusOK {
t.Fatalf("unexpected consume status: %d body=%s", consumeRR.Code, consumeRR.Body.String())
}
listReq := httptest.NewRequest(http.MethodGet, "/internal/supply-intelligence/gateway/package-changes", nil)
listRR := httptest.NewRecorder()
application.Server.Routes().ServeHTTP(listRR, listReq)
if listRR.Code != http.StatusOK {
t.Fatalf("unexpected list status: %d body=%s", listRR.Code, listRR.Body.String())
}
var listResp struct {
Items []domain.PackageChangeEvent `json:"items"`
NextCursor string `json:"next_cursor"`
}
if err := json.NewDecoder(listRR.Body).Decode(&listResp); err != nil {
t.Fatalf("decode list error: %v", err)
}
if len(listResp.Items) != 1 || listResp.Items[0].EventID != "evt-integration-1" {
t.Fatalf("unexpected list items: %+v", listResp.Items)
}
if listResp.NextCursor != "1" {
t.Fatalf("unexpected next cursor: %+v", listResp)
}
if listResp.Items[0].GatewaySyncStatus != domain.GatewaySyncStatusApplied {
t.Fatalf("unexpected sync status: %+v", listResp.Items[0])
}
}
func TestPublishConsumeOnceListFailedIntegration(t *testing.T) {
application := app.New()
publishReq := httptest.NewRequest(http.MethodPost, "/internal/supply-intelligence/publish/package-event", bytes.NewBufferString(`{"event_id":"evt-integration-failed","package_id":502,"platform":"openai","model":"gpt-fail-model","version":10,"occurred_at":"2026-05-06T20:31:00Z"}`))
publishRR := httptest.NewRecorder()
application.Server.Routes().ServeHTTP(publishRR, publishReq)
if publishRR.Code != http.StatusOK {
t.Fatalf("unexpected publish status: %d body=%s", publishRR.Code, publishRR.Body.String())
}
consumeReq := httptest.NewRequest(http.MethodPost, "/internal/supply-intelligence/gateway/consume-once", bytes.NewBufferString(`{"consumer":"gateway"}`))
consumeRR := httptest.NewRecorder()
application.Server.Routes().ServeHTTP(consumeRR, consumeReq)
if consumeRR.Code != http.StatusOK {
t.Fatalf("unexpected consume status: %d body=%s", consumeRR.Code, consumeRR.Body.String())
}
listReq := httptest.NewRequest(http.MethodGet, "/internal/supply-intelligence/gateway/package-changes", nil)
listRR := httptest.NewRecorder()
application.Server.Routes().ServeHTTP(listRR, listReq)
if listRR.Code != http.StatusOK {
t.Fatalf("unexpected list status: %d body=%s", listRR.Code, listRR.Body.String())
}
var listResp struct {
Items []domain.PackageChangeEvent `json:"items"`
NextCursor string `json:"next_cursor"`
}
if err := json.NewDecoder(listRR.Body).Decode(&listResp); err != nil {
t.Fatalf("decode list error: %v", err)
}
if len(listResp.Items) != 1 || listResp.Items[0].EventID != "evt-integration-failed" {
t.Fatalf("unexpected list items: %+v", listResp.Items)
}
if listResp.NextCursor != "1" {
t.Fatalf("unexpected next cursor: %+v", listResp)
}
if listResp.Items[0].GatewaySyncStatus != domain.GatewaySyncStatusFailed {
t.Fatalf("unexpected sync status: %+v", listResp.Items[0])
}
}
func TestDiscoveryCandidateCreateAndListIntegration(t *testing.T) {
application := app.New()
createReq := httptest.NewRequest(http.MethodPost, "/internal/supply-intelligence/discovery/candidates", bytes.NewBufferString(`{"candidate_id":"cand-int-1","account_id":701,"platform":"openai","model":"gpt-4.1-mini","source":"manual_seed","reason_code":"new_model","discovered_at":"2026-05-06T20:30:00Z"}`))
createRR := httptest.NewRecorder()
application.Server.Routes().ServeHTTP(createRR, createReq)
if createRR.Code != http.StatusOK {
t.Fatalf("unexpected create status: %d body=%s", createRR.Code, createRR.Body.String())
}
listReq := httptest.NewRequest(http.MethodGet, "/internal/supply-intelligence/discovery/candidates?status=pending_admission", nil)
listRR := httptest.NewRecorder()
application.Server.Routes().ServeHTTP(listRR, listReq)
if listRR.Code != http.StatusOK {
t.Fatalf("unexpected list status: %d body=%s", listRR.Code, listRR.Body.String())
}
var listResp struct {
Items []domain.DiscoveryCandidate `json:"items"`
}
if err := json.NewDecoder(listRR.Body).Decode(&listResp); err != nil {
t.Fatalf("decode list error: %v", err)
}
if len(listResp.Items) != 1 || listResp.Items[0].CandidateID != "cand-int-1" {
t.Fatalf("unexpected discovery list items: %+v", listResp.Items)
}
}