import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import test from 'node:test' import { readInitialization, table, seedRows } from '../sql-baseline-contract-helpers.mjs' const overseas = readInitialization('OVERSEAS') const cn = readInitialization() const read = relative => readFileSync(new URL(`../../${relative}`, import.meta.url), 'utf8') test('pool has a unique publication per trip and full durable command identity, no second capacity balance', () => { const offer = table(overseas, 'baiyue_lastleg', 'll_fleet_capacity_offer') assert.match(offer, /UNIQUE KEY\s+\w+\(trip_id\)/i) assert.match(offer, /UNIQUE KEY\s+\w+\(offer_no\)/i) assert.match(offer, /published_fingerprint CHAR\(64\) NULL/i) assert.doesNotMatch(offer, /remaining_pallet|available_pallet|reserved_pallet/i) const command = table(overseas, 'baiyue_lastleg', 'll_fleet_capacity_offer_command') assert.match(command, /UNIQUE KEY\s+\w+\(trip_id,operator_id,action,scope_no,idempotency_key\)/i) assert.match(command, /request_hash CHAR\(64\) NOT NULL/i) assert.match(command, /result_json JSON NOT NULL/i) }) test('one booking maps to exactly one commercial order and one actual incoming source', () => { const link = table(overseas, 'baiyue_lastleg', 'll_fleet_pool_order_link') for (const columns of ['booking_id', 'booking_no', 'business_type,business_order_id', 'incoming_no', 'lastleg_order_id']) { assert.match(link, new RegExp(`UNIQUE KEY\\s+\\w+\\(${columns}\\)`, 'i')) } assert.match(link, /incoming_no VARCHAR\(64\) NULL/i) assert.match(link, /lastleg_order_id BIGINT NULL/i) assert.match(link, /actual_pallet_count INT NULL/i) assert.match(link, /actual_pallet_spaces INT NULL/i) assert.doesNotMatch(link, /(?:agreed_amount|receivable_amount|paid_amount)\s+DECIMAL/i) const booking = table(overseas, 'baiyue_lastleg', 'll_fleet_external_booking') assert.match(booking, /sales_owner_user_id VARCHAR\(64\) NULL/i) assert.match(booking, /expected_pallet_count INT NULL/i) }) test('module and unified fresh baselines contain the exact same new table definitions', () => { // 比较定义而不是文件换行;仍要求列、约束及表选项完全相同。 const module = read('lastleg-service/src/main/resources/db/lastleg.sql').replace(/\r\n/g, '\n') for (const name of ['ll_fleet_capacity_offer', 'll_fleet_capacity_offer_command', 'll_fleet_pool_order_link']) { const definition = table(overseas, 'baiyue_lastleg', name).replace(/\r\n/g, '\n') assert.ok(module.includes(definition), `${name}: module and unified contract drift`) } const sql = read('deploy/sql/migrations/overseas/baiyue_lastleg/V10__fleet_capacity_pool.sql') assert.doesNotMatch(sql.replace(/^--.*$/gm, ''), /\b(?:DROP|TRUNCATE|DELETE|UPDATE|REPLACE)\b/i) assert.match(sql, /ADD COLUMN expected_pallet_count INT NULL/i) assert.match(sql, /ADD CONSTRAINT fk_fleet_booking_offer/i) }) test('customer identity joins use the same explicit collation as existing fulfillment orders', () => { for (const name of ['ll_fleet_external_booking', 'll_fleet_pool_order_link']) { assert.match(table(overseas, 'baiyue_lastleg', name), /customer_id VARCHAR\(128\) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL/i) } const sql = read('deploy/sql/migrations/overseas/baiyue_lastleg/V11__align_capacity_pool_collation.sql') assert.equal((sql.match(/MODIFY COLUMN customer_id/g) || []).length, 2) assert.doesNotMatch(sql.replace(/^--.*$/gm, ''), /\b(?:DROP|TRUNCATE|DELETE|UPDATE|REPLACE)\b/i) }) test('commercial schemas do not invent container or palletizing facts for pool orders', () => { for (const name of ['ll_local_looseboard_order']) { const definition = table(overseas, 'baiyue_lastleg', name) assert.doesNotMatch(definition, /container_type/i) assert.match(definition, /estimated_pallet_count INT NULL/i) assert.match(definition, /product_name VARCHAR\(255\) NULL/i) assert.doesNotMatch(definition, /container_type[^\n]*DEFAULT '40HQ'/i) } assert.doesNotMatch(table(overseas, 'baiyue_lastleg', 'll_us_truck_order'), /container_type/i) assert.match(table(overseas, 'baiyue_lastleg', 'll_us_truck_order'), /vehicle_type/i) assert.match(table(overseas, 'baiyue_lastleg', 'll_local_looseboard_order'), /pallet_required\s+varchar\(8\)(?:\s+COLLATE\s+\w+)?\s+(?:NULL\s+)?DEFAULT NULL/i) const sql = read('deploy/sql/migrations/overseas/baiyue_lastleg/V12__preserve_unknown_pool_order_fields.sql') assert.equal((sql.match(/MODIFY COLUMN/g) || []).length, 3) assert.doesNotMatch(sql.replace(/^--.*$/gm, ''), /\b(?:DROP|TRUNCATE|DELETE|UPDATE|REPLACE)\b/i) }) test('pool permissions are independent of trip details and default only to the platform administrator', () => { const permissions = seedRows(cn, 'iam_permission').filter(row => String(row.permission_code).startsWith('platform:fleet:capacity-offer:')) assert.equal(permissions.length, 5) assert.deepEqual(permissions.map(row => row.permission_code.split(':').at(-1)).sort(), ['inquiry-cancel','inquiry-create','inquiry-edit','list','manage']) const ids = new Set(permissions.map(row => String(row.id))) const grants = seedRows(cn, 'iam_role_permission').filter(row => ids.has(String(row.permission_id))) assert.equal(grants.length, 5) assert.ok(grants.every(row => String(row.role_id) === '4000001' && String(row.tenant_id) === '0')) })