fix(assessment): return confirmed categories with their basis and label demonstration categories

This commit is contained in:
shira committed 2026-10-09 21:24:24 +03:00
1 parent 623d41b969
commit d4903a3796
77 files changed
+1441 -288

No files matched your search

@@ -710,6 +710,7 @@ func TestAssessmentCategoryAssignmentOverHTTP(t *testing.T) {
}
wantHigh := []domain.CandidateViewConfirmedCategoryInput{{
Specialization: domain.SpecializationBackend, Grade: domain.GradeIntern, Strength: domain.CandidateViewStrengthStandard,
Basis: domain.AssessmentCategorySourceSyntheticDemo,
}}
if len(sources) != 2 || fmt.Sprint(categories[high.id]) != fmt.Sprint(wantHigh) || len(categories[low.id]) != 0 {
t.Fatalf("organization projection = %v, want only the current intern category of the confirmed candidate", categories)
@@ -0,0 +1,166 @@
//go:build integration
package integration_test
import (
"context"
"encoding/json"
"net/http"
"strings"
"testing"
"time"
v1 "lincodex/backend/internal/adapter/primary/http/v1"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/usecase/assessment"
profileusecase "lincodex/backend/internal/usecase/profile"
)
// TestConfirmedCategoryReadsOverHTTP checks the two candidate reads that show
// the confirmed category, with its basis: the availability and the profile. A
// candidate without a category gets null and an empty list. A higher passing
// result replaces the category, and a lower passing result leaves it as it is.
func TestConfirmedCategoryReadsOverHTTP(t *testing.T) {
env := bootstrap(t, postgresService, redisService)
ctx, cancel := context.WithTimeout(t.Context(), 2*assessmentAttemptHTTPTimeout)
defer cancel()
databaseURL := env.createIsolatedDatabase(ctx, t)
applyMigrations(ctx, t, databaseURL)
isolatedEnv := *env
isolatedEnv.Config.DatabaseURL = databaseURL
harness := newAssessmentAttemptHTTPHarnessWithOptions(ctx, t, &isolatedEnv,
assessment.AssessmentCategoryDecisionSettings{SyntheticDemoAssignmentsEnabled: true},
func(parts assessmentHarnessParts) []v1.HandlerOption {
return []v1.HandlerOption{v1.WithProfile(v1.NewProfileHandler(profileusecase.NewService(parts.transactor, parts.clock.Now)))}
})
bank := seedAssessmentCategoryHTTPBank(ctx, t, harness.pool, domain.DemoProductPolicyV2(), true)
threshold := passPoints(int(bank.policy.Policy().Assessment.StopRule.MaxScoredItems), int(bank.policy.Policy().Assessment.Attempt.BasicPassMark))
client := &categoryHTTPClient{harness: harness, ctx: ctx}
none := client.candidate(t, "category-read-none@example.test")
failed := client.candidate(t, "category-read-failed@example.test")
held := client.candidate(t, "category-read-held@example.test")
t.Run("a candidate without any result sees no confirmed category in either read", func(t *testing.T) {
assertAvailabilityConfirmation(t, client, none, "backend", nil, nil)
assertProfileConfirmedCategoriesEmpty(t, client, none)
})
t.Run("a candidate whose attempt confirmed nothing still sees no category", func(t *testing.T) {
run := client.run(t, failed, "backend", "intern", func(int) int { return threshold - 1 })
if run.result["outcome"] != "not_confirmed" {
t.Fatalf("failing run outcome = %v, want not_confirmed", run.result["outcome"])
}
assertAvailabilityConfirmation(t, client, failed, "backend", nil, nil)
assertProfileConfirmedCategoriesEmpty(t, client, failed)
})
var intern, junior attemptRun
t.Run("an assigned category is shown with its basis and attempt in both reads", func(t *testing.T) {
intern = client.run(t, held, "backend", "intern", func(int) int { return threshold })
assertAvailabilityConfirmation(t, client, held, "backend", "intern", "synthetic_demo")
assertAvailabilityConfirmation(t, client, held, "frontend", nil, nil)
assertProfileBackendCategory(t, client, held, "intern", "synthetic_demo", intern)
})
t.Run("a higher passing result replaces the category and a lower one never lowers it", func(t *testing.T) {
// The promotion interval counts on database time, so the finished intern
// attempt is moved past it instead of waiting for days.
moveFinishedAttemptBack(t, ctx, harness.pool, intern.id, categoryHTTPPromotionGap)
junior = client.run(t, held, "backend", "junior", func(int) int { return threshold })
if decision := decisionOf(t, junior.result); decision["status"] != "assigned" {
t.Fatalf("junior decision = %v, want assigned", decision)
}
assertAvailabilityConfirmation(t, client, held, "backend", "junior", "synthetic_demo")
assertProfileBackendCategory(t, client, held, "junior", "synthetic_demo", junior)
lower := client.run(t, held, "backend", "intern", func(int) int { return threshold })
if decision := decisionOf(t, lower.result); decision["status"] != "unchanged" || decision["current_confirmed_grade"] != "junior" {
t.Fatalf("lower passing decision = %v, want unchanged at junior", decision)
}
assertAvailabilityConfirmation(t, client, held, "backend", "junior", "synthetic_demo")
assertProfileBackendCategory(t, client, held, "junior", "synthetic_demo", junior)
})
}
// assertAvailabilityConfirmation checks the confirmed grade and basis of one
// specialization in the availability read. A nil argument stands for JSON null.
func assertAvailabilityConfirmation(t *testing.T, client *categoryHTTPClient, who categoryHTTPCandidate, specialization string, grade, basis any) {
t.Helper()
status, body := client.call(t, who, http.MethodGet, "/api/v1/me/assessment-availability", "", "")
if status != http.StatusOK {
t.Fatalf("read availability = %d %s", status, body)
}
var read struct {
Specializations []map[string]any `json:"specializations"`
}
if err := json.Unmarshal(body, &read); err != nil {
t.Fatalf("decode availability: %v", err)
}
for _, item := range read.Specializations {
if item["specialization"] != specialization {
continue
}
if item["confirmed_grade"] != grade || item["confirmed_basis"] != basis {
t.Fatalf("availability %s = grade %v basis %v, want grade %v basis %v", specialization, item["confirmed_grade"], item["confirmed_basis"], grade, basis)
}
return
}
t.Fatalf("availability lists no %s specialization: %s", specialization, body)
}
func readProfileBody(t *testing.T, client *categoryHTTPClient, who categoryHTTPCandidate) []byte {
t.Helper()
status, body := client.call(t, who, http.MethodGet, "/api/v1/me/profile", "", "")
if status != http.StatusOK {
t.Fatalf("read profile = %d %s", status, body)
}
return body
}
func assertProfileConfirmedCategoriesEmpty(t *testing.T, client *categoryHTTPClient, who categoryHTTPCandidate) {
t.Helper()
if body := readProfileBody(t, client, who); !strings.Contains(string(body), `"confirmed_categories":[]`) {
t.Fatalf("profile = %s, want an empty confirmed_categories list", body)
}
}
// assertProfileBackendCategory checks that the profile holds exactly one
// confirmed category, the backend one, with its grade, basis, and the attempt
// whose result assigned it. The assignment time is the decision time of that
// result.
func assertProfileBackendCategory(t *testing.T, client *categoryHTTPClient, who categoryHTTPCandidate, grade, basis string, attempt attemptRun) {
t.Helper()
var profile struct {
ConfirmedCategories []map[string]any `json:"confirmed_categories"`
}
if err := json.Unmarshal(readProfileBody(t, client, who), &profile); err != nil {
t.Fatalf("decode profile: %v", err)
}
if len(profile.ConfirmedCategories) != 1 {
t.Fatalf("confirmed_categories = %v, want exactly the backend category", profile.ConfirmedCategories)
}
category := profile.ConfirmedCategories[0]
if len(category) != 5 {
t.Fatalf("confirmed category has %d properties, want exactly specialization, grade, basis, confirmed_at, attempt_id: %v", len(category), category)
}
if category["specialization"] != "backend" || category["grade"] != grade || category["basis"] != basis ||
category["attempt_id"] != attempt.id.String() {
t.Fatalf("confirmed category = %v, want backend %s with basis %s from attempt %s", category, grade, basis, attempt.id)
}
confirmedAt, ok := category["confirmed_at"].(string)
if !ok {
t.Fatalf("confirmed_at = %v, want a timestamp", category["confirmed_at"])
}
assignedAt, err := time.Parse(time.RFC3339Nano, confirmedAt)
if err != nil {
t.Fatalf("parse confirmed_at %q: %v", confirmedAt, err)
}
decidedAt, err := time.Parse(time.RFC3339Nano, attempt.result["decided_at"].(string))
if err != nil {
t.Fatalf("parse decided_at: %v", err)
}
if !assignedAt.Equal(decidedAt) {
t.Fatalf("confirmed_at = %s, want the decision time %s of the attempt", assignedAt, decidedAt)
}
}
@@ -130,6 +130,7 @@ type matchEntryBody struct {
Category *struct {
Specialization string `json:"specialization"`
Grade string `json:"grade"`
Basis string `json:"basis"`
} `json:"category"`
Strength *string `json:"strength"`
PublicContext struct {
@@ -545,6 +546,13 @@ func TestMatchSearchOverHTTP(t *testing.T) {
t.Fatalf("organization A confirmed bank = %d main, %d near, %d unconfirmed; want exactly the eleven visible confirmed candidates",
len(bankA.Groups.Main), len(bankA.Groups.Near), len(bankA.Groups.Unconfirmed))
}
// Every confirmed category names its basis, so a demonstration grade is
// never presented as a standard confirmation.
for _, entry := range bankA.Groups.Main {
if entry.Category == nil || entry.Category.Basis != "synthetic_demo" {
t.Fatalf("confirmed entry %s category = %+v, want the synthetic_demo basis", entry.CandidateHandle, entry.Category)
}
}
if slices.Contains(handlesOf(bankA.Groups.Main), aliasIn(orgA.ID(), hiddenFromA.id)) {
t.Fatal("organization A received a candidate that hides it")
}