Files
pn-new-crm/tests/unit/active-interest.test.ts
Matt b966d8106d feat(active-interest): canonical predicate + fix stale getHotDeals rank
Extract activeInterestsWhere(portId) as the single source of truth for
"active interest" SQL filtering: scoped + archived_at IS NULL + outcome
IS NULL. Won deals are now CLOSED, not active — pre-2026-05-14 the
dashboard used a permissive `outcome IS NULL OR outcome = 'won'` that
double-counted won revenue against the in-flight pipeline.

Locked in PRE-DEPLOY-PLAN § 1.1.2.

Bonus catch: getHotDeals rank-CASE referenced the OLD 9-stage pipeline
names ('completed', 'contract_signed', 'contract_sent', 'deposit_10pct',
'eoi_signed', 'eoi_sent', 'in_communication', 'details_sent'). Every
row hit the ELSE 0 branch under the new 7-stage model, collapsing
ordering to updatedAt only — the widget silently stopped surfacing
"closest to closing". Rebuilt the rank ladder against the current
canonical stages (enquiry → ... → contract).

Tests: 2 unit tests assert the predicate's compiled SQL contains
"archived_at" IS NULL + "outcome" IS NULL, and never the legacy 'won'
literal.

Remaining sweep targets queued for the next commit:
- client-archive-dossier.service.ts
- client-restore.service.ts
- client-archive.service.ts
- reminders.service.ts
- berths.service.ts (recommender feasibility)
- interests.service.ts
- report-generators.ts

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 14:53:58 +02:00

38 lines
1.5 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { PgDialect } from 'drizzle-orm/pg-core';
import { activeInterestsWhere } from '@/lib/services/active-interest';
/**
* Locks in the canonical active-interest predicate per
* `docs/PRE-DEPLOY-PLAN.md` § 1.1.2 / commit f86f511. The whole pipeline
* report + recommender + reminder + restore + dossier surface depends on
* this predicate matching the same set of rows on every read; if it
* drifts, "active interest" stops meaning the same thing across
* dashboard / kanban / hot deals / PDF reports.
*/
describe('activeInterestsWhere', () => {
const PORT_ID = '11111111-1111-1111-1111-111111111111';
const dialect = new PgDialect();
it('compiles to: scoped to portId AND not archived AND no terminal outcome', () => {
const fragment = activeInterestsWhere(PORT_ID);
const compiled = dialect.sqlToQuery(fragment);
expect(compiled.sql).toContain('"port_id"');
expect(compiled.sql).toContain('"archived_at"');
expect(compiled.sql).toContain('"outcome"');
expect(compiled.sql).toMatch(/"archived_at"\s+is\s+null/i);
expect(compiled.sql).toMatch(/"outcome"\s+is\s+null/i);
expect(compiled.params).toContain(PORT_ID);
});
it('does NOT include the legacy `outcome = "won"` permissive branch', () => {
const fragment = activeInterestsWhere(PORT_ID);
const compiled = dialect.sqlToQuery(fragment);
expect(compiled.sql.toLowerCase()).not.toContain("'won'");
expect(compiled.params).not.toContain('won');
});
});