Files
LivingDexTracker/tests/support/csv.ts
T
Josh Creek de39dc78ea test: replace ad-hoc tests with a layered suite and CI workflow
Splits testing into five layers so a failure points at the responsible one:

- tests/unit    isolated utility, repository and service tests
- tests/data    validates the tracked Pokémon, game, region and dex files
- tests/integration  schema, views, constraints, RLS and repositories
- tests/bdd     executable Gherkin for user-visible behaviour
- tests/build   service worker and manifest artifacts per build variant

Replaces the two Playwright specs in client-test/ and the two Vitest files in
test/. Adds a GitHub Actions workflow running the layers as separate jobs, a
mock OAuth provider server so the Drive and Dropbox scenarios never touch real
accounts, and a wrapper that reads the local Supabase keys from
`supabase status` rather than hard-coding them.

Extracts the pure formatting helpers out of PokedexExportService so they can be
unit tested, and makes the provider endpoints configurable so the mock server
can stand in for Google and Dropbox.
2026-09-13 17:38:41 +01:00

62 lines
1.7 KiB
TypeScript

import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
export type CsvRow = Record<string, string>;
export function parseCsv(text: string): CsvRow[] {
const rows: string[][] = [];
let row: string[] = [];
let cell = '';
let quoted = false;
for (let index = 0; index < text.length; index++) {
const char = text[index];
if (char === '"') {
if (quoted && text[index + 1] === '"') {
cell += '"';
index++;
} else {
quoted = !quoted;
}
} else if (char === ',' && !quoted) {
row.push(cell);
cell = '';
} else if ((char === '\n' || char === '\r') && !quoted) {
if (char === '\r' && text[index + 1] === '\n') index++;
row.push(cell);
if (row.some((value) => value.length > 0)) rows.push(row);
row = [];
cell = '';
} else {
cell += char;
}
}
if (quoted) throw new Error('Malformed CSV: unclosed quoted field');
if (cell.length > 0 || row.length > 0) {
row.push(cell);
if (row.some((value) => value.length > 0)) rows.push(row);
}
const [headers, ...records] = rows;
if (!headers) return [];
return records.map((record, rowIndex) => {
if (record.length !== headers.length) {
throw new Error(
`Malformed CSV row ${rowIndex + 2}: expected ${headers.length} columns, received ${record.length}`
);
}
return Object.fromEntries(
headers.map((header, index) => [header.trim(), record[index].trim()])
);
});
}
export function readRepoCsv(relativePath: string): CsvRow[] {
return parseCsv(readFileSync(resolve(process.cwd(), relativePath), 'utf8'));
}
export function normalizedIdentity(...parts: Array<string | null | undefined>): string {
return parts.map((part) => (part ?? '').trim().toLocaleLowerCase('en-GB')).join('|');
}