diff --git a/test/prose-browser.mjs b/test/prose-browser.mjs index 9ff12e4..8d56227 100644 --- a/test/prose-browser.mjs +++ b/test/prose-browser.mjs @@ -4,6 +4,9 @@ import { readFile, mkdir } from "node:fs/promises"; import { extname, resolve } from "node:path"; import { chromium } from "playwright"; import { languages } from "../viewer/localization/runtime.js"; +import { createTutorial } from "@boardgamers/protocol/tutorial"; +import { chapters } from "../viewer/tutorials.js"; +import { availableMoves } from "../engine/index.js"; const dist = resolve("dist"); const server = createServer(async (req, res) => { try { @@ -69,6 +72,57 @@ try { await prose.locator(".icon-coin").first().waitFor(); } } + const saved = new Map(); + const trade = await createTutorial({ + ...chapters.trade, + storage: { + getItem: (key) => saved.get(key) ?? null, + setItem: (key, value) => saved.set(key, value), + removeItem: (key) => saved.delete(key), + }, + }); + while (trade.snapshot.step < 5) { + if (trade.snapshot.canContinue) await trade.continue(); + else { + const snap = trade.snapshot; + const step = chapters.trade.steps[snap.step]; + const move = availableMoves(snap.state, 0).find( + (m) => !step.validateMove?.(snap.state, m), + ); + assert.equal(await trade.play(move), true); + } + } + trade.destroy(); + await page.addInitScript( + (entries) => { + for (const [key, value] of entries) localStorage.setItem(key, value); + }, + [...saved], + ); + for (const locale of ["en", "fr"]) { + await page.goto(`${base}/?lesson=trade&locale=${locale}`); + const prose = page.locator(".bgs-tutorial-body > p").first(); + await prose.locator(".icon-copra").waitFor(); + assert.equal(await prose.locator(".icon-beef").count(), 2); + assert.equal(await prose.locator(".resource-symbol").count(), 0); + await page.locator("[data-color-blind]").click(); + assert.equal(await prose.locator(".resource-symbol").count(), 3); + // The guide must retain the setting when the controller renders a new step. + await page.locator(".bgs-tutorial-playback button").nth(3).click(); + assert.equal(await prose.locator(".resource-symbol").count(), 2); + await page.locator(".bgs-tutorial-playback button").nth(1).click(); + assert.equal(await prose.locator(".resource-symbol").count(), 3); + await page.locator("[data-color-blind]").click(); + assert.equal(await prose.locator(".resource-symbol").count(), 0); + assert.ok( + await page.evaluate( + () => document.documentElement.scrollWidth <= innerWidth, + ), + ); + await page + .locator(".vanuatu-tutorial") + .screenshot({ path: `.local/qa/prose-export-${locale}.png` }); + } await page.goto(`${base}/?new&hotseat&locale=fr`); await page.locator('[data-character-choice="beggar"]').click(); const help = page.locator('dialog [data-character-help="beggar"]'); diff --git a/test/prose.test.js b/test/prose.test.js index dc17e0a..b01dcd3 100644 --- a/test/prose.test.js +++ b/test/prose.test.js @@ -6,7 +6,7 @@ import { LESSON } from "../viewer/tutorial-copy.js"; import { languages } from "../viewer/localization/runtime.js"; for (const locale of Object.keys(languages)) - test(`tutorial prose preserves ${locale} grammar around the four symbols`, async () => { + test(`tutorial prose preserves ${locale} grammar around action and resource symbols`, async () => { const catalog = JSON.parse( await readFile( new URL(`../viewer/localization/${locale}.json`, import.meta.url), @@ -17,6 +17,29 @@ for (const locale of Object.keys(languages)) const html = pictogramText(text, locale); for (const name of ["coin", "point", "draw", "build"]) assert.ok(html.includes(`icon-${name}`), `${locale}: ${name}`); + for (const source of [ + LESSON.tradeCompare, + LESSON.tradeChoose, + LESSON.tradeIncomplete, + ]) { + const goods = pictogramText(catalog[source], locale, true); + assert.equal( + (goods.match(/icon-copra/g) ?? []).length, + 1, + `${locale}: copra`, + ); + assert.equal( + (goods.match(/icon-beef/g) ?? []).length, + source === LESSON.tradeChoose ? 1 : 2, + `${locale}: beef`, + ); + assert.equal( + (goods.match(/resource-symbol/g) ?? []).length, + source === LESSON.tradeChoose ? 2 : 3, + `${locale}: color-blind symbols`, + ); + } + assert.match(pictogramText(catalog.kava, locale), /icon-kava/); // Replace the generated icon with its accessible name: no translated text, // punctuation or amounts may disappear or change order. const plain = html.replace( diff --git a/viewer/prose.js b/viewer/prose.js index 8b62e55..430f9d1 100644 --- a/viewer/prose.js +++ b/viewer/prose.js @@ -112,14 +112,50 @@ const terms = { "بازیکن اول", ], }; +const goods = { + en: ["kava", "copra", "beef"], + fr: ["kava", "coprah|copra", "bœuf|boeuf"], + de: ["Kava", "Kopra", "Rindfleisch"], + nl: ["kava", "kopra", "rundvlees"], + da: ["kava", "kopra|kopraen", "oksekød"], + it: ["kava", "copra", "manzo"], + "pt-BR": ["kava", "copra", "boi"], + pl: [ + "kava|kavy|kavę|kavą", + "kopra|kopry|koprę|koprą", + "wołowina|wołowiny|wołowinę|wołowiną", + ], + ro: ["kava", "copra", "vită|vita"], + ru: [ + "кава|кавы|каву|кавой", + "копра|копры|копру|копрой", + "говядина|говядины|говядину|говядиной", + ], + el: ["κάβα", "κόπρα", "μοσχάρι"], + hi: ["कावा", "खोपरा|खोपरे", "गोमांस"], + ko: ["카바", "코프라", "소고기"], + "zh-TW": ["卡瓦", "椰乾", "牛肉"], + vi: ["kava", "cùi dừa khô", "thịt bò"], + fa: ["کاوا", "مغز خشک نارگیل", "گوشت گاو"], +}; const cache = new Map(); const escapePattern = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); function vocabulary(locale) { if (cache.has(locale)) return cache.get(locale); - const names = ["coin", "point", "build", "draw", "first"]; + const names = [ + "coin", + "point", + "build", + "draw", + "first", + "kava", + "copra", + "beef", + ]; const words = new Map( - (terms[locale] ?? terms.en).flatMap((group, i) => - group.split("|").map((word) => [word.toLowerCase(), names[i]]), + [...(terms[locale] ?? terms.en), ...(goods[locale] ?? goods.en)].flatMap( + (group, i) => + group.split("|").map((word) => [word.toLowerCase(), names[i]]), ), ); const body = [...words.keys()] @@ -137,7 +173,7 @@ function vocabulary(locale) { cache.set(locale, result); return result; } -export function pictogramText(text, locale = "en") { +export function pictogramText(text, locale = "en", colorBlind = false) { const { words, pattern } = vocabulary(resolveLocale(locale)); let result = "", end = 0; @@ -145,7 +181,7 @@ export function pictogramText(text, locale = "en") { const label = match[0], name = words.get(label.toLowerCase()); result += esc(text.slice(end, match.index)); - result += `${icon(name, label)}`; + result += `${icon(name, label, false, colorBlind)}`; end = match.index + label.length; } return result + esc(text.slice(end)); diff --git a/viewer/rules.js b/viewer/rules.js index fdca73d..2cbbc7f 100644 --- a/viewer/rules.js +++ b/viewer/rules.js @@ -17,7 +17,7 @@ import { PRICES, VALUES } from "../engine/catalog.js"; import { pictogramText } from "./prose.js"; import { translateText } from "./localization/index.js"; export function rulesHtml(lang = "en", colorBlind = false) { - const prose = (text) => pictogramText(t(text), lang); + const prose = (text) => pictogramText(t(text), lang, colorBlind); const t = (text) => translateText(text, lang); const icon = (name) => renderIcon(name, t(iconLabel(name)), false, colorBlind); diff --git a/viewer/tutorial-mount.js b/viewer/tutorial-mount.js index 4f747b3..f8402bb 100644 --- a/viewer/tutorial-mount.js +++ b/viewer/tutorial-mount.js @@ -21,9 +21,16 @@ export async function mountLesson(target, options) { const guide = document.createElement("div"); guide.className = "vanuatu-tutorial"; target.append(guide); - let errorFrame; + let errorFrame, + colorBlind = false; const ui = mountGame(target, { language: options.locale ?? "en", + onPreference: (name, value) => { + if (name === "colorBlind") { + colorBlind = value; + renderProse(tutorial.snapshot); + } + }, onMove: async (m) => { const accepted = await tutorial.play(m); if (!accepted) { @@ -59,13 +66,14 @@ export async function mountLesson(target, options) { const error = guide.querySelector('[role="alert"]'); error.setAttribute("translate", "no"); error.classList.add("tutorial-error"); - const offProse = tutorial.subscribe((snapshot) => { + function renderProse(snapshot) { [snapshot.text, snapshot.hint].forEach((source, i) => { const paragraph = paragraphs[i]; if (!paragraph || !source) return; paragraph.innerHTML = pictogramText( translateText(source, options.locale), options.locale, + colorBlind, ); }); // Validators often return the current instruction. Show it once, as the @@ -73,13 +81,14 @@ export async function mountLesson(target, options) { paragraphs[0].hidden = !!snapshot.error && snapshot.error.trim() === snapshot.text.trim(); if (snapshot.error) { - error.innerHTML = `${icon("warning", "")}${pictogramText(translateText(snapshot.error, options.locale), options.locale)}`; + error.innerHTML = `${icon("warning", "")}${pictogramText(translateText(snapshot.error, options.locale), options.locale, colorBlind)}`; guide.querySelector(".bgs-tutorial-body").hidden = false; const collapse = guide.querySelector(".bgs-tutorial-heading button"); collapse.setAttribute("aria-expanded", "true"); collapse.textContent = translateText("Hide", options.locale); } - }); + } + const offProse = tutorial.subscribe(renderProse); return () => { cancelAnimationFrame(errorFrame); offProse(); diff --git a/viewer/ui.js b/viewer/ui.js index ee927e9..bf99b9b 100644 --- a/viewer/ui.js +++ b/viewer/ui.js @@ -171,7 +171,7 @@ export function mountGame(target, options = {}) { const charName = (c) => esc(CHARACTERS[c]?.["name"] ?? c); const charHelp = (c) => esc(characterHelp[c]?.[0] ?? ""); const charHelpHtml = (c) => - `${pictogramText(translateText(characterHelp[c]?.[0] ?? "", lang), lang)}`; + `${pictogramText(translateText(characterHelp[c]?.[0] ?? "", lang), lang, colorBlind)}`; function btn(content, attr = "", className = "") { return ``; } @@ -509,15 +509,16 @@ export function mountGame(target, options = {}) { rules.dataset.presentation = rulesKey; } for (const help of dialog.querySelectorAll("[data-character-help]")) { - if (help.dataset.proseLocale === lang) continue; + if (help.dataset.proseLocale === rulesKey) continue; help.innerHTML = pictogramText( translateText( characterHelp[help.dataset.characterHelp]?.[0] ?? "", lang, ), lang, + colorBlind, ); - help.dataset.proseLocale = lang; + help.dataset.proseLocale = rulesKey; } activity.update(s, analysis, colorBlind); updateUnread();