Files
dashlibstorage/scratch/test-variants.js
2026-10-23 01:38:08 +02:00

165 lines
5.4 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 POS father/child variants and options ---');
const tempDir = mkdtempSync(join(tmpdir(), 'test-variants-'));
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('tester@example.com');
const company = await store.company.create({
name: 'Variant Corp',
iso639: 'de',
taxrate: 19,
reducedtaxrate: 7,
googleAuthId,
});
await store.google_auth.setSelectedCompany(googleAuthId, company.id);
// 1. Create a category
const cat = await store.category.createUnderParent({
googleAuthId,
parentId: company.rootCategoryId,
name: 'Kleidung',
});
// 2. Create terminal and enable category
const terminal = await store.posterminal.upsert(googleAuthId, {
name: 'POS 1',
pairingpin: '1234',
});
await store.posterminal.pair(terminal.id, {
token: 'pos-token-xyz',
clientserial: 'SN-001',
pairedAt: new Date().toISOString(),
});
await store.posterminal.setOn(googleAuthId, terminal.id, cat.id);
// 3. Create attribute name "Farbe"
const attrName = await store.attributename.create(googleAuthId, {
name: 'Farbe',
sort: 1,
exclusive: true,
});
// 4. Create base article and convert to parent
const tShirt = await store.article.createUnderParent({
googleAuthId,
parentId: cat.id,
name: 'T-Shirt Basic',
sku: 'TSHIRT',
});
await store.article.toggleParent(googleAuthId, tShirt.sku, {
isParent: true,
attributeNameId: attrName.id,
});
// 5. Add children "Rot" and "Blau"
console.log('Adding children "Rot" and "Blau"...');
const childRot = await store.article.addChild(googleAuthId, tShirt.sku, {
value: 'Rot',
name: 'T-Shirt Basic - Rot',
});
const childBlau = await store.article.addChild(googleAuthId, tShirt.sku, {
value: 'Blau',
name: 'T-Shirt Basic - Blau',
});
// 6. Query POS products
let prods = await store.posterminal.getProductList({ posterminalId: terminal.id });
let parentProd = prods.find((p) => p.sku === 'TSHIRT');
let rotProd = prods.find((p) => p.sku === childRot.article.sku);
let blauProd = prods.find((p) => p.sku === childBlau.article.sku);
console.log('Parent product:', {
sku: parentProd.sku,
is_parent: parentProd.is_parent,
parent: parentProd.parent,
variants: parentProd.variants,
options: parentProd.options,
});
assert.equal(parentProd.is_parent, '1');
assert.equal(parentProd.parent, '0');
assert.equal(parentProd.variants, '');
assert.deepEqual(parentProd.options, [{ name: 'Farbe', value: 'Rot,Blau' }]);
console.log('Child Rot:', {
sku: rotProd.sku,
is_parent: rotProd.is_parent,
parent: rotProd.parent,
variants: rotProd.variants,
sort: rotProd.sort,
options: rotProd.options,
});
assert.equal(rotProd.is_parent, '0');
assert.equal(rotProd.parent, String(parentProd._id));
assert.equal(rotProd.variants, 'Rot');
assert.equal(rotProd.sort, '0');
assert.equal(rotProd.options, null);
console.log('Child Blau:', {
sku: blauProd.sku,
is_parent: blauProd.is_parent,
parent: blauProd.parent,
variants: blauProd.variants,
sort: blauProd.sort,
options: blauProd.options,
});
assert.equal(blauProd.is_parent, '0');
assert.equal(blauProd.parent, String(parentProd._id));
assert.equal(blauProd.variants, 'Blau');
assert.equal(blauProd.sort, '1');
assert.equal(blauProd.options, null);
// 7. Add third child "Grün" and verify options list update
console.log('Adding third child "Grün"...');
const childGruen = await store.article.addChild(googleAuthId, tShirt.sku, {
value: 'Grün',
name: 'T-Shirt Basic - Grün',
});
prods = await store.posterminal.getProductList({ posterminalId: terminal.id });
parentProd = prods.find((p) => p.sku === 'TSHIRT');
assert.deepEqual(parentProd.options, [{ name: 'Farbe', value: 'Rot,Blau,Grün' }]);
console.log('Updated parent options:', parentProd.options);
// 8. Remove child "Blau" and verify options list update
console.log('Removing child "Blau"...');
await store.article.removeChild(googleAuthId, tShirt.sku, childBlau.article.sku, { deleteArticle: true });
prods = await store.posterminal.getProductList({ posterminalId: terminal.id });
parentProd = prods.find((p) => p.sku === 'TSHIRT');
assert.deepEqual(parentProd.options, [{ name: 'Farbe', value: 'Rot,Grün' }]);
console.log('Parent options after removal:', parentProd.options);
console.log('✅ ALL FATHER/CHILD VARIANT TESTS PASSED');
} finally {
try {
rmSync(tempDir, { recursive: true, force: true });
} catch {}
}
}
run().catch((err) => {
console.error('Test failed:', err);
process.exit(1);
});