114 lines
4.4 KiB
Go
114 lines
4.4 KiB
Go
package assessmentreview
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/config"
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
type operatorFake struct {
|
|
review domain.AssessmentAttemptReview
|
|
err error
|
|
calls int
|
|
actor string
|
|
}
|
|
|
|
func (operator *operatorFake) Inspect(_ context.Context, _ domain.AssessmentAttemptID, actor string) (domain.AssessmentAttemptReview, error) {
|
|
operator.calls++
|
|
operator.actor = actor
|
|
return operator.review, operator.err
|
|
}
|
|
|
|
func TestParseRequiresAnAttemptAndAnOperatorHandle(t *testing.T) {
|
|
id := uuid.New().String()
|
|
command, err := Parse([]string{"inspect", "--attempt", id, "--actor", "operator:night"})
|
|
if err != nil || command.AttemptID.String() != id || command.Actor != "operator:night" {
|
|
t.Fatalf("Parse(valid) = %+v, %v", command, err)
|
|
}
|
|
for _, args := range [][]string{
|
|
nil,
|
|
{"list"},
|
|
{"inspect"},
|
|
{"inspect", "--attempt", id},
|
|
{"inspect", "--attempt", id, "--actor", "alice"},
|
|
{"inspect", "--attempt", "not-a-uuid", "--actor", "operator:night"},
|
|
{"inspect", "--attempt", uuid.Nil.String(), "--actor", "operator:night"},
|
|
{"inspect", "--attempt", id, "--actor", "operator:night", "extra"},
|
|
} {
|
|
if _, err := Parse(args); err == nil {
|
|
t.Errorf("Parse(%v) accepted invalid arguments", args)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRunValidatesArgumentsBeforeReadingTheEnvironment(t *testing.T) {
|
|
lookup := func(string) (string, bool) {
|
|
t.Fatal("environment read before arguments were validated")
|
|
return "", false
|
|
}
|
|
if err := Run(context.Background(), []string{"inspect"}, lookup, &bytes.Buffer{}); err == nil {
|
|
t.Fatal("Run(invalid arguments) succeeded")
|
|
}
|
|
var noDatabase config.LookupEnv = func(string) (string, bool) { return "", false }
|
|
err := Run(context.Background(), []string{"inspect", "--attempt", uuid.New().String(), "--actor", "operator:night"}, noDatabase, &bytes.Buffer{})
|
|
if err == nil || !strings.Contains(err.Error(), "DATABASE_URL") {
|
|
t.Fatalf("Run(no database) error = %v, want a DATABASE_URL message", err)
|
|
}
|
|
}
|
|
|
|
func TestExecutePrintsOnlyBandCountsAndReasons(t *testing.T) {
|
|
attemptID := domain.AssessmentAttemptID(uuid.New())
|
|
operator := &operatorFake{review: domain.AssessmentAttemptReview{
|
|
AttemptID: attemptID, PolicyVersion: 4,
|
|
Measures: domain.AssessmentReviewMeasures{AnsweredItems: 9, FastAnswers: 5, ParallelAlternations: 2, ExtraNetworks: 1, MatchedIntegerItems: 3, DuplicateEvidenceBits: 9},
|
|
Outcome: domain.AssessmentReviewOutcome{
|
|
Band: domain.AssessmentReviewBandAttention,
|
|
Reasons: []domain.AssessmentReviewReason{domain.AssessmentReviewReasonFastAnswers, domain.AssessmentReviewReasonParallelSessions},
|
|
},
|
|
EvaluatedAt: time.Date(2026, time.October, 1, 10, 0, 0, 0, time.UTC), UpdatedAt: time.Date(2026, time.October, 2, 10, 0, 0, 0, time.UTC),
|
|
}}
|
|
var output bytes.Buffer
|
|
command := Command{AttemptID: attemptID, Actor: "operator:night"}
|
|
if err := Execute(context.Background(), command, operator, &output); err != nil {
|
|
t.Fatalf("Execute() error = %v", err)
|
|
}
|
|
for _, want := range []string{"attempt: " + attemptID.String(), "band: attention", "reasons: fast_answers,parallel_sessions", "policy_version: 4", "fast_answers: 5", "matched_integer_items: 3", "extra_networks: 1", "duplicate_evidence_bits: 9", "a hint for a manual check"} {
|
|
if !strings.Contains(output.String(), want) {
|
|
t.Errorf("output lacks %q:\n%s", want, output.String())
|
|
}
|
|
}
|
|
for _, forbidden := range []string{"user", "email", "address", "session", "answer:", "fingerprint"} {
|
|
if strings.Contains(output.String(), forbidden+":") {
|
|
t.Errorf("output has a %q line:\n%s", forbidden, output.String())
|
|
}
|
|
}
|
|
if operator.calls != 1 || operator.actor != "operator:night" {
|
|
t.Fatalf("operator calls/actor = %d/%q, want one call with the handle", operator.calls, operator.actor)
|
|
}
|
|
}
|
|
|
|
func TestExecuteMapsFailuresToSafeMessages(t *testing.T) {
|
|
for _, test := range []struct {
|
|
err error
|
|
want string
|
|
}{
|
|
{outbound.ErrAssessmentReviewNotFound, "no review"},
|
|
{domain.ErrInvalidAssessmentReview, "invalid"},
|
|
{errors.New("pq: password authentication failed for user lincodex at 10.0.0.5"), "operation failed"},
|
|
} {
|
|
err := Execute(context.Background(), Command{}, &operatorFake{err: test.err}, &bytes.Buffer{})
|
|
if err == nil || !strings.Contains(err.Error(), test.want) || strings.Contains(err.Error(), "10.0.0.5") {
|
|
t.Errorf("Execute(%v) error = %v, want a safe %q message", test.err, err, test.want)
|
|
}
|
|
}
|
|
}
|