Files
dashlibstorage/scripts/test-shipment.js
2026-10-12 22:32:21 +02:00

241 lines
8.7 KiB
JavaScript

import assert from 'node:assert/strict';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import LibDashStore from '../src/index.js';
import { BetterSqlite3Provider } from '../src/providers/sqlite3/index.js';
async function run() {
console.log('--- Testing Shipment Manifest & Multi-Package Shipping ---');
const tempDir = mkdtempSync(join(tmpdir(), 'test-shipment-'));
const dbFile = join(tempDir, 'test.db');
const storageDir = join(tempDir, 'storage');
const provider = new BetterSqlite3Provider({
filename: dbFile,
storageDir,
});
await provider.initialize();
const store = await new LibDashStore(provider);
try {
const googleAuthId = await store.google_auth.add('shipment-test@example.com');
const company = await store.company.create({
name: 'Shipment Test Corp',
iso639: 'de',
taxrate: 19,
reducedtaxrate: 7,
googleAuthId,
});
await store.google_auth.setSelectedCompany(googleAuthId, company.id);
// Create a warehouse
const wh = await store.warehouse.create(googleAuthId, { name: 'Hauptlager' });
// Create customer entity
const groups = await store.customergroup.list(googleAuthId);
const customer = await store.entity.create(googleAuthId, {
customergroupId: groups[0].id,
name: 'Max Mustermann',
});
// Create Item A and Item B
const itemA = await store.article.createUnderParent({
googleAuthId,
parentId: company.rootCategoryId,
name: 'Item A',
sku: 'ITEM-A',
});
const itemB = await store.article.createUnderParent({
googleAuthId,
parentId: company.rootCategoryId,
name: 'Item B',
sku: 'ITEM-B',
});
// Stock up Item A (20) and Item B (20)
await store.stock.adjust(googleAuthId, {
sku: 'ITEM-A',
warehouseId: wh.id,
amount: 20,
comment: 'Initial Stock',
});
await store.stock.adjust(googleAuthId, {
sku: 'ITEM-B',
warehouseId: wh.id,
amount: 20,
comment: 'Initial Stock',
});
// Create Order with 10x Item A, 5x Item B
const order = await store.order.create(googleAuthId, {
number: 'AU-1001',
date: '2026-10-12',
entityId: customer.id,
lines: [
{ sku: 'ITEM-A', amount: 10, net: 100, taxrate: 19 },
{ sku: 'ITEM-B', amount: 5, net: 50, taxrate: 19 },
],
});
assert.equal(order.number, 'AU-1001');
console.log('Order created:', order.number);
// 1. Create Shipment 1: 6x Item A, 3x Item B -> AU-1001-L-1
const shipment1 = await store.shipment.create(googleAuthId, {
orderId: order.id,
lines: [
{ sku: 'ITEM-A', amount: 6 },
{ sku: 'ITEM-B', amount: 3 },
],
});
assert.equal(shipment1.number, 'AU-1001-L-1');
assert.equal(shipment1.lines.length, 2);
console.log('PASS: Shipment 1 created with number:', shipment1.number);
// 2. Create Shipment 2: 5x Item A -> Fails with ceiling violation (6 + 5 > 10)
let ceilingErrorThrown = false;
try {
await store.shipment.create(googleAuthId, {
orderId: order.id,
lines: [{ sku: 'ITEM-A', amount: 5 }],
});
} catch (err) {
ceilingErrorThrown = true;
assert.match(err.message, /Shipment amount exceeds ordered amount for article \(ITEM-A\)/);
console.log('PASS: Ceiling violation caught:', err.message);
}
assert.ok(ceilingErrorThrown, 'Expected ceiling violation error to be thrown');
// Also test foreign article rejection
let foreignArticleErrorThrown = false;
try {
await store.shipment.create(googleAuthId, {
orderId: order.id,
lines: [{ sku: 'ITEM-UNKNOWN', amount: 1 }],
});
} catch (err) {
foreignArticleErrorThrown = true;
assert.match(err.message, /Article \(ITEM-UNKNOWN\) is not part of order/);
console.log('PASS: Foreign article rejected:', err.message);
}
assert.ok(foreignArticleErrorThrown, 'Expected foreign article error to be thrown');
// 3. Create Shipment 2: 4x Item A, 2x Item B -> AU-1001-L-2
const shipment2 = await store.shipment.create(googleAuthId, {
orderId: order.id,
lines: [
{ sku: 'ITEM-A', amount: 4 },
{ sku: 'ITEM-B', amount: 2 },
],
});
assert.equal(shipment2.number, 'AU-1001-L-2');
assert.equal(shipment2.lines.length, 2);
console.log('PASS: Shipment 2 created with number:', shipment2.number);
// 4. Try creating another shipment (now 10/10 of Item A and 5/5 of Item B are shipped)
let exhaustedErrorThrown = false;
try {
await store.shipment.create(googleAuthId, {
orderId: order.id,
lines: [{ sku: 'ITEM-A', amount: 1 }],
});
} catch (err) {
exhaustedErrorThrown = true;
assert.match(err.message, /Shipment amount exceeds ordered amount for article \(ITEM-A\)/);
console.log('PASS: Exhausted quantity violation caught:', err.message);
}
assert.ok(exhaustedErrorThrown, 'Expected exhausted error to be thrown');
// 5. Add packages to Shipment 1 with tracking codes
const pkg1 = await store.shipping.create(googleAuthId, {
manifestId: shipment1.id,
type: 'parcel',
state: 'draft',
trackingCode: 'TRACK-111',
carrier: 'DHL',
});
assert.equal(pkg1.trackingCode, 'TRACK-111');
assert.equal(pkg1.carrier, 'DHL');
assert.equal(pkg1.state, 'draft');
console.log('PASS: Package 1 created for Shipment 1:', pkg1.number, pkg1.trackingCode);
const pkg2 = await store.shipping.create(googleAuthId, {
manifestId: shipment1.id,
type: 'parcel',
state: 'draft',
trackingCode: 'TRACK-222',
carrier: 'DHL',
});
assert.equal(pkg2.trackingCode, 'TRACK-222');
console.log('PASS: Package 2 created for Shipment 1:', pkg2.number, pkg2.trackingCode);
// Check listByOrderId
const orderShipments = await store.shipment.listByOrderId(googleAuthId, order.id);
assert.equal(orderShipments.length, 2);
assert.equal(orderShipments[0].shippings.length, 2);
console.log('PASS: store.shipment.listByOrderId returned both shipments');
// Check getById
const fetchedShipment1 = await store.shipment.getById(googleAuthId, shipment1.id);
assert.equal(fetchedShipment1.id, shipment1.id);
assert.equal(fetchedShipment1.shippings.length, 2);
console.log('PASS: store.shipment.getById returned shipment with shippings');
// 6. Transition Package 1 to shipped (should deduct ledger stock for Shipment 1 lines: 6 of Item A, 3 of Item B)
const updatedPkg1 = await store.shipping.update(googleAuthId, pkg1.id, {
state: 'shipped',
});
assert.equal(updatedPkg1.state, 'shipped');
console.log('PASS: Package 1 transitioned to shipped');
// Transition Package 2 to shipped (stock already booked for shipment1, should not double-deduct)
const updatedPkg2 = await store.shipping.update(googleAuthId, pkg2.id, {
state: 'shipped',
});
assert.equal(updatedPkg2.state, 'shipped');
console.log('PASS: Package 2 transitioned to shipped');
// 7. Verify cannot delete shipped shipment
let deleteErrorThrown = false;
try {
await store.shipment.delete(googleAuthId, shipment1.id);
} catch (err) {
deleteErrorThrown = true;
assert.match(err.message, /Shipped shipment cannot be deleted/);
console.log('PASS: Shipped shipment delete rejected:', err.message);
}
assert.ok(deleteErrorThrown, 'Expected shipped shipment delete error');
// 8. Delete unshipped shipment 2
await store.shipment.delete(googleAuthId, shipment2.id);
const shipmentsAfterDel = await store.shipment.listByOrderId(googleAuthId, order.id);
assert.equal(shipmentsAfterDel.length, 1);
console.log('PASS: Unshipped shipment 2 successfully deleted');
// 9. Verify peek next number works for entity and other types without error
const nextEntity = await store.entity.peekNextNumber(googleAuthId);
assert.ok(typeof nextEntity === 'string' && nextEntity.length > 0);
console.log('PASS: store.entity.peekNextNumber succeeded:', nextEntity);
const numList = await store.numbers.list(googleAuthId);
assert.ok(Array.isArray(numList) && numList.length > 0);
const shipmentSeq = numList.find((n) => n.type === 'shipment');
assert.ok(shipmentSeq, 'Expected shipment sequence in numbers.list');
assert.equal(shipmentSeq.prefix, '-L-');
console.log('PASS: store.numbers.list includes shipment with prefix -L-');
console.log('--- ALL TESTS PASSED SUCCESSFULLY! ---');
} finally {
try {
rmSync(tempDir, { recursive: true, force: true });
} catch {}
}
}
run().catch((err) => {
console.error('TEST FAILED:', err);
process.exit(1);
});