u
This commit is contained in:
147
scripts/test-unique-slugs.js
Normal file
147
scripts/test-unique-slugs.js
Normal file
@@ -0,0 +1,147 @@
|
||||
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 Unique Slugs Disambiguation ---');
|
||||
|
||||
const tempDir = mkdtempSync(join(tmpdir(), 'test-slugs-'));
|
||||
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('slug-tester@example.com');
|
||||
const company = await store.company.create({
|
||||
name: 'Slug Corp',
|
||||
iso639: 'de',
|
||||
taxrate: 19,
|
||||
reducedtaxrate: 7,
|
||||
googleAuthId,
|
||||
});
|
||||
await store.google_auth.setSelectedCompany(googleAuthId, company.id);
|
||||
|
||||
console.log('1. Testing createUnderParent duplicate slugs...');
|
||||
const art1 = await store.article.createUnderParent({
|
||||
googleAuthId,
|
||||
parentId: company.rootCategoryId,
|
||||
name: 'Root Father Shirt',
|
||||
});
|
||||
const art1Res = await store.article.getBySku(googleAuthId, art1.sku);
|
||||
assert.equal(art1Res.article.slug, 'root-father-shirt', `Expected slug 'root-father-shirt', got '${art1Res.article.slug}'`);
|
||||
|
||||
const art2 = await store.article.createUnderParent({
|
||||
googleAuthId,
|
||||
parentId: company.rootCategoryId,
|
||||
name: 'Root Father Shirt',
|
||||
});
|
||||
const art2Res = await store.article.getBySku(googleAuthId, art2.sku);
|
||||
assert.equal(art2Res.article.slug, 'root-father-shirt-2', `Expected slug 'root-father-shirt-2', got '${art2Res.article.slug}'`);
|
||||
|
||||
const art3 = await store.article.createUnderParent({
|
||||
googleAuthId,
|
||||
parentId: company.rootCategoryId,
|
||||
name: 'Root Father Shirt',
|
||||
});
|
||||
const art3Res = await store.article.getBySku(googleAuthId, art3.sku);
|
||||
assert.equal(art3Res.article.slug, 'root-father-shirt-3', `Expected slug 'root-father-shirt-3', got '${art3Res.article.slug}'`);
|
||||
|
||||
console.log('2. Testing updateBySku self-collision and renaming...');
|
||||
// Update art2 without changing name
|
||||
await store.article.updateBySku(googleAuthId, art2.sku, { unitamount: 2 });
|
||||
let updated2 = await store.article.getBySku(googleAuthId, art2.sku);
|
||||
assert.equal(updated2.article.slug, 'root-father-shirt-2', `Expected unchanged slug, got '${updated2.article.slug}'`);
|
||||
|
||||
// Update art2 with same name
|
||||
await store.article.updateBySku(googleAuthId, art2.sku, { name: 'Root Father Shirt' });
|
||||
updated2 = await store.article.getBySku(googleAuthId, art2.sku);
|
||||
assert.equal(updated2.article.slug, 'root-father-shirt-2', `Expected slug to remain 'root-father-shirt-2', got '${updated2.article.slug}'`);
|
||||
|
||||
// Rename art2 to something else
|
||||
await store.article.updateBySku(googleAuthId, art2.sku, { name: 'Unique Shirt' });
|
||||
updated2 = await store.article.getBySku(googleAuthId, art2.sku);
|
||||
assert.equal(updated2.article.slug, 'unique-shirt', `Expected slug 'unique-shirt', got '${updated2.article.slug}'`);
|
||||
|
||||
// Rename art3 to 'Root Father Shirt' again: should take root-father-shirt-2 now that it is free
|
||||
await store.article.updateBySku(googleAuthId, art3.sku, { name: 'Root Father Shirt' });
|
||||
const updated3 = await store.article.getBySku(googleAuthId, art3.sku);
|
||||
assert.equal(updated3.article.slug, 'root-father-shirt-2', `Expected slug to fill gap 'root-father-shirt-2', got '${updated3.article.slug}'`);
|
||||
|
||||
console.log('3. Testing addChild duplicate slugs...');
|
||||
const attrName = await store.attributename.create(googleAuthId, { name: 'Größe' });
|
||||
await store.article.toggleFather(googleAuthId, art1.sku, {
|
||||
isFather: true,
|
||||
attributeNameId: attrName.id,
|
||||
});
|
||||
const child1 = await store.article.addChild(googleAuthId, art1.sku, {
|
||||
value: 'M',
|
||||
});
|
||||
assert.equal(child1.article.slug, 'root-father-shirt-m', `Expected child slug 'root-father-shirt-m', got '${child1.article.slug}'`);
|
||||
|
||||
// Add another child with name that yields same slug
|
||||
const child2 = await store.article.addChild(googleAuthId, art1.sku, {
|
||||
value: 'L',
|
||||
name: 'Root Father Shirt - M',
|
||||
});
|
||||
assert.equal(child2.article.slug, 'root-father-shirt-m-2', `Expected child slug 'root-father-shirt-m-2', got '${child2.article.slug}'`);
|
||||
|
||||
console.log('4. Testing importRows duplicate slugs in same batch...');
|
||||
const importRes = await store.article.importRows({
|
||||
googleAuthId,
|
||||
rows: [
|
||||
{ sku: 'IMP-1', name: 'Batch Shirt', parentId: company.rootCategoryId },
|
||||
{ sku: 'IMP-2', name: 'Batch Shirt', parentId: company.rootCategoryId },
|
||||
{ sku: 'IMP-3', name: 'Batch Shirt', parentId: company.rootCategoryId },
|
||||
],
|
||||
});
|
||||
assert.equal(importRes.errors.length, 0, `Import had errors: ${JSON.stringify(importRes.errors)}`);
|
||||
assert.equal(importRes.imported.length, 3);
|
||||
|
||||
const imp1 = await store.article.getBySku(googleAuthId, 'IMP-1');
|
||||
const imp2 = await store.article.getBySku(googleAuthId, 'IMP-2');
|
||||
const imp3 = await store.article.getBySku(googleAuthId, 'IMP-3');
|
||||
|
||||
assert.equal(imp1.article.slug, 'batch-shirt');
|
||||
assert.equal(imp2.article.slug, 'batch-shirt-2');
|
||||
assert.equal(imp3.article.slug, 'batch-shirt-3');
|
||||
|
||||
console.log('5. Testing company isolation...');
|
||||
const company2 = await store.company.create({
|
||||
name: 'Other Corp',
|
||||
iso639: 'de',
|
||||
taxrate: 19,
|
||||
reducedtaxrate: 7,
|
||||
googleAuthId,
|
||||
});
|
||||
await store.google_auth.setSelectedCompany(googleAuthId, company2.id);
|
||||
|
||||
const artCompany2 = await store.article.createUnderParent({
|
||||
googleAuthId,
|
||||
parentId: company2.rootCategoryId,
|
||||
name: 'Root Father Shirt',
|
||||
});
|
||||
const artCompany2Detail = await store.article.getBySku(googleAuthId, artCompany2.sku);
|
||||
assert.equal(artCompany2Detail.article.slug, 'root-father-shirt', `Expected 'root-father-shirt' in company 2, got '${artCompany2Detail.article.slug}'`);
|
||||
|
||||
console.log('✅ ALL SLUG TESTS PASSED');
|
||||
} finally {
|
||||
try {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
run().catch((err) => {
|
||||
console.error('Test failed:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user