/** * Joining up the per-region partner lists. * * `getpartners` answers one region at a time, and since it began scoping on * `partnerlocations` a partner who covers two cities is returned by both of * those answers. Concatenating them put Xpress-Cbe-Main on the Rider partners * page twice — two identical rows, each with its own Edit and Riders buttons * and no way to tell which was which. * * The overlap is the read being right, so these are about the caller joining * the answers up correctly rather than about the backend sending less. */ import assert from 'node:assert/strict'; import { test } from 'node:test'; import type { Partner } from '@/api/deliveries'; import { dedupePartners } from './partnerList'; const partner = (partnerid: number, over: Partial = {}): Partner => ({ partnerid, partnername: `Partner ${partnerid}`, ...over }) as Partner; test('a partner working two regions is listed once', () => { // The measured case: partner 44 has link rows for regions 1 and 2. const rows = dedupePartners([ [partner(44, { partnername: 'Xpress-Cbe-Main' })], [partner(44, { partnername: 'Xpress-Cbe-Main' })], [], ]); assert.equal(rows.length, 1); assert.equal(rows[0]?.partnerid, 44); }); test('partners in different regions are all kept', () => { const rows = dedupePartners([[partner(44)], [partner(60)], [partner(61)]]); assert.deepEqual( rows.map((r) => r.partnerid), [44, 60, 61], ); }); test('order is stable, so the list does not reshuffle as regions arrive', () => { // The three requests resolve at different speeds and re-render as they land. // A list that reorders under the cursor loses whoever was about to be clicked. const rows = dedupePartners([[partner(7), partner(3)], [partner(9)], [partner(3)]]); assert.deepEqual( rows.map((r) => r.partnerid), [7, 3, 9], ); }); test('a region that has not answered yet is not an empty list', () => { // `useQueries` gives `undefined` until a query resolves, and one region still // loading must not blank the partners the others already returned. const rows = dedupePartners([[partner(44)], undefined, []]); assert.equal(rows.length, 1); }); test('nothing anywhere is an empty list, not a crash', () => { assert.deepEqual(dedupePartners([]), []); assert.deepEqual(dedupePartners([undefined, undefined]), []); }); test('a partner with no id is dropped rather than collapsed under one key', () => { // Two of them would otherwise become a single row keyed 0 that is neither, // and no row on this page can be opened, counted or edited without an id. const rows = dedupePartners([ [partner(44), { partnername: 'No id at all' } as Partner], [{ partnerid: 0, partnername: 'Zero id' } as Partner], ]); assert.deepEqual( rows.map((r) => r.partnerid), [44], ); });