C3 — Admin skills matrix + verify-level modal
Adds the append-only skill_assessments history and the verify path, so a level can only move together with the row recording who moved it, from what level, and the comfort they saw.
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
buildAssessment,
|
||||
isSkillLevel,
|
||||
levelRank,
|
||||
SKILL_LEVELS,
|
||||
} from "./assessments";
|
||||
import { SKILL_LEVELS as DB_SKILL_LEVELS } from "@/db/schema";
|
||||
|
||||
describe("skill level ladder", () => {
|
||||
it("orders none < observer < assisted < qualified < expert", () => {
|
||||
expect(["none", "observer", "assisted", "qualified", "expert"].map(levelRank)).toEqual([
|
||||
0, 1, 2, 3, 4,
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps the domain ladder in sync with the database enum", () => {
|
||||
expect([...SKILL_LEVELS]).toEqual([...DB_SKILL_LEVELS]);
|
||||
});
|
||||
|
||||
it("recognizes only real levels", () => {
|
||||
expect(isSkillLevel("qualified")).toBe(true);
|
||||
expect(isSkillLevel("grandmaster")).toBe(false);
|
||||
expect(isSkillLevel(null)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildAssessment", () => {
|
||||
it("records an upward move with level_from taken from the stored level", () => {
|
||||
const result = buildAssessment({
|
||||
currentLevel: "none",
|
||||
newLevel: "qualified",
|
||||
selfComfort: 55,
|
||||
});
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.draft).toEqual({
|
||||
levelFrom: "none",
|
||||
levelTo: "qualified",
|
||||
comfortAtTime: 55,
|
||||
direction: "up",
|
||||
});
|
||||
});
|
||||
|
||||
it("records a downward move too — demotions are audited", () => {
|
||||
const result = buildAssessment({
|
||||
currentLevel: "expert",
|
||||
newLevel: "assisted",
|
||||
selfComfort: 40,
|
||||
});
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.draft.direction).toBe("down");
|
||||
expect(result.draft.levelFrom).toBe("expert");
|
||||
expect(result.draft.levelTo).toBe("assisted");
|
||||
});
|
||||
|
||||
it("rejects an unknown new level instead of storing it", () => {
|
||||
const result = buildAssessment({
|
||||
currentLevel: "none",
|
||||
newLevel: "grandmaster",
|
||||
selfComfort: null,
|
||||
});
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.problems[0]).toContain("observer");
|
||||
});
|
||||
|
||||
it("rejects an unknown current level", () => {
|
||||
const result = buildAssessment({
|
||||
currentLevel: "",
|
||||
newLevel: "qualified",
|
||||
selfComfort: null,
|
||||
});
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.problems[0]).toContain("unknown current level");
|
||||
});
|
||||
|
||||
it("refuses an unchanged level — no silent no-op row", () => {
|
||||
const result = buildAssessment({
|
||||
currentLevel: "assisted",
|
||||
newLevel: "assisted",
|
||||
selfComfort: 70,
|
||||
});
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.problems[0]).toContain("unchanged");
|
||||
});
|
||||
|
||||
it("snapshots a null comfort when the volunteer never rated the skill", () => {
|
||||
const result = buildAssessment({
|
||||
currentLevel: "none",
|
||||
newLevel: "observer",
|
||||
selfComfort: null,
|
||||
});
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.draft.comfortAtTime).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Skill level assessment rules — unit C3 (ARCHITECTURE §4.3, §5.8.2).
|
||||
*
|
||||
* Pure: no DB, no imports from `src/db`, so the rule can be tested without a
|
||||
* running server. The route and the data layer both call it; neither is allowed
|
||||
* to invent a level move on its own.
|
||||
*
|
||||
* The rules:
|
||||
* - Levels are an ordered ladder: none < observer < assisted < qualified < expert.
|
||||
* - A move may go up or down — demotions are audited just like promotions.
|
||||
* - `level_from` is always the volunteer's *current verified level*, read from
|
||||
* the database. A caller cannot rewrite history by passing it in.
|
||||
* - An unchanged level records nothing; a verify that does not verify is a
|
||||
* mistake, not a silent no-op row.
|
||||
* - `comfort_at_time` snapshots the volunteer's current self_comfort — the
|
||||
* signal the assessor saw — and is null when they never rated it.
|
||||
*/
|
||||
|
||||
/** Ordered lowest → highest. Keep in sync with the DB enum (tested). */
|
||||
export const SKILL_LEVELS = ["none", "observer", "assisted", "qualified", "expert"] as const;
|
||||
|
||||
export type SkillLevel = (typeof SKILL_LEVELS)[number];
|
||||
|
||||
export function isSkillLevel(value: unknown): value is SkillLevel {
|
||||
return typeof value === "string" && (SKILL_LEVELS as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
/** Ladder position: 0 = none, 4 = expert. -1 for a value outside the ladder. */
|
||||
export function levelRank(level: string): number {
|
||||
return (SKILL_LEVELS as readonly string[]).indexOf(level);
|
||||
}
|
||||
|
||||
export interface AssessmentDraft {
|
||||
/** Always the current verified level at assessment time. */
|
||||
levelFrom: SkillLevel;
|
||||
levelTo: SkillLevel;
|
||||
/** The volunteer's self_comfort at assessment time (null = never rated). */
|
||||
comfortAtTime: number | null;
|
||||
direction: "up" | "down";
|
||||
}
|
||||
|
||||
export type AssessmentResult =
|
||||
| { ok: true; draft: AssessmentDraft }
|
||||
| { ok: false; problems: string[] };
|
||||
|
||||
/**
|
||||
* Build the row to append for a level verification. `currentLevel` and
|
||||
* `newLevel` are typed as strings on purpose: both arrive from untrusted input
|
||||
* (the client for `newLevel`, the DB for `currentLevel`) and are validated here.
|
||||
*/
|
||||
export function buildAssessment(params: {
|
||||
currentLevel: string;
|
||||
newLevel: string;
|
||||
selfComfort: number | null;
|
||||
}): AssessmentResult {
|
||||
const problems: string[] = [];
|
||||
|
||||
if (!isSkillLevel(params.currentLevel)) {
|
||||
problems.push(`unknown current level "${params.currentLevel}"`);
|
||||
}
|
||||
if (!isSkillLevel(params.newLevel)) {
|
||||
problems.push(`level must be one of: ${SKILL_LEVELS.join(", ")}`);
|
||||
}
|
||||
if (problems.length > 0) return { ok: false, problems };
|
||||
|
||||
const currentLevel = params.currentLevel as SkillLevel;
|
||||
const newLevel = params.newLevel as SkillLevel;
|
||||
|
||||
if (currentLevel === newLevel) {
|
||||
return { ok: false, problems: ["level is unchanged — nothing to assess"] };
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
draft: {
|
||||
// Server-owned: the from-level comes from the stored record, never input.
|
||||
levelFrom: currentLevel,
|
||||
levelTo: newLevel,
|
||||
comfortAtTime: params.selfComfort,
|
||||
direction: levelRank(newLevel) > levelRank(currentLevel) ? "up" : "down",
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user