Spaces:
Restarting
Restarting
File size: 8,917 Bytes
3fee36c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | #!/usr/bin/env node
/**
* Cross-layer slugify test β EuroMeteo
*
* Verifies that the R (eurometeo_slugify) and JS (eurometeoSlugify)
* implementations produce identical slugs for a comprehensive set of
* station and country names covering all transliteration edge cases.
*
* Usage:
* node scripts/test-slugify-cross-layer.mjs
*
* Requirements:
* - Rscript in PATH
* - Run from meteogate/ root
*/
import { eurometeoSlugify } from './lib/eurometeo-slugify.mjs';
import { execSync } from 'child_process';
import { writeFileSync, unlinkSync } from 'fs';
import { join } from 'path';
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Test cases: [input, expected_slug]
// Covers all transliteration table entries + edge cases
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const TEST_CASES = [
// --- Basic ---
['OSLO-BLINDERN', 'oslo-blindern'],
['oslo-blindern', 'oslo-blindern'],
['Berlin', 'berlin'],
// --- German umlauts (ΓΌβue, ΓΆβoe, Γ€βae, Γβss) ---
['MΓNCHEN', 'muenchen'],
['MUENCHEN-STADT', 'muenchen-stadt'], // already transliterated
['DΓΌsseldorf', 'duesseldorf'],
['GROΓ-GERAU', 'gross-gerau'],
['Γsterreich', 'oesterreich'],
['GΓ€ufelden', 'gaeufelden'], // Γ€βae explicit: G+ae+ufelden
// --- Norwegian/Danish (ΓΈβo, Γ¦βae) ---
['OSLO - ΓSTENSJΓ', 'oslo-ostensjo'],
['KVITΓYA', 'kvitoya'],
['EDGEΓYA - KAPP HEUGLIN', 'edgeoya-kapp-heuglin'],
['ANDΓYA', 'andoya'],
['ΓRΓSKΓBING', 'aeroskobing'],
// --- Icelandic (ΓΎβth, Γ°βd) ---
['ΓΓ³rshΓΆfn', 'thorshoefn'], // ΓΎβth + ΓΆβoe explicit
['Akureyri', 'akureyri'],
['ReykjavΓk', 'reykjavik'], // Γβi via NFD
// --- Polish (Εβl) ---
['ΕΓ³dΕΊ', 'lodz'], // Εβl + Γ³βo via NFD + ΕΊβz via NFD
['WrocΕaw', 'wroclaw'],
// --- Turkish (Δ±βi) ---
['Istanbul', 'istanbul'],
['GazΔ±antep', 'gaziantep'], // Δ±βi (dotless i)
// --- Swedish ---
['BJΓRNEBORG', 'bjoerneborg'], // ΓΆβoe explicit table
['UmeΓ₯', 'umea'], // Γ₯βa via NFD
// --- French accents (via NFD) ---
['SAINT-CLEMENT-DES-BA', 'saint-clement-des-ba'],
['BΓ©ziers', 'beziers'],
['Γle-de-France', 'ile-de-france'],
// --- Hungarian (via NFD) ---
['Budapest LΓ‘gymΓ‘nyos', 'budapest-lagymanyos'],
// --- Spacing/special chars ---
['SAARBRUECKEN-BURBACH (SECONDARY)', 'saarbruecken-burbach-secondary'],
['RV36 SELJORD', 'rv36-seljord'],
['ST_FLORENT_VIEI', 'st-florent-viei'], // underscore β hyphen
[' leading trailing ', 'leading-trailing'],
// --- Country names ---
['Norway', 'norway'],
['United Kingdom', 'united-kingdom'],
['Czechia', 'czechia'],
['Bosnia and Herzegovina', 'bosnia-and-herzegovina'],
['Falkland Islands (Malvinas)', 'falkland-islands-malvinas'],
['South Georgia and the South Sandwich Islands', 'south-georgia-and-the-south-sandwich-islands'],
// --- Empty/null ---
['', ''],
[null, ''],
[undefined, ''],
];
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Run JS tests
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
console.log('βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
console.log('β EuroMeteo Slugify β Cross-Layer Test β');
console.log('βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n');
let jsPass = 0;
let jsFail = 0;
const jsResults = {};
for (const [input, expected] of TEST_CASES) {
const actual = eurometeoSlugify(input);
jsResults[input ?? 'null'] = actual;
if (actual === expected) {
jsPass++;
} else {
jsFail++;
console.log(` β JS FAIL: "${input}" β "${actual}" (expected "${expected}")`);
}
}
console.log(`JS: ${jsPass} passed, ${jsFail} failed out of ${TEST_CASES.length}\n`);
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Run R tests via Rscript
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Build an R script that sources slugify.R and tests the same inputs
// Write test inputs to a temp file to avoid shell encoding issues
const rTestInputs = TEST_CASES
.filter(([input]) => input != null && input !== undefined && input !== '')
.map(([input]) => input);
const inputFile = join(process.cwd(), 'scripts', '_test_slugify_inputs.txt');
writeFileSync(inputFile, rTestInputs.join('\n'), 'utf-8');
const rScript = `
source("funs/slugify.R")
inputs <- readLines("scripts/_test_slugify_inputs.txt", encoding = "UTF-8")
cat("R_RESULTS_START\\n")
for (inp in inputs) {
slug <- eurometeo_slugify(inp)
cat(paste0(inp, "\\t", slug, "\\n"))
}
cat("R_RESULTS_END\\n")
`;
const rScriptFile = join(process.cwd(), 'scripts', '_test_slugify.R');
writeFileSync(rScriptFile, rScript, 'utf-8');
let rOutput;
try {
rOutput = execSync('Rscript scripts/_test_slugify.R', {
cwd: process.cwd(),
encoding: 'utf-8',
timeout: 30000,
env: { ...process.env, LC_ALL: 'en_US.UTF-8' },
});
} catch (e) {
console.error(' β Failed to run Rscript:', e.message);
process.exit(1);
} finally {
try { unlinkSync(inputFile); unlinkSync(rScriptFile); } catch(e) {}
}
// Parse R results
const rLines = rOutput.split('\n');
const startIdx = rLines.indexOf('R_RESULTS_START');
const endIdx = rLines.indexOf('R_RESULTS_END');
if (startIdx === -1 || endIdx === -1) {
console.error(' β Could not parse R output');
console.error(rOutput);
process.exit(1);
}
const rResults = {};
for (let i = startIdx + 1; i < endIdx; i++) {
const parts = rLines[i].split('\t');
if (parts.length === 2) {
rResults[parts[0]] = parts[1];
}
}
let rPass = 0;
let rFail = 0;
for (const [input, expected] of TEST_CASES) {
if (input == null || input === undefined || input === '') continue;
const rSlug = rResults[input];
if (rSlug === expected) {
rPass++;
} else {
rFail++;
console.log(` β R FAIL: "${input}" β "${rSlug}" (expected "${expected}")`);
}
}
console.log(`R: ${rPass} passed, ${rFail} failed out of ${rTestInputs.length} (excl null/undefined/empty)\n`);
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Cross-layer comparison: R vs JS
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
let crossPass = 0;
let crossFail = 0;
for (const [input] of TEST_CASES) {
if (input == null || input === undefined || input === '') continue;
const jsSlug = jsResults[input];
const rSlug = rResults[input];
if (jsSlug === rSlug) {
crossPass++;
} else {
crossFail++;
console.log(` β MISMATCH: "${input}"\n JS: "${jsSlug}"\n R: "${rSlug}"`);
}
}
console.log(`Cross-layer (RβJS): ${crossPass} matched, ${crossFail} mismatched\n`);
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Summary
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const totalFail = jsFail + rFail + crossFail;
if (totalFail > 0) {
console.log(`\nβ ${totalFail} total failures`);
process.exit(1);
} else {
console.log('β
All tests passed β R and JS produce identical slugs');
process.exit(0);
}
|