Files
order_site/apps/backend/src/services/fulfillment/fulfillment-state-service.test.ts
T

90 lines
4.4 KiB
TypeScript

import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
function read(relativePath: string) {
return readFileSync(new URL(relativePath, import.meta.url), 'utf8')
}
const migration = read('../../db/migrations/140_fulfillment_state_events.sql')
const cutoverMigration = read('../../db/migrations/148_fulfillment_execution_truth.sql')
const stateService = read('./fulfillment-state-service.ts')
const channelAssign = read('./channel-assign-service.ts')
const carrierReconciliation = read('./carrier-reconciliation-service.ts')
const affiliateFallback = read('./affiliate-fallback-service.ts')
const carrierCreation = read('./carrier-creation-service.ts')
const manualFulfillment = read('../finance/manual-fulfillment-service.ts')
const manualConfig = read('../cost-center/manual-fulfillment-config-service.ts')
const productNormalize = read('../cost-center/product-normalize-service.ts')
const productCatalog = read('../cost-center/product-catalog-service.ts')
const affiliateProduct = read('../cost-center/affiliate-product-service.ts')
const workOrderProduct = read('../cost-center/work-order-product-service.ts')
const templateBootstrap = read('../cost-center/template-bootstrap-service.ts')
const schedulerJobs = read('../scheduler/finance-pipeline-jobs.ts')
const taskRepo = read('../../repositories/task-repo.ts')
test('履约状态事件表记录前后状态并禁止应用修改', () => {
assert.ok(migration.includes('CREATE TABLE IF NOT EXISTS fulfillment_state_events'))
assert.ok(migration.includes('previous_pending_detail JSONB'))
assert.ok(migration.includes('next_pending_detail JSONB'))
assert.ok(migration.includes('record_fulfillment_state_event'))
assert.ok(migration.includes("current_setting('app.fulfillment_actor_type', TRUE)"))
assert.ok(migration.includes('BEFORE UPDATE ON fulfillment_state_events'))
assert.ok(migration.includes('fulfillment_state_events is append-only'))
})
test('状态写入口串行锁正式履约记录,并在写入前校验期望状态', () => {
assert.ok(stateService.includes('FROM fulfillments'))
assert.ok(stateService.includes('FOR UPDATE'))
assert.ok(!stateService.includes('SELECT * FROM order_items WHERE id = $1 FOR UPDATE'))
assert.ok(stateService.includes('expected.status'))
assert.ok(stateService.includes('expected.channel'))
assert.ok(stateService.includes('sourceNotIn'))
assert.ok(stateService.includes("set_config('app.fulfillment_event_type'"))
assert.ok(stateService.includes('itemSnapshotPatch'))
})
test('高风险履约流程统一通过状态服务写入', () => {
for (const source of [channelAssign, carrierReconciliation, affiliateFallback]) {
assert.ok(source.includes('fulfillment-state-service'))
assert.ok(!source.includes('SET fulfillment_status ='))
assert.ok(!source.includes('fulfillment_status = $'))
}
})
test('履约配置和标准商品关联统一通过状态服务的映射接口写入', () => {
for (const source of [
carrierCreation,
manualFulfillment,
manualConfig,
productNormalize,
productCatalog,
affiliateProduct,
workOrderProduct,
templateBootstrap,
schedulerJobs,
]) {
assert.ok(source.includes('fulfillment-state-service'))
assert.doesNotMatch(source, /UPDATE\s+order_items\b[\s\S]{0,300}?\bfulfillment_config_id\s*=/i)
assert.doesNotMatch(source, /UPDATE\s+order_items\b[\s\S]{0,300}?\bcanonical_product_id\s*=/i)
}
assert.ok(stateService.includes('attachFulfillmentConfigurationsWithExecutor'))
assert.ok(stateService.includes('onlyWhenCanonicalProductNull'))
})
test('任务创建先锁正式履约记录并复用已有活跃尝试', () => {
assert.ok(taskRepo.includes('SELECT id FROM fulfillments WHERE order_item_id = $1 FOR UPDATE'))
assert.ok(taskRepo.includes('task_status NOT IN ('))
assert.ok(taskRepo.includes('ORDER BY is_current DESC, id DESC'))
})
test('订单项状态列冻结,读模型直接投影正式履约表', () => {
assert.ok(
cutoverMigration.includes('RENAME COLUMN fulfillment_status TO legacy_fulfillment_status'),
)
assert.ok(cutoverMigration.includes('CREATE VIEW v_order_item_current'))
assert.ok(cutoverMigration.includes('FROM fulfillments fulfillment'))
assert.ok(cutoverMigration.includes('DROP FUNCTION IF EXISTS sync_fulfillment_shadow(BIGINT)'))
assert.ok(cutoverMigration.includes('CREATE TRIGGER trg_fulfillments_state_event'))
})