import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { formatE911Address, formatSuite } from '../src/integrations/google.js'; // Minimal shape of the Google Address Validation response we care about. function makeResponse(components) { return { result: { address: { addressComponents: components } } }; } const fullAddress = makeResponse([ { componentType: 'street_number', componentName: { text: '77' } }, { componentType: 'route', componentName: { text: 'Willowbrook Rd' } }, { componentType: 'subpremise', componentName: { text: 'Ste 2091' } }, { componentType: 'locality', componentName: { text: 'Wayne' } }, { componentType: 'administrative_area_level_1', componentName: { text: 'NJ' } }, { componentType: 'postal_code', componentName: { text: '07470' } }, { componentType: 'country', componentName: { text: 'United States' } }, ]); describe('formatE911Address', () => { it('flattens the address components into an E911-shaped string', () => { assert.equal(formatE911Address(fullAddress), '77 Willowbrook Rd, Wayne, NJ 07470'); }); it('tolerates missing components without crashing', () => { const partial = makeResponse([ { componentType: 'street_number', componentName: { text: '10' } }, { componentType: 'route', componentName: { text: 'Main St' } }, { componentType: 'locality', componentName: { text: 'Anywhere' } }, ]); // Missing state/zip render as empty slots; the join stays intact. assert.equal(formatE911Address(partial), '10 Main St, Anywhere,'); }); it('handles an empty component list', () => { // The template renders three comma-separated sections; with every // field empty they collapse to just the separators after trim. assert.equal(formatE911Address(makeResponse([])), ', ,'); }); }); describe('formatSuite', () => { it('returns the subpremise text when present', () => { assert.equal(formatSuite(fullAddress), 'Ste 2091'); }); it('returns empty string when there is no subpremise', () => { const noSuite = makeResponse([ { componentType: 'street_number', componentName: { text: '77' } }, { componentType: 'route', componentName: { text: 'Willowbrook Rd' } }, ]); assert.equal(formatSuite(noSuite), ''); }); });