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 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 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('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('任务创建先锁订单项并复用已有活跃尝试', () => { assert.ok(taskRepo.includes('SELECT id FROM order_items WHERE id = $1 FOR UPDATE')) assert.ok(taskRepo.includes('task_status NOT IN (')) assert.ok(taskRepo.includes('ORDER BY is_current DESC, id DESC')) })