feat(assessment): record capture hints and keep one window per attempt

This commit is contained in:
shira committed 2026-10-11 13:16:52 +03:00
1 parent c975a0d506
commit bb917bf62e
108 files changed
+6649 -139

No files matched your search

@@ -3,6 +3,7 @@ package v1
import (
"context"
"errors"
"strings"
"testing"
"time"
@@ -305,3 +306,150 @@ func TestAPresenterFailureIsAServerErrorNotAnUnmarkedAttempt(t *testing.T) {
t.Fatal("GetMyAssessmentAttempt() succeeded, want an error so that the task text is never served unmarked")
}
}
type workSessionsStub struct {
holdCommand assessmentusecase.HoldWorkSessionCommand
hold assessmentusecase.WorkSessionHold
holdErr error
releaseCommand assessmentusecase.ReleaseWorkSessionCommand
releaseErr error
}
func (stub *workSessionsStub) Hold(_ context.Context, command assessmentusecase.HoldWorkSessionCommand) (assessmentusecase.WorkSessionHold, error) {
stub.holdCommand = command
return stub.hold, stub.holdErr
}
func (stub *workSessionsStub) Release(_ context.Context, command assessmentusecase.ReleaseWorkSessionCommand) error {
stub.releaseCommand = command
return stub.releaseErr
}
const testPageClient = "page-one-AAAAAAAAAAAAAAAA"
func TestHoldWorkSessionAnswersTheLeaseWithoutTheClientIdentifier(t *testing.T) {
userID, attemptID := uuid.New(), uuid.New()
until := time.Date(2026, time.October, 11, 9, 2, 0, 0, time.UTC)
stub := &workSessionsStub{hold: assessmentusecase.WorkSessionHold{HeldUntil: until, RenewEvery: 15 * time.Second}}
handler := NewAssessmentHandler(&assessmentOperationsStub{}).WithWorkSessions(stub)
response, err := handler.HoldMyAssessmentWorkSession(principalForTest(userID),
api.HoldMyAssessmentWorkSessionParams{AttemptID: attemptID, XAttemptClient: testPageClient})
if err != nil {
t.Fatalf("HoldMyAssessmentWorkSession() error = %v", err)
}
headers, ok := response.(*api.AssessmentWorkSessionHeaders)
if !ok || headers.CacheControl != api.CacheControlNoStoreNoStore ||
!time.Time(headers.Response.HeldUntil).Equal(until) || headers.Response.RenewAfterSeconds != 15 {
t.Fatalf("hold response = %T %+v, want a no-store lease that ends at %s", response, response, until)
}
if stub.holdCommand.ActorID != userID || stub.holdCommand.AttemptID != domain.AssessmentAttemptID(attemptID) || stub.holdCommand.Client != testPageClient {
t.Fatalf("hold command = %+v, want the verified principal, the attempt, and the page identifier", stub.holdCommand)
}
encoded, err := headers.Response.MarshalJSON()
if err != nil || strings.Contains(string(encoded), testPageClient) {
t.Fatalf("the response %s repeats the page identifier (error %v)", encoded, err)
}
}
func TestHoldAndReleaseWorkSessionMapTheirFailures(t *testing.T) {
attemptID := uuid.New()
stub := &workSessionsStub{}
handler := NewAssessmentHandler(&assessmentOperationsStub{}).WithWorkSessions(stub)
hold := func(err error) api.HoldMyAssessmentWorkSessionRes {
t.Helper()
stub.holdErr = err
response, handleErr := handler.HoldMyAssessmentWorkSession(principalForTest(uuid.New()),
api.HoldMyAssessmentWorkSessionParams{AttemptID: attemptID, XAttemptClient: testPageClient})
if handleErr != nil {
t.Fatalf("HoldMyAssessmentWorkSession(%v) error = %v", err, handleErr)
}
return response
}
if _, ok := hold(assessmentusecase.ErrAttemptWorkSessionBusy).(*api.HoldMyAssessmentWorkSessionConflict); !ok {
t.Fatal("a held session did not answer 409")
}
if _, ok := hold(assessmentusecase.ErrAttemptWorkSessionState).(*api.HoldMyAssessmentWorkSessionConflict); !ok {
t.Fatal("an attempt with no work session did not answer 409")
}
if _, ok := hold(assessmentusecase.ErrAttemptNotFound).(*api.HoldMyAssessmentWorkSessionNotFound); !ok {
t.Fatal("an unknown attempt did not answer 404")
}
if _, ok := hold(assessmentusecase.ErrAttemptConsentRequired).(*api.HoldMyAssessmentWorkSessionForbidden); !ok {
t.Fatal("a missing consent did not answer 403")
}
if _, ok := hold(assessmentusecase.ErrInvalidWorkSessionClient).(*api.HoldMyAssessmentWorkSessionBadRequest); !ok {
t.Fatal("a malformed page identifier did not answer 400")
}
stub.holdErr = errors.New("private database detail")
if _, err := handler.HoldMyAssessmentWorkSession(principalForTest(uuid.New()),
api.HoldMyAssessmentWorkSessionParams{AttemptID: attemptID, XAttemptClient: testPageClient}); err == nil || err.Error() != "private database detail" {
t.Fatalf("internal error mapping = %v", err)
}
stub.holdErr = nil
release, err := handler.ReleaseMyAssessmentWorkSession(principalForTest(uuid.New()),
api.ReleaseMyAssessmentWorkSessionParams{AttemptID: attemptID, XAttemptClient: testPageClient})
if err != nil {
t.Fatalf("ReleaseMyAssessmentWorkSession() error = %v", err)
}
if noContent, ok := release.(*api.ReleaseMyAssessmentWorkSessionNoContent); !ok || noContent.CacheControl != api.CacheControlNoStoreNoStore {
t.Fatalf("release response = %T, want no-store 204", release)
}
if stub.releaseCommand.Client != testPageClient || stub.releaseCommand.AttemptID != domain.AssessmentAttemptID(attemptID) {
t.Fatalf("release command = %+v", stub.releaseCommand)
}
stub.releaseErr = assessmentusecase.ErrAttemptNotFound
missing, err := handler.ReleaseMyAssessmentWorkSession(principalForTest(uuid.New()),
api.ReleaseMyAssessmentWorkSessionParams{AttemptID: attemptID, XAttemptClient: testPageClient})
if _, ok := missing.(*api.ReleaseMyAssessmentWorkSessionNotFound); !ok || err != nil {
t.Fatalf("release of an unknown attempt = %T, %v, want 404", missing, err)
}
guest, err := handler.HoldMyAssessmentWorkSession(context.Background(), api.HoldMyAssessmentWorkSessionParams{AttemptID: attemptID, XAttemptClient: testPageClient})
if _, ok := guest.(*api.HoldMyAssessmentWorkSessionUnauthorized); !ok || err != nil {
t.Fatalf("guest hold = %T, %v, want 401", guest, err)
}
}
func TestStartSessionAndAnswerPassThePageAndRefuseAHeldAttemptWith409(t *testing.T) {
snapshot := assessmentPublicSnapshot()
operations := &assessmentOperationsStub{start: outbound.AssessmentAttemptSessionResult{Response: snapshot}}
handler := NewAssessmentHandler(operations)
ctx := principalForTest(uuid.New())
params := api.StartMyAssessmentAttemptSessionParams{
AttemptID: snapshot.ID.UUID(), IdempotencyKey: "session-key", XAttemptClient: api.NewOptString(testPageClient),
}
if _, err := handler.StartMyAssessmentAttemptSession(ctx, &api.AssessmentAttemptSessionStart{ExpectedVersion: 7}, params); err != nil {
t.Fatalf("StartMyAssessmentAttemptSession() error = %v", err)
}
if operations.startCommand.Client != testPageClient {
t.Fatalf("StartSession() client = %q, want the page identifier", operations.startCommand.Client)
}
operations.startErr = assessmentusecase.ErrAttemptWorkSessionBusy
response, err := handler.StartMyAssessmentAttemptSession(ctx, &api.AssessmentAttemptSessionStart{ExpectedVersion: 7}, params)
if err != nil {
t.Fatalf("StartMyAssessmentAttemptSession(held) error = %v", err)
}
conflict, ok := response.(*api.StartMyAssessmentAttemptSessionConflict)
if !ok || api.Problem(*conflict).Code != api.ProblemCodeStateConflict {
t.Fatalf("held attempt response = %T, want 409 state_conflict", response)
}
// Without the header the request is one anonymous page.
params.XAttemptClient = api.OptString{}
operations.startErr = nil
if _, err := handler.StartMyAssessmentAttemptSession(ctx, &api.AssessmentAttemptSessionStart{ExpectedVersion: 7}, params); err != nil || operations.startCommand.Client != "" {
t.Fatalf("StartSession() without a header: client %q, error %v, want an empty client", operations.startCommand.Client, err)
}
if _, ok := assessmentAnswerFailureOf(t, assessmentusecase.ErrAttemptWorkSessionBusy).(*api.SubmitMyAssessmentAnswerConflict); !ok {
t.Fatal("a held attempt did not answer 409 to an answer")
}
}
func assessmentAnswerFailureOf(t *testing.T, err error) api.SubmitMyAssessmentAnswerRes {
t.Helper()
response, mapped := assessmentAnswerFailure(context.Background(), err)
if mapped != nil {
t.Fatalf("assessmentAnswerFailure(%v) error = %v", err, mapped)
}
return response
}