ai-election-manipulation-cases / scripts /validate-viewer.mjs
Codex
Fix Hugging Face Viewer configs in v0.4.2
ef7dd2d
Raw
History Blame Contribute Delete
3.06 kB
#!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import parquet from "parquetjs-lite";
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const expected = new Map([
["record_index", 1087],
["case_catalog", 64],
["research_view", 10],
["claim_evidence", 124]
]);
const targets = new Map([
["record_index", "record_index.csv"],
["case_catalog", "data/case_catalog.csv"],
["research_view", "data/research_view.csv"],
["claim_evidence", "data/claim_evidence.csv"]
]);
async function countRows(relativePath) {
if (relativePath.endsWith(".csv")) {
const input = fs.readFileSync(path.join(root, relativePath), "utf8").replace(/^\uFEFF/, "");
let rows = 0;
let quoted = false;
for (let index = 0; index < input.length; index += 1) {
if (input[index] === '"' && quoted && input[index + 1] === '"') index += 1;
else if (input[index] === '"') quoted = !quoted;
else if (input[index] === "\n" && !quoted) rows += 1;
}
if (quoted) throw new Error(`${relativePath}: unclosed quoted field`);
return Math.max(0, rows - 1);
}
const reader = await parquet.ParquetReader.openFile(path.join(root, relativePath));
const cursor = reader.getCursor();
let count = 0;
try {
while (await cursor.next()) count += 1;
} finally {
await reader.close();
}
return count;
}
const readme = fs.readFileSync(path.join(root, "README.md"), "utf8");
const frontmatter = readme.match(/^---\n([\s\S]*?)\n---/)?.[1] || "";
const expectedFrontmatter = new Map([
["record_index", " - config_name: record_index\n default: true\n data_files: [{split: data, path: record_index.csv}]"],
["case_catalog", " - config_name: case_catalog\n data_files: [{split: data, path: data/case_catalog.csv}]"],
["research_view", " - config_name: research_view\n data_files: [{split: orientation_only, path: data/research_view.csv}]"],
["claim_evidence", " - config_name: claim_evidence\n data_files: [{split: data, path: data/claim_evidence.csv}]"],
]);
for (const [config, rows] of expected) {
if (!frontmatter.includes(expectedFrontmatter.get(config))) throw new Error(`README has an invalid ${config} configuration`);
const actual = await countRows(targets.get(config));
if (actual !== rows) throw new Error(`${config}: expected ${rows} rows; found ${actual}`);
}
const reader = await parquet.ParquetReader.openFile(path.join(root, "record_index.parquet"));
const cursor = reader.getCursor();
const tableCounts = new Map();
try {
let row;
while ((row = await cursor.next())) {
JSON.parse(row.record_json);
tableCounts.set(row.table_name, (tableCounts.get(row.table_name) || 0) + 1);
}
} finally {
await reader.close();
}
if (tableCounts.size !== 26) throw new Error(`record_index: expected 26 tables; found ${tableCounts.size}`);
process.stdout.write(`${JSON.stringify({ status: "pass", configurations: Object.fromEntries(expected), indexed_tables: tableCounts.size })}\n`);