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 Parent 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 setParentBySku history logging...'); const parent = await store.article.createUnderParent({ googleAuthId, parentId: company.rootCategoryId, name: 'Parent Shirt', }); const child = await store.article.createUnderParent({ googleAuthId, parentId: company.rootCategoryId, name: 'Child Shirt Red', }); // Check history before linking let parentHist = await store.article.getHistoryBySku(googleAuthId, parent.sku); assert.equal(parentHist.length, 1); assert.match(parentHist[0].text, /angelegt/); // Link child to parent await store.article.setParentBySku(googleAuthId, child.sku, parent.sku); // Parent history must now contain the conversion entry and the child entry! parentHist = await store.article.getHistoryBySku(googleAuthId, parent.sku); const hasConversion = parentHist.some((h) => h.text.includes('Artikel zu Vaterartikel konvertiert')); const hasVariantAdded = parentHist.some((h) => h.text.includes('hinzugefügt')); assert.ok(hasConversion, `Parent history missing conversion row: ${JSON.stringify(parentHist)}`); assert.ok(hasVariantAdded, `Parent history missing variant added row: ${JSON.stringify(parentHist)}`); // 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-PARENT', name: 'Imported Parent', parentId: company.rootCategoryId }, { sku: 'IMP-CHILD', name: 'Imported Child', parentId: company.rootCategoryId, parentSku: 'IMP-PARENT' }, ], }); assert.equal(importRes.errors.length, 0); const impParentHist = await store.article.getHistoryBySku(googleAuthId, 'IMP-PARENT'); const impHasConversion = impParentHist.some((h) => h.text.includes('Artikel zu Vaterartikel konvertiert')); assert.ok(impHasConversion, `Imported parent missing conversion row: ${JSON.stringify(impParentHist)}`); 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); });