unit(C4): readiness scoring
Pure computeReadiness (src/domain/readiness.ts) implementing the ARCHITECTURE §5.8.3 derivation: core skills below required_level block and are listed in missing[], otherwise a weight-averaged level score (§5.4) blends 0.7 level / 0.3 self comfort, null-safe. The single most reused function in the app — picker, auto-fill, matrix, dashboard — takes its data in and touches no DB. 13 tests named after each rule.
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
computeReadiness,
|
||||
readinessLevel,
|
||||
validateReadinessInput,
|
||||
type SkillReadinessInput,
|
||||
} from "./readiness";
|
||||
|
||||
/** A single core skill, helper for the cases below. */
|
||||
const core = (over: Partial<SkillReadinessInput> = {}): SkillReadinessInput => ({
|
||||
name: "FOH",
|
||||
isCore: true,
|
||||
requiredLevel: "qualified",
|
||||
weight: 1,
|
||||
verifiedLevel: "qualified",
|
||||
selfComfort: null,
|
||||
...over,
|
||||
});
|
||||
|
||||
describe("computeReadiness — no skills", () => {
|
||||
it("blocks a position with no requirements — nothing to be ready for", () => {
|
||||
const result = computeReadiness([]);
|
||||
expect(result.blocked).toBe(true);
|
||||
expect(result.score).toBe(0);
|
||||
expect(result.missing).toEqual([]);
|
||||
expect(result.level).toBe("blocked");
|
||||
});
|
||||
});
|
||||
|
||||
describe("computeReadiness — all core skills at required", () => {
|
||||
it("is not blocked and scores 100 when every core skill meets its required level", () => {
|
||||
const result = computeReadiness([
|
||||
core({ requiredLevel: "qualified", verifiedLevel: "expert", selfComfort: null }),
|
||||
]);
|
||||
expect(result.blocked).toBe(false);
|
||||
expect(result.score).toBe(100);
|
||||
expect(result.missing).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("computeReadiness — core skill below required", () => {
|
||||
it("blocks and lists the below-required core skill in missing[]", () => {
|
||||
const result = computeReadiness([
|
||||
core({ name: "FOH", requiredLevel: "qualified", verifiedLevel: "assisted", selfComfort: 80 }),
|
||||
]);
|
||||
expect(result.blocked).toBe(true);
|
||||
expect(result.missing).toEqual(["FOH"]);
|
||||
});
|
||||
|
||||
it("blocks when any one of several core skills is below required", () => {
|
||||
const result = computeReadiness([
|
||||
core({ name: "FOH", requiredLevel: "qualified", verifiedLevel: "qualified" }),
|
||||
core({ name: "Wireless", requiredLevel: "assisted", verifiedLevel: "observer" }),
|
||||
]);
|
||||
expect(result.blocked).toBe(true);
|
||||
expect(result.missing).toEqual(["Wireless"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("computeReadiness — non-core gaps", () => {
|
||||
it("does not block on a non-core gap but scores below 100", () => {
|
||||
const result = computeReadiness([
|
||||
core({ name: "FOH", requiredLevel: "qualified", verifiedLevel: "expert", selfComfort: null }),
|
||||
{
|
||||
name: "Slides",
|
||||
isCore: false,
|
||||
requiredLevel: "qualified",
|
||||
weight: 1,
|
||||
verifiedLevel: "assisted",
|
||||
selfComfort: null,
|
||||
},
|
||||
]);
|
||||
expect(result.blocked).toBe(false);
|
||||
expect(result.score).toBeLessThan(100);
|
||||
expect(result.missing).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("computeReadiness — null comfort is level-only", () => {
|
||||
it("uses the level score alone when comfort is null (no comfort blend)", () => {
|
||||
// qualified → levelScore 0.80, comfort null → blended 0.80 → 80.
|
||||
const result = computeReadiness([core({ verifiedLevel: "qualified", selfComfort: null })]);
|
||||
expect(result.score).toBe(80);
|
||||
});
|
||||
|
||||
it("blends 0.7 level / 0.3 comfort when comfort is present", () => {
|
||||
// qualified 0.80, comfort 50 → 0.7*0.80 + 0.3*0.50 = 0.71 → 71.
|
||||
const result = computeReadiness([core({ verifiedLevel: "qualified", selfComfort: 50 })]);
|
||||
expect(result.score).toBe(71);
|
||||
});
|
||||
|
||||
it("averages only the rated core skills when some comfort is null (null-safe mean)", () => {
|
||||
// Two core skills: FOH unrated (dropped), Wireless comfort 100 → comfortMean 1.0.
|
||||
// Both qualified (0.80) → level 0.80; blended 0.7*0.80 + 0.3*1.0 = 0.86 → 86.
|
||||
const result = computeReadiness([
|
||||
core({ name: "FOH", selfComfort: null }),
|
||||
core({ name: "Wireless", selfComfort: 100 }),
|
||||
]);
|
||||
expect(result.score).toBe(86);
|
||||
});
|
||||
});
|
||||
|
||||
describe("computeReadiness — expert with full comfort", () => {
|
||||
it("scores 100 and reads as expert", () => {
|
||||
const result = computeReadiness([core({ verifiedLevel: "expert", selfComfort: 100 })]);
|
||||
expect(result.score).toBe(100);
|
||||
expect(result.level).toBe("expert");
|
||||
expect(result.blocked).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("readinessLevel — score bands", () => {
|
||||
it("maps a score to the §5.8.2 readiness label", () => {
|
||||
expect(readinessLevel(100)).toBe("expert");
|
||||
expect(readinessLevel(90)).toBe("expert");
|
||||
expect(readinessLevel(80)).toBe("qualified");
|
||||
expect(readinessLevel(60)).toBe("assisted");
|
||||
expect(readinessLevel(30)).toBe("observer");
|
||||
expect(readinessLevel(10)).toBe("none");
|
||||
});
|
||||
});
|
||||
|
||||
describe("validateReadinessInput", () => {
|
||||
it("accepts a well-formed skill set", () => {
|
||||
const problems = validateReadinessInput([
|
||||
{ verifiedLevel: "expert", requiredLevel: "qualified", weight: 2, selfComfort: 90 },
|
||||
]);
|
||||
expect(problems).toEqual([]);
|
||||
});
|
||||
|
||||
it("rejects an out-of-range weight", () => {
|
||||
const problems = validateReadinessInput([
|
||||
{ verifiedLevel: "expert", requiredLevel: "qualified", weight: 9, selfComfort: null },
|
||||
]);
|
||||
expect(problems[0]).toContain("weight");
|
||||
});
|
||||
|
||||
it("rejects an unknown level", () => {
|
||||
const problems = validateReadinessInput([
|
||||
{ verifiedLevel: "grandmaster" as never, requiredLevel: "qualified", selfComfort: null },
|
||||
]);
|
||||
expect(problems[0]).toContain("verifiedLevel");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,165 @@
|
||||
/**
|
||||
* Position readiness scoring — unit C4 (ARCHITECTURE §5.8.3, §5.4).
|
||||
*
|
||||
* Pure: no DB, no imports from `src/db`. The only dependency is the skill-level
|
||||
* ladder from `./assessments` (C3), which is itself tested to stay in sync with
|
||||
* the DB enum. C4 is the single most reused function in the app — picker,
|
||||
* auto-fill, matrix, dashboard all call it — so the rule lives here in one
|
||||
* auditable place and every caller computes the same number.
|
||||
*
|
||||
* The derivation (ARCHITECTURE §5.8.3):
|
||||
* - For each CORE skill, if the volunteer's verified level is below the
|
||||
* position's required level → BLOCKED (the skill is listed in `missing`).
|
||||
* - `score = Σ(weight_i × levelScore(skill_i)) / Σ(weight_i)` across every
|
||||
* skill the position lists (core gates; non-core only ranks).
|
||||
* - `comfort = mean(self_comfort of the position's CORE skills)`, null-safe:
|
||||
* a core skill the volunteer never rated is dropped from the mean, and if
|
||||
* every core skill is unrated the comfort term disappears entirely.
|
||||
* - `blended = comfort == null ? score : 0.7 * score + 0.3 * (comfort / 100)`
|
||||
* (ARCHITECTURE §5.4: 70% level, 30% self-reported comfort).
|
||||
* - `score = round(blended * 100)`, an integer 0..100.
|
||||
*
|
||||
* `levelScore` (§5.4): none 0 · observer 0.35 · assisted 0.55 · qualified 0.80
|
||||
* · expert 1.00. A level is compared to the required level by ladder rank, so
|
||||
* "at or above required" never blocks.
|
||||
*/
|
||||
|
||||
import { isSkillLevel, levelRank, SKILL_LEVELS, type SkillLevel } from "./assessments";
|
||||
|
||||
/** The five competency levels, re-exported so callers need one import. */
|
||||
export { SKILL_LEVELS, isSkillLevel };
|
||||
export type { SkillLevel };
|
||||
|
||||
/**
|
||||
* §5.4 level → proficiency weight. Deliberately NOT the ladder rank: a
|
||||
* `qualified` operator is 0.80, not 3.0, so the blended score stays on a 0..1
|
||||
* scale.
|
||||
*/
|
||||
const LEVEL_SCORE: Record<SkillLevel, number> = {
|
||||
none: 0,
|
||||
observer: 0.35,
|
||||
assisted: 0.55,
|
||||
qualified: 0.8,
|
||||
expert: 1,
|
||||
};
|
||||
|
||||
/** Readiness label derived from the final 0..100 score (§5.8.2 bands). */
|
||||
export type ReadinessLevel = "expert" | "qualified" | "assisted" | "observer" | "none" | "blocked";
|
||||
|
||||
export interface SkillReadinessInput {
|
||||
/** Display name — surfaced in `missing` when a core skill blocks. */
|
||||
name: string;
|
||||
/** Core skills hard-gate; non-core skills only affect the score. */
|
||||
isCore: boolean;
|
||||
/** The position's `required_level` for this skill (ARCHITECTURE §4.3). */
|
||||
requiredLevel: SkillLevel;
|
||||
/** 1..5 — the skill's weight in the score (defaults to 1 when omitted). */
|
||||
weight?: number;
|
||||
/** The volunteer's verified level on this skill. */
|
||||
verifiedLevel: SkillLevel;
|
||||
/** The volunteer's self-reported comfort, 0..100, or null if never rated. */
|
||||
selfComfort: number | null;
|
||||
}
|
||||
|
||||
export interface ReadinessResult {
|
||||
/** Blended 0..100 score. */
|
||||
score: number;
|
||||
/** True when a core skill is below its required level, or there are no skills. */
|
||||
blocked: boolean;
|
||||
/** Names of core skills that are below their required level. */
|
||||
missing: string[];
|
||||
/** Readiness label derived from the score; `blocked` when blocked. */
|
||||
level: ReadinessLevel;
|
||||
}
|
||||
|
||||
/** Score bands → readiness label, matching the §5.8.2 comfort guidance. */
|
||||
export function readinessLevel(score: number): ReadinessLevel {
|
||||
if (score >= 90) return "expert";
|
||||
if (score >= 75) return "qualified";
|
||||
if (score >= 50) return "assisted";
|
||||
if (score >= 25) return "observer";
|
||||
return "none";
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute a volunteer's readiness for a position.
|
||||
*
|
||||
* `skills` is the position's `position_skills` set enriched with the
|
||||
* volunteer's competency per skill (verified level, self comfort). It is pure:
|
||||
* the caller resolves the joins; this function only scores.
|
||||
*/
|
||||
export function computeReadiness(skills: readonly SkillReadinessInput[]): ReadinessResult {
|
||||
// A position with no requirements cannot be scored — nothing to be ready FOR.
|
||||
if (skills.length === 0) {
|
||||
return { score: 0, blocked: true, missing: [], level: "blocked" };
|
||||
}
|
||||
|
||||
const missing: string[] = [];
|
||||
let weightSum = 0;
|
||||
let weightedLevelSum = 0;
|
||||
const coreComforts: number[] = [];
|
||||
|
||||
for (const skill of skills) {
|
||||
const weight = skill.weight ?? 1;
|
||||
|
||||
// Core skill below the required gate → block and name it.
|
||||
if (skill.isCore && levelRank(skill.verifiedLevel) < levelRank(skill.requiredLevel)) {
|
||||
missing.push(skill.name);
|
||||
}
|
||||
|
||||
weightSum += weight;
|
||||
weightedLevelSum += weight * LEVEL_SCORE[skill.verifiedLevel];
|
||||
|
||||
// Comfort is the mean over CORE skills only; null is dropped (null-safe).
|
||||
if (skill.isCore && skill.selfComfort !== null) {
|
||||
coreComforts.push(skill.selfComfort);
|
||||
}
|
||||
}
|
||||
|
||||
const levelScoreAvg = weightSum > 0 ? weightedLevelSum / weightSum : 0;
|
||||
const comfortMean =
|
||||
coreComforts.length > 0
|
||||
? coreComforts.reduce((sum, value) => sum + value, 0) / coreComforts.length / 100
|
||||
: null;
|
||||
|
||||
const blended = comfortMean === null ? levelScoreAvg : 0.7 * levelScoreAvg + 0.3 * comfortMean;
|
||||
const score = Math.round(blended * 100);
|
||||
const blocked = missing.length > 0;
|
||||
|
||||
return {
|
||||
score,
|
||||
blocked,
|
||||
missing,
|
||||
level: blocked ? "blocked" : readinessLevel(score),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a readiness payload before it is scored. Returns a list of
|
||||
* human-readable problems (empty when valid) so the route can 400 instead of
|
||||
* silently scoring garbage.
|
||||
*/
|
||||
export function validateReadinessInput(
|
||||
skills: readonly Pick<SkillReadinessInput, "weight" | "verifiedLevel" | "requiredLevel" | "selfComfort">[],
|
||||
): string[] {
|
||||
const problems: string[] = [];
|
||||
|
||||
skills.forEach((skill, i) => {
|
||||
if (!isSkillLevel(skill.verifiedLevel)) {
|
||||
problems.push(`skill[${i}].verifiedLevel must be one of: ${SKILL_LEVELS.join(", ")}`);
|
||||
}
|
||||
if (!isSkillLevel(skill.requiredLevel)) {
|
||||
problems.push(`skill[${i}].requiredLevel must be one of: ${SKILL_LEVELS.join(", ")}`);
|
||||
}
|
||||
const weight = skill.weight;
|
||||
if (weight !== undefined && (!Number.isInteger(weight) || weight < 1 || weight > 5)) {
|
||||
problems.push(`skill[${i}].weight must be an integer from 1 to 5`);
|
||||
}
|
||||
const comfort = skill.selfComfort;
|
||||
if (comfort !== null && (typeof comfort !== "number" || comfort < 0 || comfort > 100)) {
|
||||
problems.push(`skill[${i}].selfComfort must be between 0 and 100`);
|
||||
}
|
||||
});
|
||||
|
||||
return problems;
|
||||
}
|
||||
Reference in New Issue
Block a user