import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import test from 'node:test' const storeSource = readFileSync(new URL('./firstLegOrders.ts', import.meta.url), 'utf8') const apiSource = readFileSync(new URL('../api/firstLeg.ts', import.meta.url), 'utf8') test('declares the documented merchant warehouse and first-leg endpoints', () => { assert.match(apiSource, /url: '\/merchant\/v1\/warehouses'/) assert.match(apiSource, /url: '\/merchant\/v1\/orders\/first-leg'/) assert.match(apiSource, /`\/merchant\/v1\/orders\/first-leg\/\$\{id\}`/) assert.match(apiSource, /`\/merchant\/v1\/orders\/first-leg\/\$\{id\}\/submit`/) assert.match(apiSource, /`\/merchant\/v1\/warehouses\/\$\{id\}`/) }) test('maps transport and status fields instead of treating container mode as transport mode', () => { assert.match(storeSource, /order\.transportMode === 'SEA'.*order\.containerMode === 'FULL_CONTAINER' \? 'FCL' : 'LCL'/s) assert.match(storeSource, /draft\.transportMode === 'ROAD'\) return 'TRUCK'/) assert.match(storeSource, /status === 'SUBMITTED'\) return 'PENDING_REVIEW'/) assert.match(storeSource, /status === 'REJECTED'\) return 'PENDING_REVIEW'/) assert.match(storeSource, /status === 'PROCESSING'\) return 'IN_TRANSIT'/) }) test('uses real API data without falling back to demo warehouses or orders', () => { assert.match(storeSource, /orders\.value = \[\]/) assert.match(storeSource, /warehouses\.value = \[\]/) assert.match(storeSource, /Promise\.all\(\[getMerchantFirstLegOrders\(\), getMerchantWarehouses\(\)\]\)/) assert.doesNotMatch(storeSource, /DEMO_PLATFORM_RECEIVING_WAREHOUSES|initialOrders/) }) test('sends backend-required channel, customs, destination and cargo fields', () => { for (const field of ['pickupMethod', 'channelCode', 'channelName', 'taxMode', 'customsInspection', 'destinationType', 'packageType']) { assert.match(storeSource, new RegExp(field)) } assert.match(storeSource, /containerMode: draft\.transportMode === 'FCL' \? 'FULL_CONTAINER'/) assert.match(storeSource, /customerId: draft\.shipperPartyId/) }) test('does not expose the out-of-scope cancellation write', () => { assert.doesNotMatch(storeSource, /cancelMerchant|function cancel|\/cancel/) assert.doesNotMatch(apiSource, /\/cancel/) }) test('sends an idempotency key for create and submit writes', () => { assert.match(apiSource, /'Idempotency-Key': createIdempotencyKey\('first-leg-create'\)/) assert.match(apiSource, /'Idempotency-Key': createIdempotencyKey\(`first-leg-submit-\$\{id\}`\)/) }) test('does not expose the contradictory backend rejected state in the client model', () => { assert.match(storeSource, /status === 'REJECTED'\) return 'PENDING_REVIEW'/) assert.match(storeSource, /item\.toStatus === 'SUBMITTED' \|\| item\.toStatus === 'REJECTED' \? 'SUBMITTED'/) }) test('maps backend warehouse snapshots and fulfillment courier fields', () => { assert.match(storeSource, /order\.receivingWarehouseAddress/) assert.match(storeSource, /order\.expressCompany \?\? ''/) assert.match(storeSource, /order\.expressTrackingNo \?\? ''/) assert.match(storeSource, /getMerchantWarehouseDetail\(id\)/) assert.match(storeSource, /receivingCapability\.cutoffTime/) }) test('keeps failed order loads distinct from empty results', () => { assert.match(storeSource, /const loadError = ref\(''\)/) assert.match(storeSource, /loadError\.value = error instanceof Error/) assert.match(storeSource, /return \{ orders, warehouses, loading, loadedFromApi, loadError/) })