import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import test from 'node:test' import { adminPreservesOrderNumber, IMMUTABLE_TAILLEG_EVENT_FIXTURE, TAILLEG_ORDER_NUMBER, validContainerOrderGroup, validEventReplay, validOwnerSnapshots, } from './tailleg-owner-order-number-contract.mjs' const read = (relativeUrl) => readFileSync(new URL(relativeUrl, import.meta.url), 'utf8') const eventV2 = read('../../common-event-contract/src/main/java/com/baiyue/common/event/FirstLegLoadingPlanCompletedV2.java') const producer = read('../../firstleg/src/main/java/com/baiyue/firstleg/service/LoadingCompletedOutboxService.java') const producerRows = read('../../firstleg/src/main/java/com/baiyue/firstleg/mapper/LoadingCompletedEventMapper.java') const consumer = read('../../lastleg-service/src/main/java/com/baiyue/lastleg/service/LoadingCompletedConsumer.java') const orderEntity = read('../../lastleg-service/src/main/java/com/baiyue/lastleg/entity/LastlegOrder.java') const orderService = read('../../lastleg-service/src/main/java/com/baiyue/lastleg/service/LastlegOrderService.java') const generator = read('../../lastleg-service/src/main/java/com/baiyue/lastleg/service/LastlegOrderNoGenerator.java') const generatorTests = read('../../lastleg-service/src/test/java/com/baiyue/lastleg/service/LastlegOrderNoGeneratorTest.java') const consumerTests = read('../../lastleg-service/src/test/java/com/baiyue/lastleg/service/LoadingCompletedConsumerTest.java') const adminApi = read('../../../apps/admin-web/src/modules/overseas-warehouse/api/lastMileOrders.ts') const adminPresentation = read('../../../apps/admin-web/src/modules/overseas-warehouse/lastMilePresentation.ts') const adminList = read('../../../apps/admin-web/src/modules/overseas-warehouse/pages/LastMileOrdersView.vue') const adminDetail = read('../../../apps/admin-web/src/modules/overseas-warehouse/pages/LastMileOrderDetailView.vue') test('new 15-order event requires one non-empty trusted owner snapshot', () => { const orders = Array.from({ length: 15 }, (_, index) => ({ sourceInboundId: `inbound-${index + 1}`, customerId: 'customer-1', customerName: '验收货主', })) assert.equal(validOwnerSnapshots(orders, 15), true) assert.equal(validOwnerSnapshots(orders.map((order, index) => index === 7 ? { ...order, customerName: '' } : order), 15), false) assert.equal(validOwnerSnapshots(orders.map((order, index) => index === 7 ? { ...order, customerName: '另一货主' } : order), 15), false) }) test('the processed historical event is immutable and cannot be reused for acceptance', () => { assert.deepEqual(IMMUTABLE_TAILLEG_EVENT_FIXTURE, { eventId: 'b569c7f6-3493-4a0f-a944-dd42698f183b', status: 'PROCESSED', payloadMutable: false, reusableForAcceptance: false, }) }) test('V2 producer freezes customerId and customerName and rejects a blank name', () => { assert.match(eventV2, /List items,\s*String customerId,\s*String customerName,\s*String orderType/) assert.match(producerRows, /b\.customer_name/) assert.match(producer, /first\.getCustomerName\(\)/) assert.match(producer, /(?:customerName|getCustomerName)[\s\S]{0,300}(?:isBlank|hasText)/) }) test('Lastleg rejects a missing owner snapshot, persists it, and never calls IAM to repair it', () => { assert.match(consumer, /source\.customerName\(\)/) assert.match(consumer, /customerName[\s\S]{0,240}(?:isBlank|hasText)/) assert.match(consumer, /order\.setCustomerName\(customerName\)/) assert.match(orderEntity, /private String customerName;/) assert.doesNotMatch(consumer, /(?:Iam|IAM|\/iam|CustomerClient|customer.*client)/) }) test('Lastleg query DTO and Admin list/detail use the frozen owner name', () => { assert.match(orderService, /o\.getCustomerName\(\)/) assert.match(adminApi, /customerName\?: string \| null/) assert.match(adminPresentation, /owner:\s*text\(dto\.customerName\)/) assert.match(adminDetail, /label:\s*'货主',\s*value:\s*text\(order\?\.customerName\)/) }) test('new tailleg number is Shanghai daily LMO-yyMMdd-#### with concurrency and cross-day tests', () => { assert.equal(TAILLEG_ORDER_NUMBER.pattern.test('LMO-260827-0001'), true) assert.equal(TAILLEG_ORDER_NUMBER.pattern.test('LMO2087379674555461633'), false) assert.equal(TAILLEG_ORDER_NUMBER.timeZone, 'Asia/Shanghai') assert.match(generator, /Asia\/Shanghai/) assert.match(generator, /yyMMdd/) assert.match(generator, /LMO-/) assert.match(generator, /%04d|padStart\(4|String\.format/) assert.match(generatorTests, /(?:concurrent|并发)/i) assert.match(generatorTests, /(?:cross.?day|next.?day|跨天)/i) assert.match(generatorTests, /\^LMO-\\d\{6\}-\\d\{4\}\$/) }) test('one container with 15 inbound rows shares one new LMO number', () => { const rows = Array.from({ length: 15 }, (_, index) => ({ containerNo: 'CONT-15', orderNo: 'LMO-260827-0042', sourceInboundId: `inbound-${index + 1}`, })) assert.equal(validContainerOrderGroup(rows), true) assert.equal(validContainerOrderGroup(rows.map((row, index) => index === 14 ? { ...row, orderNo: 'LMO-260827-0043' } : row)), false) assert.match(consumer, /String orderNo\s*=\s*orderNoGenerator\.[^(]+\(payload\.containerNo\(\)\);[\s\S]*for \(int index/) assert.match(consumer, /createOrder\(envelope, payload, source, orderNo, rawSource\)/) }) test('same event replay adds nothing and preserves the allocated number', () => { const before = { orderCount: 15, orderNo: 'LMO-260827-0042', detailCount: 30, historyCount: 15 } assert.equal(validEventReplay(before, { ...before }), true) assert.equal(validEventReplay(before, { ...before, orderCount: 30 }), false) assert.match(consumer, /if\s*\(existing\s*!=\s*null\)[\s\S]{0,400}return countOrders/) assert.match(consumerTests, /(?:replay|duplicate|重复)[\s\S]{0,900}(?:orderNo|OrderNo)/i) }) test('Admin displays and copies the exact queried number without reformatting', () => { const value = 'LMO-260827-0042' assert.equal(adminPreservesOrderNumber(value, value, value), true) assert.equal(adminPreservesOrderNumber(value, value.replaceAll('-', ''), value), false) assert.match(adminPresentation, /orderNo:\s*text\(dto\.orderNo\)/) assert.match(adminList, /prop:\s*'orderNo'/) assert.match(adminDetail, /reference-value="referenceValue"/) const adminOrderSources = `${adminList}\n${adminDetail}` assert.match(adminOrderSources, /(?:clipboard\.writeText|copyText|copyToClipboard)[\s\S]{0,240}(?:orderNo|referenceValue)|(?:orderNo|referenceValue)[\s\S]{0,240}(?:clipboard\.writeText|copyText|copyToClipboard)/) })