Add dataset library, extract-to-dataset, and render-time reference resolution

This commit is contained in:
2026-06-05 15:49:40 +03:00
parent 25849461e0
commit a4e4d96d3b
41 changed files with 3909 additions and 19 deletions
+55
View File
@@ -0,0 +1,55 @@
import { describe, expect, test } from 'vitest';
import { profileData } from './profile';
describe('profileData', () => {
test('profiles a JSON-array dataset fully', () => {
const rows = [
{ city: 'Kyiv', pop: 2900000, capital: 'true' },
{ city: 'Lviv', pop: 720000, capital: 'false' },
];
const profile = profileData(rows, 123);
expect(profile.rowCount).toBe(2);
expect(profile.columnCount).toBe(3);
expect(profile.columns).toEqual(['city', 'pop', 'capital']);
expect(profile.columnTypes).toEqual([
{ name: 'city', type: 'string' },
{ name: 'pop', type: 'number' },
{ name: 'capital', type: 'boolean' },
]);
expect(profile.size).toBe(123);
});
test('null rows (URL) return the N/A profile but keep size', () => {
const profile = profileData(null, 42);
expect(profile.rowCount).toBeNull();
expect(profile.columnCount).toBeNull();
expect(profile.columns).toEqual([]);
expect(profile.columnTypes).toEqual([]);
expect(profile.size).toBe(42);
});
test('an empty array returns the N/A profile but keeps size', () => {
const profile = profileData([], 7);
expect(profile.rowCount).toBeNull();
expect(profile.columnCount).toBeNull();
expect(profile.columns).toEqual([]);
expect(profile.size).toBe(7);
});
test('column order follows first-seen key order across ragged rows', () => {
const rows = [{ a: 1 }, { b: 2, a: 3 }, { c: 4 }];
const profile = profileData(rows, 0);
expect(profile.columns).toEqual(['a', 'b', 'c']);
});
test('respects the sampling cap: rows beyond the head do not affect type', () => {
// First 200 rows numeric; row 201 is text. With head-only sampling the
// column still reads as number (an accepted, documented trade-off).
const rows: Array<Record<string, unknown>> = [];
for (let i = 0; i < 200; i++) rows.push({ v: i });
rows.push({ v: 'tail-text' });
const profile = profileData(rows, 0);
expect(profile.rowCount).toBe(201); // count reflects the whole payload
expect(profile.columnTypes).toEqual([{ name: 'v', type: 'number' }]);
});
});