This commit is contained in:
seb
2026-10-15 20:14:51 +02:00
parent 7c8c24eb4d
commit 2179b2e241
23 changed files with 1242 additions and 105 deletions

View File

@@ -63,8 +63,8 @@ async function run() {
assert.ok(order1.id, 'Order must have an id');
assert.equal(order1.type, 'order');
assert.equal(order1.entityId, customer.id);
assert.equal(order1.externalNumber, 'EXT-QUOTE-123');
assert.ok(order1.number, 'Order must have auto-allocated number');
assert.equal(order1.externalNumber, quote.number);
assert.equal(order1.number, 'AUF-000001');
assert.equal(order1.lines.length, 1);
assert.equal(order1.lines[0].sku, art.sku);
assert.equal(order1.lines[0].amount, 5);
@@ -106,6 +106,35 @@ async function run() {
{ message: 'Quote not found' }
);
// 6. Quote without entity should throw 'No entity selected' if no entity passed
const quoteWithoutEntity = await store.quote.create(googleAuthId, {
number: 'Q-NO-ENT',
date: '2026-09-01T10:00:00Z',
lines: [{ sku: art.sku, name: 'Gadget', amount: 1, net: 10, taxrate: 19 }],
});
assert.equal(quoteWithoutEntity.entityId, null);
await assert.rejects(
async () => {
await store.order.createFromQuote(googleAuthId, quoteWithoutEntity.id);
},
{ message: 'No entity selected' }
);
// 7. Explicitly overriding with entityId: null should also throw 'No entity selected'
await assert.rejects(
async () => {
await store.order.createFromQuote(googleAuthId, quote.id, { entityId: null });
},
{ message: 'No entity selected' }
);
// 8. Passing entityId for quote without entity should succeed
const orderFromNoEnt = await store.order.createFromQuote(googleAuthId, quoteWithoutEntity.id, {
entityId: customer.id,
});
assert.equal(orderFromNoEnt.entityId, customer.id);
console.log('PASS: All order.createFromQuote tests passed successfully!');
} finally {
try {

View File

@@ -214,7 +214,76 @@ async function run() {
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
// 9. Test order2: One item, some items, all items, and automatic ordersummary updates
const order2 = await store.order.create(googleAuthId, {
number: 'AU-1002',
date: '2026-10-15',
entityId: customer.id,
lines: [
{ sku: 'ITEM-A', amount: 10, net: 100, taxrate: 19 },
{ sku: 'ITEM-B', amount: 5, net: 50, taxrate: 19 },
],
});
// Check initial ordersummary for order2
let sum2 = provider.db.prepare('SELECT * FROM ordersummary WHERE manifest_id = ?').get(order2.id);
assert.equal(sum2.ordered_quantity, 15);
assert.equal(sum2.shipped_quantity, 0);
assert.equal(sum2.is_shipped, 0);
// 9a. Ship ONE item (3x ITEM-A) via store.order.createShipment
const shOne = await store.order.createShipment(googleAuthId, order2.id, {
lines: [{ sku: 'ITEM-A', amount: 3 }],
});
assert.equal(shOne.number, 'AU-1002-L-1');
assert.equal(shOne.lines.length, 1);
assert.equal(shOne.lines[0].sku, 'ITEM-A');
assert.equal(shOne.lines[0].amount, 3);
// Verify ordersummary updated: shipped_quantity = 3, is_shipped = 0
sum2 = provider.db.prepare('SELECT * FROM ordersummary WHERE manifest_id = ?').get(order2.id);
assert.equal(sum2.shipped_quantity, 3);
assert.equal(sum2.is_shipped, 0);
console.log('PASS: 9a. Shipped ONE item; ordersummary updated with shipped_quantity=3, is_shipped=0');
// 9b. Ship SOME items with omitted amount (e.g. ITEM-A with no amount -> defaults to remaining 7)
const shSome = await store.order.createShipment(googleAuthId, order2.id, {
lines: [{ sku: 'ITEM-A' }],
});
assert.equal(shSome.number, 'AU-1002-L-2');
assert.equal(shSome.lines.length, 1);
assert.equal(shSome.lines[0].sku, 'ITEM-A');
assert.equal(shSome.lines[0].amount, 7);
// Verify ordersummary updated: shipped_quantity = 10 (3 + 7), is_shipped = 0
sum2 = provider.db.prepare('SELECT * FROM ordersummary WHERE manifest_id = ?').get(order2.id);
assert.equal(sum2.shipped_quantity, 10);
assert.equal(sum2.is_shipped, 0);
console.log('PASS: 9b. Shipped SOME items with omitted amount; ordersummary updated with shipped_quantity=10, is_shipped=0');
// 9c. Ship ALL remaining items by omitting lines entirely ({})
const shAll = await store.order.createShipment(googleAuthId, order2.id, {});
assert.equal(shAll.number, 'AU-1002-L-3');
assert.equal(shAll.lines.length, 1);
assert.equal(shAll.lines[0].sku, 'ITEM-B');
assert.equal(shAll.lines[0].amount, 5);
// Verify ordersummary updated: shipped_quantity = 15, is_shipped = 1 (order is fully shipped!)
sum2 = provider.db.prepare('SELECT * FROM ordersummary WHERE manifest_id = ?').get(order2.id);
assert.equal(sum2.shipped_quantity, 15);
assert.equal(sum2.is_shipped, 1);
console.log('PASS: 9c. Shipped ALL remaining items with omitted lines; ordersummary updated with shipped_quantity=15, is_shipped=1');
// 9d. Calling createShipment when all items already shipped throws 'All items already shipped'
await assert.rejects(
async () => {
await store.order.createShipment(googleAuthId, order2.id);
},
{ message: 'All items already shipped' }
);
console.log('PASS: 9d. Re-shipping fully shipped order rejected with "All items already shipped"');
// 10. 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);
@@ -223,8 +292,41 @@ async function run() {
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-');
// 11. Test store.shipment.list, store.shipment.update, and rebuildSummaries
const listAll = await store.shipment.list(googleAuthId, { offset: 0, limit: 10 });
assert.ok(listAll.total >= 3, `Expected at least 3 shipments, got ${listAll.total}`);
assert.equal(listAll.limit, 10);
assert.equal(listAll.offset, 0);
const s1 = listAll.shipments.find((s) => s.id === shOne.id);
assert.ok(s1, 'Expected shOne in shipment list');
assert.equal(s1.orderNumber, 'AU-1002');
assert.equal(s1.orderId, order2.id);
assert.equal(s1.state, 'new');
assert.equal(s1.positions, 1);
assert.equal(s1.totalQuantity, 3);
console.log('PASS: store.shipment.list returned shipments with order info and state');
// 11a. Search by order number
const listSearchOrder = await store.shipment.list(googleAuthId, { search: 'AU-1002', offset: 0, limit: 10 });
assert.equal(listSearchOrder.total, 3);
console.log('PASS: store.shipment.list filtered by orderNumber search');
// 11b. Update shipment state to 'delivered'
const updatedSh = await store.shipment.update(googleAuthId, shOne.id, { state: 'delivered' });
assert.equal(updatedSh.state, 'delivered');
// 11c. Filter by state = 'delivered'
const listDelivered = await store.shipment.list(googleAuthId, { state: 'delivered', offset: 0, limit: 10 });
assert.equal(listDelivered.total, 1);
assert.equal(listDelivered.shipments[0].id, shOne.id);
console.log('PASS: store.shipment.update set state="delivered" and filtered correctly');
// 11d. Rebuild shipment summaries
await store.shipment.rebuildSummaries(googleAuthId);
const listAfterRebuild = await store.shipment.list(googleAuthId, { state: 'delivered', offset: 0, limit: 10 });
assert.equal(listAfterRebuild.total, 1);
console.log('PASS: store.shipment.rebuildSummaries preserved delivered state and rebuilt correctly');
console.log('--- ALL TESTS PASSED SUCCESSFULLY! ---');
} finally {