Files
AgendaPro/server/lib/scheduling.test.ts
AgendaPro Dev d796538fd9 feat: rebrand AgendaPro -> AgendaMax + calendar fixes + current-week demo seed
- Rebrand all user-facing text to AgendaMax
- Fix sidebar scrolling: card container no longer overflows html (flex h-full min-h-0)
- Fix calendar toolbar hover: active buttons keep readable contrast
- Sticky calendar toolbar (month/week/day always visible on scroll)
- Seed guarantees 2-4 appointments per day for current week (Mon-Sat)
2026-07-27 10:11:16 -06:00

146 lines
7.4 KiB
TypeScript

// server/lib/scheduling.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import {
parseWorkingHours, isoDayOfWeek, getWorkingHoursForDate,
normalizeText, tokens, specialtyMatch, overlaps, hasConflict, scoreCandidate,
pickBestSlotEmployee, BUSY_STATUSES,
} from "./scheduling.ts";
import type { CandidateInfo, BusyWindow } from "./scheduling.ts";
test("parseWorkingHours: json válido → mapa 1..7", () => {
const m = parseWorkingHours(JSON.stringify({ 1: { start: "09:00", end: "20:00" }, 6: null, 7: null }));
assert.equal(m?.[1]?.start, "09:00");
assert.equal(m?.[6], null);
assert.equal(m?.[7], null);
assert.equal(m?.[2], null); // ausente → null
});
test("parseWorkingHours: null/invalid → null", () => {
assert.equal(parseWorkingHours(null), null);
assert.equal(parseWorkingHours("no-json"), null);
});
test("isoDayOfWeek: lunes=1, domingo=7", () => {
assert.equal(isoDayOfWeek(new Date("2026-07-27T12:00:00")), 1); // lunes
assert.equal(isoDayOfWeek(new Date("2026-07-26T12:00:00")), 7); // domingo
});
test("getWorkingHoursForDate: empleado tiene prioridad sobre negocio", () => {
const emp = parseWorkingHours(JSON.stringify({ 1: { start: "10:00", end: "14:00" } }));
const biz = parseWorkingHours(JSON.stringify({ 1: { start: "09:00", end: "20:00" } }));
const r = getWorkingHoursForDate(emp, biz, new Date("2026-07-27T12:00:00")); // lunes
assert.equal(r?.startMs, new Date(2026, 6, 27, 10, 0).getTime());
});
test("getWorkingHoursForDate: día cerrado → null", () => {
const biz = parseWorkingHours(JSON.stringify({ 6: null, 7: null })); // fin de semana cerrado
assert.equal(getWorkingHoursForDate(null, biz, new Date("2026-07-25T12:00:00")), null); // sábado
});
test("normalizeText/tokens: quita acentos y lowercase", () => {
assert.equal(normalizeText("Coloración"), "coloracion");
assert.deepEqual(tokens("Corte de cabello"), ["corte", "de", "cabello"]);
});
test("specialtyMatch: coincidencia exacta de etiqueta → 1", () => {
assert.equal(specialtyMatch(["corte"], { name: "Corte de cabello", category: "Cabello" }), 1);
});
test("specialtyMatch: etiqueta con acento/case → 1", () => {
assert.equal(specialtyMatch(["Coloración"], { name: "Tinte", category: "Coloración" }), 1);
});
test("specialtyMatch: sin etiquetas → 0.5", () => {
assert.equal(specialtyMatch([], { name: "Corte", category: "Cabello" }), 0.5);
});
test("specialtyMatch: etiqueta no relacionada → 0.5", () => {
assert.equal(specialtyMatch(["barba"], { name: "Manicura", category: "Uñas" }), 0.5);
});
test("overlaps: bordes inclusivos de no-traslape", () => {
// una cita termina exactamente cuando empieza otra → NO traslape
assert.equal(overlaps(100, 200, 200, 300), false);
// traslape real
assert.equal(overlaps(100, 250, 200, 300), true);
assert.equal(overlaps(200, 300, 100, 250), true);
});
test("hasConflict: lista vacía → false", () => {
assert.equal(hasConflict([], 100, 200), false);
});
test("scoreCandidate: pesos 40/20/40", () => {
// todo al máximo → 100
assert.equal(scoreCandidate({ specialtyMatch: 1, efficiency: 1, loadBalance: 0 }), 100);
// todo al mínimo → 0
assert.equal(scoreCandidate({ specialtyMatch: 0, efficiency: 0, loadBalance: 1 }), 0);
// sólo specialty (0.5) → 20 (40·0.5)
assert.equal(scoreCandidate({ specialtyMatch: 0.5, efficiency: 0, loadBalance: 1 }), 20);
});
test("scoreCandidate: mayor efficiency → mayor score", () => {
const low = scoreCandidate({ specialtyMatch: 0.5, efficiency: 0.2, loadBalance: 0.5 });
const high = scoreCandidate({ specialtyMatch: 0.5, efficiency: 0.9, loadBalance: 0.5 });
assert.ok(high > low);
});
test("scoreCandidate: menor load (más disponible) → mayor score", () => {
const busy = scoreCandidate({ specialtyMatch: 0.5, efficiency: 0.5, loadBalance: 0.9 });
const free = scoreCandidate({ specialtyMatch: 0.5, efficiency: 0.5, loadBalance: 0.1 });
assert.ok(free > busy);
});
test("scoreCandidate: fairness — libre+ineficiente vence a ocupado+eficiente (misma especialidad)", () => {
// Con los pesos viejos (50/30/20) el efficiency estático (30) aplastaba al load (20):
// un senior ocupado siempre ganaba. Con 40/20/40, el load (40) supera al efficiency (20).
// A: especialista ocupado (load 0.8) y muy eficiente (0.9)
const a = scoreCandidate({ specialtyMatch: 0.5, efficiency: 0.9, loadBalance: 0.8 });
// B: especialista libre (load 0.1) e ineficiente (0.2)
const b = scoreCandidate({ specialtyMatch: 0.5, efficiency: 0.2, loadBalance: 0.1 });
assert.equal(a, 40 * 0.5 + 20 * 0.9 + 40 * (1 - 0.8)); // 46
assert.equal(b, 40 * 0.5 + 20 * 0.2 + 40 * (1 - 0.1)); // 60
assert.ok(b > a, "el especialista libre debe vencer al ocupado+eficiente (anti-burnout)");
});
test("pickBestSlotEmployee: empate en score → gana el de menor loadBalance (no por id)", () => {
const bizWh = parseWorkingHours(JSON.stringify({ 1: { start: "09:00", end: "17:00" } }));
const startMs = new Date(2026, 6, 27, 13, 0).getTime(); // lunes, dentro de horario
const endMs = new Date(2026, 6, 27, 14, 0).getTime();
// A (id menor, senior): especialidad coincide (sm=1), ocupado media jornada (load=0.5)
// → 40·1 + 20·0.5 + 40·0.5 = 70
// B (id mayor, junior): sin especialidad (sm=0.5), libre (load=0)
// → 40·0.5 + 20·0.5 + 40·1 = 70 → empate exacto; menor load gana
const A: CandidateInfo = { id: 1, name: "Senior", color: "#fff", role: "stylist", specialties: ["corte"], efficiency_score: 50, empWh: null };
const B: CandidateInfo = { id: 2, name: "Junior", color: "#fff", role: "stylist", specialties: [], efficiency_score: 50, empWh: null };
const busy = new Map<number, BusyWindow[]>([
[1, [{ startMs: new Date(2026, 6, 27, 9, 0).getTime(), endMs: new Date(2026, 6, 27, 13, 0).getTime() }]],
[2, []],
]);
const best = pickBestSlotEmployee([A, B], busy, bizWh, { name: "Corte", category: "Cabello" }, startMs, endMs);
assert.equal(best?.id, 2, "el junior libre gana el empate por menor loadBalance, NO por id menor");
});
test("pickBestSlotEmployee: empate total (score+load) → decisión determinista y reproducible", () => {
const bizWh = parseWorkingHours(JSON.stringify({ 1: { start: "09:00", end: "17:00" } }));
const startMs = new Date(2026, 6, 27, 9, 0).getTime();
const endMs = new Date(2026, 6, 27, 10, 0).getTime();
// Dos candidatos idénticos salvo el id → mismo score y mismo loadBalance → hash decide.
const A: CandidateInfo = { id: 10, name: "Diez", color: "#fff", role: "s", specialties: ["corte"], efficiency_score: 50, empWh: null };
const B: CandidateInfo = { id: 20, name: "Veinte", color: "#fff", role: "s", specialties: ["corte"], efficiency_score: 50, empWh: null };
const busy = new Map<number, BusyWindow[]>([[10, []], [20, []]]);
const r1 = pickBestSlotEmployee([A, B], busy, bizWh, { name: "Corte", category: "Cabello" }, startMs, endMs);
const r2 = pickBestSlotEmployee([A, B], busy, bizWh, { name: "Corte", category: "Cabello" }, startMs, endMs);
assert.ok(r1 && (r1.id === 10 || r1.id === 20), "debe elegir uno de los dos candidatos");
assert.equal(r1?.id, r2?.id, "misma entrada → misma decisión (hash determinista, reproducible)");
});
test("BUSY_STATUSES: scheduled+completed sí cuentan; no_show NO (silla vacía)", () => {
const s: string[] = [...BUSY_STATUSES];
assert.ok(s.includes("scheduled"));
assert.ok(s.includes("completed"));
assert.equal(s.includes("no_show"), false);
assert.equal(s.length, 2);
});