feat(assessment): add table families that pick a variant row by seed

This commit is contained in:
shira committed 2026-10-11 08:19:17 +03:00
1 parent f1147b1885
commit 5e1db37746
16 files changed
+1023 -56

No files matched your search

@@ -78,3 +78,69 @@ func TestBundleRejectsInvalidDocuments(t *testing.T) {
t.Fatalf("Drafts(no families) error = %v, want ErrInvalidAssessmentFamily", err)
}
}
// tableBundleDocument is a bundle with a declarative family: the variants are
// rows of a table, and the answers are written in the rows.
const tableBundleDocument = `{
"name": "table-bundle-v1",
"category": "backend",
"author": "operator:author",
"review": {"status": "approved", "reviewer": "operator:reviewer"},
"families": [{
"key": "http-status-table",
"version": 1,
"specialization": "http",
"title": "Status codes",
"seed_generator": {
"statement_template": "Which status code does {{.situation}} return?",
"table": {"rows": [
{"values": {"situation": "a missing resource"}, "answer": "404", "distractors": ["400", "410", "500"]},
{"values": {"situation": "a request without credentials"}, "answer": "401", "distractors": ["403", "407"]}
]}
},
"answer_key": {"answer_format": "choice", "reference_solution": "Match the situation to the status code."},
"rubric": {"principle": "Compare with the key.", "criteria": [{"id": "correct_answer", "description": "Selects the key."}]}
}]
}`
func TestTableFamilyBundleIsParsedAndDrafted(t *testing.T) {
bundle, err := ParseBundle(strings.NewReader(tableBundleDocument))
if err != nil {
t.Fatalf("ParseBundle(table bundle) error = %v", err)
}
drafts, err := bundle.Drafts()
if err != nil || len(drafts) != 1 {
t.Fatalf("Drafts() = %d drafts, %v, want one", len(drafts), err)
}
if drafts[0].HashScheme() != 2 {
t.Fatalf("table family scheme = %d, want 2", drafts[0].HashScheme())
}
again, err := bundle.Drafts()
if err != nil || again[0].ContentHash() != drafts[0].ContentHash() {
t.Fatalf("Drafts() twice hashed differently: %v", err)
}
}
func TestTableFamilyBundleRefusesAFormatErrorWithTheRowIndex(t *testing.T) {
broken := strings.Replace(tableBundleDocument, `"answer": "401"`, `"answer": "403"`, 1)
bundle, err := ParseBundle(strings.NewReader(broken))
if err != nil {
t.Fatalf("ParseBundle() error = %v", err)
}
_, err = bundle.Drafts()
if !errors.Is(err, domain.ErrInvalidAssessmentFamily) || !strings.Contains(err.Error(), "rows[1]") {
t.Fatalf("Drafts(answer repeated among the distractors) error = %v, want ErrInvalidAssessmentFamily naming rows[1]", err)
}
// An unknown field of a row is a format error at parse time: the table
// holds data only, and a field that could carry code is not ignored.
for name, replacement := range map[string]string{
"a script field": `"values": {"situation": "a missing resource"}, "script": "1+1", "answer": "404"`,
"a formula field": `"values": {"situation": "a missing resource"}, "formula": "len(values)", "answer": "404"`,
} {
document := strings.Replace(tableBundleDocument, `"values": {"situation": "a missing resource"}, "answer": "404"`, replacement, 1)
if _, err := ParseBundle(strings.NewReader(document)); !errors.Is(err, domain.ErrInvalidAssessmentFamily) {
t.Fatalf("ParseBundle(%s) error = %v, want ErrInvalidAssessmentFamily", name, err)
}
}
}