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

@@ -478,6 +478,51 @@ func TestAssessmentBank(t *testing.T) {
assertPgError(t, "unknown hash scheme", err, assessmentCheckSQLState, "assessment_families_hash_scheme_known")
})
t.Run("a table family is stored with hash scheme 2 and imports again unchanged", func(t *testing.T) {
fixture := newFixture(t)
bundle := fixture.demoBundle
bundle.Name = "table-family-bundle"
bundle.Families = choiceTableBankFamilies(t, bundle.Families[0])
first, err := fixture.bank.ImportBundle(ctx, bundle)
if err != nil || len(first.Created) != 3 || len(first.Unchanged) != 0 {
t.Fatalf("ImportBundle(table bundle) = %+v, %v, want three created versions", first, err)
}
fixture.assertCount(t, `SELECT count(*) FROM assessment_families WHERE hash_scheme = 2`, 2)
fixture.assertCount(t, `SELECT count(*) FROM assessment_families WHERE hash_scheme = 1`, 1)
again, err := fixture.bank.ImportBundle(ctx, bundle)
if err != nil || len(again.Created) != 0 || len(again.Unchanged) != 3 {
t.Fatalf("ImportBundle(table bundle again) = %+v, %v, want three unchanged versions", again, err)
}
// The stored row is verified with its own scheme when it is read, and
// the same seed gives the same variant from it.
for _, family := range bundle.Families {
key := domain.AssessmentFamilyKey(family.Key)
stored, err := fixture.store.AssessmentBank().GetFamilyVersion(ctx, key, 1)
if err != nil {
t.Fatalf("GetFamilyVersion(%s) error = %v", key, err)
}
wantScheme := int16(2)
if family.SeedGenerator.Table == nil {
wantScheme = 1
}
if stored.HashScheme() != wantScheme {
t.Fatalf("family %s scheme = %d, want %d", key, stored.HashScheme(), wantScheme)
}
one, err := fixture.bank.GenerateVariant(ctx, key, 1, 77)
if err != nil {
t.Fatalf("GenerateVariant(%s) error = %v", key, err)
}
two, err := fixture.bank.GenerateVariant(ctx, key, 1, 77)
if err != nil || one.Statement != two.Statement || one.ReferenceAnswer != two.ReferenceAnswer || !slices.Equal(one.Options, two.Options) {
t.Fatalf("GenerateVariant(%s) twice = %+v and %+v (%v), want one variant", key, one, two, err)
}
}
// A table row edited in the database fails the hash, whatever the edit
// path: the trigger refuses the edit, and the row stays as imported.
_, err = fixture.pool.Exec(ctx, `UPDATE assessment_families SET seed_generator = replace(seed_generator::text, '"404"', '"405"')::jsonb WHERE hash_scheme = 2`)
assertPgError(t, "edit a table", err, assessmentIntegritySQLState, "assessment_families_immutable")
})
t.Run("families are listed in pages after a cursor", func(t *testing.T) {
fixture := newFixture(t)
bundle := fixture.reviewedBundle(5)
@@ -17,11 +17,11 @@ import (
"lincodex/backend/internal/usecase/assessment"
)
// choiceBankFamilies returns the families of a bank that offers the choice
// answer format next to the integer one: a choice family whose distractors
// come from error models, and the first demo family. The choice family reuses
// the rubric and the identity of the first demo family.
func choiceBankFamilies(t *testing.T, base assessment.BundleFamily) []assessment.BundleFamily {
// choiceTableBankFamilies returns the families of a bank that offers every new
// answer format: a choice family whose distractors come from error models, a
// table family with integer answers, and a table family with choice answers.
// They reuse the rubric and the identity of the first demo family.
func choiceTableBankFamilies(t *testing.T, base assessment.BundleFamily) []assessment.BundleFamily {
t.Helper()
choice := base
choice.Key = "choice-lru-hits"
@@ -33,7 +33,32 @@ func choiceBankFamilies(t *testing.T, base assessment.BundleFamily) []assessment
StatementTemplate: "A cache of capacity {{.capacity}} drops its least recently used key when it is full. Keys are read in this order: {{.sequence}}. How many reads find their key in the cache?",
}
choice.AnswerKey = domain.AssessmentAnswerKey{AnswerFormat: domain.AnswerFormatChoice, ReferenceSolution: "Simulate the cache."}
return []assessment.BundleFamily{choice, base}
integerTable := base
integerTable.Key = "table-integer-limits"
integerTable.SeedGenerator = domain.AssessmentSeedGenerator{
StatementTemplate: "A pool opens {{.connections}} connections at most and each request holds one for {{.seconds}} seconds. How many requests per minute can it serve at most?",
Table: &domain.AssessmentVariantTable{Rows: []domain.AssessmentVariantRow{
{Values: map[string]string{"connections": "10", "seconds": "2"}, Answer: "300"},
{Values: map[string]string{"connections": "4", "seconds": "3"}, Answer: "80"},
{Values: map[string]string{"connections": "6", "seconds": "5"}, Answer: "72"},
{Values: map[string]string{"connections": "20", "seconds": "1"}, Answer: "1200"},
}},
}
integerTable.AnswerKey = domain.AssessmentAnswerKey{AnswerFormat: domain.AnswerFormatInteger, ReferenceSolution: "Multiply by sixty and divide by the hold time."}
choiceTable := base
choiceTable.Key = "table-choice-status"
choiceTable.SeedGenerator = domain.AssessmentSeedGenerator{
StatementTemplate: "Which status code does an API return for {{.situation}}?",
Table: &domain.AssessmentVariantTable{Rows: []domain.AssessmentVariantRow{
{Values: map[string]string{"situation": "a resource that does not exist"}, Answer: "404", Distractors: []string{"400", "410", "500"}},
{Values: map[string]string{"situation": "a call without credentials"}, Answer: "401", Distractors: []string{"403", "407", "511"}},
{Values: map[string]string{"situation": "an update on a stale version"}, Answer: "412", Distractors: []string{"409", "422", "428"}},
}},
}
choiceTable.AnswerKey = domain.AssessmentAnswerKey{AnswerFormat: domain.AnswerFormatChoice, ReferenceSolution: "Match the situation to the status code."}
return []assessment.BundleFamily{choice, integerTable, choiceTable}
}
// allowedItemKeys are the fields that a candidate item may carry per format.
@@ -42,7 +67,7 @@ var allowedItemContentKeys = map[string][]string{
"choice": {"format", "multiple", "options"},
}
func TestAssessmentChoiceFamiliesAreServedAndGradedOverHTTP(t *testing.T) {
func TestAssessmentChoiceAndTableFamiliesAreServedAndGradedOverHTTP(t *testing.T) {
env := bootstrap(t, postgresService, redisService)
ctx, cancel := context.WithTimeout(t.Context(), assessmentAttemptHTTPTimeout)
defer cancel()
@@ -53,9 +78,18 @@ func TestAssessmentChoiceFamiliesAreServedAndGradedOverHTTP(t *testing.T) {
harness := newAssessmentAttemptHTTPHarness(ctx, t, &isolatedEnv)
source := loadDemoBundleFile(t)
source.Families = choiceBankFamilies(t, source.Families[0])
source.Families = choiceTableBankFamilies(t, source.Families[0])
seedAssessmentAttemptHTTPBankFrom(ctx, t, harness.pool, nil, source)
// The bank holds both kinds of generator: the table families are stored
// with hash scheme 2 and the algorithmic family with scheme 1.
for scheme, want := range map[int]bool{1: true, 2: true} {
var stored int
if err := harness.pool.QueryRow(ctx, `SELECT count(*) FROM assessment_families WHERE hash_scheme = $1`, scheme).Scan(&stored); err != nil || (stored > 0) != want {
t.Fatalf("families with hash scheme %d = %d, %v, want some", scheme, stored, err)
}
}
passwordHash, err := harness.credentials.HashNewPassword(ctx, sessionHTTPTestPass)
if err != nil {
t.Fatalf("HashNewPassword() error = %v", err)