96 lines
3.4 KiB
JavaScript
96 lines
3.4 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { test } from 'node:test';
|
|
import { fetchOzonDictionaryBatch, fetchOzonDictionaryValues } from '../src/ozon-client.ts';
|
|
|
|
const input = {
|
|
attributeId: '123',
|
|
categoryId: '456',
|
|
typeId: '789',
|
|
};
|
|
|
|
test('属性字典按 Ozon 游标读取多页', async () => {
|
|
const previousFetch = globalThis.fetch;
|
|
const requests = [];
|
|
globalThis.fetch = async (url, options) => {
|
|
requests.push({ url, body: JSON.parse(options.body) });
|
|
const data = requests.length === 1
|
|
? { result: [{ id: 1, value: '中国' }], has_next: true }
|
|
: { result: [{ id: 2, value: '法国' }], has_next: false };
|
|
return new Response(JSON.stringify(data), { status: 200 });
|
|
};
|
|
try {
|
|
const values = await fetchOzonDictionaryValues('client', 'key', input);
|
|
assert.deepEqual(values.map((value) => value.value), ['中国', '法国']);
|
|
assert.equal(requests.length, 2);
|
|
assert.equal(requests[0].body.last_value_id, 0);
|
|
assert.equal(requests[1].body.last_value_id, 1);
|
|
assert.ok(requests.every((request) => request.url.endsWith('/attribute/values')));
|
|
} finally {
|
|
globalThis.fetch = previousFetch;
|
|
}
|
|
});
|
|
|
|
test('关键词使用 Ozon 字典搜索接口', async () => {
|
|
const previousFetch = globalThis.fetch;
|
|
const requests = [];
|
|
globalThis.fetch = async (url, options) => {
|
|
requests.push({ url, body: JSON.parse(options.body) });
|
|
return new Response(JSON.stringify({ result: [{ id: 3, value: '儿童' }] }), {
|
|
status: 200,
|
|
});
|
|
};
|
|
try {
|
|
const values = await fetchOzonDictionaryValues('client', 'key', {
|
|
...input,
|
|
query: '儿童',
|
|
});
|
|
assert.equal(values.length, 1);
|
|
assert.equal(requests.length, 1);
|
|
assert.ok(requests[0].url.endsWith('/attribute/values/search'));
|
|
assert.equal(requests[0].body.value, '儿童');
|
|
} finally {
|
|
globalThis.fetch = previousFetch;
|
|
}
|
|
});
|
|
|
|
test('Ozon 返回重复游标时停止翻页', async () => {
|
|
const previousFetch = globalThis.fetch;
|
|
let calls = 0;
|
|
globalThis.fetch = async () => {
|
|
calls += 1;
|
|
return new Response(JSON.stringify({
|
|
result: [{ id: 1, value: '中国' }],
|
|
has_next: true,
|
|
}), { status: 200 });
|
|
};
|
|
try {
|
|
const values = await fetchOzonDictionaryValues('client', 'key', input);
|
|
assert.equal(values.length, 1);
|
|
assert.equal(calls, 2);
|
|
} finally {
|
|
globalThis.fetch = previousFetch;
|
|
}
|
|
});
|
|
|
|
test('平台字典批量同步会报告分页是否完整', async () => {
|
|
const previousFetch = globalThis.fetch;
|
|
const requests = [];
|
|
globalThis.fetch = async (_url, options) => {
|
|
requests.push(JSON.parse(options.body));
|
|
const page = requests.length;
|
|
return new Response(JSON.stringify({
|
|
result: [{ id: page, value: `候选值 ${page}` }],
|
|
has_next: page < 2,
|
|
}), { status: 200 });
|
|
};
|
|
try {
|
|
const batch = await fetchOzonDictionaryBatch('client', 'key', input);
|
|
assert.equal(batch.isComplete, true);
|
|
assert.deepEqual(batch.values.map((value) => value.id), [1, 2]);
|
|
assert.equal(requests[0].limit, 2000);
|
|
assert.equal(requests[1].last_value_id, 1);
|
|
} finally {
|
|
globalThis.fetch = previousFetch;
|
|
}
|
|
});
|