mirror of
https://github.com/olehomelchenko/astrolabe.git
synced 2026-08-08 02:02:33 +00:00
54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { detectFormat, detectFormatFromUrl } from './format-detection';
|
|
|
|
describe('detectFormat', () => {
|
|
it('detects a JSON array of objects with high confidence', () => {
|
|
expect(detectFormat('[{"a":1},{"a":2}]')).toEqual({ format: 'json', confidence: 'high' });
|
|
});
|
|
|
|
it('detects a single JSON object as json', () => {
|
|
expect(detectFormat('{"a":1}')).toEqual({ format: 'json', confidence: 'high' });
|
|
});
|
|
|
|
it('detects a TopoJSON topology object', () => {
|
|
const topo = JSON.stringify({ type: 'Topology', objects: {}, arcs: [] });
|
|
expect(detectFormat(topo)).toEqual({ format: 'topojson', confidence: 'high' });
|
|
});
|
|
|
|
it('detects CSV from a comma-separated header + row with medium confidence', () => {
|
|
expect(detectFormat('a,b,c\n1,2,3')).toEqual({ format: 'csv', confidence: 'medium' });
|
|
});
|
|
|
|
it('detects TSV from a tab-separated header + row with medium confidence', () => {
|
|
expect(detectFormat('a\tb\tc\n1\t2\t3')).toEqual({ format: 'tsv', confidence: 'medium' });
|
|
});
|
|
|
|
it('prefers TSV over CSV when both delimiters appear in the header', () => {
|
|
expect(detectFormat('a\tb,c\n1\t2,3').format).toBe('tsv');
|
|
});
|
|
|
|
it('returns low confidence / null for unrecognized input', () => {
|
|
expect(detectFormat('just a sentence')).toEqual({ format: null, confidence: 'low' });
|
|
});
|
|
|
|
it('returns low confidence / null for empty input', () => {
|
|
expect(detectFormat(' ')).toEqual({ format: null, confidence: 'low' });
|
|
});
|
|
});
|
|
|
|
describe('detectFormatFromUrl', () => {
|
|
it.each([
|
|
['https://example.com/data.csv', 'csv'],
|
|
['https://example.com/data.tsv', 'tsv'],
|
|
['https://example.com/data.json', 'json'],
|
|
['https://example.com/world.topojson', 'topojson'],
|
|
['https://example.com/data.csv?v=2', 'csv'],
|
|
])('infers format from %s', (url, expected) => {
|
|
expect(detectFormatFromUrl(url)).toBe(expected);
|
|
});
|
|
|
|
it('returns null when no known extension is present', () => {
|
|
expect(detectFormatFromUrl('https://example.com/data')).toBeNull();
|
|
});
|
|
});
|