49 lines
1.6 KiB
Go
49 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.cin.su/m/Assistant/adapters/postgres"
|
|
)
|
|
|
|
type fakeTruthRuntimeStatusReader struct {
|
|
status postgres.TruthRuntimeStatus
|
|
err error
|
|
}
|
|
|
|
func (f fakeTruthRuntimeStatusReader) TruthRuntimeStatus(context.Context) (postgres.TruthRuntimeStatus, error) {
|
|
return f.status, f.err
|
|
}
|
|
|
|
func TestTruthRuntimeStatusReportsEvidenceCounts(t *testing.T) {
|
|
response := httptest.NewRecorder()
|
|
truthRuntimeStatusHandler(fakeTruthRuntimeStatusReader{status: postgres.TruthRuntimeStatus{
|
|
BundleCount: 4,
|
|
ClaimsByVerification: map[string]int64{"UNVERIFIED": 3, "VERIFIED": 1},
|
|
BeliefsByState: map[string]int64{"PROVISIONAL": 4},
|
|
SourcesByFreshness: map[string]int64{"FRESH": 4},
|
|
ObservedAtUnixMillis: 1700000000000,
|
|
}}).ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/v1/research/status", nil))
|
|
if response.Code != http.StatusOK {
|
|
t.Fatalf("status code = %d", response.Code)
|
|
}
|
|
for _, expected := range []string{"\"status\":\"ready\"", "\"bundle_count\":4", "\"VERIFIED\":1", "\"PROVISIONAL\":4", "1700000000000"} {
|
|
if !strings.Contains(response.Body.String(), expected) {
|
|
t.Fatalf("response missing %q: %s", expected, response.Body.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTruthRuntimeStatusFailsClosedOnDatabaseError(t *testing.T) {
|
|
response := httptest.NewRecorder()
|
|
truthRuntimeStatusHandler(fakeTruthRuntimeStatusReader{err: errors.New("database unavailable")}).ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/v1/research/status", nil))
|
|
if response.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("status code = %d", response.Code)
|
|
}
|
|
}
|