Files
order_site/apps/backend/src/services/finance/finance-derived-rebuild.test.ts
T

80 lines
3.3 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/167_finance_derived_scope.sql')
const salesFacts = read('./sales-facts-channel-service.ts')
const ledger = read('./finance-ledger-service.ts')
const rebuild = read('../../../scripts/rebuild-finance-derived.ts')
const audit = read('../../../scripts/audit-finance-derived.ts')
const launchVerification = read('../../../../../scripts/verify-finance-launch.sql')
test('财务销售范围只从正式履约和商业订单生成', () => {
assert.ok(migration.includes('CREATE OR REPLACE VIEW v_finance_order_item_scope'))
assert.ok(migration.includes("fulfillment.status IN ('pending', 'assigned')"))
assert.ok(migration.includes("order_record.pay_status IN ('paid', 'refunded')"))
assert.ok(migration.includes('idx_finance_entries_sales_fact_day'))
assert.ok(salesFacts.includes('FROM v_finance_order_item_scope scope'))
assert.ok(!salesFacts.includes('FROM v_order_item_current oi\n JOIN orders o'))
})
test('销售凭证同步覆盖事实日期和已有凭证日期,删除事实后能自动冲平', () => {
assert.ok(ledger.includes('WITH target_days AS'))
assert.ok(ledger.includes('FROM sales_daily_facts'))
assert.ok(ledger.includes('SELECT (occurred_at AT TIME ZONE'))
assert.ok(ledger.includes('UNION'))
assert.ok(ledger.includes('LEFT JOIN fact_days'))
})
test('财务重建默认 dry-run,只清理可重建数据并保护原始事实', () => {
assert.ok(rebuild.includes("const CONFIRMATION = 'REBUILD_DERIVED'"))
assert.ok(rebuild.includes('if (!execute)'))
assert.ok(rebuild.includes('clearEstimatedSnapshots'))
assert.ok(rebuild.includes('clearSalesFacts'))
assert.ok(rebuild.includes('assertProtectedTotals'))
assert.doesNotMatch(
rebuild,
/DELETE\s+FROM\s+(?:orders|order_items|order_refunds|manual_expenses|fund_entries)\b/i,
)
assert.doesNotMatch(rebuild, /DELETE\s+FROM\s+finance_journal_entries\b/i)
})
test('财务边界同时覆盖销售范围、现有事实和已过账凭证日期', () => {
assert.match(
rebuild,
/FROM v_finance_order_item_scope\s+UNION ALL\s+SELECT stat_date\s+FROM sales_daily_facts/,
)
assert.match(
audit,
/FROM v_finance_order_item_scope\s+UNION ALL\s+SELECT stat_date\s+FROM sales_daily_facts/,
)
})
test('财务上线门禁只把未冲销的人工快照视为有效', () => {
assert.match(
launchVerification,
/WHERE cs\.subject_type = 'manual_case'[\s\S]+AND cs\.superseded_at IS NULL[\s\S]+AND cs\.reversed_at IS NULL/,
)
assert.match(
launchVerification,
/WHERE subject_type = 'manual_case'[\s\S]+AND reversed_at IS NULL[\s\S]+AND subject_id > 0/,
)
assert.match(
launchVerification,
/WHERE status = 'assigned'[\s\S]+AND \([\s\S]+channel IS NULL[\s\S]+OR channel NOT IN/,
)
})
test('财务审计核对销售范围、事实、总账、快照和对账', () => {
assert.ok(audit.includes('v_finance_order_item_scope'))
assert.ok(audit.includes('sales_daily_facts'))
assert.ok(audit.includes("ref_type = 'sales_fact'"))
assert.ok(audit.includes('duplicateActiveSnapshots'))
assert.ok(audit.includes('unresolvedReconciliations'))
assert.ok(audit.includes('process.exitCode = 1'))
})