these too

This commit is contained in:
Andras Schmelczer 2026-05-04 16:19:15 +01:00
parent d4dde21ad2
commit 90c47afe17
11 changed files with 1045 additions and 0 deletions

View file

@ -0,0 +1,67 @@
import { describe, expect, it } from 'vitest';
import type { FeatureMeta } from '../types';
import { apiUrl, assertOk, buildFilterString, isAbortError } from './api';
describe('api utilities', () => {
it('builds API URLs from endpoint names, paths, and params', () => {
expect(apiUrl('features')).toBe('/api/features');
expect(apiUrl('/custom/path')).toBe('/custom/path');
expect(apiUrl('hexagons', new URLSearchParams({ bounds: '1,2,3,4' }))).toBe(
'/api/hexagons?bounds=1%2C2%2C3%2C4'
);
});
it('throws helpful errors for non-OK responses', () => {
expect(() => assertOk(new Response(null, { status: 204 }), 'empty')).not.toThrow();
expect(() =>
assertOk(new Response(null, { status: 404, statusText: 'Not Found' }), 'lookup')
).toThrow('lookup: HTTP 404 Not Found');
});
it('recognizes AbortError instances', () => {
const abort = new Error('Aborted');
abort.name = 'AbortError';
const regular = new Error('nope');
expect(isAbortError(abort)).toBe(true);
expect(isAbortError(regular)).toBe(false);
});
it('serializes numeric, absolute, and enum filters for backend routes', () => {
const features: FeatureMeta[] = [
{ name: 'Last known price', type: 'numeric', min: 0, max: 1_000_000 },
{
name: 'Estimated current price',
type: 'numeric',
absolute: true,
histogram: { min: 0, max: 2_000_000, p1: 0, p99: 2_000_000, counts: [1] },
},
{ name: 'Property type', type: 'enum', values: ['Flat', 'House'] },
];
expect(
buildFilterString(
{
'Last known price': [100_000, 500_000],
'Estimated current price': [0, 2_000_000],
'Property type': ['Flat', 'House'],
},
features
)
).toBe(
'Last known price:100000:500000;;Estimated current price:0:inf;;Property type:Flat|House'
);
expect(
buildFilterString(
{
'Last known price': [100_000, 500_000],
'Property type': ['Flat'],
},
features,
'Last known price'
)
).toBe('Property type:Flat');
});
});