unit(D5): conflict engine (pure function, hard vs soft)

classifyConflicts(input) encodes the 10 conflict rules from
ARCHITECTURE §5.3 / BUILD-SPEC §7 as pure hard/soft classifications:
- hard (block): inactive/unapproved, duplicate-in-instance,
  core-skill-below-required, shadow-mentor-not-ready
- soft (warn, override with note): blackout-overlap, monthly-cap,
  back-to-back, comfort-below-50, stale, readiness-below-threshold

Reuses the pure predicates it encodes: isUnavailable (D4),
isMentorReady (C5), levelRank (C3). No DB, no route, no migration.
hasHardConflict/hardConflicts/softConflicts let callers filter by
severity. Table-driven test: one case per rule + edges (20 tests).
This commit is contained in:
Tiel Coder
2026-11-18 11:41:51 +00:00
parent deb4f35fb8
commit 467f193389
3 changed files with 613 additions and 1 deletions
+22 -1
View File
@@ -522,7 +522,28 @@ Build log for Media Team Hub. One unit per commit, top to bottom. Append after e
- **Cancel, never delete.** `cancelUnavailability` flips `status='cancelled'`; the row persists so historical assignment conflicts stay explainable (rule 5 in §5.3 warns, doesn't hard-block). `isUnavailable` skips non-`active` records, so a cancelled blackout no longer excludes the volunteer.
- **Constraints enforced in two layers.** The pure `validateUnavailabilityRange` rejects bad ranges at the API boundary (400 with a clear message); the DB `chk_unavailability_*` CHECK constraints are the backstop so a bad range can never reach the table.
**Next: D5 — conflict engine (pure function, hard vs soft conflicts).**
## D5 — Conflict engine (hard vs soft) — DONE 2026-11-18
**Scope (from BUILD-PLAN.md):** conflict engine — pure function, hard vs soft conflicts. Done when: table-driven test, 10 cases, one per rule in BUILD-SPEC §7. ARCHITECTURE §5.3 / BUILD-SPEC §7.
**Delivered:**
- `src/domain/conflicts.ts` — pure `classifyConflicts(input)` returns every reason a candidate should not take a slot, each tagged `hard` (blocks) or `soft` (warns; override with a note). Reuses the pure predicates it encodes: `isUnavailable` (D4) for blackouts, `isMentorReady` (C5) for the shadow mentor, `levelRank` (C3) for the core-skill gate. Plus `hasHardConflict` / `hardConflicts` / `softConflicts` so the picker and auto-filler filter by severity. No DB imports.
- `src/domain/conflicts.test.ts` — table-driven: one case per rule (ten cases, one per row of §5.3), plus a clean baseline, a multi-conflict case, and the branch edges (unapproved-but-active, a valid shadow pairing, never-served, unrated comfort, core skill AT the required level). 20 tests total.
**No schema change, no route, no migration.** D5 is a pure function (zero UI, zero DB) — the BUILD-PLAN's highest-risk unit, isolated exactly that way. The data layer (D6/D8) resolves the joins and passes the facts in; this module only encodes the rules.
**Verification (all exit 0):**
- `tsc --noEmit` ✓ · `eslint .` ✓ (0 problems) · `vitest run` ✓ (**160** total; D5 = 20) · `next build` ✓
**Decisions:**
- **Hard vs soft, keyed by code.** Each conflict has a stable `code` and a `severity`. The assignment write path and auto-filler reject on `hardConflicts(...)`; the picker shows all of them, greasing the soft ones as warnings the admin overrides with a note.
- **One code per rule, ten cases.** The 10 rules map 1:1 to ARCHITECTURE §5.3 (4 hard: inactive/unapproved, duplicate-in-instance, core-skill-below-required, shadow-mentor-not-ready; 6 soft: blackout-overlap, monthly-cap, back-to-back, comfort-below-50, stale, readiness-below-threshold). The test table has exactly one case per rule.
- **Comfort never blocks (rule 14).** §7 rule 14 says self-comfort lowers rank and warns but never blocks; `comfort_below_50` is soft even though the picker treats it as a warning.
- **Never-served is not stale.** Rule 9 (staleness) requires a non-null `lastServedOn`; a volunteer who has never served the skill is a readiness/level concern (surfaced by readiness), not a "needs refresher" — flagging a beginner as stale would mislead.
- **Staleness takes a reference date.** The function is pure and deterministic, so `daysBetween(lastServedOn, today)` takes `today` as input rather than reading the clock — the test pins it to a fixed date.
- **Shadow mentor reuses `isMentorReady`.** A shadow slot (§7 rule 16) needs a mentor who is mentor-ready for that exact skill; the C5 predicate is called directly, and an unpaired shadow (`pairedMentor` null) is a hard conflict.
**Next: D6 — candidate ranking service (availability filter → readiness → score).**
---