feat(tenancies-p4): public-map status flip via active permanent tenancy

- derivePublicStatus gains optional hasActivePermanentTenancy flag;
  precedence updated to "sold > under_offer > available" where
  Sold can come from EITHER berths.status='sold' (admin set) OR an
  active permanent-class tenancy (only when module enabled).
- Permanent-class tenure types defined in one place
  (isPermanentTenureType): permanent | fee_simple | strata_lot.
  Seasonal / fixed_term tenancies do NOT flip — they fall through to
  the existing under_offer / available precedence.
- /api/public/berths (list) + /api/public/berths/[mooringNumber]
  (single) both gate the lookup on isTenanciesModuleEnabled(portId).
  Disabled module = lookup skipped entirely, preserving pre-module
  behaviour for ports that haven't opted in.
- 8 new unit tests covering: flip from available, flip from under_offer,
  explicit sold idempotency, false-flag fallthrough, default-omit pre-
  module behaviour, permanent-class membership for each tenure type,
  and null/undefined/unknown rejection.

Verified: tsc clean, 1493/1493 vitest.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-25 15:17:06 +02:00
parent 20549fb22e
commit bfb29ab619
4 changed files with 137 additions and 14 deletions

View File

@@ -1,6 +1,10 @@
import { describe, it, expect } from 'vitest';
import { derivePublicStatus, toPublicBerth } from '@/lib/services/public-berths';
import {
derivePublicStatus,
isPermanentTenureType,
toPublicBerth,
} from '@/lib/services/public-berths';
import type { Berth, BerthMapData } from '@/lib/db/schema/berths';
function makeBerth(overrides: Partial<Berth> = {}): Berth {
@@ -93,6 +97,43 @@ describe('derivePublicStatus', () => {
it('plain available stays available', () => {
expect(derivePublicStatus('available', false)).toBe('Available');
});
// ─── Tenancies P4: permanent-tenancy flip ─────────────────────────────
describe('hasActivePermanentTenancy flag', () => {
it('flips an "available" berth to "Sold" when an active permanent tenancy exists', () => {
expect(derivePublicStatus('available', false, true)).toBe('Sold');
});
it('flips an "under_offer" berth to "Sold" (permanent tenancy is the strongest signal)', () => {
expect(derivePublicStatus('under_offer', true, true)).toBe('Sold');
});
it('explicit berths.status="sold" still resolves to "Sold" (idempotent)', () => {
expect(derivePublicStatus('sold', false, true)).toBe('Sold');
});
it('false flag falls through to existing precedence', () => {
expect(derivePublicStatus('available', true, false)).toBe('Under Offer');
expect(derivePublicStatus('available', false, false)).toBe('Available');
});
it('default-omitted flag preserves pre-module behaviour', () => {
expect(derivePublicStatus('available', true)).toBe('Under Offer');
});
});
});
describe('isPermanentTenureType', () => {
it('returns true for permanent-class tenure types', () => {
expect(isPermanentTenureType('permanent')).toBe(true);
expect(isPermanentTenureType('fee_simple')).toBe(true);
expect(isPermanentTenureType('strata_lot')).toBe(true);
});
it('returns false for seasonal / fixed-term', () => {
expect(isPermanentTenureType('seasonal')).toBe(false);
expect(isPermanentTenureType('fixed_term')).toBe(false);
});
it('returns false for null / undefined / unknown values', () => {
expect(isPermanentTenureType(null)).toBe(false);
expect(isPermanentTenureType(undefined)).toBe(false);
expect(isPermanentTenureType('garbage')).toBe(false);
});
});
describe('toPublicBerth', () => {