import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import test from 'node:test' import { PRODUCT_CATALOG, buildOrderPayload, diversifyRoutes, parseArguments, validateCount } from './create-pending-review-orders.mjs' const source = readFileSync(new URL('./create-pending-review-orders.mjs', import.meta.url), 'utf8') test('uses the fixed merchant account and stops at pending review', () => { assert.ok(source.includes("USERNAME: '17512095155'"), 'fixed username is missing') assert.ok(/PASSWORD:\s*'[^']*'/.test(source), 'CONFIG.PASSWORD is missing') assert.ok(/COUNT:\s*\d+/.test(source), 'CONFIG.COUNT is missing') assert.match(source, /COUNT:\s*50/) assert.match(source, /randomInt\(1, 31\)/) assert.match(source, /loadPricedRoutes/) assert.match(source, /destinationWarehouseCode/) assert.match(source, /channelCode/) assert.match(source, /pricingTaxMode/) assert.equal(PRODUCT_CATALOG.length, 8) assert.match(source, /\/api\/merchant\/v1\/orders\/first-leg\/\$\{encodeURIComponent\(orderId\)\}\/submit/) assert.match(source, /detail\?\.order\?\.status !== 'SUBMITTED'/) assert.match(source, /\/api\/merchant\/v1\/orders\/\$\{encodeURIComponent\(orderId\)\}/) assert.match(source, /unifiedOrder\?\.summary\?\.mainStatus !== 'PENDING_REVIEW'/) assert.match(source, /mainStatus: unifiedOrder\.summary\.mainStatus/) assert.doesNotMatch(source, /partners\/candidates\?type=SHIPPER/) assert.doesNotMatch(source, /\/api\/platform\/v1\/iam\/tenants/) assert.doesNotMatch(source, /\/approve/) }) test('supports a bounded fixed order count', () => { assert.equal(validateCount('1'), 1) assert.equal(validateCount('10'), 10) assert.throws(() => validateCount('0'), /between 1 and 100/) assert.throws(() => validateCount('101'), /between 1 and 100/) assert.deepEqual(parseArguments(['--count', '5', '--gateway=http://localhost:9000']), { count: '5', gateway: 'http://localhost:9000', }) }) test('builds an order payload owned by the fixed profile', () => { const payload = buildOrderPayload({ marker: 'PENDING-TEST', index: 2, profile: { id: '44', tenantName: '固定测试客户' }, master: { receiving: { id: '11', warehouseName: '国内仓' }, destination: { id: '22', warehouseName: 'ONT8', warehouseCode: 'ONT8', countryCode: 'US' }, channel: { channelCode: 'SEA-US', channelName: '美西海运' }, pricingTaxMode: 'TAX_INCLUDED', }, product: PRODUCT_CATALOG[0], packageCount: 7, productImageFileId: '33', }) assert.equal(payload.receivingWarehouseId, '11') assert.equal(payload.destinationWarehouseId, '22') assert.equal(payload.customerId, '44') assert.equal(payload.customerName, '固定测试客户') assert.equal(payload.cargoes.length, 1) assert.equal(payload.cargoes[0].productImageFileId, '33') assert.equal(payload.cargoes[0].cargoName, PRODUCT_CATALOG[0].cargoName) assert.equal(payload.cargoes[0].packageCount, 7) assert.equal(payload.cargoes[0].totalQuantity, PRODUCT_CATALOG[0].unitsPerPackage * 7) assert.equal(payload.cargoes[0].grossWeightKg, Number((PRODUCT_CATALOG[0].cartonGrossWeightKg * 7).toFixed(3))) assert.equal(payload.cargoes[0].declaredValue, PRODUCT_CATALOG[0].unitDeclaredValue * PRODUCT_CATALOG[0].unitsPerPackage * 7) }) test('interleaves different logistics combinations before repeating one', () => { const route = (receivingId, channelCode, pricingTaxMode, destinationCode) => ({ receiving: { id: receivingId }, channel: { channelCode }, pricingTaxMode, destination: { warehouseCode: destinationCode }, }) const diversified = diversifyRoutes([ route('1', 'SEA-A', 'TAX_INCLUDED', 'A1'), route('1', 'SEA-A', 'TAX_INCLUDED', 'A2'), route('1', 'SEA-B', 'TAX_EXCLUDED', 'B1'), route('1', 'SEA-B', 'TAX_EXCLUDED', 'B2'), ]) const firstKey = `${diversified[0].channel.channelCode}|${diversified[0].pricingTaxMode}` const secondKey = `${diversified[1].channel.channelCode}|${diversified[1].pricingTaxMode}` assert.notEqual(firstKey, secondKey) })