122 lines
5.1 KiB
JavaScript
122 lines
5.1 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 Father History & AttributeValue Shapes ---');
|
|
|
|
const tempDir = mkdtempSync(join(tmpdir(), 'test-hist-attr-'));
|
|
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: 'Test Corp',
|
|
iso639: 'de',
|
|
taxrate: 19,
|
|
reducedtaxrate: 7,
|
|
googleAuthId,
|
|
});
|
|
await store.google_auth.setSelectedCompany(googleAuthId, company.id);
|
|
|
|
console.log('1. Testing setFatherBySku history logging...');
|
|
const father = await store.article.createUnderParent({
|
|
googleAuthId,
|
|
parentId: company.rootCategoryId,
|
|
name: 'Father Shirt',
|
|
});
|
|
const child = await store.article.createUnderParent({
|
|
googleAuthId,
|
|
parentId: company.rootCategoryId,
|
|
name: 'Child Shirt Red',
|
|
});
|
|
|
|
// Check history before linking
|
|
let fatherHist = await store.article.getHistoryBySku(googleAuthId, father.sku);
|
|
assert.equal(fatherHist.length, 1);
|
|
assert.match(fatherHist[0].text, /angelegt/);
|
|
|
|
// Link child to father
|
|
await store.article.setFatherBySku(googleAuthId, child.sku, father.sku);
|
|
|
|
// Father history must now contain the conversion entry and the child entry!
|
|
fatherHist = await store.article.getHistoryBySku(googleAuthId, father.sku);
|
|
const hasConversion = fatherHist.some((h) => h.text.includes('Artikel zu Vaterartikel konvertiert'));
|
|
const hasVariantAdded = fatherHist.some((h) => h.text.includes('hinzugefügt'));
|
|
assert.ok(hasConversion, `Father history missing conversion row: ${JSON.stringify(fatherHist)}`);
|
|
assert.ok(hasVariantAdded, `Father history missing variant added row: ${JSON.stringify(fatherHist)}`);
|
|
|
|
// Child history must contain the assignment entry
|
|
const childHist = await store.article.getHistoryBySku(googleAuthId, child.sku);
|
|
const hasAssigned = childHist.some((h) => h.text.includes('als Variante von'));
|
|
assert.ok(hasAssigned, `Child history missing variant assigned row: ${JSON.stringify(childHist)}`);
|
|
|
|
console.log('2. Testing importRows Pass 2 history logging...');
|
|
const importRes = await store.article.importRows({
|
|
googleAuthId,
|
|
rows: [
|
|
{ sku: 'IMP-FATHER', name: 'Imported Father', parentId: company.rootCategoryId },
|
|
{ sku: 'IMP-CHILD', name: 'Imported Child', parentId: company.rootCategoryId, fatherSku: 'IMP-FATHER' },
|
|
],
|
|
});
|
|
assert.equal(importRes.errors.length, 0);
|
|
|
|
const impFatherHist = await store.article.getHistoryBySku(googleAuthId, 'IMP-FATHER');
|
|
const impHasConversion = impFatherHist.some((h) => h.text.includes('Artikel zu Vaterartikel konvertiert'));
|
|
assert.ok(impHasConversion, `Imported father missing conversion row: ${JSON.stringify(impFatherHist)}`);
|
|
|
|
console.log('3. Testing flexible attributevalue request shapes in setByArticleSku...');
|
|
const attrName = await store.attributename.create(googleAuthId, { name: 'Farbe' });
|
|
const val1 = await store.attributevalue.create(googleAuthId, attrName.id, { value: 'Rot' });
|
|
const val2 = await store.attributevalue.create(googleAuthId, attrName.id, { value: 'Blau' });
|
|
|
|
// Test A: Array of IDs [val1.id, val2.id]
|
|
let res = await store.articleattributevalue.setByArticleSku(googleAuthId, child.sku, [val1.id]);
|
|
assert.equal(res.length, 1);
|
|
assert.equal(res[0].attributeValueId, val1.id);
|
|
|
|
// Test B: Array of objects with .id [{ id: val1.id }]
|
|
res = await store.articleattributevalue.setByArticleSku(googleAuthId, child.sku, [{ id: val2.id }]);
|
|
assert.equal(res.length, 1);
|
|
assert.equal(res[0].attributeValueId, val2.id);
|
|
|
|
// Test C: Array of objects with .attributeValueId
|
|
res = await store.articleattributevalue.setByArticleSku(googleAuthId, child.sku, [{ attributeValueId: val1.id }]);
|
|
assert.equal(res.length, 1);
|
|
assert.equal(res[0].attributeValueId, val1.id);
|
|
|
|
// Test D: Array of objects with attributeId and value
|
|
res = await store.articleattributevalue.setByArticleSku(googleAuthId, child.sku, [{ attributeId: attrName.id, value: 'Blau' }]);
|
|
assert.equal(res.length, 1);
|
|
assert.equal(res[0].attributeValueId, val2.id);
|
|
|
|
// Test E: Array of objects with attributeName and value
|
|
res = await store.articleattributevalue.setByArticleSku(googleAuthId, child.sku, [{ attributeName: 'Farbe', value: 'Rot' }]);
|
|
assert.equal(res.length, 1);
|
|
assert.equal(res[0].attributeValueId, val1.id);
|
|
|
|
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);
|
|
});
|