Files
dashlibstorage/scripts/test-attributevalue-pic.js
2026-10-28 01:46:48 +01:00

109 lines
3.8 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 { optimizeImage } from 'wasm-image-optimization';
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 svg = `<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><rect width="100" height="100" fill="rgb(25,25,112)"/></svg>`;
const encoded = await optimizeImage({ image: svg, format: 'avif' });
const imageBuffer = Buffer.from(encoded.data);
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);
});