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

@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math"
"time"
"github.com/google/uuid"
@@ -29,10 +30,18 @@ type AssessmentPresenter interface {
Present(snapshot domain.AssessmentAttemptPublicSnapshot) (domain.AssessmentAttemptPublicSnapshot, error)
}
// AssessmentWorkSessions holds and releases the single active work session of a
// real attempt for the open page that calls.
type AssessmentWorkSessions interface {
Hold(ctx context.Context, command assessmentusecase.HoldWorkSessionCommand) (assessmentusecase.WorkSessionHold, error)
Release(ctx context.Context, command assessmentusecase.ReleaseWorkSessionCommand) error
}
// AssessmentHandler serves the candidate's frozen assessment attempts.
type AssessmentHandler struct {
operations AssessmentOperations
presenter AssessmentPresenter
operations AssessmentOperations
presenter AssessmentPresenter
workSessions AssessmentWorkSessions
}
func NewAssessmentHandler(operations AssessmentOperations) *AssessmentHandler {
@@ -49,6 +58,13 @@ func (handler *AssessmentHandler) WithPresenter(presenter AssessmentPresenter) *
return handler
}
// WithWorkSessions enables the hold and release operations of the work session.
// Without it they answer as not implemented.
func (handler *AssessmentHandler) WithWorkSessions(sessions AssessmentWorkSessions) *AssessmentHandler {
handler.workSessions = sessions
return handler
}
// attemptResponse presents the snapshot and maps it to the response. Every
// operation that returns an attempt goes through it.
func (handler *AssessmentHandler) attemptResponse(snapshot domain.AssessmentAttemptPublicSnapshot) (api.AssessmentAttempt, error) {
@@ -114,6 +130,26 @@ func (handler *Handler) SubmitMyAssessmentAnswer(
return handler.assessment.SubmitMyAssessmentAnswer(ctx, request, params)
}
func (handler *Handler) HoldMyAssessmentWorkSession(
ctx context.Context,
params api.HoldMyAssessmentWorkSessionParams,
) (api.HoldMyAssessmentWorkSessionRes, error) {
if handler.assessment == nil || handler.assessment.workSessions == nil {
return handler.UnimplementedHandler.HoldMyAssessmentWorkSession(ctx, params)
}
return handler.assessment.HoldMyAssessmentWorkSession(ctx, params)
}
func (handler *Handler) ReleaseMyAssessmentWorkSession(
ctx context.Context,
params api.ReleaseMyAssessmentWorkSessionParams,
) (api.ReleaseMyAssessmentWorkSessionRes, error) {
if handler.assessment == nil || handler.assessment.workSessions == nil {
return handler.UnimplementedHandler.ReleaseMyAssessmentWorkSession(ctx, params)
}
return handler.assessment.ReleaseMyAssessmentWorkSession(ctx, params)
}
func (handler *Handler) FinalizeMyAssessmentAttempt(
ctx context.Context,
request *api.AssessmentFinalization,
@@ -199,6 +235,7 @@ func (handler *AssessmentHandler) StartMyAssessmentAttemptSession(
result, err := handler.operations.StartSession(ctx, assessmentusecase.StartAttemptSessionCommand{
ActorID: principal.UserID, AttemptID: domain.AssessmentAttemptID(params.AttemptID),
ExpectedVersion: int64(request.ExpectedVersion), IdempotencyKey: params.IdempotencyKey,
Client: params.XAttemptClient.Or(""),
})
if err != nil {
return assessmentSessionFailure(ctx, err)
@@ -236,6 +273,7 @@ func (handler *AssessmentHandler) SubmitMyAssessmentAnswer(
ActorID: principal.UserID, AttemptID: domain.AssessmentAttemptID(params.AttemptID),
ItemID: uuid.UUID(request.ItemID), ExpectedVersion: int64(request.ExpectedVersion),
IdempotencyKey: params.IdempotencyKey, Answer: answer, Origin: assessmentRequestOrigin(ctx),
Client: params.XAttemptClient.Or(""),
})
if err != nil {
return assessmentAnswerFailure(ctx, err)
@@ -250,6 +288,46 @@ func (handler *AssessmentHandler) SubmitMyAssessmentAnswer(
return &api.AssessmentAttemptHeaders{CacheControl: api.CacheControlNoStoreNoStore, Response: response}, nil
}
func (handler *AssessmentHandler) HoldMyAssessmentWorkSession(
ctx context.Context,
params api.HoldMyAssessmentWorkSessionParams,
) (api.HoldMyAssessmentWorkSessionRes, error) {
principal, ok := PrincipalFromContext(ctx)
if !ok {
return (*api.HoldMyAssessmentWorkSessionUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil
}
hold, err := handler.workSessions.Hold(ctx, assessmentusecase.HoldWorkSessionCommand{
ActorID: principal.UserID, AttemptID: domain.AssessmentAttemptID(params.AttemptID),
Client: params.XAttemptClient, Origin: assessmentRequestOrigin(ctx),
})
if err != nil {
return assessmentHoldFailure(ctx, err)
}
return &api.AssessmentWorkSessionHeaders{
CacheControl: api.CacheControlNoStoreNoStore,
Response: api.AssessmentWorkSession{
HeldUntil: api.Timestamp(hold.HeldUntil), RenewAfterSeconds: int32(math.Ceil(hold.RenewEvery.Seconds())),
},
}, nil
}
func (handler *AssessmentHandler) ReleaseMyAssessmentWorkSession(
ctx context.Context,
params api.ReleaseMyAssessmentWorkSessionParams,
) (api.ReleaseMyAssessmentWorkSessionRes, error) {
principal, ok := PrincipalFromContext(ctx)
if !ok {
return (*api.ReleaseMyAssessmentWorkSessionUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil
}
err := handler.workSessions.Release(ctx, assessmentusecase.ReleaseWorkSessionCommand{
ActorID: principal.UserID, AttemptID: domain.AssessmentAttemptID(params.AttemptID), Client: params.XAttemptClient,
})
if err != nil {
return assessmentReleaseFailure(ctx, err)
}
return &api.ReleaseMyAssessmentWorkSessionNoContent{CacheControl: api.CacheControlNoStoreNoStore}, nil
}
func (handler *AssessmentHandler) FinalizeMyAssessmentAttempt(
ctx context.Context,
request *api.AssessmentFinalization,
@@ -407,6 +485,8 @@ func assessmentSessionFailure(ctx context.Context, err error) (api.StartMyAssess
case errors.Is(err, assessmentusecase.ErrAttemptVersionConflict), errors.Is(err, outbound.ErrAssessmentAttemptVersionConflict),
errors.Is(err, domain.ErrAssessmentAttemptVersionConflict):
return (*api.StartMyAssessmentAttemptSessionPreconditionFailed)(&problem.New(ctx, api.ProblemCodeVersionConflict).Response), nil
case errors.Is(err, assessmentusecase.ErrAttemptWorkSessionBusy):
return (*api.StartMyAssessmentAttemptSessionConflict)(&problem.New(ctx, api.ProblemCodeStateConflict).Response), nil
case errors.Is(err, assessmentusecase.ErrAttemptKeyReused), errors.Is(err, outbound.ErrAssessmentAttemptIdempotencyConflict):
return (*api.StartMyAssessmentAttemptSessionUnprocessableEntity)(&problem.New(ctx, api.ProblemCodeIdempotencyKeyReused).Response), nil
case errors.Is(err, assessmentusecase.ErrInvalidAttemptKey):
@@ -418,6 +498,35 @@ func assessmentSessionFailure(ctx context.Context, err error) (api.StartMyAssess
}
}
func assessmentHoldFailure(ctx context.Context, err error) (api.HoldMyAssessmentWorkSessionRes, error) {
switch {
case errors.Is(err, assessmentusecase.ErrAttemptForbidden), errors.Is(err, assessmentusecase.ErrAttemptEmailNotVerified),
errors.Is(err, assessmentusecase.ErrAttemptConsentRequired):
return (*api.HoldMyAssessmentWorkSessionForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil
case errors.Is(err, assessmentusecase.ErrAttemptNotFound), errors.Is(err, outbound.ErrAssessmentAttemptNotFound):
return (*api.HoldMyAssessmentWorkSessionNotFound)(&problem.New(ctx, api.ProblemCodeNotFound).Response), nil
case errors.Is(err, assessmentusecase.ErrAttemptWorkSessionBusy), errors.Is(err, assessmentusecase.ErrAttemptWorkSessionState):
return (*api.HoldMyAssessmentWorkSessionConflict)(&problem.New(ctx, api.ProblemCodeStateConflict).Response), nil
case errors.Is(err, assessmentusecase.ErrInvalidWorkSessionClient):
return (*api.HoldMyAssessmentWorkSessionBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationHeader, "/X-Attempt-Client").Response), nil
default:
return nil, err
}
}
func assessmentReleaseFailure(ctx context.Context, err error) (api.ReleaseMyAssessmentWorkSessionRes, error) {
switch {
case errors.Is(err, assessmentusecase.ErrAttemptForbidden), errors.Is(err, assessmentusecase.ErrAttemptEmailNotVerified):
return (*api.ReleaseMyAssessmentWorkSessionForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil
case errors.Is(err, assessmentusecase.ErrAttemptNotFound), errors.Is(err, outbound.ErrAssessmentAttemptNotFound):
return (*api.ReleaseMyAssessmentWorkSessionNotFound)(&problem.New(ctx, api.ProblemCodeNotFound).Response), nil
case errors.Is(err, assessmentusecase.ErrInvalidWorkSessionClient):
return (*api.ReleaseMyAssessmentWorkSessionBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationHeader, "/X-Attempt-Client").Response), nil
default:
return nil, err
}
}
func assessmentAnswerFailure(ctx context.Context, err error) (api.SubmitMyAssessmentAnswerRes, error) {
switch {
case errors.Is(err, assessmentusecase.ErrAttemptForbidden), errors.Is(err, assessmentusecase.ErrAttemptEmailNotVerified),
@@ -428,7 +537,7 @@ func assessmentAnswerFailure(ctx context.Context, err error) (api.SubmitMyAssess
case errors.Is(err, assessmentusecase.ErrAttemptVersionConflict), errors.Is(err, outbound.ErrAssessmentAttemptVersionConflict),
errors.Is(err, domain.ErrAssessmentAttemptVersionConflict):
return (*api.SubmitMyAssessmentAnswerPreconditionFailed)(&problem.New(ctx, api.ProblemCodeVersionConflict).Response), nil
case errors.Is(err, assessmentusecase.ErrAttemptStateConflict):
case errors.Is(err, assessmentusecase.ErrAttemptStateConflict), errors.Is(err, assessmentusecase.ErrAttemptWorkSessionBusy):
return (*api.SubmitMyAssessmentAnswerConflict)(&problem.New(ctx, api.ProblemCodeStateConflict).Response), nil
case errors.Is(err, assessmentusecase.ErrAttemptKeyReused), errors.Is(err, outbound.ErrAssessmentAttemptIdempotencyConflict):
return (*api.SubmitMyAssessmentAnswerUnprocessableEntity)(&problem.New(ctx, api.ProblemCodeIdempotencyKeyReused).Response), nil