This commit is contained in:
seb
2026-10-11 17:26:11 +02:00
parent 7ffb21aabb
commit 7f4092971d
17 changed files with 3076 additions and 2616 deletions

View File

@@ -0,0 +1,115 @@
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import sharp from 'sharp';
import LibDashStore from '../src/index.js';
import { BetterSqlite3Provider } from '../src/providers/sqlite3/index.js';
async function run() {
console.log('--- Testing attributevaluepic & attributevalue image operations ---');
const tempDir = mkdtempSync(join(tmpdir(), 'test-attrval-pic-'));
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);
const attr = await store.attributename.create(googleAuthId, {
name: 'Color',
exclusive: true,
});
const val = await store.attributevalue.create(googleAuthId, attr.id, {
value: 'Midnight Blue',
});
assert.ok(val.id > 0);
// Create a 100x100 test image
const imageBuffer = await sharp({
create: {
width: 100,
height: 100,
channels: 4,
background: { r: 25, g: 25, b: 112, alpha: 1 },
},
})
.avif()
.toBuffer();
console.log('1. Adding image to attribute value...');
const addResult = await store.attributevalue.addImage(googleAuthId, val.id, imageBuffer);
assert.equal(addResult.id, val.id);
assert.ok(addResult.fullHash);
assert.ok(addResult.thumbW > 0);
assert.ok(addResult.thumbH > 0);
assert.ok(addResult.thumbHash);
assert.equal(addResult.pics.length, 1);
assert.equal(addResult.pics[0].fullHash, addResult.fullHash);
// Verify row in attributevaluepic
const rows = await provider.query('SELECT * FROM attributevaluepic WHERE attributevalue_id = @id', {
id: val.id,
});
assert.equal(rows.rows.length, 1);
assert.equal(rows.rows[0].attributevalue_id, val.id);
// Add again to test replacement
console.log('2. Replacing image on attribute value...');
const replaceResult = await store.attributevalue.addImage(googleAuthId, val.id, imageBuffer);
assert.equal(replaceResult.id, val.id);
const rowsAfterReplace = await provider.query('SELECT * FROM attributevaluepic WHERE attributevalue_id = @id', {
id: val.id,
});
assert.equal(rowsAfterReplace.rows.length, 1);
// Delete image
console.log('3. Deleting image from attribute value...');
const deleteResult = await store.attributevalue.deleteImage(googleAuthId, val.id);
assert.equal(deleteResult.id, val.id);
assert.equal(deleteResult.thumbW, null);
assert.equal(deleteResult.thumbH, null);
assert.equal(deleteResult.thumbHash, null);
assert.deepEqual(deleteResult.pics, []);
const rowsAfterDelete = await provider.query('SELECT * FROM attributevaluepic WHERE attributevalue_id = @id', {
id: val.id,
});
assert.equal(rowsAfterDelete.rows.length, 0);
// Deleting again should fail with Image not found
await assert.rejects(
async () => {
await store.attributevalue.deleteImage(googleAuthId, val.id);
},
/Image not found/
);
console.log('All attributevaluepic tests passed successfully!');
} finally {
try {
rmSync(tempDir, { recursive: true, force: true });
} catch {}
}
}
run().catch((err) => {
console.error('Test failed:', err);
process.exit(1);
});