u
This commit is contained in:
171
scratch/test-category-subcategories.js
Normal file
171
scratch/test-category-subcategories.js
Normal file
@@ -0,0 +1,171 @@
|
||||
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 test() {
|
||||
console.log('--- Testing setOn and setOff with subcategories ---');
|
||||
|
||||
const tempDir = mkdtempSync(join(tmpdir(), 'test-posterminal-subcats-'));
|
||||
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('subcat-tester@example.com');
|
||||
const company = await store.company.create({
|
||||
name: 'SubCat Corp',
|
||||
iso639: 'de',
|
||||
taxrate: 19,
|
||||
reducedtaxrate: 7,
|
||||
googleAuthId,
|
||||
});
|
||||
await store.google_auth.setSelectedCompany(googleAuthId, company.id);
|
||||
|
||||
const terminal = await store.posterminal.upsert(googleAuthId, {
|
||||
name: 'POS 1',
|
||||
pairingpin: '1234',
|
||||
});
|
||||
await store.posterminal.pair(terminal.id, {
|
||||
token: 'pos-token-subcat',
|
||||
clientserial: 'SN-001',
|
||||
pairedAt: new Date().toISOString(),
|
||||
});
|
||||
|
||||
// Create Category Tree:
|
||||
// Root -> Cat 4 -> SubCat 41 -> SubCat 411
|
||||
// Root -> Cat 5
|
||||
const cat4 = await store.category.createUnderParent({
|
||||
googleAuthId,
|
||||
parentId: company.rootCategoryId,
|
||||
name: 'Getränke',
|
||||
});
|
||||
const subCat41 = await store.category.createUnderParent({
|
||||
googleAuthId,
|
||||
parentId: cat4.id,
|
||||
name: 'Alkoholfrei',
|
||||
});
|
||||
const subCat411 = await store.category.createUnderParent({
|
||||
googleAuthId,
|
||||
parentId: subCat41.id,
|
||||
name: 'Cola',
|
||||
});
|
||||
const cat5 = await store.category.createUnderParent({
|
||||
googleAuthId,
|
||||
parentId: company.rootCategoryId,
|
||||
name: 'Snacks',
|
||||
});
|
||||
|
||||
// Create articles in various categories
|
||||
const artCola = await store.article.createUnderParent({
|
||||
googleAuthId,
|
||||
parentId: subCat411.id,
|
||||
name: 'Cola 0.5L',
|
||||
});
|
||||
|
||||
const artSnack = await store.article.createUnderParent({
|
||||
googleAuthId,
|
||||
parentId: cat5.id,
|
||||
name: 'Chips',
|
||||
});
|
||||
|
||||
// Set old timestamps on categories and articles to test updated_at touch
|
||||
provider.db.prepare("UPDATE category SET updated_at = '2020-01-01T00:00:00.000Z'").run();
|
||||
provider.db.prepare("UPDATE article SET updated_at = '2020-01-01T00:00:00.000Z'").run();
|
||||
|
||||
console.log('1. Testing setOn on parent category Cat 4 (should enable Cat 4, SubCat 41, and SubCat 411)...');
|
||||
const onResult = await store.posterminal.setOn(googleAuthId, terminal.id, cat4.id);
|
||||
console.log('Category IDs after setOn(cat4):', onResult.categoryIds);
|
||||
|
||||
assert(onResult.categoryIds.includes(cat4.id), 'Should include cat4');
|
||||
assert(onResult.categoryIds.includes(subCat41.id), 'Should include subCat41');
|
||||
assert(onResult.categoryIds.includes(subCat411.id), 'Should include subCat411');
|
||||
assert(!onResult.categoryIds.includes(cat5.id), 'Should NOT include cat5');
|
||||
|
||||
// Check that categories in the subtree were touched
|
||||
const catsAfterOn = provider.db.prepare('SELECT id, updated_at FROM category WHERE id IN (?, ?, ?)').all(cat4.id, subCat41.id, subCat411.id);
|
||||
for (const c of catsAfterOn) {
|
||||
assert.notEqual(c.updated_at, '2020-01-01T00:00:00.000Z', `Category ${c.id} updated_at should be touched on setOn`);
|
||||
}
|
||||
|
||||
// Check that articles in the subtree were touched
|
||||
const artAfterOn = provider.db.prepare('SELECT id, updated_at FROM article WHERE id = ?').get(artCola.id);
|
||||
assert.notEqual(artAfterOn.updated_at, '2020-01-01T00:00:00.000Z', 'Article updated_at should be touched on setOn');
|
||||
|
||||
// Cat 5 article should NOT be touched
|
||||
const artSnackAfterOn = provider.db.prepare('SELECT id, updated_at FROM article WHERE id = ?').get(artSnack.id);
|
||||
assert.equal(artSnackAfterOn.updated_at, '2020-01-01T00:00:00.000Z', 'Unrelated article updated_at should not be touched');
|
||||
|
||||
console.log('2. Also enabling Cat 5...');
|
||||
await store.posterminal.setOn(googleAuthId, terminal.id, cat5.id);
|
||||
|
||||
// Reset timestamps to test setOff touch
|
||||
provider.db.prepare("UPDATE category SET updated_at = '2020-01-01T00:00:00.000Z'").run();
|
||||
provider.db.prepare("UPDATE article SET updated_at = '2020-01-01T00:00:00.000Z'").run();
|
||||
|
||||
console.log('3. Testing setOff on parent category Cat 4 (should disable Cat 4, SubCat 41, and SubCat 411)...');
|
||||
const offResult = await store.posterminal.setOff(googleAuthId, terminal.id, cat4.id);
|
||||
console.log('Category IDs after setOff(cat4):', offResult.categoryIds);
|
||||
|
||||
assert(!offResult.categoryIds.includes(cat4.id), 'Should NOT include cat4');
|
||||
assert(!offResult.categoryIds.includes(subCat41.id), 'Should NOT include subCat41');
|
||||
assert(!offResult.categoryIds.includes(subCat411.id), 'Should NOT include subCat411');
|
||||
assert(offResult.categoryIds.includes(cat5.id), 'Should still include cat5');
|
||||
|
||||
// Check that categories in the subtree were touched on setOff
|
||||
const catsAfterOff = provider.db.prepare('SELECT id, updated_at FROM category WHERE id IN (?, ?, ?)').all(cat4.id, subCat41.id, subCat411.id);
|
||||
for (const c of catsAfterOff) {
|
||||
assert.notEqual(c.updated_at, '2020-01-01T00:00:00.000Z', `Category ${c.id} updated_at should be touched on setOff`);
|
||||
}
|
||||
|
||||
// Check that article in the subtree was touched on setOff
|
||||
const artAfterOff = provider.db.prepare('SELECT id, updated_at FROM article WHERE id = ?').get(artCola.id);
|
||||
assert.notEqual(artAfterOff.updated_at, '2020-01-01T00:00:00.000Z', 'Article updated_at should be touched on setOff');
|
||||
|
||||
// Verify obituaries generated
|
||||
const obituariesAfter = provider.db
|
||||
.prepare('SELECT entity_type, entity_id FROM posterminalobituary WHERE posterminal_id = ? ORDER BY id ASC')
|
||||
.all(terminal.id);
|
||||
console.log('Obituaries after setOff:', obituariesAfter);
|
||||
|
||||
const catObituaryIds = obituariesAfter.filter(o => o.entity_type === 'category').map(o => o.entity_id);
|
||||
assert(catObituaryIds.includes(cat4.id), 'Should have obituary for cat4');
|
||||
assert(catObituaryIds.includes(subCat41.id), 'Should have obituary for subCat41');
|
||||
assert(catObituaryIds.includes(subCat411.id), 'Should have obituary for subCat411');
|
||||
|
||||
const artObituaryIds = obituariesAfter.filter(o => o.entity_type === 'article').map(o => o.entity_id);
|
||||
assert(artObituaryIds.includes(artCola.id), 'Should have obituary for artCola');
|
||||
assert(!artObituaryIds.includes(artSnack.id), 'Should NOT have obituary for artSnack');
|
||||
|
||||
console.log('4. Testing setOn on an intermediate subcategory (SubCat 41)...');
|
||||
const subOnResult = await store.posterminal.setOn(googleAuthId, terminal.id, subCat41.id);
|
||||
console.log('Category IDs after setOn(subCat41):', subOnResult.categoryIds);
|
||||
assert(!subOnResult.categoryIds.includes(cat4.id), 'Parent cat4 should not be enabled when enabling subCat41');
|
||||
assert(subOnResult.categoryIds.includes(subCat41.id), 'subCat41 should be enabled');
|
||||
assert(subOnResult.categoryIds.includes(subCat411.id), 'Child subCat411 should be enabled');
|
||||
|
||||
console.log('✅ ALL SUBCATEGORY SET_ON / SET_OFF TESTS PASSED');
|
||||
} finally {
|
||||
try {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
test()
|
||||
.then(() => {
|
||||
process.exit(0);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('Test failed:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user