Files
dashlibstorage/scripts/test-unique-names.js
2026-10-04 19:12:30 +02:00

109 lines
4.5 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 uniqueness guards: attributename / attributevalue / propertyname ---');
const tempDir = mkdtempSync(join(tmpdir(), 'test-unique-names-'));
const provider = new BetterSqlite3Provider({
filename: join(tempDir, 'test.db'),
storageDir: join(tempDir, 'storage'),
});
await provider.initialize();
const store = await new LibDashStore(provider);
try {
const googleAuthId = await store.google_auth.add('unique-tester@example.com');
const company = await store.company.create({
name: 'Unique Corp',
iso639: 'de',
taxrate: 19,
reducedtaxrate: 7,
googleAuthId,
});
await store.google_auth.setSelectedCompany(googleAuthId, company.id);
console.log('1. POST /attributename with a duplicate name must throw a stable conflict error...');
const an1 = await store.attributename.create(googleAuthId, { name: 'Farbe' });
await assert.rejects(
() => store.attributename.create(googleAuthId, { name: 'Farbe' }),
{ message: 'Attribute name already exists' },
);
console.log('2. POST /attributename/{id}/value with a duplicate value...');
const av1 = await store.attributevalue.create(googleAuthId, an1.id, { value: 'Rot' });
await assert.rejects(
() => store.attributevalue.create(googleAuthId, an1.id, { value: 'Rot' }),
{ message: 'Attribute value already exists' },
);
console.log('3. POST /propertyname with a duplicate name...');
const pn1 = await store.propertyname.create(googleAuthId, { name: 'Notiz' });
await assert.rejects(
() => store.propertyname.create(googleAuthId, { name: 'Notiz' }),
{ message: 'Property name already exists' },
);
console.log('4. PATCH rename onto an existing name/value...');
const an2 = await store.attributename.create(googleAuthId, { name: 'Größe' });
await assert.rejects(
() => store.attributename.update(googleAuthId, an2.id, { name: 'Farbe' }),
{ message: 'Attribute name already exists' },
);
const av2 = await store.attributevalue.create(googleAuthId, an1.id, { value: 'Blau' });
await assert.rejects(
() => store.attributevalue.update(googleAuthId, av2.id, { value: 'Rot' }),
{ message: 'Attribute value already exists' },
);
const pn2 = await store.propertyname.create(googleAuthId, { name: 'Länge' });
await assert.rejects(
() => store.propertyname.update(googleAuthId, pn2.id, { name: 'Notiz' }),
{ message: 'Property name already exists' },
);
console.log('5. Nothing was persisted by the rejected writes...');
const anList = await store.attributename.list(googleAuthId);
assert.deepEqual(anList.map((a) => a.name).sort(), ['Farbe', 'Größe']);
const avList = await store.attributevalue.listByAttributeId(googleAuthId, an1.id);
assert.deepEqual(avList.map((v) => v.value).sort(), ['Blau', 'Rot']);
const pnList = await store.propertyname.list(googleAuthId);
assert.deepEqual(pnList.map((p) => p.name).sort(), ['Länge', 'Notiz']);
console.log('6. Renames to a free name still work...');
await store.attributename.update(googleAuthId, an2.id, { name: 'Größe 2' });
await store.propertyname.update(googleAuthId, pn2.id, { name: 'Länge 2' });
await store.attributevalue.update(googleAuthId, av2.id, { value: 'Grün' });
const renamed = await store.attributevalue.listByAttributeId(googleAuthId, an1.id);
assert.ok(renamed.some((v) => v.value === 'Grün'));
console.log('7. Same name in a different company is allowed (uniqueness is company-scoped)...');
const otherId = await store.google_auth.add('other-tester@example.com');
const otherCompany = await store.company.create({
name: 'Other Corp',
iso639: 'de',
taxrate: 19,
reducedtaxrate: 7,
googleAuthId: otherId,
});
await store.google_auth.setSelectedCompany(otherId, otherCompany.id);
const foreign = await store.attributename.create(otherId, { name: 'Farbe' });
assert.ok(foreign.id);
console.log('\nPASS - all uniqueness guards throw stable, greppable errors and persist nothing.');
} finally {
await provider.purge();
rmSync(tempDir, { recursive: true, force: true });
}
}
run().catch((err) => {
console.error('\nFAIL:', err?.message ?? err);
process.exit(1);
});