Fix #47: Settings menu #50
@@ -16,13 +16,6 @@ const data = props.data;
|
||||
|
||||
const { locale } = useI18n();
|
||||
|
||||
|
||||
const changeLocale = (lang) => {
|
||||
console.log(lang);
|
||||
locale.value = lang.code;
|
||||
SetUserSetting('lang', lang.code);
|
||||
}
|
||||
|
||||
const id = data.id;
|
||||
|
||||
const rows = ref([{id: "account-settings", value: "settings.tabs.account-settings"}]);
|
||||
@@ -37,13 +30,40 @@ function getLocaleFromCode(code){
|
||||
}
|
||||
}
|
||||
|
||||
const selectedLocale = ref("");
|
||||
// The language to show in the dropdown: always one Dragonroll offers, because a
|
||||
// user that never picked one has none stored.
|
||||
function localeToShow(code){
|
||||
return getLocaleFromCode(code) || locales[0];
|
||||
}
|
||||
|
||||
// vue-i18n answers every translation with `SyntaxError: cannot support locale type`
|
||||
// once it is given a locale it has no messages for, and the locale is global: putting
|
||||
// a language that is not stored (the server answers `settings: undefined` for a user
|
||||
// that never picked one) or one no longer offered took the settings window and the
|
||||
// main menu behind it down (issue #47). Only known codes go into `locale`.
|
||||
function applyLanguage(code){
|
||||
const stored = getLocaleFromCode(code);
|
||||
if(!stored) return false;
|
||||
locale.value = stored.code;
|
||||
selectedLocale.value = stored;
|
||||
return true;
|
||||
}
|
||||
|
||||
const selectedLocale = ref(localeToShow(locale.value));
|
||||
|
||||
const changeLocale = (lang) => {
|
||||
if(!applyLanguage(lang?.code)) return;
|
||||
SetUserSetting('lang', lang.code);
|
||||
}
|
||||
|
||||
onBeforeMount(() => {
|
||||
GetUserSetting('lang').then(value => {
|
||||
locale.value = value;
|
||||
selectedLocale.value = getLocaleFromCode(value); // Set selected in dropdown
|
||||
});
|
||||
if(GetUser().admin) rows.value.push({
|
||||
// Set selected in dropdown. Without settings to read from the server the language
|
||||
// Dragonroll is already showing stays the one on screen.
|
||||
GetUserSetting('lang')
|
||||
.then((value) => applyLanguage(value))
|
||||
.catch(() => {});
|
||||
|
||||
if(GetUser()?.admin) rows.value.push({
|
||||
id: "site-administration",
|
||||
value: "settings.tabs.site-administration"
|
||||
});
|
||||
@@ -78,7 +98,7 @@ function OpenManageAccounts(){
|
||||
}
|
||||
|
||||
const getLocaleName = (locale) => {
|
||||
return locale.name;
|
||||
return locale ? locale.name : "";
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ClearWindow, GetFirstWindowId } from './Windows'
|
||||
import { ClearWindow, CreateWindow, GetFirstWindowId } from './Windows'
|
||||
|
||||
/*
|
||||
Put here all dragonroll windows
|
||||
@@ -26,7 +26,12 @@ const defWindows = {
|
||||
settings: {
|
||||
title: "windows.settings",
|
||||
component: () => import('~/components/windows/SettingsWindow.vue'),
|
||||
close: () => ClearWindow({type: 'settings'}),
|
||||
// The settings window is only opened from the main menu, so closing it goes back
|
||||
// there: CreateWindow leaves a window that is already open alone.
|
||||
close: () => {
|
||||
ClearWindow({ type: 'settings' });
|
||||
CreateWindow('main_menu');
|
||||
},
|
||||
movable: true
|
||||
},
|
||||
create_campaign: {
|
||||
|
||||
170
tests/settings-window.test.js
Normal file
170
tests/settings-window.test.js
Normal file
@@ -0,0 +1,170 @@
|
||||
// Tests for the settings menu opened from the main menu (Syndria98/dragonroll#47): it used to show a
|
||||
// "Syntax Error" instead of the menu, and the main menu did not come back after closing it.
|
||||
//
|
||||
// vue-i18n answers every translation with `SyntaxError: cannot support locale type` from the moment the
|
||||
// locale it holds is something it has no messages for. The settings window put `GetUserSetting('lang')`
|
||||
// straight into that locale, and for a user that never picked a language that is `undefined`: the server
|
||||
// answers GET /user/get-settings with `settings: undefined`. The locale is global, so the one bad write
|
||||
// broke every window on screen — the settings window and the main menu behind it.
|
||||
//
|
||||
// The repository root installs no dependencies, so the window's script is lifted out of its `<script setup>`
|
||||
// block and run in a `node:vm` context with `locale` and `selectedLocale` as plain refs, the way
|
||||
// tests/sidebar-section-pane.test.js loads the sidebar script. The window wiring (coming back to the main
|
||||
// menu on close, the strings the menu draws) is read from the sources like tests/item-sheet-window.test.js.
|
||||
//
|
||||
// Run with `npm test` from the repository root, or `node --test tests/`.
|
||||
|
||||
const { test } = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const vm = require("node:vm");
|
||||
|
||||
const read = (...parts) => fs.readFileSync(path.join(__dirname, "..", ...parts), "utf8");
|
||||
|
||||
const windowSource = read("frontend", "app", "components", "windows", "SettingsWindow.vue");
|
||||
const windowDefsSource = read("frontend", "app", "services", "WindowDefinitions.js");
|
||||
const userRouteSource = read("backend", "src", "routes", "user.js");
|
||||
|
||||
const script = /<script setup>([\s\S]*?)<\/script>/.exec(windowSource)?.[1];
|
||||
|
||||
// Top-level `function name(){...}` or `const name = (...) => {...}` of the script block: their bodies are
|
||||
// indented, so the first `}` in column 0 closes them.
|
||||
function declarationSource(name) {
|
||||
assert.ok(script, "expected the settings window to have a <script setup> block");
|
||||
const decl = new RegExp(`(function ${name}\\s*\\(|const ${name}\\s*=)[\\s\\S]*?\\n\\}`).exec(script)?.[0];
|
||||
assert.ok(decl, `expected a top-level ${name} in the settings window script`);
|
||||
return decl;
|
||||
}
|
||||
|
||||
// The languages Dragonroll offers (frontend/i18n/locales.ts).
|
||||
const LOCALES = [
|
||||
{ code: "en", name: "English" },
|
||||
{ code: "es", name: "Español" },
|
||||
{ code: "ca", name: "Català" },
|
||||
];
|
||||
|
||||
// The settings window with the language it is showing. `saved` collects every language the window asks the
|
||||
// server to store, so a test can tell a language that was applied from one that was merely answered with.
|
||||
function settingsWindow({ locale = "en" } = {}) {
|
||||
const body = [
|
||||
declarationSource("getLocaleFromCode"),
|
||||
declarationSource("localeToShow"),
|
||||
declarationSource("applyLanguage"),
|
||||
declarationSource("getLocaleName"),
|
||||
declarationSource("changeLocale"),
|
||||
"return { localeToShow, applyLanguage, getLocaleName, changeLocale };",
|
||||
].join("\n\n");
|
||||
|
||||
const state = {
|
||||
locale: { value: locale },
|
||||
selectedLocale: { value: null },
|
||||
saved: [],
|
||||
};
|
||||
|
||||
const api = vm.compileFunction(body, ["locale", "selectedLocale", "locales", "SetUserSetting"], {
|
||||
parsingContext: vm.createContext({}),
|
||||
})(
|
||||
state.locale,
|
||||
state.selectedLocale,
|
||||
LOCALES,
|
||||
(key, value) => state.saved.push([key, value])
|
||||
);
|
||||
|
||||
return Object.assign(state, api);
|
||||
}
|
||||
|
||||
test("a user without a stored language keeps the one on screen", () => {
|
||||
const settings = settingsWindow({ locale: "en" });
|
||||
// What GetUserSetting('lang') answers for a user that never picked a language: the server has no
|
||||
// settings for them at all.
|
||||
settings.applyLanguage(undefined);
|
||||
|
||||
assert.equal(settings.locale.value, "en", "the language on screen must not be replaced by nothing");
|
||||
assert.equal(typeof settings.locale.value, "string", "vue-i18n only takes a locale it knows a type for");
|
||||
});
|
||||
|
||||
test("a stored language Dragonroll does not offer is not applied either", () => {
|
||||
for (const stored of ["", null, "fr", "en-US", 0, {}]) {
|
||||
const settings = settingsWindow({ locale: "en" });
|
||||
settings.applyLanguage(stored);
|
||||
assert.equal(settings.locale.value, "en", `${String(stored)} is no language vue-i18n takes`);
|
||||
}
|
||||
});
|
||||
|
||||
test("the stored language is applied when it is one of the offered ones", () => {
|
||||
for (const [code, name] of [["es", "Español"], ["ca", "Català"]]) {
|
||||
const settings = settingsWindow({ locale: "en" });
|
||||
assert.equal(settings.applyLanguage(code), true, `${code} must be applied`);
|
||||
assert.equal(settings.locale.value, code);
|
||||
assert.equal(settings.selectedLocale.value.name, name, "the dropdown shows the language applied");
|
||||
}
|
||||
});
|
||||
|
||||
test("the dropdown always shows a language, and never one the window has not got", () => {
|
||||
const settings = settingsWindow({ locale: "en" });
|
||||
assert.deepEqual(settings.localeToShow(undefined), LOCALES[0], "no language stored: the first one is shown");
|
||||
assert.deepEqual(settings.localeToShow("de"), LOCALES[0], "an unknown code shows the same language");
|
||||
assert.deepEqual(settings.localeToShow("ca"), LOCALES[2]);
|
||||
assert.match(
|
||||
script,
|
||||
/const selectedLocale = ref\(localeToShow\(locale\.value\)\)/,
|
||||
"the dropdown starts on the language being shown instead of on an empty row"
|
||||
);
|
||||
});
|
||||
|
||||
test("the dropdown label survives a language that is not there", () => {
|
||||
const settings = settingsWindow();
|
||||
assert.equal(settings.getLocaleName(undefined), "");
|
||||
assert.equal(settings.getLocaleName(LOCALES[1]), "Español");
|
||||
});
|
||||
|
||||
test("picking a language saves it, and picking nothing saves nothing", () => {
|
||||
const settings = settingsWindow();
|
||||
settings.changeLocale(LOCALES[2]);
|
||||
assert.deepEqual(settings.saved, [["lang", "ca"]]);
|
||||
assert.equal(settings.locale.value, "ca");
|
||||
|
||||
const nothing = settingsWindow();
|
||||
nothing.changeLocale(undefined);
|
||||
assert.deepEqual(nothing.saved, [], "a language that was not applied is not saved either");
|
||||
assert.equal(nothing.locale.value, "en");
|
||||
});
|
||||
|
||||
test("the window never hands the server's answer to vue-i18n directly", () => {
|
||||
assert.match(script, /GetUserSetting\(['"]lang['"]\)/, "the stored language is still read from the server");
|
||||
assert.match(
|
||||
script,
|
||||
/GetUserSetting\(['"]lang['"]\)\s*\.then\(\s*\(value\) => applyLanguage\(value\)\s*\)/,
|
||||
"it goes through applyLanguage, which only accepts a language Dragonroll offers"
|
||||
);
|
||||
const writes = [...script.matchAll(/locale\.value\s*=\s*([^;]+);/g)].map((write) => write[1].trim());
|
||||
assert.deepEqual(writes, ["stored.code"], "only a language looked up in the offered list reaches vue-i18n");
|
||||
assert.match(script, /\.catch\(/, "a settings request that fails leaves the language alone");
|
||||
});
|
||||
|
||||
test("GET /user/get-settings answers without a settings object for a user that saved none", () => {
|
||||
// The window has to cope with what this answers: the reason #47 shows up for every fresh account.
|
||||
assert.match(userRouteSource, /router\.get\(['"]\/get-settings['"]/);
|
||||
assert.match(userRouteSource, /settings: data\.settings/);
|
||||
});
|
||||
|
||||
test("closing the settings window goes back to the main menu", () => {
|
||||
const definition = /^\s{2}settings:\s*\{[\s\S]*?\n \},/m.exec(windowDefsSource)?.[0];
|
||||
assert.ok(definition, "expected the settings window to be registered");
|
||||
assert.match(definition, /SettingsWindow\.vue/);
|
||||
assert.match(definition, /ClearWindow\(\s*\{\s*type:\s*['"]settings['"]\s*\}\s*\)/);
|
||||
assert.match(definition, /CreateWindow\(\s*['"]main_menu['"]\s*\)/);
|
||||
});
|
||||
|
||||
test("every locale names the settings menu and its tabs", () => {
|
||||
for (const locale of ["en", "es", "ca"]) {
|
||||
const settings = JSON.parse(read("frontend", "i18n", "locales", locale, "settings.json"));
|
||||
assert.ok(settings.tabs?.["account-settings"], `${locale} is missing the account settings tab`);
|
||||
assert.ok(settings.tabs?.["site-administration"], `${locale} is missing the site administration tab`);
|
||||
assert.ok(settings["account-settings"]?.language, `${locale} is missing the language label`);
|
||||
assert.ok(settings["site-administration"]?.["manage-accounts"], `${locale} is missing manage accounts`);
|
||||
const windows = JSON.parse(read("frontend", "i18n", "locales", locale, "windows.json"));
|
||||
assert.ok(windows.settings, `${locale} is missing the settings window title`);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user