import { describe, expect, it } from 'vitest' import { createMunicipalityProfile, fetchAvailableJurisdictions, fetchMunicipalityProfiles, MunicipalityValidationError, updateMunicipalityProfile, type MunicipalityProfile, } from './municipalityContracts' const makeProfile = (overrides: Partial = {}): MunicipalityProfile => ({ profileId: 'abc123', jCode: 'FAIR01', displayName: 'Fairview Borough', updatedAt: '2026-05-06T12:00:00Z', updatedBy: 'user@test.com', legacyName: 'Fairview Borough', legacyMailingAddress: '100 Main St', legacyCityStateZip: 'Fairview, PA 16415', ...overrides, }) // ── fetchMunicipalityProfiles ───────────────────────────────────────────────── describe('fetchMunicipalityProfiles', () => { it('returns profiles on 200', async () => { const stub = async () => new Response(JSON.stringify([makeProfile()]), { status: 200 }) const result = await fetchMunicipalityProfiles(stub) expect(result).toHaveLength(1) expect(result[0].jCode).toBe('FAIR01') expect(result[0].legacyName).toBe('Fairview Borough') }) it('throws on non-200', async () => { const stub = async () => new Response('{}', { status: 500 }) await expect(fetchMunicipalityProfiles(stub)).rejects.toThrow('500') }) }) // ── createMunicipalityProfile ───────────────────────────────────────────────── describe('createMunicipalityProfile', () => { it('returns profile on 200', async () => { const stub = async () => new Response(JSON.stringify(makeProfile()), { status: 200 }) const result = await createMunicipalityProfile('FAIR01', 'Fairview', stub) expect(result.profileId).toBe('abc123') expect(result.jCode).toBe('FAIR01') }) it('throws MunicipalityValidationError on 422 with descriptive message', async () => { const stub = async () => new Response(JSON.stringify({ error: "No legacy jurisdiction found for JCode 'NOPE'." }), { status: 422, }) await expect(createMunicipalityProfile('NOPE', null, stub)).rejects.toSatisfy( (e) => e instanceof MunicipalityValidationError && e.message.includes('NOPE'), ) }) it('throws generic Error on other non-200 status', async () => { const stub = async () => new Response('{}', { status: 500 }) await expect(createMunicipalityProfile('FAIR01', null, stub)).rejects.toThrow('500') await expect(createMunicipalityProfile('FAIR01', null, stub)).rejects.not.toSatisfy( (e) => e instanceof MunicipalityValidationError, ) }) }) // ── updateMunicipalityProfile ───────────────────────────────────────────────── describe('updateMunicipalityProfile', () => { it('returns updated profile on 200', async () => { const stub = async () => new Response(JSON.stringify(makeProfile({ displayName: 'New Name' })), { status: 200 }) const result = await updateMunicipalityProfile('abc123', 'New Name', stub) expect(result.displayName).toBe('New Name') }) it('throws MunicipalityValidationError on 422', async () => { const stub = async () => new Response(JSON.stringify({ error: 'Profile not found.' }), { status: 422 }) await expect(updateMunicipalityProfile('ghost', 'X', stub)).rejects.toSatisfy( (e) => e instanceof MunicipalityValidationError, ) }) }) // ── fetchAvailableJurisdictions ─────────────────────────────────────────────── describe('fetchAvailableJurisdictions', () => { it('returns jurisdictions on 200', async () => { const stub = async () => new Response( JSON.stringify([ { jCode: 'FAIR01', name: 'Fairview Borough' }, { jCode: 'LAKE02', name: null }, ]), { status: 200 }, ) const result = await fetchAvailableJurisdictions(stub) expect(result).toHaveLength(2) expect(result[0].jCode).toBe('FAIR01') expect(result[0].name).toBe('Fairview Borough') expect(result[1].name).toBeNull() }) it('throws on non-200', async () => { const stub = async () => new Response('{}', { status: 503 }) await expect(fetchAvailableJurisdictions(stub)).rejects.toThrow('503') }) }) // ── MunicipalityValidationError ─────────────────────────────────────────────── describe('MunicipalityValidationError', () => { it('has correct name and message', () => { const err = new MunicipalityValidationError('JCode not found') expect(err.name).toBe('MunicipalityValidationError') expect(err.message).toBe('JCode not found') expect(err).toBeInstanceOf(Error) }) })