Spaces:
Running
Running
Download scripts/test-slugify-cross-layer.mjs from climateexploer/meteogate: direct link, hf CLI and curl.
- Browser
- Download file 8.92 kB
-
https://huggingface.co/spaces/climateexploer/meteogate/resolve/main/scripts/test-slugify-cross-layer.mjs
- Command line
-
hf download hf://spaces/climateexploer/meteogate/scripts/test-slugify-cross-layer.mjs
-
curl -L -o test-slugify-cross-layer.mjs https://huggingface.co/spaces/climateexploer/meteogate/resolve/main/scripts/test-slugify-cross-layer.mjs
8.92 kB
| /** | |
| * 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); | |
| } | |