Files
kuentagym/frontend/scripts/check-locales.mjs
T
dsantillan 903a435af3
Tests / test (push) Canceled after 0s
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s
Build a routine with AI: the screen the builder was missing
"Add exercise" now has a sibling: it asks which muscles, whether the session
starts with cardio, and how hard — then the Coach fills the routine in. Three
taps instead of picking ten exercises one at a time, and it is the only place in
the app that asks what you want to train today.

Why it exists in this shape:

- The Coach's own intake asks about the *person* — goal, days, equipment, limits
  — and is answered once. Nothing in it asks which muscles, so a three-day plan
  reasonably came back full body; that is what prompted this. The builder asks
  only what changes between sessions and reads the rest from the saved profile,
  so a second routine costs three taps and not a questionnaire.

- It adds a NEW routine rather than filling the one you opened. The questions are
  about a session you are about to build, and replacing the routine somebody
  opened would be the wrong guess. It does not touch the week either: nobody was
  asked about days.

- The muscle buttons are the catalogue's own body-part words ("back", "waist"),
  which the Library and the body map already translate, so a button and the list
  behind it cannot drift apart. "Light" and "Hard" are NOT reused for the effort
  chips: those keys already mean the light theme and hard sets, and a chip reading
  "Claro" for an easy session would be nonsense in Spanish.

The async half: requestPlan only *starts* a job (the server runs it in the
background and the result arrives on a status poll), so `awaitCoachJob` waits for
that job by the id of the last finished one — a proposal left over from an
earlier question can never be mistaken for this answer — and throws with the
job's own error class for jobErrorText to phrase.

Five tests drive the whole thing: the three steps, the exact questions sent (the
muscles as bp/tg selectors with cardio in the library, not only in the sentence),
that what comes back lands as a new routine with the cardios and the modes the app
owns normalised, that a failure is shown in the app's words and can be retried,
and that an answer with no routine is refused rather than added empty.

The 22 new strings exist in Spanish only, by decision. Rather than leave
check-locales.mjs permanently red — a guard nobody reads is not a guard — the
pending set is written down in src/locales/PENDING.md: the check still fails on
anything NEW, and the debt is a list somebody can work through.
2026-09-21 15:56:51 -04:00

90 lines
3.9 KiB
JavaScript

#!/usr/bin/env node
// Guards the one invariant every locale in src/locales/ shares: the same key set.
// English is the source language and has no locale file, so a key that exists in only
// some locales falls back to English silently, mid-sentence, with nothing failing
// anywhere. This catches that at review time instead of in the app.
//
// node scripts/check-locales.mjs
//
// The reference is the union of all locales, not one blessed file: a key added to a
// single locale then flags the other ten instead of passing unnoticed.
//
// Keys listed in src/locales/PENDING.md are reported and counted, not failed on. That file exists
// because a guard that is red for a known reason is a guard nobody reads: the moment it always
// fails, the next genuine drift it catches looks exactly like the noise. Keeping the pending set
// written down means this check still goes red for anything *new*, and the debt stays a list
// somebody can work through rather than a permanent red line.
import { readdirSync, readFileSync, existsSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
const localesDir = join(dirname(fileURLToPath(import.meta.url)), '..', 'src', 'locales')
const files = readdirSync(localesDir).filter(f => f.endsWith('.js')).sort()
if (!files.length) {
console.error(`No locale files found in ${localesDir}`)
process.exit(1)
}
// One pending key per line prefixed with "- ", the shape a Markdown list already has.
const pendingPath = join(localesDir, 'PENDING.md')
const pending = new Set(
existsSync(pendingPath)
? readFileSync(pendingPath, 'utf8').split('\n')
.filter(line => line.trim().startsWith('- '))
.map(line => line.trim().slice(2).trim())
: []
)
const locales = new Map()
for (const file of files) {
const { default: dict } = await import(pathToFileURL(join(localesDir, file)).href)
if (!dict || typeof dict !== 'object') {
console.error(`${file}: no default-exported object`)
process.exit(1)
}
locales.set(file.replace(/\.js$/, ''), new Set(Object.keys(dict)))
}
// A key every locale already carries cannot be pending: it would let a locale drop it silently.
const stalePending = [...pending].filter(k => [...locales.values()].every(keys => keys.has(k)))
if (stalePending.length) {
console.error('\nThese keys are in every locale already and must come out of PENDING.md:')
for (const k of stalePending) console.error(` ${JSON.stringify(k)}`)
process.exit(1)
}
// How many locales carry each key — 1 means the key was added to a single file only,
// which is the usual shape of the bug and worth naming separately from plain gaps.
const seen = new Map()
for (const keys of locales.values()) for (const k of keys) seen.set(k, (seen.get(k) || 0) + 1)
const union = [...seen.keys()]
let failed = false
const waiting = new Set()
for (const [lang, keys] of locales) {
const gaps = union.filter(k => !keys.has(k))
const missing = gaps.filter(k => !pending.has(k))
for (const k of gaps) if (pending.has(k)) waiting.add(k)
// A key only one locale carries is never "pending": it means that locale invented a string
// nobody else has, which is a mistake rather than a translation waiting to happen.
const orphans = union.filter(k => keys.has(k) && seen.get(k) === 1 && !pending.has(k))
if (missing.length || orphans.length) {
failed = true
console.error(`\n${lang}.js: ${keys.size}/${union.length} keys`)
for (const k of missing) console.error(` missing: ${JSON.stringify(k)}`)
for (const k of orphans) console.error(` only here: ${JSON.stringify(k)}`)
}
}
if (failed) {
console.error('\nLocale key sets differ. Every locale must carry the same keys.')
process.exit(1)
}
const tail = waiting.size
? `${waiting.size} key${waiting.size === 1 ? '' : 's'} pending translation in ${locales.size - 1} locales (see src/locales/PENDING.md)`
: 'in sync'
console.log(`${locales.size} locales, ${union.length} keys each — ${tail}.`)