Testing a multilingual maths worksheet with node:test: the label and the prompt both say 문제 #5189
lmo0317
started this conversation in
Show and tell
Replies: 1 comment
|
The fixture as posted already passes — the failure only appears when translation is applied to the joined string. The fix is architectural: translate discrete UI strings at the boundary, never the composed output. Here is the working version plus a regression test that pins both failure modes. Verified locally on Node v22.23.4 ( const test = require('node:test');
const assert = require('node:assert/strict');
const question = '문제: x + 2 = 5';
const dictionary = {
en: { '문제': 'Problem' },
zh: { '문제': '题目' },
};
// ❌ BROKEN — join first, then translate the whole string:
// the student's question gets clobbered too.
function brokenText(locale, original) {
return [dictionary[locale]['문제'], original].join('\n')
.replaceAll('문제', dictionary[locale]['문제']);
}
// ✅ FIXED — translate each discrete string, then compose.
// The composed result is never re-translated.
function worksheetText(locale, original) {
return [dictionary[locale]['문제'], original].join('\n');
}
for (const [locale, label] of [['en', 'Problem'], ['zh', '题目']]) {
test(`broken: join-then-translate clobbers the question in ${locale}`, () => {
assert.notEqual(brokenText(locale, question), `${label}\n${question}`);
});
test(`fixed: translates only the worksheet label in ${locale}`, () => {
const text = worksheetText(locale, question);
assert.equal(text, `${label}\n${question}`);
assert.ok(text.endsWith(question), 'original question preserved verbatim');
});
}Why this is the right shape for the ModuMath renderer:
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I'm on the team building ModuMath, a browser app for students to work through a maths problem and try another with the same reasoning. Here's a small test from that work. The interface switches between Korean, English and Chinese; the original question on a printable worksheet should stay in the language the student typed.
The Korean word 문제 means “problem”. Using it both as a worksheet label and inside the student's question makes a useful fixture. If the renderer joins the strings and then translates every occurrence, its heading looks right and its question is wrong. An assertion that only checks for the English heading misses the error.
Here's the reduced text-only example. Save as
worksheet.test.cjsand runnode --test worksheet.test.cjs:To see the failure, join the Korean label and question first, then apply
.replaceAll('문제', dictionary[locale]['문제'])to the result. Both tests fail on the second line.In our app's tests we also load our own browser scripts in a
node:vmcontext with a small document stub and a local translation dictionary. We change the interface language on the same worksheet and check both the new heading and the original mixed-language expression. That exercises the worksheet renderer without a running server. The text-only example above isolates the collision; it does not exercise browser layout.All reactions