Phase 6b of the berth-recommender refactor (see
docs/berth-recommender-and-pdf-plan.md §3.2, §3.3, §4.7b, §11.1, §14.6).
Builds on the Phase 6a pluggable storage backend (commit 83693dd) — every
file write goes through `getStorageBackend()`; no direct minio imports.
Schema (migration 0030_berth_pdf_versions):
- new table `berth_pdf_versions` with monotonic `version_number` per
berth, `storage_key` (renamed convention from §4.7a), sha256, size,
`download_url_expires_at` cache slot for §11.1 signed-URL throttling,
and `parse_results` jsonb for the audit trail.
- new column `berths.current_pdf_version_id` (deferred from Phase 0)
with FK to `berth_pdf_versions(id)` ON DELETE SET NULL.
- relations + types exported from `schema/berths.ts`.
3-tier reverse parser (`lib/services/berth-pdf-parser.ts`):
1. AcroForm via pdf-lib — pulls named fields (`length_ft`,
`mooring_number`, etc.) at confidence 1. Sample PDF has 0 such
fields, so this is defensive coverage for future templates.
2. OCR via Tesseract.js — positional/regex heuristics keyed off the
§9.2 layout (Length/Width/Water Depth as `<imperial> / <metric>`,
`WEEK HIGH / LOW`, `CONFIRMED THROUGH UNTIL <date>`, etc.). Returns
per-field confidence + global mean; flags imperial-vs-metric drift
>1% in `warnings`.
3. AI fallback — gated via `getResolvedOcrConfig()` (existing
openai/claude provider). Surfaced from the diff dialog only when
`shouldOfferAiTier()` returns true (mean OCR confidence below
0.55 threshold), so OPENAI_API_KEY isn't burned on every upload.
Service layer (`lib/services/berth-pdf.service.ts`):
- `uploadBerthPdf()` — magic-byte check, size cap, version-number
bump + current pointer in one transaction.
- `reconcilePdfWithBerth()` — auto-applies fields where CRM is null;
flags conflicts when CRM and PDF disagree; tolerates ±1% on numeric
columns; warns on mooring-number-in-PDF mismatch (§14.6).
- `applyParseResults()` — hard allowlist of writable columns;
stamps `appliedFields` onto `parse_results` for audit.
- `rollbackToVersion()` — pointer flip only, never re-parses (§14.6).
- `listBerthPdfVersions()` — version list with 15-min signed URLs.
- `getMaxUploadMb()` — port-override → global → default 15 lookup
on `system_settings.berth_pdf_max_upload_mb`.
§14.6 critical mitigations:
- Magic-byte check (`%PDF-`) on every upload; mismatch deletes the
storage object and rejects the request.
- Size cap from `system_settings.berth_pdf_max_upload_mb` (default
15 MB); enforced in the upload-url presign AND server-side.
- 0-byte uploads rejected.
- Mooring-number mismatch surfaces as a `warnings[]` entry on the
reconcile result so the rep sees it in the diff dialog.
- Imperial vs metric ±1% tolerance in both the parser warnings and
the reconcile equality check.
- Path traversal already blocked at the storage layer (Phase 6a).
API + UI:
- `POST /api/v1/berths/[id]/pdf-upload-url` — presigned URL (S3) or
HMAC-signed proxy URL (filesystem) sized to the per-port cap.
- `POST /api/v1/berths/[id]/pdf-versions` — verifies the upload via
`backend.head()`, writes the row, bumps `current_pdf_version_id`.
- `GET /api/v1/berths/[id]/pdf-versions` — version list + signed URLs.
- `POST /api/v1/berths/[id]/pdf-versions/[versionId]/rollback`.
- `POST /api/v1/berths/[id]/pdf-versions/parse-results/apply` —
rep-confirmed diff payload.
- New "Documents" tab on the berth detail page (`berth-tabs.tsx`)
with current-PDF panel, version history, Replace PDF button, and
`<PdfReconcileDialog>` for the auto-applied + conflicts UX.
System settings:
- `berth_pdf_max_upload_mb` (default 15) — caps presigned-upload size
+ server-side validation. Resolved port-override → global → default.
Tests:
- `tests/unit/services/berth-pdf-parser.test.ts` — magic bytes,
feet-inches, human dates, full §9.2-shaped OCR text → 18 fields,
drift warning, AI-tier gate.
- `tests/unit/services/berth-pdf-acroform.test.ts` — synthetic
pdf-lib AcroForm round-trip.
- `tests/integration/berth-pdf-versions.test.ts` — upload, version-
number bump, magic-byte rejection, reconcile auto-applied vs
conflicts vs ±1% tolerance, mooring-number warning,
applyParseResults allowlist enforcement, rollback semantics.
Acceptance: `pnpm exec tsc --noEmit` clean, `pnpm exec vitest run`
green at 1103/1103.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
150 lines
4.4 KiB
TypeScript
150 lines
4.4 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
|
|
import { derivePublicStatus, toPublicBerth } from '@/lib/services/public-berths';
|
|
import type { Berth, BerthMapData } from '@/lib/db/schema/berths';
|
|
|
|
function makeBerth(overrides: Partial<Berth> = {}): Berth {
|
|
return {
|
|
id: 'b1',
|
|
portId: 'p1',
|
|
mooringNumber: 'A1',
|
|
area: 'A',
|
|
status: 'available',
|
|
lengthFt: '206.69',
|
|
widthFt: '46.56',
|
|
draftFt: '14.5',
|
|
lengthM: '63',
|
|
widthM: '14.19',
|
|
draftM: '4.42',
|
|
widthIsMinimum: false,
|
|
nominalBoatSize: '200',
|
|
nominalBoatSizeM: '60.96',
|
|
waterDepth: '16.08',
|
|
waterDepthM: '4.9',
|
|
waterDepthIsMinimum: false,
|
|
sidePontoon: 'Quay PT',
|
|
powerCapacity: '330',
|
|
voltage: '480',
|
|
mooringType: 'Side Pier / Med Mooring',
|
|
cleatType: 'A5',
|
|
cleatCapacity: '20-24 ton break load',
|
|
bollardType: 'Bull bollard type B',
|
|
bollardCapacity: '40 ton break load',
|
|
access: 'Car (3t) to Vessel',
|
|
price: '3528000',
|
|
priceCurrency: 'USD',
|
|
weeklyRateHighUsd: null,
|
|
weeklyRateLowUsd: null,
|
|
dailyRateHighUsd: null,
|
|
dailyRateLowUsd: null,
|
|
pricingValidUntil: null,
|
|
bowFacing: 'East',
|
|
berthApproved: false,
|
|
tenureType: 'permanent',
|
|
tenureYears: null,
|
|
tenureStartDate: null,
|
|
tenureEndDate: null,
|
|
statusLastChangedBy: null,
|
|
statusLastChangedReason: null,
|
|
statusLastModified: null,
|
|
statusOverrideMode: null,
|
|
lastImportedAt: null,
|
|
currentPdfVersionId: null,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function makeMapData(overrides: Partial<BerthMapData> = {}): BerthMapData {
|
|
return {
|
|
id: 'm1',
|
|
berthId: 'b1',
|
|
svgPath: 'M0 0',
|
|
x: '922.819',
|
|
y: '930.721',
|
|
transform: '',
|
|
fontSize: '32',
|
|
extraData: {},
|
|
updatedAt: new Date(),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe('derivePublicStatus', () => {
|
|
it('"sold" wins over the specific-interest signal', () => {
|
|
expect(derivePublicStatus('sold', true)).toBe('Sold');
|
|
expect(derivePublicStatus('sold', false)).toBe('Sold');
|
|
});
|
|
it('"under_offer" maps to "Under Offer"', () => {
|
|
expect(derivePublicStatus('under_offer', false)).toBe('Under Offer');
|
|
});
|
|
it('a specific-interest link promotes "available" → "Under Offer"', () => {
|
|
expect(derivePublicStatus('available', true)).toBe('Under Offer');
|
|
});
|
|
it('plain available stays available', () => {
|
|
expect(derivePublicStatus('available', false)).toBe('Available');
|
|
});
|
|
});
|
|
|
|
describe('toPublicBerth', () => {
|
|
it('maps every NocoDB-style field with numeric coercion', () => {
|
|
const out = toPublicBerth(makeBerth(), makeMapData(), false);
|
|
expect(out.Id).toBe('b1');
|
|
expect(out['Mooring Number']).toBe('A1');
|
|
expect(out.Length).toBe(206.69);
|
|
expect(out.Width).toBe(46.56);
|
|
expect(out.Draft).toBe(14.5);
|
|
expect(out['Power Capacity']).toBe(330);
|
|
expect(out.Voltage).toBe(480);
|
|
expect(out['Nominal Boat Size']).toBe(200);
|
|
expect(out.Status).toBe('Available');
|
|
expect(out['Mooring Type']).toBe('Side Pier / Med Mooring');
|
|
expect(out['Bow Facing']).toBe('East');
|
|
expect(out.Area).toBe('A');
|
|
expect(out.Access).toBe('Car (3t) to Vessel');
|
|
});
|
|
|
|
it('inlines map data when present', () => {
|
|
const out = toPublicBerth(makeBerth(), makeMapData(), false);
|
|
expect(out['Map Data']).toEqual({
|
|
path: 'M0 0',
|
|
x: '922.819',
|
|
y: '930.721',
|
|
transform: '',
|
|
fontSize: '32',
|
|
});
|
|
});
|
|
|
|
it('omits map data entirely when missing', () => {
|
|
const out = toPublicBerth(makeBerth(), null, false);
|
|
expect(out['Map Data']).toBeUndefined();
|
|
});
|
|
|
|
it('numeric fields stored as strings round-trip cleanly', () => {
|
|
const out = toPublicBerth(makeBerth({ lengthFt: '42' }), null, false);
|
|
expect(out.Length).toBe(42);
|
|
});
|
|
|
|
it('null numeric inputs surface as null (not NaN)', () => {
|
|
const out = toPublicBerth(
|
|
makeBerth({ lengthFt: null, widthFt: null, draftFt: null }),
|
|
null,
|
|
false,
|
|
);
|
|
expect(out.Length).toBeNull();
|
|
expect(out.Width).toBeNull();
|
|
expect(out.Draft).toBeNull();
|
|
});
|
|
|
|
it('promotes status to "Under Offer" when a specific-interest link exists', () => {
|
|
const out = toPublicBerth(makeBerth({ status: 'available' }), null, true);
|
|
expect(out.Status).toBe('Under Offer');
|
|
});
|
|
|
|
it('preserves "Sold" even when a specific-interest link exists (defensive)', () => {
|
|
const out = toPublicBerth(makeBerth({ status: 'sold' }), null, true);
|
|
expect(out.Status).toBe('Sold');
|
|
});
|
|
});
|