Files
pn-new-crm/tests/unit/services/berth-recommender.test.ts
Matt 98211066a5
Some checks failed
Build & Push Docker Images / lint (push) Successful in 2m4s
Build & Push Docker Images / build-and-push (push) Has been cancelled
fix(legacy-stage): purge 9-stage enum keys from rank tables and stale copy
L-001 hunt landed these:

  - src/lib/services/clients.service.ts — stageRank used pre-refactor
    9-stage names exclusively (`contract_signed`, `deposit_10pct`, …).
    Every modern 7-stage interest fell to rank 0, making client-list
    "most-progressed deal" sort effectively random. Modern values now
    own the canonical ranks; legacy aliases map to their 7-stage
    equivalents so historical audit data still sorts.

  - src/lib/services/berth-recommender.service.ts — STAGE_ORDER had
    the same 9-stage shape. LATE_STAGE_THRESHOLD pointed at the (now
    nonexistent) `deposit_10pct` slot. Reworked to the 7-stage scale;
    threshold now at `deposit_paid` (5).

  - Stale comments referencing `deposit_10pct` in schema (clients,
    financial) and client-archive services updated to current copy.

  - Smart-archive dialog rendered `i.pipelineStage` as raw enum; now
    routes through `stageLabelFor` (the new helper added with A2).

Test fixture updates: berth-recommender.test.ts numeric inputs
re-mapped to the new 7-stage scale (eoi_signed=5 → eoi=3, etc.).
1373/1373 vitest pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-15 01:18:13 +02:00

190 lines
5.2 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
classifyTier,
computeHeat,
DEFAULT_RECOMMENDER_SETTINGS,
} from '@/lib/services/berth-recommender.service';
describe('classifyTier', () => {
it('"A" when there is no interest history at all', () => {
expect(classifyTier({ activeInterestCount: 0, lostCount: 0, maxActiveStage: 0 })).toBe('A');
});
it('"B" when only lost interests exist (no active)', () => {
expect(classifyTier({ activeInterestCount: 0, lostCount: 2, maxActiveStage: 0 })).toBe('B');
});
// L-001 renumber: 7-stage ranks are 1=enquiry, 2=qualified/nurturing,
// 3=eoi, 4=reservation, 5=deposit_paid, 6=contract. Tier D fires at
// deposit_paid (5) or later.
it('"C" when an active interest is in an early stage (eoi)', () => {
expect(classifyTier({ activeInterestCount: 1, lostCount: 0, maxActiveStage: 3 })).toBe('C');
});
it('"C" even when a prior interest was lost, if there is an active one', () => {
expect(classifyTier({ activeInterestCount: 1, lostCount: 5, maxActiveStage: 2 })).toBe('C');
});
it('"D" when an active interest is at deposit or beyond', () => {
expect(classifyTier({ activeInterestCount: 1, lostCount: 0, maxActiveStage: 5 })).toBe('D');
expect(classifyTier({ activeInterestCount: 1, lostCount: 0, maxActiveStage: 6 })).toBe('D');
});
it('still "C" at reservation (stage 4) - tier D only kicks in at deposit', () => {
expect(classifyTier({ activeInterestCount: 1, lostCount: 0, maxActiveStage: 4 })).toBe('C');
});
});
describe('computeHeat', () => {
const w = DEFAULT_RECOMMENDER_SETTINGS;
const NOW = new Date('2026-05-05T00:00:00Z');
it('zero heat when nothing in history', () => {
const h = computeHeat(
{
latestFallthroughAt: null,
totalInterestCount: 0,
eoiSignedCount: 0,
fallthroughMaxStage: 0,
},
w,
NOW,
);
expect(h.total).toBe(0);
});
it('full recency for a fall-through within the last 30 days', () => {
const h = computeHeat(
{
latestFallthroughAt: new Date('2026-04-25T00:00:00Z'),
totalInterestCount: 0,
eoiSignedCount: 0,
fallthroughMaxStage: 1,
},
w,
NOW,
);
// recency component should be the full heat_weight_recency (30)
expect(h.recency).toBeCloseTo(30, 1);
});
it('zero recency for an ancient fall-through (>1 year)', () => {
const h = computeHeat(
{
latestFallthroughAt: new Date('2024-01-01T00:00:00Z'),
totalInterestCount: 0,
eoiSignedCount: 0,
fallthroughMaxStage: 1,
},
w,
NOW,
);
expect(h.recency).toBe(0);
});
it('full furthest-stage when the fall-through reached deposit', () => {
const h = computeHeat(
{
latestFallthroughAt: null,
totalInterestCount: 0,
eoiSignedCount: 0,
fallthroughMaxStage: 5, // deposit_paid (was deposit_10pct=6 pre-refactor)
},
w,
NOW,
);
expect(h.furthestStage).toBeCloseTo(40, 1);
});
it('saturates interest-count at 5+', () => {
const h = computeHeat(
{
latestFallthroughAt: null,
totalInterestCount: 10,
eoiSignedCount: 0,
fallthroughMaxStage: 0,
},
w,
NOW,
);
expect(h.interestCount).toBeCloseTo(15, 1); // full weight
});
it('saturates EOI-count at 3+', () => {
const h = computeHeat(
{
latestFallthroughAt: null,
totalInterestCount: 0,
eoiSignedCount: 5,
fallthroughMaxStage: 0,
},
w,
NOW,
);
expect(h.eoiCount).toBeCloseTo(15, 1);
});
it('total ≈ 100 when everything is maxed', () => {
const h = computeHeat(
{
latestFallthroughAt: new Date('2026-04-25T00:00:00Z'),
totalInterestCount: 5,
eoiSignedCount: 3,
fallthroughMaxStage: 6,
},
w,
NOW,
);
expect(h.total).toBeGreaterThanOrEqual(99);
expect(h.total).toBeLessThanOrEqual(100);
});
it('respects tunable per-port weights (skewed toward recency)', () => {
const skewed = {
heatWeightRecency: 100,
heatWeightFurthestStage: 0,
heatWeightInterestCount: 0,
heatWeightEoiCount: 0,
};
const recent = computeHeat(
{
latestFallthroughAt: new Date('2026-04-25T00:00:00Z'),
totalInterestCount: 0,
eoiSignedCount: 0,
fallthroughMaxStage: 0,
},
skewed,
NOW,
);
expect(recent.total).toBeCloseTo(100, 1);
const old = computeHeat(
{
latestFallthroughAt: new Date('2024-01-01T00:00:00Z'),
totalInterestCount: 5,
eoiSignedCount: 3,
fallthroughMaxStage: 6,
},
skewed,
NOW,
);
expect(old.total).toBe(0);
});
it('zero-weights guard (no division-by-zero blow-up)', () => {
const zeros = {
heatWeightRecency: 0,
heatWeightFurthestStage: 0,
heatWeightInterestCount: 0,
heatWeightEoiCount: 0,
};
const h = computeHeat(
{
latestFallthroughAt: new Date(),
totalInterestCount: 5,
eoiSignedCount: 3,
fallthroughMaxStage: 6,
},
zeros,
NOW,
);
expect(h.total).toBe(0);
});
});