This commit is contained in:
seb
2026-10-17 05:10:28 +02:00
parent a194cf6b8b
commit f8cc01baf0
25 changed files with 1034 additions and 54 deletions

142
scripts/test-carrier.js Normal file
View File

@@ -0,0 +1,142 @@
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 Carrier Store API ---');
const tempDir = mkdtempSync(join(tmpdir(), 'test-carrier-'));
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('carrier-test@example.com');
const company = await store.company.create({
name: 'Carrier Test Corp',
iso639: 'de',
taxrate: 19,
reducedtaxrate: 7,
googleAuthId,
});
await store.google_auth.setSelectedCompany(googleAuthId, company.id);
// 1. Create carrier with defaults
const c1 = await store.carrier.create(googleAuthId, { name: 'DHL' });
assert.equal(c1.name, 'DHL');
assert.equal(c1.sort, 0);
assert.equal(c1.hasPackages, false);
assert.equal(c1.isReverse, false);
console.log('PASS: Carrier 1 created with defaults');
// 2. Create carrier with full flags
const c2 = await store.carrier.create(googleAuthId, {
name: 'UPS',
sort: 5,
hasPackages: true,
isReverse: true,
});
assert.equal(c2.name, 'UPS');
assert.equal(c2.sort, 5);
assert.equal(c2.hasPackages, true);
assert.equal(c2.isReverse, true);
console.log('PASS: Carrier 2 created with custom flags');
// 3. Auto-increment sort
const c3 = await store.carrier.create(googleAuthId, {
name: 'DPD',
hasPackages: true,
});
assert.equal(c3.name, 'DPD');
assert.equal(c3.sort, 6);
assert.equal(c3.hasPackages, true);
assert.equal(c3.isReverse, false);
console.log('PASS: Carrier 3 auto-incremented sort');
// 4. Duplicate name validation
await assert.rejects(
() => store.carrier.create(googleAuthId, { name: 'DHL' }),
{ message: 'Carrier already exists' },
);
console.log('PASS: Duplicate carrier rejected');
// 5. Empty name validation
await assert.rejects(
() => store.carrier.create(googleAuthId, { name: ' ' }),
{ message: 'Name required' },
);
console.log('PASS: Empty carrier name rejected');
// 6. getById
const fetched = await store.carrier.getById(googleAuthId, c2.id);
assert.deepEqual(fetched, c2);
const notFound = await store.carrier.getById(googleAuthId, 9999);
assert.equal(notFound, undefined);
console.log('PASS: getById fetched carrier correctly');
// 7. list
const list = await store.carrier.list(googleAuthId);
assert.equal(list.length, 3);
assert.equal(list[0].name, 'DHL');
assert.equal(list[1].name, 'UPS');
assert.equal(list[2].name, 'DPD');
console.log('PASS: list returned sorted carriers');
// 8. update
const updated = await store.carrier.update(googleAuthId, c1.id, {
name: 'DHL Express',
hasPackages: true,
isReverse: false,
});
assert.equal(updated.name, 'DHL Express');
assert.equal(updated.hasPackages, true);
console.log('PASS: update modified carrier');
// 9. reorder
const ordered = await store.carrier.order(googleAuthId, {
carrierIds: [c3.id, c2.id, c1.id],
});
assert.deepEqual(ordered.carrierIds, [c3.id, c2.id, c1.id]);
const listAfterOrder = await store.carrier.list(googleAuthId);
assert.equal(listAfterOrder[0].id, c3.id);
assert.equal(listAfterOrder[0].sort, 0);
assert.equal(listAfterOrder[1].id, c2.id);
assert.equal(listAfterOrder[1].sort, 1);
assert.equal(listAfterOrder[2].id, c1.id);
assert.equal(listAfterOrder[2].sort, 2);
console.log('PASS: order successfully reordered carriers');
// 10. delete
await store.carrier.delete(googleAuthId, c3.id);
const listAfterDelete = await store.carrier.list(googleAuthId);
assert.equal(listAfterDelete.length, 2);
console.log('PASS: delete removed carrier');
await assert.rejects(
() => store.carrier.delete(googleAuthId, c3.id),
{ message: 'Carrier not found' },
);
console.log('PASS: delete non-existent carrier rejected');
console.log('--- ALL CARRIER TESTS PASSED! ---');
} finally {
try {
rmSync(tempDir, { recursive: true, force: true });
} catch {}
}
}
run().catch((err) => {
console.error(err);
process.exit(1);
});

View File

@@ -54,6 +54,10 @@ async function run() {
name: 'Max Mustermann',
});
const carrier = await store.carrier.create(googleAuthId, {
name: 'Standard Carrier',
});
// 2. Setup tracked article and untracked article
const trackedArt = await store.article.createUnderParent({
googleAuthId,
@@ -117,6 +121,7 @@ async function run() {
});
const shippingOpen = await store.shipping.create(googleAuthId, {
manifestId: shipment.id,
carrierId: carrier.id,
type: 'parcel',
state: 'open',
});
@@ -219,6 +224,7 @@ async function run() {
async () => {
await store.shipping.create(googleAuthId, {
manifestId: order.id,
carrierId: carrier.id,
type: 'express',
state: 'shipped',
});
@@ -251,6 +257,7 @@ async function run() {
const directShipped = await store.shipping.create(googleAuthId, {
manifestId: shipment2.id,
carrierId: carrier.id,
type: 'parcel',
state: 'shipped',
});

View File

@@ -150,29 +150,47 @@ async function run() {
}
assert.ok(exhaustedErrorThrown, 'Expected exhausted error to be thrown');
// 5. Add packages to Shipment 1 with tracking codes
// 5. Add packages to Shipment 1 with tracking codes and carrier
const carrier = await store.carrier.create(googleAuthId, {
name: 'DHL',
hasPackages: true,
isReverse: false,
});
assert.equal(carrier.name, 'DHL');
assert.equal(carrier.hasPackages, true);
assert.equal(carrier.isReverse, false);
const pkg1 = await store.shipping.create(googleAuthId, {
manifestId: shipment1.id,
carrierId: carrier.id,
type: 'parcel',
state: 'draft',
trackingCode: 'TRACK-111',
carrier: 'DHL',
});
assert.equal(pkg1.trackingCode, 'TRACK-111');
assert.equal(pkg1.carrierId, carrier.id);
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,
carrierId: carrier.id,
type: 'parcel',
state: 'draft',
trackingCode: 'TRACK-222',
carrier: 'DHL',
});
assert.equal(pkg2.trackingCode, 'TRACK-222');
assert.equal(pkg2.carrierId, carrier.id);
console.log('PASS: Package 2 created for Shipment 1:', pkg2.number, pkg2.trackingCode);
// Verify carrier in use prevents deletion
await assert.rejects(
() => store.carrier.delete(googleAuthId, carrier.id),
{ message: 'Carrier in use' },
);
console.log('PASS: Carrier delete rejected while referenced in shipping');
// Check listByOrderId
const orderShipments = await store.shipment.listByOrderId(googleAuthId, order.id);
assert.equal(orderShipments.length, 2);