import test from 'node:test'; import assert from 'node:assert/strict'; import { readProductImages, withProductImages } from '../src/services/library-images.js'; const sample = '/9j/4AAQSkZJRgABAQAAAQABAAD/2Q=='; test('批量图片只匹配完整商品ID,读取时不发送服务凭据', async () => { const result = await readProductImages(['jd:12', 'taobao:12', 'bad'], { base: '/test-images-1', fetchImpl: async (url, options) => { assert.deepEqual(JSON.parse(new URL(url, 'https://example.test').searchParams.get('where')), { productId: { $in: ['jd:12', 'taobao:12'] } }); assert.deepEqual(Object.keys(options.headers).sort(), ['Accept', 'X-Parse-Application-Id']); return { ok: true, json: async () => ({ results: [{ productId: 'jd:12', thumbnailBase64: sample }, { productId: 'jd:13', thumbnailBase64: sample }] }) }; } }); assert.equal(result.size, 1); assert.ok(result.get('jd:12').thumbnail.startsWith('data:image/jpeg;base64,')); assert.equal(result.has('taobao:12'), false); }); test('图片重复或损坏不串图,图片服务失败保留商品资料', async () => { const result = await readProductImages(['jd:15', 'jd:16'], { base: '/test-images-2', fetchImpl: async () => ({ ok: true, json: async () => ({ results: [ { productId: 'jd:15', thumbnailBase64: sample }, { productId: 'jd:15', thumbnailBase64: sample }, { productId: 'jd:16', thumbnailBase64: '' }, ] }) }) }); assert.equal(result.size, 0); const data = { records: [{ record_id: 'jd:17', title: '原商品资料' }] }; const enriched = await withProductImages(data, { op: 'browse', entity: 'products' }, { base: '/test-images-failure', fetchImpl: async () => { throw Error('offline'); } }); assert.deepEqual(enriched.records, data.records); assert.equal(enriched.imagesUnavailable, true); });