feat: sync shop icons and continue imports in v1.1.4
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const Module = require('node:module');
|
||||
const path = require('node:path');
|
||||
const test = require('node:test');
|
||||
|
||||
const shopIconPath = path.resolve(__dirname, '../src/shop-icon.js');
|
||||
|
||||
function pngBuffer() {
|
||||
return Buffer.from([137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 0]);
|
||||
}
|
||||
|
||||
function loadShopIcon(api) {
|
||||
const originalLoad = Module._load;
|
||||
Module._load = function(request, parent, isMain) {
|
||||
if (request === '@orbito/api-lib') return api;
|
||||
return originalLoad.call(this, request, parent, isMain);
|
||||
};
|
||||
delete require.cache[shopIconPath];
|
||||
try {
|
||||
return require(shopIconPath);
|
||||
} finally {
|
||||
Module._load = originalLoad;
|
||||
}
|
||||
}
|
||||
|
||||
test('discovers, downloads and persists a favicon once before registering the source', async () => {
|
||||
const calls = [];
|
||||
const config = new Map();
|
||||
const registered = [];
|
||||
const page = Buffer.from('<html><head><link rel="shortcut icon" href="img/store-icon.png"></head></html>');
|
||||
const shopIcon = loadShopIcon({
|
||||
Config: {
|
||||
get: async (key, fallback) => config.has(key) ? config.get(key) : fallback,
|
||||
set: async (key, value) => config.set(key, value),
|
||||
},
|
||||
Core: {
|
||||
fetch: async url => {
|
||||
calls.push(url);
|
||||
if (url === 'https://shop.test/b2b/') {
|
||||
return { status: 200, data: page, headers: { 'content-type': 'text/html' } };
|
||||
}
|
||||
if (url === 'https://shop.test/b2b/img/store-icon.png') {
|
||||
return { status: 200, data: pngBuffer(), headers: { 'content-type': 'image/png' } };
|
||||
}
|
||||
throw new Error(`Unexpected URL: ${url}`);
|
||||
},
|
||||
callModule: async (...args) => registered.push(args),
|
||||
},
|
||||
Log: { warn() {} },
|
||||
});
|
||||
|
||||
const first = await shopIcon.ensureShopIcon({
|
||||
apiUrl: 'https://shop.test/b2b/api/',
|
||||
shopName: 'Sklep B2B',
|
||||
force: true,
|
||||
});
|
||||
const second = await shopIcon.ensureShopIcon({
|
||||
apiUrl: 'https://shop.test/b2b/api/',
|
||||
shopName: 'Sklep B2B',
|
||||
force: false,
|
||||
});
|
||||
|
||||
assert.deepEqual(calls, [
|
||||
'https://shop.test/b2b/',
|
||||
'https://shop.test/b2b/img/store-icon.png',
|
||||
]);
|
||||
assert.equal(first.cacheHit, false);
|
||||
assert.equal(second.cacheHit, true);
|
||||
assert.equal(first.mime, 'image/png');
|
||||
assert.match(first.dataUrl, /^data:image\/png;base64,/);
|
||||
assert.equal(config.get('runtime.shopIcon').sourceUrl, 'https://shop.test/b2b/img/store-icon.png');
|
||||
assert.deepEqual(registered[0].slice(0, 2), ['core/orders', 'source.register']);
|
||||
assert.equal(registered[0][2].name, 'Sklep B2B');
|
||||
assert.equal(registered[0][2].iconDataUrl, first.dataUrl);
|
||||
assert.equal(registered.length, 2, 'cache hits still re-register metadata after a module restart');
|
||||
});
|
||||
|
||||
test('a failed refresh retains the last icon for the same shop', async () => {
|
||||
const oldIcon = `data:image/png;base64,${pngBuffer().toString('base64')}`;
|
||||
let stored = {
|
||||
shopUrl: 'https://shop.test/',
|
||||
dataUrl: oldIcon,
|
||||
mime: 'image/png',
|
||||
sourceUrl: 'https://shop.test/old.png',
|
||||
fetchedAt: '2026-01-01T00:00:00.000Z',
|
||||
};
|
||||
const shopIcon = loadShopIcon({
|
||||
Config: {
|
||||
get: async () => stored,
|
||||
set: async (key, value) => { stored = value; },
|
||||
},
|
||||
Core: {
|
||||
fetch: async () => ({ status: 503, data: Buffer.alloc(0), headers: {} }),
|
||||
callModule: async () => ({}),
|
||||
},
|
||||
Log: { warn() {} },
|
||||
});
|
||||
|
||||
const result = await shopIcon.ensureShopIcon({
|
||||
apiUrl: 'https://shop.test/api',
|
||||
shopName: 'Sklep',
|
||||
force: true,
|
||||
});
|
||||
|
||||
assert.equal(result.dataUrl, oldIcon);
|
||||
assert.equal(result.sourceUrl, 'https://shop.test/old.png');
|
||||
assert.equal(stored.dataUrl, oldIcon);
|
||||
});
|
||||
|
||||
test('favicon discovery rejects cross-host candidates and recognizes common binary formats', () => {
|
||||
const shopIcon = loadShopIcon({ Config: {}, Core: {}, Log: { warn() {} } });
|
||||
const candidates = shopIcon.faviconCandidates(
|
||||
'<link rel="icon" href="https://cdn.test/icon.png"><link rel="apple-touch-icon" href="/touch.png">',
|
||||
'https://shop.test/catalog/'
|
||||
);
|
||||
|
||||
assert.deepEqual(candidates, [
|
||||
'https://cdn.test/icon.png',
|
||||
'https://shop.test/touch.png',
|
||||
'https://shop.test/catalog/favicon.ico',
|
||||
]);
|
||||
assert.equal(shopIcon.detectIconMime(pngBuffer()), 'image/png');
|
||||
assert.equal(shopIcon.detectIconMime(Buffer.from([0, 0, 1, 0])), 'image/vnd.microsoft.icon');
|
||||
assert.equal(shopIcon.storefrontUrl('https://shop.test/catalog/api?key=secret'), 'https://shop.test/catalog/');
|
||||
});
|
||||
Reference in New Issue
Block a user