feat(ai-image): AI生图开发
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import test from 'node:test';
|
||||
import { collectCurrentProduct } from '../lib/source-parsers.ts';
|
||||
|
||||
function fixtureSkuModel(html: string) {
|
||||
const marker = '"skuModel": ';
|
||||
const start = html.indexOf(marker);
|
||||
assert.ok(start >= 0);
|
||||
const source = html.slice(start + marker.length);
|
||||
let depth = 0;
|
||||
let quoted = false;
|
||||
let escaped = false;
|
||||
for (let index = 0; index < source.length; index += 1) {
|
||||
const char = source[index];
|
||||
if (quoted) {
|
||||
if (escaped) escaped = false;
|
||||
else if (char === '\\') escaped = true;
|
||||
else if (char === '"') quoted = false;
|
||||
} else if (char === '"') quoted = true;
|
||||
else if (char === '{') depth += 1;
|
||||
else if (char === '}' && --depth === 0) return JSON.parse(source.slice(0, index + 1));
|
||||
}
|
||||
throw new Error('1688 样本缺少完整 skuModel');
|
||||
}
|
||||
|
||||
test('1688 真实样本按 skuInfoMap 的 price 和规格图片生成逐 SKU 数据', async () => {
|
||||
const html = await readFile(new URL('../../../packages/reference/1688.html', import.meta.url), 'utf8');
|
||||
const skuModel = fixtureSkuModel(html);
|
||||
const root = {
|
||||
scripts: [],
|
||||
querySelector: () => null,
|
||||
querySelectorAll: () => [],
|
||||
} as unknown as Document;
|
||||
const payload = await collectCurrentProduct(
|
||||
root,
|
||||
'https://detail.1688.com/offer/123456789.html',
|
||||
undefined,
|
||||
async () => ({ skuModel }),
|
||||
);
|
||||
assert.equal(payload.variants.length, Object.keys(skuModel.skuInfoMap).length);
|
||||
for (const [name, row] of Object.entries<any>(skuModel.skuInfoMap)) {
|
||||
const variant = payload.variants.find((item) => item.sourceSku === String(row.skuId));
|
||||
assert.ok(variant, `缺少 ${name}`);
|
||||
assert.equal(variant.displayName, name);
|
||||
assert.equal(variant.sourcePrice?.amount, row.price);
|
||||
const image = skuModel.skuProps[0].value.find((item: any) => item.name === name)?.imageUrl;
|
||||
assert.ok(payload.assets.some((asset) =>
|
||||
asset.variantSourceKey === variant.sourceVariantKey && asset.url === image
|
||||
), `${name} 未关联规格图片`);
|
||||
}
|
||||
const scriptRoot = {
|
||||
...root,
|
||||
scripts: [{ type: 'text/javascript', textContent: html }],
|
||||
} as unknown as Document;
|
||||
const withoutBridge = await collectCurrentProduct(
|
||||
scriptRoot,
|
||||
'https://detail.1688.com/offer/123456789.html',
|
||||
);
|
||||
assert.equal(withoutBridge.variants.length, payload.variants.length);
|
||||
assert.equal(withoutBridge.assets.filter((asset) => asset.variantSourceKey).length, payload.variants.length);
|
||||
});
|
||||
Reference in New Issue
Block a user