import assert from 'node:assert/strict' import test from 'node:test' import type { PlanQuota, QuotaSourceOption } from '../src/api/pl/quota-cert' import { initialLegacySelection, measuredSources } from '../src/components/business/pl-plan-quota-dialog/stop-model' function source(partial: Partial & Pick): QuotaSourceOption { return { productName: '硫酸', quantity: '1', unit: '吨', contractId: '', contractProductId: '', ...partial } } function plan(partial: Partial): PlanQuota { return { planId: 'plan', planNo: 'PO', configuration: {}, sourceOptions: [], usages: [], ...partial } } test('legacy plan restores the saved measuring side and the opposite pairing', () => { const value = plan({ measurementVersion: 'legacy_single_side', sourceOptions: [ source({ sourceStopType: 'load', sourceStopProductId: 'L', planProductId: 'P', partyName: '装货方' }), source({ sourceStopType: 'delivery', sourceStopProductId: 'D', planProductId: 'P', partyName: '送货方' }) ], configuration: { sourceMapping: [ { sourceStopType: 'load', sourceStopProductId: 'L', relatedStopProductIds: ['D'] } ] } }) const selection = initialLegacySelection(value) assert.equal(selection.sides.P, 'load') assert.deepEqual(selection.pairs.L, ['D']) assert.deepEqual( measuredSources(value, selection.sides).map((row) => row.sourceStopProductId), ['L'] ) }) test('legacy plan with both sides and no saved mapping waits for a choice', () => { const value = plan({ sourceOptions: [ source({ sourceStopType: 'load', sourceStopProductId: 'L', planProductId: 'P' }), source({ sourceStopType: 'delivery', sourceStopProductId: 'D', planProductId: 'P' }) ] }) const selection = initialLegacySelection(value) assert.equal(selection.sides.P, '') assert.deepEqual(measuredSources(value, selection.sides), []) }) test('a single delivery side is selected, and per-stop plans keep every side', () => { const legacy = plan({ sourceOptions: [source({ sourceStopType: 'delivery', sourceStopProductId: 'D', planProductId: 'P' })] }) assert.equal(initialLegacySelection(legacy).sides.P, 'delivery') const current = plan({ measurementVersion: 'per_stop_v2', sourceOptions: [ source({ sourceStopType: 'load', sourceStopProductId: 'L', planProductId: 'P' }), source({ sourceStopType: 'delivery', sourceStopProductId: 'D', planProductId: 'P' }) ] }) assert.deepEqual( measuredSources(current, {}).map((row) => row.sourceStopProductId), ['L', 'D'] ) })