- 新增 153 半自动履约迁移、后台任务接口与客户履约接口 - 接入虎牙 Connector 登录会话、绑定与一次性 CK 注入票据 - 新增后台半自动履约页面和客户履约入口 - 完善手工履约配置、成本确认事务与运行时连接器配置 - 同步权威文档、环境示例、部署脚本和迁移展示逻辑
260 lines
11 KiB
TypeScript
260 lines
11 KiB
TypeScript
import assert from 'node:assert/strict'
|
||
import { readFile } from 'node:fs/promises'
|
||
import test from 'node:test'
|
||
|
||
import { resolveManualFulfillmentCost } from './manual-fulfillment-service.js'
|
||
|
||
test('人工成本:固定成本按配置积分 × 平台换算率', () => {
|
||
assert.deepEqual(
|
||
resolveManualFulfillmentCost({
|
||
creditQuantity: 4,
|
||
unitPriceFen: 100,
|
||
providedCostFen: undefined,
|
||
}),
|
||
{ costFen: 400, costMode: 'fixed' },
|
||
)
|
||
// 固定成本时手填金额不生效(客服零输入)。
|
||
assert.equal(
|
||
resolveManualFulfillmentCost({ creditQuantity: 2, unitPriceFen: 100, providedCostFen: 999 })
|
||
.costFen,
|
||
200,
|
||
)
|
||
})
|
||
|
||
test('人工成本:备用金和统一结算金额支持两位小数', () => {
|
||
assert.deepEqual(
|
||
resolveManualFulfillmentCost({
|
||
creditQuantity: 12.34,
|
||
unitPriceFen: 100,
|
||
providedCostFen: undefined,
|
||
}),
|
||
{ costFen: 1234, costMode: 'fixed' },
|
||
)
|
||
})
|
||
|
||
test('人工成本:成本不固定必须手填正数金额', () => {
|
||
assert.deepEqual(
|
||
resolveManualFulfillmentCost({
|
||
creditQuantity: 0,
|
||
unitPriceFen: 1,
|
||
providedCostFen: 1234,
|
||
}),
|
||
{ costFen: 1234, costMode: 'variable' },
|
||
)
|
||
assert.throws(
|
||
() => resolveManualFulfillmentCost({ creditQuantity: 0, unitPriceFen: 1 }),
|
||
/成本不固定/,
|
||
)
|
||
assert.throws(
|
||
() => resolveManualFulfillmentCost({ creditQuantity: 0, unitPriceFen: 1, providedCostFen: 0 }),
|
||
/成本不固定/,
|
||
)
|
||
assert.throws(
|
||
() =>
|
||
resolveManualFulfillmentCost({
|
||
creditQuantity: 0,
|
||
unitPriceFen: 1,
|
||
providedCostFen: -1,
|
||
}),
|
||
/金额无效/,
|
||
)
|
||
})
|
||
|
||
test('人工确认必须单事务:扣款与快照同事务,且使用 with-client 版本', async () => {
|
||
const source = await readFile(new URL('./manual-fulfillment-service.ts', import.meta.url), 'utf8')
|
||
const start = source.indexOf('export async function confirmManualFulfillment')
|
||
const end = source.indexOf('async function resolveFundAccountId')
|
||
const body = source.slice(start, end)
|
||
|
||
const executeStart = body.indexOf('const execute = async (client: PoolClient)')
|
||
const executeEnd = body.indexOf('const result = options.client', executeStart)
|
||
const transactionalBody = body.slice(executeStart, executeEnd)
|
||
assert.ok(executeStart >= 0 && executeEnd > executeStart, '人工确认必须抽出事务执行体')
|
||
assert.ok(
|
||
body.includes(
|
||
'options.client ? await execute(options.client) : await withTransaction(execute)',
|
||
),
|
||
'默认路径必须包在事务里,外部事务必须复用调用方 client',
|
||
)
|
||
assert.ok(
|
||
transactionalBody.includes('writeFundExpense(client'),
|
||
'备用金扣款必须在调用方事务内执行',
|
||
)
|
||
assert.ok(
|
||
body.includes('consumePlatformCreditWithClient(client'),
|
||
'积分扣减必须复用调用方事务(不能再用自带事务的 consumePlatformCredit)',
|
||
)
|
||
assert.ok(
|
||
!body.includes('consumePlatformCredit('),
|
||
'confirm 内不得调用自带事务的 consumePlatformCredit',
|
||
)
|
||
assert.ok(
|
||
body.includes('FOR UPDATE OF oi') && body.includes("subject_type = 'manual_case'"),
|
||
'必须先锁订单项并复查快照,避免并发重复扣款',
|
||
)
|
||
assert.ok(
|
||
body.includes("'manual_case', $1") && body.includes('subjectSource'),
|
||
'manual_case 的 subject_id 必须是 order_item_id,资金来源只写 metadata',
|
||
)
|
||
})
|
||
|
||
test('人工确认前必须自动匹配履约配置,配置保存/定时任务也要回填', async () => {
|
||
const [manualSource, configSource, jobSource, migrationSource, reconcileMigration] =
|
||
await Promise.all([
|
||
readFile(new URL('./manual-fulfillment-service.ts', import.meta.url), 'utf8'),
|
||
readFile(
|
||
new URL('../cost-center/manual-fulfillment-config-service.ts', import.meta.url),
|
||
'utf8',
|
||
),
|
||
readFile(new URL('../scheduler/finance-pipeline-jobs.ts', import.meta.url), 'utf8'),
|
||
readFile(new URL('../../db/migrations/106_manual_sku_key.sql', import.meta.url), 'utf8'),
|
||
readFile(
|
||
new URL('../../db/migrations/135_reconcile_manual_standard_products.sql', import.meta.url),
|
||
'utf8',
|
||
),
|
||
])
|
||
assert.ok(
|
||
manualSource.includes('matchManualFulfillmentConfigWithClient(client, orderItemId)') &&
|
||
manualSource.includes('attachFulfillmentConfigurationsWithExecutor(client'),
|
||
'确认时必须按 SKU 自动挂渠道',
|
||
)
|
||
assert.ok(
|
||
configSource.includes('rematchManualFulfillments({') &&
|
||
configSource.includes('skuKey: config.sku_key') &&
|
||
configSource.includes('dateFrom: shiftDate(today, -29)'),
|
||
'保存人工 SKU 配置后必须立即回填最近 30 天订单项',
|
||
)
|
||
const saveStart = configSource.indexOf('export async function saveManualFulfillmentConfig')
|
||
const saveEnd = configSource.indexOf('async function retryPendingManualItems', saveStart)
|
||
const saveBody = configSource.slice(saveStart, saveEnd)
|
||
assert.ok(
|
||
saveBody.indexOf('withTransaction(') <
|
||
saveBody.indexOf('await syncManualChannelRule(saved, client)'),
|
||
'标准商品、人工配置和人工渠道规则必须同一事务提交',
|
||
)
|
||
assert.ok(
|
||
jobSource.includes("fulfillment_channel = 'manual'") &&
|
||
jobSource.includes('fulfillment_config_id'),
|
||
'人工载体管道必须挂载人工配置',
|
||
)
|
||
assert.ok(
|
||
configSource.includes('manual_sku_key'),
|
||
'人工 SKU 匹配必须统一走 order_items.manual_sku_key 生成列',
|
||
)
|
||
assert.ok(
|
||
configSource.includes('amount_fen, rate_bps') &&
|
||
configSource.includes("0, '1970-01-01T00:00:00+08:00', 'manual', 'active'"),
|
||
'人工履约固定成本版本必须同时写入 amount_fen 和 rate_bps,避免保存成功但后处理报错',
|
||
)
|
||
const deleteStart = configSource.indexOf('export async function deleteManualSkuClassification')
|
||
const deleteEnd = configSource.indexOf('export async function rematchManualFulfillments')
|
||
const deleteBody = configSource.slice(deleteStart, deleteEnd)
|
||
assert.ok(
|
||
!deleteBody.includes('containsCjk(key)'),
|
||
'移除错误人工分类不能要求 SKU 含中文,否则纯数字等脏数据永远删不掉',
|
||
)
|
||
assert.ok(
|
||
migrationSource.includes("spec_json->>'skuNick'") &&
|
||
migrationSource.includes("spec_json->>'outSkuSn'") &&
|
||
migrationSource.includes('sku_code') &&
|
||
migrationSource.includes('sku_name'),
|
||
'生成列必须保留 sku_code / sku_name 兜底',
|
||
)
|
||
const listStart = configSource.indexOf('export async function listManualSkus')
|
||
const listEnd = configSource.indexOf('export async function getManualSkuOverview')
|
||
const listBody = configSource.slice(listStart, listEnd)
|
||
assert.ok(
|
||
listBody.includes('FROM product_catalog p') && listBody.includes('productSourceFilterSql'),
|
||
'人工成本配置必须以 source_kind=manual 的标准商品为唯一清单来源',
|
||
)
|
||
assert.ok(
|
||
listBody.includes('JOIN internal_skus product_sku') &&
|
||
!listBody.includes('substring(p.canonical_sku'),
|
||
'人工成本配置必须通过 internal_sku_id 读取内部 SKU,不能解析 man: 前缀',
|
||
)
|
||
assert.ok(
|
||
reconcileMigration.includes('tmp_manual_product_merge') &&
|
||
reconcileMigration.includes("status = 'archived'"),
|
||
'存量重复人工 SKU 必须合并到同一标准商品并归档重复项',
|
||
)
|
||
})
|
||
|
||
test('统一结算金额未知时只生成待对账账单,不写 0 元实际成本', async () => {
|
||
const source = await readFile(new URL('./manual-fulfillment-service.ts', import.meta.url), 'utf8')
|
||
|
||
assert.ok(source.includes("platformKey === 'on_account' && costFen <= 0"))
|
||
assert.ok(source.includes('if (!onAccountCostPending)'))
|
||
assert.ok(source.includes("reason: 'on_account_cost_pending'"))
|
||
assert.ok(source.includes('costPending: onAccountCostPending'))
|
||
})
|
||
|
||
test('人工成本缺失由管道按配置与快照生成并收口', async () => {
|
||
const [pipelineSource, manualSource] = await Promise.all([
|
||
readFile(new URL('../scheduler/finance-pipeline-jobs.ts', import.meta.url), 'utf8'),
|
||
readFile(new URL('./manual-fulfillment-service.ts', import.meta.url), 'utf8'),
|
||
])
|
||
assert.ok(pipelineSource.includes("'manual_cost_missing'"), '管道必须产出人工成本缺失异常')
|
||
assert.ok(
|
||
pipelineSource.includes("cs.subject_type = 'manual_case'") &&
|
||
pipelineSource.includes('cs.order_item_id = oi.id'),
|
||
'人工成本缺失必须以人工快照是否存在为判定',
|
||
)
|
||
assert.ok(
|
||
manualSource.includes("reason = 'manual_cost_missing'"),
|
||
'确认履约后必须收口人工成本缺失异常',
|
||
)
|
||
})
|
||
|
||
test('迁移 104 提供订单项级人工成本唯一约束与 subject 语义约束', async () => {
|
||
const source = await readFile(
|
||
new URL('../../db/migrations/104_finance_controls.sql', import.meta.url),
|
||
'utf8',
|
||
)
|
||
assert.ok(source.includes('uq_cost_snapshots_manual_order_item'), '缺少订单项级人工快照唯一索引')
|
||
assert.ok(
|
||
source.includes("WHERE subject_type = 'manual_case' AND superseded_at IS NULL"),
|
||
'唯一索引必须只约束有效人工快照',
|
||
)
|
||
assert.ok(
|
||
source.includes('subject_id <> order_item_id') &&
|
||
source.includes('chk_cost_snapshots_manual_subject'),
|
||
'必须把历史 subject_id 归一并加语义约束',
|
||
)
|
||
})
|
||
|
||
test('总账/对账/关账/平台退款能力已接入', async () => {
|
||
const [migration, ledger, reconciliation, refund, manual] = await Promise.all([
|
||
readFile(new URL('../../db/migrations/104_finance_controls.sql', import.meta.url), 'utf8'),
|
||
readFile(new URL('./finance-ledger-service.ts', import.meta.url), 'utf8'),
|
||
readFile(new URL('./finance-reconciliation-service.ts', import.meta.url), 'utf8'),
|
||
readFile(new URL('./order-refund-service.ts', import.meta.url), 'utf8'),
|
||
readFile(new URL('./manual-fulfillment-service.ts', import.meta.url), 'utf8'),
|
||
])
|
||
assert.ok(
|
||
migration.includes('finance_journal_entries') && migration.includes('finance_journal_lines'),
|
||
)
|
||
assert.ok(migration.includes('finance_periods') && migration.includes('finance_reconciliations'))
|
||
assert.ok(
|
||
ledger.includes('finance_period_closed') && ledger.includes('syncFinanceLedgerFromFacts'),
|
||
)
|
||
assert.ok(reconciliation.includes('amount_mismatch') && reconciliation.includes('missing_order'))
|
||
assert.ok(
|
||
refund.includes('approveKuaishouIndustryRefund') &&
|
||
refund.includes("source: 'platform'") &&
|
||
refund.includes('platform_refund_not_supported'),
|
||
'平台退款必须走平台 API 并落 source=platform',
|
||
)
|
||
const kuaishouRoute = await readFile(
|
||
new URL('../../routes/admin/kuaishou-industry.ts', import.meta.url),
|
||
'utf8',
|
||
)
|
||
assert.ok(
|
||
kuaishouRoute.includes('/kuaishou-industry/orders/:orderId/refunds/approve-and-record'),
|
||
'平台退款路由必须明确收口在快手行业前缀下',
|
||
)
|
||
assert.ok(
|
||
manual.includes('fundAccountId') && manual.includes('creditOwnerAdminUserId'),
|
||
'管理员/财务必须可以指定客服账户代录',
|
||
)
|
||
})
|