feat(assessment): watermark the task text of real attempts

This commit is contained in:
shira committed 2026-10-11 10:44:30 +03:00
1 parent 24b64d698a
commit c975a0d506
57 files changed
+2429 -92

No files matched your search

@@ -235,3 +235,73 @@ func assessmentPublicSnapshot() domain.AssessmentAttemptPublicSnapshot {
func domainCodePointer[T ~string](value T) *T {
return &value
}
type presenterStub struct{ err error }
func (stub presenterStub) Present(snapshot domain.AssessmentAttemptPublicSnapshot) (domain.AssessmentAttemptPublicSnapshot, error) {
if stub.err != nil {
return domain.AssessmentAttemptPublicSnapshot{}, stub.err
}
snapshot.Watermark = &domain.AssessmentAttemptPublicWatermark{Code: "7K3QD-9XM2T"}
item := *snapshot.CurrentItem
item.Prompt += "\u200b"
snapshot.CurrentItem = &item
return snapshot, nil
}
func TestEveryAttemptResponsePassesThroughThePresenter(t *testing.T) {
snapshot := assessmentPublicSnapshot()
operations := &assessmentOperationsStub{read: snapshot, start: outbound.AssessmentAttemptSessionResult{Response: snapshot}}
handler := NewAssessmentHandler(operations).WithPresenter(presenterStub{})
ctx := principalForTest(uuid.New())
read, err := handler.GetMyAssessmentAttempt(ctx, api.GetMyAssessmentAttemptParams{AttemptID: snapshot.ID.UUID()})
if err != nil {
t.Fatalf("GetMyAssessmentAttempt() error = %v", err)
}
started, err := handler.StartMyAssessmentAttemptSession(ctx, &api.AssessmentAttemptSessionStart{ExpectedVersion: 7},
api.StartMyAssessmentAttemptSessionParams{AttemptID: snapshot.ID.UUID(), IdempotencyKey: "session-key"})
if err != nil {
t.Fatalf("StartMyAssessmentAttemptSession() error = %v", err)
}
for name, response := range map[string]any{"read": read, "start": started} {
headers, ok := response.(*api.AssessmentAttemptHeaders)
if !ok {
t.Fatalf("%s response = %T, want attempt headers", name, response)
}
qualified, ok := headers.Response.GetAssessmentQualifiedAttempt()
if !ok {
t.Fatalf("%s response is not a qualified attempt", name)
}
code, hasCode := qualified.Watermark.Get()
item, _ := qualified.CurrentItem.Get()
if !hasCode || code.Code != "7K3QD-9XM2T" || item.Prompt != "Choose the number\u200b" {
t.Fatalf("%s response watermark = %+v/%v and prompt %q, want the presented values", name, code, hasCode, item.Prompt)
}
}
}
func TestAnAttemptWithoutAPresenterCarriesNoWatermark(t *testing.T) {
snapshot := assessmentPublicSnapshot()
handler := NewAssessmentHandler(&assessmentOperationsStub{read: snapshot})
read, err := handler.GetMyAssessmentAttempt(principalForTest(uuid.New()), api.GetMyAssessmentAttemptParams{AttemptID: snapshot.ID.UUID()})
if err != nil {
t.Fatalf("GetMyAssessmentAttempt() error = %v", err)
}
headers, ok := read.(*api.AssessmentAttemptHeaders)
if !ok {
t.Fatalf("read response = %T, want attempt headers", read)
}
qualified, _ := headers.Response.GetAssessmentQualifiedAttempt()
if qualified.Watermark.IsSet() {
t.Fatal("an attempt served without a presenter carries a watermark")
}
}
func TestAPresenterFailureIsAServerErrorNotAnUnmarkedAttempt(t *testing.T) {
snapshot := assessmentPublicSnapshot()
handler := NewAssessmentHandler(&assessmentOperationsStub{read: snapshot}).WithPresenter(presenterStub{err: errors.New("seed unavailable")})
if _, err := handler.GetMyAssessmentAttempt(principalForTest(uuid.New()), api.GetMyAssessmentAttemptParams{AttemptID: snapshot.ID.UUID()}); err == nil {
t.Fatal("GetMyAssessmentAttempt() succeeded, want an error so that the task text is never served unmarked")
}
}