import { resolveParcel, itemUnitWeights } from '../shared/utils/parcel'; import { orderToCheckoutValidatePayload } from '../features/order-ship/utils/orderToCheckoutValidatePayload'; import ordersToCheckoutValidatePayload from '../features/orders/utils/ordersToCheckoutValidatePayload'; import { buildQuotePayloadFromParcel } from '../features/order-ship/utils/orderToQuotePayload'; import type { Order, ParcelDimensions, PluginSettings, Quote } from '../types'; const quote = { service: { slug: 'service-slug' }, tags: [], availableExtras: [], dates: [], } as unknown as Quote; const makeSettings = ( over: object = {} ) => ( { shipping: { country: 'GB', countryName: 'United Kingdom', contactName: 'Shop', email: 's@example.com', town: 'London', property: '1', postcode: 'SW1A 1AA', street: 'Street', }, tax: {}, packages: [], defaultPackageId: '', overrides: { defaultWeightKg: 2 }, ...over, } ) as unknown as PluginSettings; const item = ( weight: number | string | null, quantity = 1, over: object = {} ) => ( { product_id: 1, variation_id: 0, name: 'Ceramic mug', quantity, total: '20.00', dimensions: { length: '', width: '', height: '', weight }, tariff_code: '691110', country_of_manufacture: 'GB', ...over, } ); const makeOrder = ( items: object[], parcel: ParcelDimensions | null = null, units = { weight: 'kg', dimension: 'cm' } ) => ( { id: 1502, shipping: { name: 'Test Customer', address_1: '1 High Street', city: 'Leeds', postcode: 'LS1 1AA', country: 'GB', phone: '', }, customer: { name: 'Test Customer', email: 'test@example.com' }, items, units, parcel, cheapestQuote: quote, } ) as unknown as Order; // ------------------------------------------------------------------------- describe( 'resolveParcel', () => { it( 'uses the weight entered in Packaging first', () => { const parcel = { length: 30, width: 20, height: 10, weight: 1 }; const result = resolveParcel( makeOrder( [ item( 3, 2 ) ], parcel ), makeSettings() ); expect( result ).toMatchObject( { ...parcel, weightSource: 'packaging' } ); } ); it( 'otherwise adds up the product weights by quantity', () => { const result = resolveParcel( makeOrder( [ item( 1.5, 2 ), item( 0.5, 1 ) ] ), makeSettings() ); expect( result.weight ).toBeCloseTo( 3.5 ); expect( result.weightSource ).toBe( 'products' ); expect( result.missingWeightItems ).toEqual( [] ); } ); it( 'counts the Default Weight per unit for each product without a weight', () => { const result = resolveParcel( makeOrder( [ item( '', 2 ), item( null, 1 ) ] ), makeSettings() ); expect( result.weight ).toBe( 6 ); expect( result.weightSource ).toBe( 'default' ); expect( result.defaultWeight ).toBe( 2 ); expect( result.missingWeightItems ).toHaveLength( 2 ); } ); it( 'falls back to 1 kg in store units when there is no Default Weight', () => { const settings = makeSettings( { overrides: {} } ); expect( resolveParcel( makeOrder( [ item( '' ) ] ), settings ).weight ).toBe( 1 ); const inGrams = makeOrder( [ item( '' ) ], null, { weight: 'g', dimension: 'cm' } ); expect( resolveParcel( inGrams, settings ).weight ).toBeCloseTo( 1000 ); } ); it( 'adds the Default Weight for the products without one to the others', () => { const result = resolveParcel( makeOrder( [ item( 1 ), item( '', 1, { name: 'Tea towel' } ) ] ), makeSettings() ); expect( result.weight ).toBe( 3 ); expect( result.weightSource ).toBe( 'default' ); expect( result.missingWeightItems ).toEqual( [ 'Tea towel' ] ); } ); it( 'takes the size from the default package, else 30×20×15 cm, never from the products', () => { // Eight 40×30×20 boxes: product sizes are ignored (no stacking to 160 cm). const big = makeOrder( [ item( 1, 8, { dimensions: { length: 40, width: 30, height: 20, weight: 1 } } ), ] ); const settings = makeSettings( { packages: [ { id: 'pkg_1', name: 'Box', length: 25, width: 15, height: 0, weight: 0 } ], defaultPackageId: 'pkg_1', } ); // A missing side of the default package falls back to the 30×20×15 cm side. expect( resolveParcel( big, settings ) ).toMatchObject( { length: 25, width: 15, height: 15 } ); expect( resolveParcel( big, makeSettings() ) ).toMatchObject( { length: 30, width: 20, height: 15, } ); } ); it( 'converts the 30×20×15 cm fallback to the store unit', () => { const order = makeOrder( [ item( 1 ) ], null, { weight: 'kg', dimension: 'mm' } ); expect( resolveParcel( order, makeSettings() ) ).toMatchObject( { length: 300, width: 200, height: 150, } ); } ); it( 'works without settings', () => { expect( resolveParcel( makeOrder( [ item( '' ) ] ), null ).weight ).toBe( 1 ); } ); } ); // ------------------------------------------------------------------------- describe( 'itemUnitWeights', () => { it( 'keeps each product weight', () => { const order = makeOrder( [ item( 1, 2 ), item( 0.5, 1 ) ] ); expect( itemUnitWeights( order, makeSettings() ) ).toEqual( [ 1, 0.5 ] ); } ); it( 'uses the Default Weight for a product without one, never a share of another', () => { const order = makeOrder( [ item( 1, 1 ), item( '', 2 ) ] ); expect( itemUnitWeights( order, makeSettings() ) ).toEqual( [ 1, 2 ] ); } ); it( 'uses 1 kg when there is no Default Weight', () => { const order = makeOrder( [ item( '', 1 ) ] ); expect( itemUnitWeights( order, makeSettings( { overrides: {} } ) ) ).toEqual( [ 1 ] ); } ); } ); // ------------------------------------------------------------------------- describe( 'virtual items and the parcel value', () => { const order = () => makeOrder( [ item( 0.5, 1, { name: 'Mug', total: '8.00', dimensions: { length: 10, width: 10, height: 10, weight: 0.5 } } ), item( '', 1, { name: 'E-book', total: '5.00', needs_shipping: false } ), ] ); it( 'quotes and books the same value: only items that ship', () => { const quoted = buildQuotePayloadFromParcel( order(), makeSettings().shipping as any, resolveParcel( order(), makeSettings() ) ); const single = orderToCheckoutValidatePayload( order(), makeSettings(), quote, [] ); const bulk = ordersToCheckoutValidatePayload( { orders: [ order() ], settings: makeSettings() } ); expect( quoted?.parcels[ 0 ].value ).toBe( 800 ); expect( single?.items[ 0 ].parcels[ 0 ].value ).toBe( 800 ); expect( bulk?.items[ 0 ].parcels[ 0 ].value ).toBe( 800 ); } ); it( 'still lists every line in the customs contents', () => { const single = orderToCheckoutValidatePayload( order(), makeSettings(), quote, [] ); const bulk = ordersToCheckoutValidatePayload( { orders: [ order() ], settings: makeSettings() } ); expect( single?.items[ 0 ].parcels[ 0 ].contents?.map( ( c ) => c.description ) ).toEqual( [ 'Mug', 'E-book' ] ); expect( bulk?.items[ 0 ].parcels[ 0 ].contents?.map( ( c ) => c.value ) ).toEqual( [ 800, 500 ] ); } ); } ); describe( 'booking payloads use the resolved parcel', () => { it( 'single order: books the Packaging parcel for products without weights', () => { const parcel = { length: 30, width: 20, height: 10, weight: 1 }; const payload = orderToCheckoutValidatePayload( makeOrder( [ item( '', 2 ) ], parcel ), makeSettings(), quote, [] ); const sent = payload?.items[ 0 ].parcels[ 0 ]; expect( sent ).toMatchObject( parcel ); // The entered parcel weight does not change the item weights. expect( sent?.contents?.[ 0 ].weight ).toBe( 2 ); } ); it( 'single order: keeps a weighted item at its own weight next to one without', () => { const payload = orderToCheckoutValidatePayload( makeOrder( [ item( 0.5, 1, { name: 'Cookies' } ), item( '', 1, { name: 'Pouch' } ) ] ), makeSettings(), quote, [] ); const sent = payload?.items[ 0 ].parcels[ 0 ]; expect( sent?.weight ).toBe( 2.5 ); expect( sent?.contents?.map( ( c ) => c.weight ) ).toEqual( [ 0.5, 2 ] ); } ); it( 'single order: books the Default Weight when nothing else is set', () => { const payload = orderToCheckoutValidatePayload( makeOrder( [ item( '' ) ] ), makeSettings(), quote, [] ); expect( payload?.items[ 0 ].parcels[ 0 ].weight ).toBe( 2 ); } ); it( 'bulk: never books a parcel weight of 0', () => { const payload = ordersToCheckoutValidatePayload( { orders: [ makeOrder( [ item( '', 1 ) ] ) ], settings: makeSettings(), } ); const sent = payload?.items[ 0 ].parcels[ 0 ]; expect( sent?.weight ).toBe( 2 ); expect( sent?.contents?.[ 0 ].weight ).toBe( 2 ); } ); it( 'bulk: still books the product weights when they are set', () => { const payload = ordersToCheckoutValidatePayload( { orders: [ makeOrder( [ item( 0.75, 2 ) ] ) ], settings: makeSettings(), } ); expect( payload?.items[ 0 ].parcels[ 0 ].weight ).toBe( 1.5 ); } ); it( 'quotes the same single parcel that is booked', () => { const parcel = { length: 30.4, width: 20, height: 10, weight: 1.25 }; const order = makeOrder( [ item( '', 2 ), item( 3, 1 ) ], parcel ); const quotePayload = buildQuotePayloadFromParcel( order, makeSettings().shipping as any, resolveParcel( order, makeSettings() ) ); expect( quotePayload?.parcels ).toEqual( [ { ref: '1502', weight: 1.25, length: 30.4, width: 20, height: 10, value: 4000 }, ] ); const booked = orderToCheckoutValidatePayload( order, makeSettings(), quote, [] ); expect( booked?.items[ 0 ].parcels[ 0 ] ).toMatchObject( { weight: 1.25, length: 30.4, width: 20, height: 10, } ); } ); it( 'leaves rounding to the server, so fractional store units match the booking', () => { // A metre store: the 30×20×15 cm fallback is 0.3×0.2×0.15 m and must not be rounded. const order = makeOrder( [ item( 1 ) ], null, { weight: 'kg', dimension: 'm' } ); const parcel = resolveParcel( order, makeSettings() ); const quoted = buildQuotePayloadFromParcel( order, makeSettings().shipping as any, parcel ); const booked = orderToCheckoutValidatePayload( order, makeSettings(), quote, [] ); expect( quoted?.parcels[ 0 ] ).toMatchObject( { length: 0.3, width: 0.2, height: 0.15 } ); expect( booked?.items[ 0 ].parcels[ 0 ] ).toMatchObject( { length: 0.3, width: 0.2, height: 0.15 } ); } ); } ); describe( 'resolveParcel virtual items', () => { it( 'leaves virtual items out of the weight and size, as checkout does', () => { const order = makeOrder( [ item( 0.5, 1, { dimensions: { length: 10, width: 10, height: 10, weight: 0.5 } } ), item( '', 1, { name: 'E-book', needs_shipping: false } ), ] ); // No default weight for the e-book; the default box. Matches ParcelBuilderTest. expect( resolveParcel( order, makeSettings() ) ).toMatchObject( { length: 30, width: 20, height: 15, weight: 0.5, missingWeightItems: [], } ); } ); } ); describe( 'resolveParcel mixed orders', () => { it( 'uses the default package once, whatever the mix of items', () => { const order = makeOrder( [ item( 0.4, 1, { dimensions: { length: 10, width: 10, height: 10, weight: 0.4 } } ), item( '', 5 ), ] ); const settings = makeSettings( { packages: [ { id: 'pkg_1', name: 'Box', length: 35, width: 25, height: 12, weight: 0 } ], defaultPackageId: 'pkg_1', } ); expect( resolveParcel( order, settings ) ).toMatchObject( { length: 35, width: 25, height: 12, weight: 10.4 } ); // No default package: the 30×20×15 cm fallback box. expect( resolveParcel( order, makeSettings() ) ).toMatchObject( { length: 30, width: 20, height: 15 } ); } ); } );