This commit is contained in:
seb
2026-10-24 06:19:37 +02:00
parent 1da82e2eb9
commit 62d0643b99
7 changed files with 365 additions and 0 deletions

View File

@@ -345,4 +345,80 @@ describe('store.category', () => {
);
});
});
describe('generateImage', () => {
it('rejects invalid size', async () => {
const cat = await create('Gen Invalid Size');
await assert.rejects(
() => store.category.generateImage(tenant.googleAuthId, cat.id, { size: 5 }),
{ message: 'Invalid size, must be 2, 3, or 4' },
);
});
it('rejects category without any article images', async () => {
const cat = await create('Gen Empty Cat');
await assert.rejects(
() => store.category.generateImage(tenant.googleAuthId, cat.id),
{ message: 'No images found for category articles' },
);
});
it('falls back to 2x2 when fewer than 7 images are available', async () => {
const cat = await create('Mosaic Parent Cat');
const sub = await create('Mosaic Sub Cat', cat.id);
// Create 2 articles in parent, 2 articles in subcategory with images
for (let i = 0; i < 2; i++) {
const art = await createArticle(store, tenant, { parentId: cat.id, name: `Parent Art ${i}` });
await store.article.addImage(tenant.googleAuthId, art.sku, await makePng({ r: 50 * i, g: 100, b: 150 }));
}
for (let i = 0; i < 2; i++) {
const art = await createArticle(store, tenant, { parentId: sub.id, name: `Sub Art ${i}` });
await store.article.addImage(tenant.googleAuthId, art.sku, await makePng({ r: 150, g: 50 * i, b: 200 }));
}
// 4 images available (< 7), default without size should fall back to 2x2
const res = await store.category.generateImage(tenant.googleAuthId, cat.id);
assert.equal(res.id, cat.id);
assert.equal(res.size, 2);
assert.equal(res.grid, '2x2');
assert.equal(res.articleCount, 4);
assert.ok(res.fullHash);
assert.ok(res.thumbW > 0);
// Category tree exposes the new mosaic image
const treeCat = await findInTree(cat.id);
assert.equal(treeCat.fullHash, res.fullHash);
});
it('uses 3x3 when 7 or more images are available or explicit size=3', async () => {
const cat = await create('Mosaic 3x3 Cat');
for (let i = 0; i < 7; i++) {
const art = await createArticle(store, tenant, { parentId: cat.id, name: `3x3 Art ${i}` });
await store.article.addImage(tenant.googleAuthId, art.sku, await makePng({ r: 30 * i, g: 70, b: 90 }));
}
const res = await store.category.generateImage(tenant.googleAuthId, cat.id);
assert.equal(res.id, cat.id);
assert.equal(res.size, 3);
assert.equal(res.grid, '3x3');
assert.equal(res.articleCount, 7);
assert.ok(res.fullHash);
});
it('supports explicit size parameter (e.g. size=4)', async () => {
const cat = await create('Mosaic 4x4 Cat');
for (let i = 0; i < 4; i++) {
const art = await createArticle(store, tenant, { parentId: cat.id, name: `4x4 Art ${i}` });
await store.article.addImage(tenant.googleAuthId, art.sku, await makePng({ r: 40 * i, g: 120, b: 50 }));
}
const res = await store.category.generateImage(tenant.googleAuthId, cat.id, { size: 4 });
assert.equal(res.id, cat.id);
assert.equal(res.size, 4);
assert.equal(res.grid, '4x4');
assert.ok(res.fullHash);
});
});
});