import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import test from 'node:test' import { normalizeModuleOrder, parseArguments } from '../admin-test-env-acceptance.mjs' import { cleanupSucceeded, formalFeeItems, run } from './fee-items.mjs' const source = readFileSync(new URL('./fee-items.mjs', import.meta.url), 'utf8') test('fee detail binding does not send the removed settlement switch', () => { assert.match(source, /const detailPayload = \{/) assert.doesNotMatch(source, /settlementIncluded/) }) test('referenced fee-item deletion remains a conflict assertion', () => { assert.match(source, /'任意客户费用明细引用后公共项目不可删除'[\s\S]*conflict\(protectedDeleteResponse\)/) }) test('cleanup treats successful deletion and an already absent resource as clean', () => { assert.equal(cleanupSucceeded({ http: 200, body: { code: '200' } }), true) assert.equal(cleanupSucceeded({ http: 200, body: { code: 'A0404' } }), true) assert.equal(cleanupSucceeded({ http: 404, body: {} }), true) assert.equal(cleanupSucceeded({ http: 200, body: { code: 'A0422' } }), false) }) test('no automatic historical fee-item cleanup remains', () => { assert.doesNotMatch(source, /cleanupStaleAcceptanceItems|isAcceptanceFeeItem/) }) for (const missingBaseline of [false, true]) test(`fixed fee catalogue preserves old test items and performs no master writes; missing=${missingBaseline}`, async () => { const codes = ['FBA_HEAD_FREIGHT', 'PRIVATE_LAST_MILE_DELIVERY', ...Array.from({ length: 110 }, (_, i) => `FEE_TYPE_${String(i + 1).padStart(3, '0')}`)] const catalogue = codes.map((feeCode, i) => ({ id: String(i + 1), feeCode, tenantId: '0', status: 'ACTIVE', tradeScope: i < 84 ? 'DOMESTIC' : 'INTERNATIONAL', category: '标准', feeTypeName: `费用${i}` })) catalogue.push({ id: '999', feeCode: 'ACPT_123456', feeTypeName: '全量验收费用-123456', tenantId: '0', status: 'DISABLED', category: '临时', tradeScope: 'DOMESTIC' }) if (missingBaseline) catalogue.splice(3, 1) const calls = [], gaps = [] const context = { options: { fixedCustomerId: '101' }, marker: 'TEST', createdResources: {}, async request(method, path, body, token) { calls.push({ method, path }) const response = data => ({ http: 200, path, body: { code: '200', data } }) if (path === '/api/auth/login') return response({ accessToken: 'client', refreshToken: 'refresh' }) if (path === '/api/auth/logout') return response({}) assert.equal(method, 'GET') if (token === 'client') return { http: 403, body: { code: 'A0403' } } const params = new URL(path, 'https://test.invalid').searchParams const selected = catalogue.filter(row => ['category', 'tradeScope', 'status'].every(key => !params.has(key) || row[key] === params.get(key)) && (!params.has('keyword') || row.feeTypeName.includes(params.get('keyword')))) const size = Number(params.get('size')), page = Number(params.get('page')) return response({ records: selected.slice((page - 1) * size, page * size), total: selected.length, page, size, pages: Math.ceil(selected.length / size) }) }, apiSuccess(_m, _n, response) { return response.body.data }, assert(_m, name, _r, passed) { assert.ok(passed, name) }, addCoverageGap(...args) { gaps.push(args) }, } const before = structuredClone(catalogue) if (missingBaseline) await assert.rejects(run(context), /112条/) else { await run(context) assert.equal(context.createdResources.feeItems.mode, 'READ_ONLY') assert.equal(gaps.length, 1) assert.equal(calls.filter(row => row.path === '/api/auth/logout').length, 1) } assert.deepEqual(catalogue, before) assert.ok(calls.every(row => row.method === 'GET' || row.path.startsWith('/api/auth/'))) }) test('正式费用按编码识别,扩展停用项目保留且不掩盖缺失正式编码', () => { const rows = [{ feeCode: 'FBA_HEAD_FREIGHT' }, { feeCode: 'FEE_TYPE_001' }, { feeCode: 'PRIVATE_LAST_MILE_DELIVERY' }, { feeCode: 'FEE_EXT_OTHER', status: 'DISABLED' }] assert.deepEqual(formalFeeItems(rows), rows.slice(0, 3)) assert.equal(rows.length, 4) assert.equal(formalFeeItems([{ feeCode: 'FEE_TYPE_111' }]).length, 0) }) test('formal baseline includes both system fee items in addition to the 110 controlled items', () => { assert.match(source, /const BASELINE_COUNT = 112/) assert.match(source, /const INTERNATIONAL_COUNT = 28/) assert.match(source, /'FBA_HEAD_FREIGHT'/) assert.match(source, /'PRIVATE_LAST_MILE_DELIVERY'/) }) test('fee-items injects its same-marker merchant and order fixtures for focused execution', () => { const expected = ['merchant-qualification-fixture', 'orders', 'fee-items'] assert.deepEqual(normalizeModuleOrder(['fee-items']), expected) assert.deepEqual(normalizeModuleOrder(['fee-items', 'orders']), expected) assert.deepEqual(parseArguments(['--modules', 'fee-items']).modules, expected) })