feat(edit): 定价试算开发、SKU 样式功能修改
This commit is contained in:
@@ -0,0 +1,466 @@
|
||||
import { beforeEach, afterEach, describe, it, expect, vi } from 'vitest';
|
||||
import { randomUUID, randomInt } from 'node:crypto';
|
||||
import { loadEnvFile } from 'node:process';
|
||||
import { Client } from 'pg';
|
||||
import type { Database, DatabaseTransaction } from '@atlas/database';
|
||||
import type {
|
||||
CommissionImportRequest,
|
||||
CommissionImportPublish,
|
||||
} from '@atlas/contracts';
|
||||
import { CommissionImportService } from '../src/pricing/commission-import.service.js';
|
||||
import { parseCommissionDocument } from '../src/pricing/commission-import-parser.js';
|
||||
import { IdempotencyService } from '../src/common/idempotency.service.js';
|
||||
import { PricingService } from '../src/pricing/pricing.service.js';
|
||||
import { fingerprint } from '../src/pricing/pricing-engine.js';
|
||||
import type { AppConfig } from '../src/config/app-config.module.js';
|
||||
loadEnvFile(new URL('../../../.env', import.meta.url));
|
||||
const makeDocument = (
|
||||
parent = '900000001',
|
||||
child = '900000002',
|
||||
rates = ['12.00', '14.00', '20.50'],
|
||||
) => ({
|
||||
code: 1,
|
||||
msg: '获取成功',
|
||||
data: [
|
||||
{
|
||||
id: 1,
|
||||
label: '测试分组',
|
||||
value: '测试分组',
|
||||
cate_id: parent,
|
||||
children: [
|
||||
{
|
||||
id: '1.1',
|
||||
label: '测试类目',
|
||||
value: '测试类目',
|
||||
cate_id: child,
|
||||
children: rates.map((rate, i) => ({
|
||||
id: `1.1.${i + 1}`,
|
||||
label: `${['售价 ≤ 1500₽', '1500₽ < 售价 ≤ 5000₽', '售价 > 5000₽'][i]} (${rate}%)`,
|
||||
value: `${i + 1},${rate}`,
|
||||
})),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
describe('佣金来源解析', () => {
|
||||
it('保留三个精确区间并将百分数转换为比例', () => {
|
||||
const c = parseCommissionDocument(makeDocument())[0];
|
||||
expect(c.bands.map((b) => b.commission_rate)).toEqual([
|
||||
'0.120000',
|
||||
'0.140000',
|
||||
'0.205000',
|
||||
]);
|
||||
expect(
|
||||
c.bands.map((b) => [
|
||||
b.min_price,
|
||||
b.max_price,
|
||||
b.min_inclusive,
|
||||
b.max_inclusive,
|
||||
]),
|
||||
).toEqual([
|
||||
['0', '1500', false, true],
|
||||
['1500', '5000', false, true],
|
||||
['5000', null, false, false],
|
||||
]);
|
||||
});
|
||||
it('拒绝标签和值不一致、漏档、重复类目及超大文件', () => {
|
||||
const d = makeDocument();
|
||||
d.data[0].children[0].children[0].value = '1,15.00';
|
||||
expect(() => parseCommissionDocument(d)).toThrow();
|
||||
expect(() => parseCommissionDocument(makeDocument('1', '1'))).toThrow();
|
||||
const missing = makeDocument();
|
||||
missing.data[0].children[0].children.pop();
|
||||
expect(() => parseCommissionDocument(missing)).toThrow();
|
||||
expect(() =>
|
||||
parseCommissionDocument({ x: 'a'.repeat(2 * 1024 * 1024) }),
|
||||
).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
// 所有业务数据在测试事务内,结束一律回滚;不会发布或替换用户的实际佣金版本。
|
||||
describe('佣金导入 PostgreSQL 集成', () => {
|
||||
let client: Client,
|
||||
db: Database,
|
||||
service: CommissionImportService,
|
||||
pricing: PricingService;
|
||||
let user: string,
|
||||
workspace: string,
|
||||
platform: string,
|
||||
store: string,
|
||||
leaf: string,
|
||||
oldId: string,
|
||||
oldRule: string;
|
||||
let request: CommissionImportRequest;
|
||||
let savepoint = 0;
|
||||
beforeEach(async () => {
|
||||
const connection = new URL(process.env.MIGRATOR_DATABASE_URL!);
|
||||
connection.username = process.env.POSTGRES_ADMIN_USER!;
|
||||
connection.password = process.env.POSTGRES_ADMIN_PASSWORD!;
|
||||
client = new Client({ connectionString: connection.toString() });
|
||||
await client.connect();
|
||||
await client.query('BEGIN');
|
||||
user = randomUUID();
|
||||
workspace = randomUUID();
|
||||
await client.query(
|
||||
`INSERT INTO atlas.users(id,email,password_hash) VALUES($1,$2,'test-only')`,
|
||||
[user, `${user}@example.test`],
|
||||
);
|
||||
await client.query(
|
||||
`INSERT INTO atlas.workspaces(id,name,slug) VALUES($1,'佣金回滚测试',$2)`,
|
||||
[workspace, workspace],
|
||||
);
|
||||
await client.query(
|
||||
`INSERT INTO atlas.workspace_memberships(workspace_id,user_id,role) VALUES($1,$2,'owner')`,
|
||||
[workspace, user],
|
||||
);
|
||||
await client.query(
|
||||
`INSERT INTO atlas.system_admin_grants(user_id) VALUES($1)`,
|
||||
[user],
|
||||
);
|
||||
await client.query('SET LOCAL ROLE atlas_app');
|
||||
await client.query(`SELECT set_config('app.workspace_id',$1,true)`, [
|
||||
workspace,
|
||||
]);
|
||||
platform = (
|
||||
await client.query(
|
||||
`SELECT id FROM atlas.platforms WHERE upper(code::text)='OZON'`,
|
||||
)
|
||||
).rows[0].id;
|
||||
store = (
|
||||
await client.query(
|
||||
`INSERT INTO atlas.stores(workspace_id,platform_id,name,platform_client_id,currency_code,status,credential_status) VALUES($1,$2,'佣金测试',$3,'RUB','active','valid') RETURNING id`,
|
||||
[workspace, platform, randomUUID()],
|
||||
)
|
||||
).rows[0].id;
|
||||
const p = String(randomInt(100000000, 999999990)),
|
||||
c = String(Number(p) + 1);
|
||||
const parent = (
|
||||
await client.query(
|
||||
`INSERT INTO atlas.platform_product_categories(platform_id,external_category_id,external_type_id,name,path,is_active) VALUES($1,$2,'0','测试分组','测试分组',false) RETURNING id`,
|
||||
[platform, p],
|
||||
)
|
||||
).rows[0].id;
|
||||
const cat = (
|
||||
await client.query(
|
||||
`INSERT INTO atlas.platform_product_categories(platform_id,parent_id,external_category_id,external_type_id,name,path,is_active) VALUES($1,$2,$3,'0','测试类目','测试分组 / 测试类目',false) RETURNING id`,
|
||||
[platform, parent, c],
|
||||
)
|
||||
).rows[0].id;
|
||||
leaf = (
|
||||
await client.query(
|
||||
`INSERT INTO atlas.platform_product_categories(platform_id,parent_id,external_category_id,external_type_id,name,path) VALUES($1,$2,$3,'991','测试类型','测试类型') RETURNING id`,
|
||||
[platform, cat, c],
|
||||
)
|
||||
).rows[0].id;
|
||||
request = {
|
||||
platform_id: platform,
|
||||
version: `qa-${randomUUID()}`,
|
||||
source_reference: '自动化测试,事务回滚',
|
||||
captured_on: '2026-09-28',
|
||||
document: makeDocument(p, c),
|
||||
};
|
||||
const oldCategory = parseCommissionDocument(
|
||||
makeDocument(p, c, ['10.00', '11.00', '12.00']),
|
||||
)[0];
|
||||
oldCategory.mapped_types = 1;
|
||||
await client.query(
|
||||
`UPDATE atlas.commission_imports SET is_current=false WHERE platform_id=$1 AND is_current`,
|
||||
[platform],
|
||||
);
|
||||
oldId = (
|
||||
await client.query(
|
||||
`INSERT INTO atlas.commission_imports(platform_id,version,source_reference,captured_on,document,content_hash,categories,summary,activated_by,activated_at) VALUES($1,$2,'旧测试版本','2026-09-27','{}',$3,$4,'{}',$5,now()-interval '1 day') RETURNING id`,
|
||||
[
|
||||
platform,
|
||||
`old-${randomUUID()}`,
|
||||
fingerprint(randomUUID()),
|
||||
JSON.stringify([oldCategory]),
|
||||
user,
|
||||
],
|
||||
)
|
||||
).rows[0].id;
|
||||
const cc = (
|
||||
await client.query(
|
||||
`INSERT INTO atlas.commission_categories(platform_id,source_category_key,name,path,source,status) VALUES($1,$2,'测试类目',$3,'competitor_capture','active') RETURNING id`,
|
||||
[platform, `maozierp:${c}`, `test-${c}`],
|
||||
)
|
||||
).rows[0].id;
|
||||
oldRule = (
|
||||
await client.query(
|
||||
`INSERT INTO atlas.commission_category_rules(commission_category_id,fulfillment,market_code,currency_code,commission_rate,source,version,status,effective_from,import_id) VALUES($1,'rfbs','RU','RUB',0.1,'competitor_capture','old','active',now()-interval '1 day',$2) RETURNING id`,
|
||||
[cc, oldId],
|
||||
)
|
||||
).rows[0].id;
|
||||
await client.query(
|
||||
`INSERT INTO atlas.platform_product_category_commission_mappings(platform_id,platform_product_category_id,commission_category_id,fulfillment,source,status,effective_from,import_id) VALUES($1,$2,$3,'rfbs','competitor_capture','active',now()-interval '1 day',$4)`,
|
||||
[platform, leaf, cc, oldId],
|
||||
);
|
||||
const transaction = async <T>(
|
||||
work: (tx: DatabaseTransaction) => Promise<T>,
|
||||
) => {
|
||||
const sp = `operation_${++savepoint}`;
|
||||
await client.query(`SAVEPOINT ${sp}`);
|
||||
try {
|
||||
const result = await work(client);
|
||||
await client.query(`RELEASE SAVEPOINT ${sp}`);
|
||||
return result;
|
||||
} catch (e) {
|
||||
await client.query(`ROLLBACK TO SAVEPOINT ${sp}`);
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
db = {
|
||||
transaction,
|
||||
query: (sql: string, args: unknown[]) =>
|
||||
transaction((tx) => tx.query(sql, args)),
|
||||
workspaceTransaction: (
|
||||
id: string,
|
||||
work: (tx: DatabaseTransaction) => Promise<unknown>,
|
||||
) =>
|
||||
transaction(async (tx) => {
|
||||
await tx.query(
|
||||
`SELECT set_config('app.workspace_id',$1,true)`,
|
||||
[id],
|
||||
);
|
||||
return work(tx);
|
||||
}),
|
||||
} as unknown as Database;
|
||||
const config = {
|
||||
IDEMPOTENCY_KEY_SECRET: 'commission-test-idempotency-secret',
|
||||
pricingAllowDraftRates: false,
|
||||
} as AppConfig;
|
||||
const idem = new IdempotencyService(db, config);
|
||||
service = new CommissionImportService(db, idem);
|
||||
pricing = new PricingService(db, idem, config);
|
||||
});
|
||||
afterEach(async () => {
|
||||
if (client) {
|
||||
await client.query('ROLLBACK');
|
||||
await client.end();
|
||||
}
|
||||
});
|
||||
const publishInput = async (
|
||||
s: CommissionImportService,
|
||||
r: CommissionImportRequest,
|
||||
): Promise<CommissionImportPublish> => ({
|
||||
...r,
|
||||
preview_hash: (await s.preview(r)).preview_hash,
|
||||
confirm_scope: true,
|
||||
});
|
||||
it('预览差异,启用三档规则,按内部及外部类目查询;旧规则保留', async () => {
|
||||
const p = await service.preview(request);
|
||||
expect(p.summary.eligible).toBe(1);
|
||||
expect(p.categories[0].change).toBe('changed');
|
||||
const r = await service.publish(
|
||||
workspace,
|
||||
user,
|
||||
randomUUID(),
|
||||
await publishInput(service, request),
|
||||
);
|
||||
expect(r.status).toBe(201);
|
||||
const rules = await pricing.commissionRules(workspace, store, leaf);
|
||||
expect(rules.bands.map((b) => b.commission_rate)).toEqual([
|
||||
'0.120000',
|
||||
'0.140000',
|
||||
'0.205000',
|
||||
]);
|
||||
expect(
|
||||
(
|
||||
await pricing.commissionRules(
|
||||
workspace,
|
||||
store,
|
||||
undefined,
|
||||
p.categories[0].source_category_id,
|
||||
)
|
||||
).bands,
|
||||
).toEqual(rules.bands);
|
||||
expect(
|
||||
(
|
||||
await client.query(
|
||||
`SELECT status,commission_rate FROM atlas.commission_category_rules WHERE id=$1`,
|
||||
[oldRule],
|
||||
)
|
||||
).rows[0],
|
||||
).toEqual({ status: 'archived', commission_rate: '0.100000' });
|
||||
expect((await service.preview(request)).preview_hash).not.toBe(
|
||||
p.preview_hash,
|
||||
);
|
||||
});
|
||||
it('相同幂等请求只发布一次,旧预览不能再启用', async () => {
|
||||
const input = await publishInput(service, request),
|
||||
key = randomUUID();
|
||||
const a = await service.publish(workspace, user, key, input);
|
||||
expect(await service.publish(workspace, user, key, input)).toEqual(
|
||||
JSON.parse(JSON.stringify(a)),
|
||||
);
|
||||
await expect(
|
||||
service.publish(workspace, user, randomUUID(), input),
|
||||
).rejects.toMatchObject({
|
||||
response: { error: { code: 'COMMISSION_PREVIEW_STALE' } },
|
||||
});
|
||||
const fresh = await publishInput(service, request);
|
||||
await expect(
|
||||
service.publish(workspace, user, randomUUID(), fresh),
|
||||
).rejects.toMatchObject({
|
||||
response: { error: { code: 'COMMISSION_IMPORT_EXISTS' } },
|
||||
});
|
||||
});
|
||||
it('0% 佣金可以启用并用于类目查询', async () => {
|
||||
const d = request.document as ReturnType<typeof makeDocument>;
|
||||
d.data[0].children[0].children = makeDocument('1', '2', [
|
||||
'0.00',
|
||||
'0.00',
|
||||
'0.00',
|
||||
]).data[0].children[0].children;
|
||||
const p = await service.preview(request);
|
||||
expect(p.summary).toMatchObject({
|
||||
eligible: 1,
|
||||
excluded: 0,
|
||||
mapped_types: 1,
|
||||
});
|
||||
await service.publish(
|
||||
workspace,
|
||||
user,
|
||||
randomUUID(),
|
||||
await publishInput(service, request),
|
||||
);
|
||||
expect(
|
||||
(await pricing.commissionRules(workspace, store, leaf)).bands.map(
|
||||
(b) => b.commission_rate,
|
||||
),
|
||||
).toEqual(['0.000000', '0.000000', '0.000000']);
|
||||
});
|
||||
it('错误父类目被提示;无可映射规则时拒绝空版本', async () => {
|
||||
const d = request.document as ReturnType<typeof makeDocument>;
|
||||
d.data[0].cate_id = '123';
|
||||
expect(
|
||||
(await service.preview(request)).categories[0].issues.join(),
|
||||
).toContain('父类目');
|
||||
await expect(
|
||||
service.publish(
|
||||
workspace,
|
||||
user,
|
||||
randomUUID(),
|
||||
await publishInput(service, request),
|
||||
),
|
||||
).rejects.toMatchObject({
|
||||
response: { error: { code: 'COMMISSION_IMPORT_INVALID' } },
|
||||
});
|
||||
});
|
||||
it('目录变化后必须重新预览', async () => {
|
||||
const input = await publishInput(service, request);
|
||||
await client.query(
|
||||
`UPDATE atlas.platform_product_categories SET is_active=false WHERE id=$1`,
|
||||
[leaf],
|
||||
);
|
||||
await expect(
|
||||
service.publish(workspace, user, randomUUID(), input),
|
||||
).rejects.toMatchObject({
|
||||
response: { error: { code: 'COMMISSION_PREVIEW_STALE' } },
|
||||
});
|
||||
});
|
||||
it('撤销管理员授权阻止发布,其他工作空间不能读取门店规则', async () => {
|
||||
const input = await publishInput(service, request);
|
||||
await client.query('RESET ROLE');
|
||||
await client.query(
|
||||
`UPDATE atlas.system_admin_grants SET revoked_at=now() WHERE user_id=$1`,
|
||||
[user],
|
||||
);
|
||||
await client.query('SET LOCAL ROLE atlas_app');
|
||||
await expect(
|
||||
service.publish(workspace, user, randomUUID(), input),
|
||||
).rejects.toMatchObject({
|
||||
response: { error: { code: 'SYSTEM_ADMIN_REQUIRED' } },
|
||||
});
|
||||
await expect(
|
||||
pricing.commissionRules(randomUUID(), store, leaf),
|
||||
).rejects.toMatchObject({
|
||||
response: { error: { code: 'STORE_NOT_FOUND' } },
|
||||
});
|
||||
});
|
||||
it('有目录异常时无需额外确认,仍可启用其他可用类目', async () => {
|
||||
const d = request.document as ReturnType<typeof makeDocument>;
|
||||
d.data[0].children.push(
|
||||
makeDocument('1', '899999999').data[0].children[0],
|
||||
);
|
||||
const preview = await service.preview(request);
|
||||
expect(preview.summary).toMatchObject({ eligible: 1, excluded: 1 });
|
||||
const input = await publishInput(service, request);
|
||||
await service.publish(workspace, user, randomUUID(), input);
|
||||
expect(
|
||||
(await pricing.commissionRules(workspace, store, leaf)).bands,
|
||||
).toHaveLength(3);
|
||||
expect(
|
||||
(
|
||||
await service.detail((await service.list(platform))[0].id)
|
||||
).categories.filter((c) => c.status === 'excluded'),
|
||||
).toHaveLength(1);
|
||||
});
|
||||
it('后续写入失败时旧版本保持有效', async () => {
|
||||
const input = await publishInput(service, request);
|
||||
const original = client.query.bind(client);
|
||||
const spy = vi.spyOn(client, 'query').mockImplementation(((
|
||||
sql: string,
|
||||
args: unknown[],
|
||||
) => {
|
||||
if (
|
||||
typeof sql === 'string' &&
|
||||
sql.includes(
|
||||
'INSERT INTO atlas.platform_product_category_commission_mappings',
|
||||
)
|
||||
)
|
||||
throw new Error('模拟映射写入失败');
|
||||
return original(sql, args);
|
||||
}) as typeof client.query);
|
||||
try {
|
||||
await expect(
|
||||
service.publish(workspace, user, randomUUID(), input),
|
||||
).rejects.toThrow('模拟映射写入失败');
|
||||
} finally {
|
||||
spy.mockRestore();
|
||||
}
|
||||
expect(
|
||||
(
|
||||
await client.query(
|
||||
'SELECT is_current FROM atlas.commission_imports WHERE id=$1',
|
||||
[oldId],
|
||||
)
|
||||
).rows[0].is_current,
|
||||
).toBe(true);
|
||||
expect(
|
||||
(
|
||||
await client.query(
|
||||
'SELECT status FROM atlas.commission_category_rules WHERE id=$1',
|
||||
[oldRule],
|
||||
)
|
||||
).rows[0].status,
|
||||
).toBe('active');
|
||||
expect(
|
||||
(
|
||||
await client.query(
|
||||
'SELECT id FROM atlas.commission_imports WHERE version=$1',
|
||||
[request.version],
|
||||
)
|
||||
).rowCount,
|
||||
).toBe(0);
|
||||
});
|
||||
it('数据库拒绝直接改写已启用的历史费率', async () => {
|
||||
await expect(
|
||||
db.transaction((tx) =>
|
||||
tx.query(
|
||||
`UPDATE atlas.commission_category_rules SET commission_rate=0.9 WHERE id=$1`,
|
||||
[oldRule],
|
||||
),
|
||||
),
|
||||
).rejects.toMatchObject({ code: '23514' });
|
||||
expect(
|
||||
(
|
||||
await client.query(
|
||||
`SELECT commission_rate FROM atlas.commission_category_rules WHERE id=$1`,
|
||||
[oldRule],
|
||||
)
|
||||
).rows[0].commission_rate,
|
||||
).toBe('0.100000');
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
||||
import { describe, it } from 'vitest';
|
||||
import { draftPricingEnabled } from '../src/config/app-config.module.js';
|
||||
|
||||
describe('草稿费率开发模式边界', () => {
|
||||
describe('草稿费率配置', () => {
|
||||
it('开发环境默认开放,可显式关闭;测试环境需显式开启', () => {
|
||||
assert.equal(draftPricingEnabled('development', 'auto'), true);
|
||||
assert.equal(draftPricingEnabled('development', 'false'), false);
|
||||
@@ -10,8 +10,9 @@ describe('草稿费率开发模式边界', () => {
|
||||
assert.equal(draftPricingEnabled('test', 'true'), true);
|
||||
});
|
||||
|
||||
it('生产环境即使误设开关也不能使用草稿费率', () => {
|
||||
assert.equal(draftPricingEnabled('production', 'true'), false);
|
||||
assert.equal(draftPricingEnabled('production', 'auto'), false);
|
||||
it('生产环境默认允许草稿卡,可显式关闭', () => {
|
||||
assert.equal(draftPricingEnabled('production', 'true'), true);
|
||||
assert.equal(draftPricingEnabled('production', 'auto'), true);
|
||||
assert.equal(draftPricingEnabled('production', 'false'), false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -99,6 +99,17 @@ describe('十进制定价引擎', () => {
|
||||
expect(o.can_confirm).toBe(false);
|
||||
expect(Number(o.profit_cny)).toBeLessThan(0);
|
||||
});
|
||||
it('零佣金仍生成正式报价,平台佣金费用为零', () => {
|
||||
const o = calculatePricing(
|
||||
input(),
|
||||
'RUB',
|
||||
'12.3',
|
||||
[rule],
|
||||
[{ ...commission, commission_rate: '0.000000' }],
|
||||
).options[0];
|
||||
expect(o.commission_rate).toBe('0.000000');
|
||||
expect(o.fees.commission_cny).toBe('0.0000');
|
||||
});
|
||||
it('实际重、尺寸和开区间边界精确生效', () => {
|
||||
const i = input();
|
||||
i.package.shipping_weight_g = '500';
|
||||
@@ -241,3 +252,74 @@ describe('十进制定价引擎', () => {
|
||||
expect(option.can_confirm).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('导入佣金的卢布分档边界', () => {
|
||||
const bands: CommissionRule[] = [
|
||||
{
|
||||
...commission,
|
||||
min_price: '0',
|
||||
max_price: '1500',
|
||||
min_inclusive: false,
|
||||
commission_rate: '0.12',
|
||||
},
|
||||
{
|
||||
...commission,
|
||||
min_price: '1500',
|
||||
max_price: '5000',
|
||||
min_inclusive: false,
|
||||
commission_rate: '0.14',
|
||||
},
|
||||
{
|
||||
...commission,
|
||||
min_price: '5000',
|
||||
max_price: null,
|
||||
min_inclusive: false,
|
||||
commission_rate: '0.205',
|
||||
},
|
||||
];
|
||||
it.each([
|
||||
['1500', '0.12'],
|
||||
['1500.01', '0.14'],
|
||||
['5000', '0.14'],
|
||||
['5000.01', '0.205'],
|
||||
])('售价 %s 只命中对应佣金档', (price, rate) => {
|
||||
const i = input();
|
||||
i.pricing.mode = 'fixed_sale';
|
||||
i.pricing.selling_price = price;
|
||||
const result = calculatePricing(
|
||||
i,
|
||||
'RUB',
|
||||
'12.3',
|
||||
[{ ...rule, max_merchandise_value: null }],
|
||||
bands,
|
||||
);
|
||||
expect(result.options).toHaveLength(1);
|
||||
expect(Number(result.options[0].commission_rate)).toBe(Number(rate));
|
||||
});
|
||||
it('人民币售价换算卢布后匹配费率', () => {
|
||||
const i = input();
|
||||
i.pricing.mode = 'fixed_sale';
|
||||
i.pricing.selling_price = '150';
|
||||
const result = calculatePricing(
|
||||
i,
|
||||
'CNY',
|
||||
'10',
|
||||
[{ ...rule, max_merchandise_value: null }],
|
||||
bands,
|
||||
);
|
||||
expect(result.options).toHaveLength(1);
|
||||
expect(Number(result.options[0].commission_rate)).toBe(0.12);
|
||||
i.pricing.selling_price = '150.01';
|
||||
expect(
|
||||
Number(
|
||||
calculatePricing(
|
||||
i,
|
||||
'CNY',
|
||||
'10',
|
||||
[{ ...rule, max_merchandise_value: null }],
|
||||
bands,
|
||||
).options[0].commission_rate,
|
||||
),
|
||||
).toBe(0.14);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -577,11 +577,13 @@ describe('SKU 试算与原子确认 HTTP / PostgreSQL', () => {
|
||||
);
|
||||
const input = quote();
|
||||
input.pricing.temporary_commission_rate = null;
|
||||
assert.equal(
|
||||
(await request('/pricing/quote-previews', 'POST', input)).error
|
||||
.code,
|
||||
'PRICING_COMMISSION_UNAVAILABLE',
|
||||
const withoutCommission = await request(
|
||||
'/pricing/quote-previews',
|
||||
'POST',
|
||||
input,
|
||||
);
|
||||
assert.equal(withoutCommission.error.code, 'PRICING_COMMISSION_UNAVAILABLE');
|
||||
assert.match(withoutCommission.error.message, /本次自定义参数/);
|
||||
input.pricing.temporary_commission_rate = '0.12';
|
||||
input.pricing.mode = 'fixed_sale';
|
||||
input.pricing.selling_price = '10';
|
||||
@@ -598,7 +600,7 @@ describe('SKU 试算与原子确认 HTTP / PostgreSQL', () => {
|
||||
};
|
||||
assert.equal((await save(state, payload)).status, 409);
|
||||
});
|
||||
it('开发草稿可试算并确认到商品编辑页,但不能通过首次上架校验', async () => {
|
||||
it('草稿费率可试算、确认且不阻断首次上架预检', async () => {
|
||||
await db.query(
|
||||
`UPDATE atlas.logistics_rate_cards SET status='draft',reviewed_at=NULL,reviewed_by_user_id=NULL WHERE id=$1`,
|
||||
[card],
|
||||
@@ -637,11 +639,13 @@ describe('SKU 试算与原子确认 HTTP / PostgreSQL', () => {
|
||||
{ 'X-Atlas-Platform-Id': platform },
|
||||
);
|
||||
assert.equal(validation.status, 200, JSON.stringify(validation));
|
||||
assert.ok(
|
||||
assert.equal(
|
||||
validation.data.variants[0].issues.some(
|
||||
(issue: { code: string }) =>
|
||||
issue.code === 'PRICING_RATE_REVIEW_REQUIRED',
|
||||
issue.code === 'PRICING_RATE_REVIEW_REQUIRED' ||
|
||||
issue.code === 'PRICING_REQUIRED',
|
||||
),
|
||||
false,
|
||||
);
|
||||
const rateCard = await db.query<{ status: string; reviewed_at: Date | null }>(
|
||||
'SELECT status,reviewed_at FROM atlas.logistics_rate_cards WHERE id=$1',
|
||||
|
||||
@@ -276,6 +276,7 @@ describe('商品编辑 M3.0–M3.3 HTTP 集成测试', () => {
|
||||
assert.equal(editor.response.status, 200);
|
||||
assert.equal(editor.body.data.product.id, first.productId);
|
||||
assert.equal(editor.body.data.profile.id, first.profileId);
|
||||
assert.equal(editor.body.data.profile.fulfillment, 'rfbs');
|
||||
assert.equal(editor.body.data.variants.length, 1);
|
||||
assert.equal(editor.body.data.variants[0].listing.status, 'not_listed');
|
||||
});
|
||||
@@ -2189,4 +2190,129 @@ describe('商品编辑 M3.0–M3.3 HTTP 集成测试', () => {
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('包装重量模式独立保存,并允许 SKU 已确认重量继承通用尺寸', async () => {
|
||||
await database.workspaceTransaction(workspaceId, async (tx) => {
|
||||
const store = await tx.query<{ id: string }>(
|
||||
`SELECT id FROM atlas.stores
|
||||
WHERE workspace_id = current_setting('app.workspace_id')::uuid
|
||||
AND platform_id = $1 AND is_default = true LIMIT 1`,
|
||||
[platformId],
|
||||
);
|
||||
if (!store.rows.length)
|
||||
await tx.query(
|
||||
`INSERT INTO atlas.stores
|
||||
(workspace_id, platform_id, name, platform_client_id,
|
||||
currency_code, is_default, status, credential_status)
|
||||
VALUES (current_setting('app.workspace_id')::uuid, $1,
|
||||
'包装规格模式测试店铺', $2, 'RUB', true, 'active', 'valid')`,
|
||||
[platformId, `editor-package-${randomUUID()}`],
|
||||
);
|
||||
});
|
||||
const created = await createCollectionItem('包装规格模式测试商品');
|
||||
const entered = await request(
|
||||
`/api/collection-items/${created.collectionItemId}/editor-contexts`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: headers({
|
||||
'X-Atlas-Platform-Id': platformId,
|
||||
'Idempotency-Key': randomUUID(),
|
||||
}),
|
||||
},
|
||||
);
|
||||
assert.equal(entered.response.status, 201);
|
||||
const before = await request(`/api/products/${created.productId}/editor`, {
|
||||
headers: headers({ 'X-Atlas-Platform-Id': platformId }),
|
||||
});
|
||||
assert.equal(before.response.status, 200);
|
||||
const snapshot = before.body.data;
|
||||
const variant = snapshot.variants[0];
|
||||
const payload = {
|
||||
ai_source_material: snapshot.source_material,
|
||||
ai_source_overridden_fields: snapshot.source_material_state.overridden_fields,
|
||||
common: {
|
||||
...snapshot.product.common,
|
||||
default_physical_spec: snapshot.product.default_physical_spec,
|
||||
default_package_spec: {
|
||||
length_mm: '120',
|
||||
width_mm: '60',
|
||||
height_mm: '30',
|
||||
shipping_weight_g: null,
|
||||
status: 'missing',
|
||||
},
|
||||
package_weight_mode: 'variant',
|
||||
package_dimensions_mode: 'common',
|
||||
},
|
||||
profile: {
|
||||
target_store_id: snapshot.profile.target_store.id,
|
||||
fulfillment: 'rfbs',
|
||||
title: snapshot.profile.title,
|
||||
description: snapshot.profile.description,
|
||||
tags: snapshot.profile.tags,
|
||||
platform_product_category_id: snapshot.profile.category.id,
|
||||
category_source: snapshot.profile.category.source,
|
||||
},
|
||||
variants: [
|
||||
{
|
||||
id: variant.id,
|
||||
client_key: null,
|
||||
display_name: variant.display_name,
|
||||
source_sku: variant.source_sku,
|
||||
specification_values: variant.specification_values,
|
||||
purchase_cost_cny: variant.purchase_cost_cny,
|
||||
title: variant.title,
|
||||
tags: variant.tags,
|
||||
description: variant.description,
|
||||
physical_spec: variant.physical_spec,
|
||||
package_spec: {
|
||||
length_mm: null,
|
||||
width_mm: null,
|
||||
height_mm: null,
|
||||
shipping_weight_g: '400',
|
||||
status: 'confirmed',
|
||||
},
|
||||
offer_id: variant.listing.offer_id,
|
||||
media_asset_ids: variant.media.map((item: { asset_id: string }) => item.asset_id),
|
||||
},
|
||||
],
|
||||
};
|
||||
const saved = await request(`/api/products/${created.productId}/editor-draft`, {
|
||||
method: 'PUT',
|
||||
headers: headers({
|
||||
'X-Atlas-Platform-Id': platformId,
|
||||
'Idempotency-Key': randomUUID(),
|
||||
'If-Match': `"${snapshot.product.revision}:${snapshot.profile.revision}"`,
|
||||
}),
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
assert.equal(saved.response.status, 200, JSON.stringify(saved.body));
|
||||
const after = await request(`/api/products/${created.productId}/editor`, {
|
||||
headers: headers({ 'X-Atlas-Platform-Id': platformId }),
|
||||
});
|
||||
assert.equal(after.body.data.product.package_weight_mode, 'variant');
|
||||
assert.equal(after.body.data.product.package_dimensions_mode, 'common');
|
||||
assert.equal(after.body.data.product.default_package_spec.shipping_weight_g, null);
|
||||
assert.equal(after.body.data.variants[0].package_spec.shipping_weight_g, '400.000');
|
||||
assert.equal(after.body.data.variants[0].effective_package_spec.length_mm, '120.000');
|
||||
assert.equal(after.body.data.variants[0].effective_package_spec.status, 'confirmed');
|
||||
const {
|
||||
package_weight_mode: _weightMode,
|
||||
package_dimensions_mode: _dimensionsMode,
|
||||
...legacyCommon
|
||||
} = payload.common;
|
||||
const legacySave = await request(`/api/products/${created.productId}/editor-draft`, {
|
||||
method: 'PUT',
|
||||
headers: headers({
|
||||
'X-Atlas-Platform-Id': platformId,
|
||||
'Idempotency-Key': randomUUID(),
|
||||
'If-Match': `"${after.body.data.product.revision}:${after.body.data.profile.revision}"`,
|
||||
}),
|
||||
body: JSON.stringify({ ...payload, common: legacyCommon }),
|
||||
});
|
||||
assert.equal(legacySave.response.status, 200, JSON.stringify(legacySave.body));
|
||||
const unchangedMode = await request(`/api/products/${created.productId}/editor`, {
|
||||
headers: headers({ 'X-Atlas-Platform-Id': platformId }),
|
||||
});
|
||||
assert.equal(unchangedMode.body.data.product.package_weight_mode, 'variant');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user