feat(practice): store code submissions and practice states apart from attempts
This commit is contained in:
1 parent
d5e169e71b
commit
51770eea6c
61 files changed
+8584
-135
No files matched your search
@@ -0,0 +1,568 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// ErrInvalidAssessmentPractice reports practice content, a submission, or a
|
||||
// run result that breaks the rules below.
|
||||
var ErrInvalidAssessmentPractice = errors.New("invalid assessment practice")
|
||||
|
||||
// Bounds of practice content and submissions. The runner contract bounds the
|
||||
// technical limits and the payload; these are the product bounds of a task.
|
||||
const (
|
||||
// PracticeHiddenGroups is the number of hidden test groups: the verdict
|
||||
// scores one point for each group that passes.
|
||||
PracticeHiddenGroups = 3
|
||||
// MaxPracticeSourceBytes is the runner bound of a source, which is also
|
||||
// the largest max_source_length a task may set in characters.
|
||||
MaxPracticeSourceBytes = 65536
|
||||
// MaxPracticePublicTests bounds the public examples of a task, and
|
||||
// MaxPracticeCustomTests the candidate's own tests of one debug run.
|
||||
MaxPracticePublicTests = 10
|
||||
MaxPracticeCustomTests = 10
|
||||
// MaxPracticeHiddenTests bounds the tests of one hidden group, as the
|
||||
// runner bounds the tests of a group.
|
||||
MaxPracticeHiddenTests = 50
|
||||
// MaxPracticeExampleText bounds the input and the expected output of a
|
||||
// public example and of a candidate test in characters.
|
||||
MaxPracticeExampleText = 10000
|
||||
// MaxPracticeHiddenText bounds the input and the expected output of a
|
||||
// hidden test in bytes, as the runner bounds a test text.
|
||||
MaxPracticeHiddenText = 262144
|
||||
// maxPracticeLanguages is the number of task languages.
|
||||
maxPracticeLanguages = 2
|
||||
|
||||
redactedPracticeTask = "[redacted practice task]"
|
||||
redactedPracticeSubmission = "[redacted practice submission]"
|
||||
)
|
||||
|
||||
// AssessmentPracticeKind is the role of a practice in its attempt.
|
||||
type AssessmentPracticeKind string
|
||||
|
||||
const (
|
||||
AssessmentPracticeMain AssessmentPracticeKind = "main"
|
||||
AssessmentPracticeAdditional AssessmentPracticeKind = "additional"
|
||||
)
|
||||
|
||||
func (kind AssessmentPracticeKind) IsKnown() bool {
|
||||
return kind == AssessmentPracticeMain || kind == AssessmentPracticeAdditional
|
||||
}
|
||||
|
||||
// AttemptState is the attempt state in which the practice is the current
|
||||
// item: only then does it accept runs and submissions.
|
||||
func (kind AssessmentPracticeKind) AttemptState() AssessmentAttemptState {
|
||||
if kind == AssessmentPracticeAdditional {
|
||||
return AssessmentAttemptStateAdditionalPracticePending
|
||||
}
|
||||
return AssessmentAttemptStatePracticePending
|
||||
}
|
||||
|
||||
// AssessmentPracticeStatus is the state of a practice as the candidate sees it.
|
||||
type AssessmentPracticeStatus string
|
||||
|
||||
const (
|
||||
AssessmentPracticeNotStarted AssessmentPracticeStatus = "not_started"
|
||||
AssessmentPracticePending AssessmentPracticeStatus = "pending"
|
||||
AssessmentPracticeFinished AssessmentPracticeStatus = "finished"
|
||||
AssessmentPracticeFailed AssessmentPracticeStatus = "failed"
|
||||
)
|
||||
|
||||
func (status AssessmentPracticeStatus) IsKnown() bool {
|
||||
switch status {
|
||||
case AssessmentPracticeNotStarted, AssessmentPracticePending, AssessmentPracticeFinished, AssessmentPracticeFailed:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// AcceptsFinalSubmission reports whether a new final solution may be
|
||||
// submitted: nothing was submitted yet, or the sandbox failed the latest one.
|
||||
func (status AssessmentPracticeStatus) AcceptsFinalSubmission() bool {
|
||||
return status == AssessmentPracticeNotStarted || status == AssessmentPracticeFailed
|
||||
}
|
||||
|
||||
// AssessmentPracticeFailureReason says why the sandbox gave no verdict. It
|
||||
// never depends on the solution.
|
||||
type AssessmentPracticeFailureReason string
|
||||
|
||||
const (
|
||||
// AssessmentPracticeSandboxBusy: the practice queue was full.
|
||||
AssessmentPracticeSandboxBusy AssessmentPracticeFailureReason = "sandbox_busy"
|
||||
// AssessmentPracticeSandboxError: the infrastructure could not run it.
|
||||
AssessmentPracticeSandboxError AssessmentPracticeFailureReason = "sandbox_error"
|
||||
)
|
||||
|
||||
func (reason AssessmentPracticeFailureReason) IsKnown() bool {
|
||||
return reason == AssessmentPracticeSandboxBusy || reason == AssessmentPracticeSandboxError
|
||||
}
|
||||
|
||||
// PracticeSubmissionMode separates a debug run from the graded solution.
|
||||
type PracticeSubmissionMode string
|
||||
|
||||
const (
|
||||
// PracticeModePublic runs the public examples and the candidate's own
|
||||
// tests. It never runs or reports a hidden group.
|
||||
PracticeModePublic PracticeSubmissionMode = "public"
|
||||
// PracticeModeFinal runs the hidden groups that grade the practice.
|
||||
PracticeModeFinal PracticeSubmissionMode = "final"
|
||||
)
|
||||
|
||||
func (mode PracticeSubmissionMode) IsKnown() bool {
|
||||
return mode == PracticeModePublic || mode == PracticeModeFinal
|
||||
}
|
||||
|
||||
// PracticeJobState is the state of the execution job of a submission.
|
||||
type PracticeJobState string
|
||||
|
||||
const (
|
||||
PracticeJobQueued PracticeJobState = "queued"
|
||||
PracticeJobRunning PracticeJobState = "running"
|
||||
PracticeJobFinished PracticeJobState = "finished"
|
||||
// PracticeJobFailed means the infrastructure gave up on the job. It is
|
||||
// never a verdict on the code.
|
||||
PracticeJobFailed PracticeJobState = "failed"
|
||||
)
|
||||
|
||||
func (state PracticeJobState) IsKnown() bool {
|
||||
switch state {
|
||||
case PracticeJobQueued, PracticeJobRunning, PracticeJobFinished, PracticeJobFailed:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// PracticeCheckerKind says how the output of a test is compared.
|
||||
type PracticeCheckerKind string
|
||||
|
||||
const (
|
||||
PracticeCheckerExact PracticeCheckerKind = "exact"
|
||||
PracticeCheckerTokens PracticeCheckerKind = "tokens"
|
||||
PracticeCheckerNumbers PracticeCheckerKind = "numbers"
|
||||
PracticeCheckerCustom PracticeCheckerKind = "custom"
|
||||
)
|
||||
|
||||
// PracticeTest is one input with the output the checker expects.
|
||||
type PracticeTest struct {
|
||||
Input string `json:"input"`
|
||||
ExpectedOutput string `json:"expected_output"`
|
||||
}
|
||||
|
||||
// PracticeTestGroup is one hidden group of tests.
|
||||
type PracticeTestGroup struct {
|
||||
Tests []PracticeTest `json:"tests"`
|
||||
}
|
||||
|
||||
// PracticeCheckerProgram is the checker program of a custom checker.
|
||||
type PracticeCheckerProgram struct {
|
||||
Language TaskLanguageCode `json:"language"`
|
||||
Source string `json:"source"`
|
||||
}
|
||||
|
||||
// PracticeChecker configures the comparison of every group of the task.
|
||||
type PracticeChecker struct {
|
||||
Kind PracticeCheckerKind `json:"kind"`
|
||||
AbsTolerance float64 `json:"abs_tolerance,omitempty"`
|
||||
RelTolerance float64 `json:"rel_tolerance,omitempty"`
|
||||
Program *PracticeCheckerProgram `json:"program,omitempty"`
|
||||
}
|
||||
|
||||
// PracticeTask is the private content of a practice item: what the runner
|
||||
// needs to run a solution. The hidden groups are task secrets; they never
|
||||
// reach a candidate, a log, or an error. The statement and the public
|
||||
// projection of the item belong to the attempt item, not to this record.
|
||||
//
|
||||
// String, GoString, Format, and LogValue redact the whole task.
|
||||
type PracticeTask struct {
|
||||
// Languages lists the languages a solution may use, one or two.
|
||||
Languages []TaskLanguageCode `json:"languages"`
|
||||
// MaxSourceLength bounds the solution in characters.
|
||||
MaxSourceLength int `json:"max_source_length"`
|
||||
// TimeLimitMs, MemoryLimitMB, and OutputLimitKB apply to every test.
|
||||
TimeLimitMs int32 `json:"time_limit_ms"`
|
||||
MemoryLimitMB int32 `json:"memory_limit_mb"`
|
||||
OutputLimitKB int32 `json:"output_limit_kb"`
|
||||
Checker PracticeChecker `json:"checker"`
|
||||
// PublicTests are the public examples the candidate sees.
|
||||
PublicTests []PracticeTest `json:"public_tests"`
|
||||
// HiddenGroups are the PracticeHiddenGroups groups that grade a final
|
||||
// solution.
|
||||
HiddenGroups []PracticeTestGroup `json:"hidden_groups"`
|
||||
}
|
||||
|
||||
func (task PracticeTask) String() string { return redactedPracticeTask }
|
||||
func (task PracticeTask) GoString() string { return redactedPracticeTask }
|
||||
func (task PracticeTask) Format(state fmt.State, _ rune) {
|
||||
writeRedacted(state, redactedPracticeTask)
|
||||
}
|
||||
func (task PracticeTask) LogValue() slog.Value { return slog.StringValue(redactedPracticeTask) }
|
||||
|
||||
// Validate checks the product rules of the task. The runner rules (limit
|
||||
// ranges, checker parameters, the CPU budget, the payload size) are checked
|
||||
// when a run request is built from it. Errors never quote task content.
|
||||
func (task PracticeTask) Validate() error {
|
||||
const location = "PracticeTask.Validate"
|
||||
if err := validatePracticeLanguages(task.Languages); err != nil {
|
||||
return err
|
||||
}
|
||||
if task.MaxSourceLength < 1 || task.MaxSourceLength > MaxPracticeSourceBytes {
|
||||
return practiceError(location, "max source length must be between 1 and 65536")
|
||||
}
|
||||
if task.TimeLimitMs <= 0 || task.MemoryLimitMB <= 0 || task.OutputLimitKB <= 0 {
|
||||
return practiceError(location, "limits must be positive")
|
||||
}
|
||||
if err := task.Checker.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(task.PublicTests) < 1 || len(task.PublicTests) > MaxPracticePublicTests {
|
||||
return practiceError(location, "public tests must have 1 to 10 entries")
|
||||
}
|
||||
for _, test := range task.PublicTests {
|
||||
if err := test.validate(MaxPracticeExampleText, true); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return validatePracticeHiddenGroups(task.HiddenGroups)
|
||||
}
|
||||
|
||||
func validatePracticeLanguages(languages []TaskLanguageCode) error {
|
||||
const location = "domain.validatePracticeLanguages"
|
||||
if len(languages) < 1 || len(languages) > maxPracticeLanguages {
|
||||
return practiceError(location, "languages must have one or two entries")
|
||||
}
|
||||
seen := make(map[TaskLanguageCode]struct{}, len(languages))
|
||||
for _, language := range languages {
|
||||
if _, duplicate := seen[language]; duplicate || !language.IsKnown() {
|
||||
return practiceError(location, "languages must be distinct known task languages")
|
||||
}
|
||||
seen[language] = struct{}{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validatePracticeHiddenGroups(groups []PracticeTestGroup) error {
|
||||
const location = "domain.validatePracticeHiddenGroups"
|
||||
if len(groups) != PracticeHiddenGroups {
|
||||
return practiceError(location, "hidden groups must have exactly three entries")
|
||||
}
|
||||
for _, group := range groups {
|
||||
if len(group.Tests) < 1 || len(group.Tests) > MaxPracticeHiddenTests {
|
||||
return practiceError(location, "a hidden group must have 1 to 50 tests")
|
||||
}
|
||||
for _, test := range group.Tests {
|
||||
if err := test.validate(MaxPracticeHiddenText, false); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AllowsLanguage reports whether a solution may use the language.
|
||||
func (task PracticeTask) AllowsLanguage(language TaskLanguageCode) bool {
|
||||
for _, allowed := range task.Languages {
|
||||
if allowed == language {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (checker PracticeChecker) validate() error {
|
||||
const location = "PracticeChecker.validate"
|
||||
switch checker.Kind {
|
||||
case PracticeCheckerExact, PracticeCheckerTokens, PracticeCheckerNumbers:
|
||||
if checker.Program != nil {
|
||||
return practiceError(location, "only a custom checker has a program")
|
||||
}
|
||||
case PracticeCheckerCustom:
|
||||
if checker.Program == nil || !checker.Program.Language.IsKnown() || strings.TrimSpace(checker.Program.Source) == "" {
|
||||
return practiceError(location, "a custom checker needs a program in a task language")
|
||||
}
|
||||
default:
|
||||
return practiceError(location, "checker kind must be exact, tokens, numbers, or custom")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validate checks one test. maxLength counts characters when byCharacters
|
||||
// is set and bytes otherwise.
|
||||
func (test PracticeTest) validate(maxLength int, byCharacters bool) error {
|
||||
const location = "PracticeTest.validate"
|
||||
for _, text := range []string{test.Input, test.ExpectedOutput} {
|
||||
if !utf8.ValidString(text) || strings.ContainsRune(text, 0) {
|
||||
return practiceError(location, "test texts must be valid UTF-8 without NUL")
|
||||
}
|
||||
length := len(text)
|
||||
if byCharacters {
|
||||
length = utf8.RuneCountInString(text)
|
||||
}
|
||||
if length > maxLength {
|
||||
return practiceError(location, "a test text is too long")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidatePracticeCustomTests checks the candidate's own tests of a debug run.
|
||||
func ValidatePracticeCustomTests(tests []PracticeTest) error {
|
||||
if len(tests) > MaxPracticeCustomTests {
|
||||
return practiceError("domain.ValidatePracticeCustomTests", "at most 10 custom tests are allowed")
|
||||
}
|
||||
for _, test := range tests {
|
||||
if err := test.validate(MaxPracticeExampleText, true); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidatePracticeSource checks a solution source against the task bound in
|
||||
// characters and the runner bound in bytes.
|
||||
func ValidatePracticeSource(source string, maxLength int) error {
|
||||
const location = "domain.ValidatePracticeSource"
|
||||
switch {
|
||||
case strings.TrimSpace(source) == "":
|
||||
return practiceError(location, "source must not be blank")
|
||||
case !utf8.ValidString(source) || strings.ContainsRune(source, 0):
|
||||
return practiceError(location, "source must be valid UTF-8 without NUL")
|
||||
case len(source) > MaxPracticeSourceBytes || utf8.RuneCountInString(source) > maxLength:
|
||||
return practiceError(location, "source is too long")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AssessmentPractice is one practice item of an attempt with its state.
|
||||
type AssessmentPractice struct {
|
||||
ID uuid.UUID
|
||||
AttemptID AssessmentAttemptID
|
||||
UserID uuid.UUID
|
||||
Kind AssessmentPracticeKind
|
||||
Task PracticeTask
|
||||
Status AssessmentPracticeStatus
|
||||
FailureReason AssessmentPracticeFailureReason
|
||||
// SubmittedAt is the time of the latest final submission; zero before
|
||||
// the first one.
|
||||
SubmittedAt time.Time
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// Validate checks that the practice can be stored or was restored intact.
|
||||
func (practice AssessmentPractice) Validate() error {
|
||||
const location = "AssessmentPractice.Validate"
|
||||
switch {
|
||||
case practice.ID == uuid.Nil, practice.AttemptID.IsZero(), practice.UserID == uuid.Nil:
|
||||
return practiceError(location, "practice, attempt, and owner IDs are required")
|
||||
case !practice.Kind.IsKnown(), !practice.Status.IsKnown():
|
||||
return practiceError(location, "kind and status must be known")
|
||||
case (practice.Status == AssessmentPracticeFailed) != practice.FailureReason.IsKnown(),
|
||||
practice.Status != AssessmentPracticeFailed && practice.FailureReason != "":
|
||||
return practiceError(location, "only a failed practice has a failure reason")
|
||||
case (practice.Status == AssessmentPracticeNotStarted) != practice.SubmittedAt.IsZero():
|
||||
return practiceError(location, "only a submitted practice has a submission time")
|
||||
case practice.CreatedAt.IsZero():
|
||||
return practiceError(location, "creation time is required")
|
||||
}
|
||||
return practice.Task.Validate()
|
||||
}
|
||||
|
||||
// View is the candidate's view of the practice state. It holds nothing of
|
||||
// the task.
|
||||
func (practice AssessmentPractice) View() AssessmentPracticeView {
|
||||
view := AssessmentPracticeView{
|
||||
Kind: practice.Kind, ItemID: practice.ID, Status: practice.Status, FailureReason: practice.FailureReason,
|
||||
}
|
||||
if !practice.SubmittedAt.IsZero() {
|
||||
submittedAt := practice.SubmittedAt
|
||||
view.SubmittedAt = &submittedAt
|
||||
}
|
||||
return view
|
||||
}
|
||||
|
||||
// AssessmentPracticeView is the state of a practice that the candidate may
|
||||
// see: never the task content, the hidden groups, or the source.
|
||||
type AssessmentPracticeView struct {
|
||||
Kind AssessmentPracticeKind
|
||||
ItemID uuid.UUID
|
||||
Status AssessmentPracticeStatus
|
||||
SubmittedAt *time.Time
|
||||
FailureReason AssessmentPracticeFailureReason
|
||||
}
|
||||
|
||||
// PracticeSourceDigest is the SHA-256 of a source; two final submissions are
|
||||
// the same solution when their languages and digests are equal.
|
||||
type PracticeSourceDigest [sha256.Size]byte
|
||||
|
||||
// DigestPracticeSource returns the digest of a source.
|
||||
func DigestPracticeSource(source string) PracticeSourceDigest {
|
||||
return sha256.Sum256([]byte(source))
|
||||
}
|
||||
|
||||
// PracticeSubmission is one stored submission of candidate code. Its request
|
||||
// part is immutable.
|
||||
//
|
||||
// String, GoString, Format, and LogValue redact the source and the tests,
|
||||
// which are candidate work.
|
||||
type PracticeSubmission struct {
|
||||
ID uuid.UUID
|
||||
PracticeID uuid.UUID
|
||||
UserID uuid.UUID
|
||||
Mode PracticeSubmissionMode
|
||||
Language TaskLanguageCode
|
||||
Source string
|
||||
CustomTests []PracticeTest
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (submission PracticeSubmission) String() string { return redactedPracticeSubmission }
|
||||
func (submission PracticeSubmission) GoString() string { return redactedPracticeSubmission }
|
||||
func (submission PracticeSubmission) Format(state fmt.State, _ rune) {
|
||||
writeRedacted(state, redactedPracticeSubmission)
|
||||
}
|
||||
func (submission PracticeSubmission) LogValue() slog.Value {
|
||||
return slog.GroupValue(
|
||||
slog.String("submission_id", submission.ID.String()),
|
||||
slog.String("mode", string(submission.Mode)),
|
||||
slog.String("language", string(submission.Language)),
|
||||
)
|
||||
}
|
||||
|
||||
// Validate checks the submission against the task of its practice.
|
||||
func (submission PracticeSubmission) Validate(task PracticeTask) error {
|
||||
const location = "PracticeSubmission.Validate"
|
||||
switch {
|
||||
case submission.ID == uuid.Nil, submission.PracticeID == uuid.Nil, submission.UserID == uuid.Nil:
|
||||
return practiceError(location, "submission, practice, and owner IDs are required")
|
||||
case !submission.Mode.IsKnown():
|
||||
return practiceError(location, "mode must be public or final")
|
||||
case !task.AllowsLanguage(submission.Language):
|
||||
return practiceError(location, "language is not offered by the task")
|
||||
case submission.Mode == PracticeModeFinal && len(submission.CustomTests) > 0:
|
||||
return practiceError(location, "a final submission has no custom tests")
|
||||
case submission.CreatedAt.IsZero():
|
||||
return practiceError(location, "creation time is required")
|
||||
}
|
||||
if err := ValidatePracticeSource(submission.Source, task.MaxSourceLength); err != nil {
|
||||
return err
|
||||
}
|
||||
return ValidatePracticeCustomTests(submission.CustomTests)
|
||||
}
|
||||
|
||||
// PracticeOutcome is the verdict of a test, a group, a compile step, or a
|
||||
// run, as the runner reports it.
|
||||
type PracticeOutcome string
|
||||
|
||||
const (
|
||||
PracticeOutcomeAccepted PracticeOutcome = "accepted"
|
||||
PracticeOutcomeWrongAnswer PracticeOutcome = "wrong_answer"
|
||||
PracticeOutcomeCompileError PracticeOutcome = "compile_error"
|
||||
PracticeOutcomeTimeLimitExceeded PracticeOutcome = "time_limit_exceeded"
|
||||
PracticeOutcomeMemoryLimitExceeded PracticeOutcome = "memory_limit_exceeded"
|
||||
PracticeOutcomeOutputLimitExceeded PracticeOutcome = "output_limit_exceeded"
|
||||
PracticeOutcomeRuntimeError PracticeOutcome = "runtime_error"
|
||||
)
|
||||
|
||||
func (outcome PracticeOutcome) IsKnown() bool {
|
||||
switch outcome {
|
||||
case PracticeOutcomeAccepted, PracticeOutcomeWrongAnswer, PracticeOutcomeCompileError,
|
||||
PracticeOutcomeTimeLimitExceeded, PracticeOutcomeMemoryLimitExceeded,
|
||||
PracticeOutcomeOutputLimitExceeded, PracticeOutcomeRuntimeError:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// PracticeTestOutcome is the result of one evaluated public test.
|
||||
type PracticeTestOutcome struct {
|
||||
ID string `json:"id"`
|
||||
Outcome PracticeOutcome `json:"outcome"`
|
||||
TimeMs int64 `json:"time_ms"`
|
||||
MemoryKB int64 `json:"memory_kb"`
|
||||
Diagnostic string `json:"diagnostic,omitempty"`
|
||||
}
|
||||
|
||||
// PracticeGroupOutcome is the result of one group. A hidden group keeps only
|
||||
// its outcome and counts, never its tests.
|
||||
type PracticeGroupOutcome struct {
|
||||
ID string `json:"id"`
|
||||
Hidden bool `json:"hidden"`
|
||||
Outcome PracticeOutcome `json:"outcome"`
|
||||
PassedTests int `json:"passed_tests"`
|
||||
TotalTests int `json:"total_tests"`
|
||||
Tests []PracticeTestOutcome `json:"tests,omitempty"`
|
||||
}
|
||||
|
||||
// PracticeRunResult is the stored result of a run.
|
||||
type PracticeRunResult struct {
|
||||
Outcome PracticeOutcome `json:"outcome"`
|
||||
// CompileDiagnostic is the compiler message of a compile error. It can
|
||||
// quote the source, never test data.
|
||||
CompileDiagnostic string `json:"compile_diagnostic,omitempty"`
|
||||
Groups []PracticeGroupOutcome `json:"groups"`
|
||||
DurationMs int64 `json:"duration_ms"`
|
||||
}
|
||||
|
||||
// Validate checks that the result can be stored and shown.
|
||||
func (result PracticeRunResult) Validate() error {
|
||||
const location = "PracticeRunResult.Validate"
|
||||
if !result.Outcome.IsKnown() || result.DurationMs < 0 {
|
||||
return practiceError(location, "outcome must be known and duration not negative")
|
||||
}
|
||||
if result.CompileDiagnostic != "" && result.Outcome != PracticeOutcomeCompileError {
|
||||
return practiceError(location, "only a compile error has a compile diagnostic")
|
||||
}
|
||||
for _, group := range result.Groups {
|
||||
if group.ID == "" || !group.Outcome.IsKnown() || group.PassedTests < 0 || group.PassedTests > group.TotalTests {
|
||||
return practiceError(location, "group outcome and counts are inconsistent")
|
||||
}
|
||||
if group.Hidden && len(group.Tests) > 0 {
|
||||
return practiceError(location, "a hidden group keeps no test results")
|
||||
}
|
||||
for _, test := range group.Tests {
|
||||
if test.ID == "" || !test.Outcome.IsKnown() {
|
||||
return practiceError(location, "test outcome is unknown")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Public returns the result without hidden groups. Only this projection may
|
||||
// be shown for a debug run.
|
||||
func (result PracticeRunResult) Public() PracticeRunResult {
|
||||
public := result
|
||||
public.Groups = make([]PracticeGroupOutcome, 0, len(result.Groups))
|
||||
for _, group := range result.Groups {
|
||||
if !group.Hidden {
|
||||
public.Groups = append(public.Groups, group)
|
||||
}
|
||||
}
|
||||
return public
|
||||
}
|
||||
|
||||
// PracticeRunView is a debug run as the candidate sees it: its state and the
|
||||
// public result. A debug run never runs a hidden group, and the view never
|
||||
// carries one.
|
||||
type PracticeRunView struct {
|
||||
ID uuid.UUID
|
||||
PracticeID uuid.UUID
|
||||
State PracticeJobState
|
||||
CreatedAt time.Time
|
||||
FinishedAt *time.Time
|
||||
Result *PracticeRunResult
|
||||
}
|
||||
|
||||
func practiceError(location, reason string) error {
|
||||
return fmt.Errorf("%s - %s: %w", location, reason, ErrInvalidAssessmentPractice)
|
||||
}
|
||||
Reference in new issue
Block a user