Files
atlas-seller-kit/apps/extension/test/request-errors.test.mts
T

57 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import assert from 'node:assert/strict';
import test from 'node:test';
import {
backgroundError,
requestError,
unwrapBackgroundResponse,
} from '../lib/request-errors.ts';
import { validateCollectedPayload } from '../lib/collection-payload-validation.ts';
import { PRODUCT_VARIANT_LIMIT, productPublishPreflightRequestSchema } from '@atlas/contracts';
const payload = {
source: {
platform: '1688', itemKey: '123456789',
canonicalUrl: 'https://detail.1688.com/offer/123456789.html',
collectedAt: '2026-10-05T10:00:00.000Z', parserVersion: 'test',
},
common: { attributes: [], categories: [] },
variantAxes: [], variants: [], assets: [], evidence: [],
};
test('采集支持超过 100 个 SKU,上限与草稿一致,超限时不静默截断', () => {
const variants = Array.from({ length: PRODUCT_VARIANT_LIMIT + 1 }, (_, index) => ({
sourceVariantKey: String(index), displayName: `SKU ${index}`,
selections: [], confidence: 'verified', evidenceRefs: [],
}));
assert.throws(() => validateCollectedPayload({ ...payload, variants }), /采集数据不符合要求.*variants/);
assert.equal(variants.length, PRODUCT_VARIANT_LIMIT + 1);
assert.equal(validateCollectedPayload({ ...payload, variants: variants.slice(0, 101) }).variants.length, 101);
assert.equal(validateCollectedPayload({ ...payload, variants: variants.slice(0, PRODUCT_VARIANT_LIMIT) }).variants.length, PRODUCT_VARIANT_LIMIT);
assert.equal(validateCollectedPayload(payload).source.itemKey, '123456789');
});
test('提高采集容量不放宽每批 50 个 SKU 的首次上架约束', () => {
const ids = Array.from({ length: 51 }, () => crypto.randomUUID());
assert.equal(productPublishPreflightRequestSchema.safeParse({ variant_ids: ids.slice(0, 50) }).success, true);
assert.equal(productPublishPreflightRequestSchema.safeParse({ variant_ids: ids }).success, false);
});
test('采集字段路径错误可经 Chrome 可序列化响应返回浮窗', () => {
const error = requestError({ error: {
message: '请求参数不符合要求',
details: [{ field: 'variantAxes.0.values.0.name', message: 'Too small: expected string to have >=1 characters' }],
} }, 422);
assert.equal(error.status, 422);
const response = JSON.parse(JSON.stringify(backgroundError(error)));
assert.throws(() => unwrapBackgroundResponse(response), /variantAxes\.0\.values\.0\.name/);
assert.throws(() => unwrapBackgroundResponse(response), /请求参数不符合要求/);
});
test('通用 HTTP 错误及空后台响应仍可操作,成功响应保持原有结构', () => {
assert.equal(requestError(null, 503).message, '请求失败(HTTP 503)');
assert.equal(requestError({ error: { message: '登录过期', details: [null, { token: '不应展示' }] } }, 401).message, '登录过期');
assert.throws(() => unwrapBackgroundResponse(undefined), /请重载扩展并刷新商品页/);
const success = { result: { data: { editor_entry_path: '/products/test/editor' } } };
assert.strictEqual(unwrapBackgroundResponse(success), success);
});