Files
order_site/apps/backend/src/services/finance/manual-fulfillment-integrity.test.ts
T

204 lines
7.9 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 { 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)
assert.ok(body.includes('withTransaction('), '人工确认必须包在事务里')
assert.ok(
body.indexOf('withTransaction(') < body.indexOf('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] = 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'),
])
assert.ok(
manualSource.includes('matchManualFulfillmentConfigWithClient(client, orderItemId, now)'),
'确认时必须按 SKU 自动挂渠道',
)
assert.ok(
configSource.includes('rematchManualFulfillments({') &&
configSource.includes('skuKey: config.sku_key') &&
configSource.includes('dateFrom: shiftDate(today, -29)'),
'保存人工 SKU 配置后必须立即回填最近 30 天订单项',
)
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,避免保存成功但后处理报错',
)
assert.ok(
migrationSource.includes("spec_json->>'skuNick'") &&
migrationSource.includes("spec_json->>'outSkuSn'") &&
migrationSource.includes('sku_code') &&
migrationSource.includes('sku_name'),
'生成列必须保留 sku_code / sku_name 兜底',
)
})
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'),
'管理员/财务必须可以指定客服账户代录',
)
})