import { bookingTonsSql } from './booking-tons.sql'; describe('bookingTonsSql', () => { const sql = bookingTonsSql('b'); // The regression this exists for: a plain COALESCE stops at the portal's // literal 0 for container bookings and reports them as weighing nothing. it('treats a stored 0 as "no figure" on both booking-level columns', () => { expect(sql).toContain('NULLIF(b.bulk_total_weight_tons, 0)'); expect(sql).toContain('NULLIF(b.cargo_total_weight_vgm, 0)'); }); it('falls back to the per-line container VGM, excluding soft-deleted lines', () => { expect(sql).toContain('SUM(bc.total_vgm_tons)'); expect(sql).toContain('freight.booking_container bc'); expect(sql).toContain('bc.booking_id = b.id'); expect(sql).toContain('bc.deleted_at IS NULL'); }); it('never returns NULL, so callers may SUM it directly', () => { expect(sql.trimEnd().endsWith('0)')).toBe(true); }); it('rewrites every reference when embedded under another alias', () => { const aliased = bookingTonsSql('bk'); expect(aliased).not.toMatch(/\bb\./); expect(aliased).toContain('bk.cargo_total_weight_vgm'); expect(aliased).toContain('bc.booking_id = bk.id'); }); });