diff --git a/src/domain/mentor.test.ts b/src/domain/mentor.test.ts new file mode 100644 index 0000000..d2995d3 --- /dev/null +++ b/src/domain/mentor.test.ts @@ -0,0 +1,59 @@ +import { describe, expect, it } from "vitest"; +import { isMentorReady, validateMentorDesignation } from "./mentor"; + +/** A fully-qualified mentor candidate: expert, opted in, admin-approved. */ +const ready = { + verifiedLevel: "expert" as const, + mentorMinLevel: "expert" as const, + mentorOptIn: true, + adminApproved: true, +}; + +describe("isMentorReady", () => { + it("is true when the volunteer is expert, opted in, and admin-approved", () => { + expect(isMentorReady(ready)).toBe(true); + }); + + it("is false for an expert who never opted in — expertise alone does not volunteer someone", () => { + expect(isMentorReady({ ...ready, mentorOptIn: false })).toBe(false); + }); + + it("is false for someone opted in but not yet admin-approved", () => { + expect(isMentorReady({ ...ready, adminApproved: false })).toBe(false); + }); + + it("is false below the skill's mentor_min_level even with opt-in and approval", () => { + expect( + isMentorReady({ + verifiedLevel: "qualified", + mentorMinLevel: "expert", + mentorOptIn: true, + adminApproved: true, + }), + ).toBe(false); + }); + + it("is true once the volunteer clears mentor_min_level, opts in, and is approved", () => { + expect( + isMentorReady({ + verifiedLevel: "expert", + mentorMinLevel: "qualified", + mentorOptIn: true, + adminApproved: true, + }), + ).toBe(true); + }); +}); + +describe("validateMentorDesignation", () => { + it("accepts two real levels", () => { + expect(validateMentorDesignation({ verifiedLevel: "expert", mentorMinLevel: "qualified" })).toEqual( + [], + ); + }); + + it("rejects an unknown level", () => { + const problems = validateMentorDesignation({ verifiedLevel: "grandmaster", mentorMinLevel: "qualified" }); + expect(problems[0]).toContain("verifiedLevel"); + }); +}); diff --git a/src/domain/mentor.ts b/src/domain/mentor.ts new file mode 100644 index 0000000..dafaebb --- /dev/null +++ b/src/domain/mentor.ts @@ -0,0 +1,65 @@ +/** + * Mentor designation — unit C5 (ARCHITECTURE §5.8.5). + * + * Pure: no DB, no imports from `src/db`. The only dependency is the skill-level + * ladder from `./assessments` (C3), which is tested to stay in sync with the DB + * enum. `mentor_ready` is a per-skill predicate, not a global title — a + * volunteer can mentor one skill and not another. + * + * The rule (ARCHITECTURE §5.8.5): + * - `mentor_ready` = `verified_level >= skill.mentor_min_level` + * AND `mentor_opt_in` (the volunteer agreed to teach) + * AND admin-approved. + * + * Expertise alone does not volunteer someone: an expert who never opted in, or + * who an admin has not approved, is not mentor-ready. A shadow slot (§5.8.5) + * requires a mentor who is mentor_ready for that exact skill, so this predicate + * is the gate the shadow validation (C6) calls. + */ + +import { levelRank, SKILL_LEVELS, type SkillLevel } from "./assessments"; + +export { SKILL_LEVELS }; +export type { SkillLevel }; + +export interface MentorReadyInput { + /** The volunteer's verified level on this skill. */ + verifiedLevel: SkillLevel; + /** The skill's configured minimum level to mentor (ARCHITECTURE §4.3). */ + mentorMinLevel: SkillLevel; + /** The volunteer agreed to teach this skill (user_skills.mentor_opt_in). */ + mentorOptIn: boolean; + /** An admin has approved the designation. */ + adminApproved: boolean; +} + +/** + * Is this volunteer mentor-ready for one skill? Pure predicate — the data layer + * and the shadow validation (C6) both call it, so the rule and the code cannot + * drift. + */ +export function isMentorReady(input: MentorReadyInput): boolean { + if (levelRank(input.verifiedLevel) < levelRank(input.mentorMinLevel)) return false; + if (!input.mentorOptIn) return false; + if (!input.adminApproved) return false; + return true; +} + +/** + * Validate a mentor-designation payload before it is written. Returns a list of + * human-readable problems (empty when valid) so the route can 400 instead of + * storing an inconsistent row. + */ +export function validateMentorDesignation(input: { + verifiedLevel: string; + mentorMinLevel: string; +}): string[] { + const problems: string[] = []; + if (!SKILL_LEVELS.includes(input.verifiedLevel as SkillLevel)) { + problems.push(`verifiedLevel must be one of: ${SKILL_LEVELS.join(", ")}`); + } + if (!SKILL_LEVELS.includes(input.mentorMinLevel as SkillLevel)) { + problems.push(`mentorMinLevel must be one of: ${SKILL_LEVELS.join(", ")}`); + } + return problems; +}