diff --git a/e2e/tests/ui.test.mjs b/e2e/tests/ui.test.mjs index 421775da..4b2e4930 100644 --- a/e2e/tests/ui.test.mjs +++ b/e2e/tests/ui.test.mjs @@ -7188,6 +7188,96 @@ test("a Pro user's fingerprint tab shows the device, and says why when it cannot }); }); +test("a languages edit typed one key at a time is kept and saved", async () => { + await withApp("ui-fingerprint-languages", async (app) => { + const created = await createUiProfile(app, "Languages Device"); + const file = path.join( + app.dataRoot, + "data", + "profiles", + created.id, + "metadata.json", + ); + const stored = JSON.parse(await readFile(file, "utf8")); + stored.version = "154.0.8037.57"; + delete stored.wayfern_config.fingerprint; + stored.wayfern_config.identity_id = "6f9619ff-8b86-4d01-b42d-00c04fc964ff"; + await writeFile(file, JSON.stringify(stored)); + await app.restart(); + await app.waitForText("Languages Device"); + const device = { + userAgent: "Mozilla/5.0 (Macintosh) E2E/1.0", + platform: "MacIntel", + language: "en-US", + languages: ["en-US", "uk-UA", "uk", "ru"], + }; + try { + await stubCommand(app, "get_profile_fingerprint", JSON.stringify(device)); + await stubCommand(app, "cloud_get_user", { + logged_in_at: "2020-01-01T00:00:00Z", + user: { + id: "languages-pro", + email: "languages@example.test", + plan: "pro", + planPeriod: "monthly", + subscriptionStatus: "active", + profileLimit: 50, + isPrimaryDevice: true, + }, + }); + await app.invoke("plugin:event|emit", { + event: "cloud-auth-changed", + payload: null, + }); + await app.clickSelector( + `[data-slot="profile-inspect-trigger"][data-profile-id="${created.id}"]`, + ); + await app.clickSelector( + '[data-slot="profile-info-section"][data-section="fingerprint"]', + ); + const shown = () => + app.execute( + `const input = document.querySelector("#languages"); + return input && !input.matches(":disabled") ? input.value : null;`, + ); + await app.waitFor( + async () => (await shown()) === JSON.stringify(device.languages), + { description: "the device's languages in the field" }, + ); + + // Deleting the three other languages from the end, one key at a time as + // a person does: every text between the first and the last key is a + // list that does not parse, and the field must keep each one. + const field = await app.execute( + `return document.querySelector("#languages");`, + ); + const removed = ',"uk-UA","uk","ru"]'; + await app.session.sendKeys(field, "\uE003".repeat(removed.length)); + assert.equal(await shown(), '["en-US"'); + await app.session.sendKeys(field, "]"); + assert.equal(await shown(), '["en-US"]'); + + await app.clickText(en.common.buttons.save, { exact: true }); + await app.waitFor( + async () => { + const [profile] = await app.invoke("list_browser_profiles"); + const edits = profile.wayfern_config.identity_overrides; + return edits && JSON.parse(edits).languages?.join() === "en-US"; + }, + { description: "the saved languages edit" }, + ); + const [saved] = await app.invoke("list_browser_profiles"); + assert.deepEqual(JSON.parse(saved.wayfern_config.identity_overrides), { + languages: ["en-US"], + }); + assert.equal(await shown(), '["en-US"]'); + await app.capture("fingerprint-languages-saved"); + } finally { + await restoreStubs(app); + } + }); +}); + test("Pro users can edit and save launch arguments in each profile", async () => { await withApp( "ui-launch-args", diff --git a/package.json b/package.json index 7922fa74..e976d570 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "build": "next build", "start": "next start", "clips": "node scripts/clips/record.mjs", - "test": "pnpm test:themes && pnpm test:tips && pnpm test:window-decorations && pnpm test:cookie-bot-limits && pnpm test:cookie-bot-outcomes && pnpm test:recipes && pnpm test:agent-console && pnpm test:agent-tasks && pnpm test:api-examples && pnpm test:backend-errors && pnpm test:i18n-parity && pnpm test:proxy-string && pnpm test:proxy-type && pnpm test:proxy-first-hop-claims && pnpm test:profile-search && pnpm test:palette-search && pnpm test:ui-sounds && pnpm test:schedules && pnpm test:team-activity && pnpm test:table-tools && pnpm test:motion && pnpm test:donut-lab && pnpm test:profile-store && pnpm test:licenses && pnpm test:xray-packaging && pnpm test:appimage-metainfo && pnpm test:appimage-system-libs && pnpm test:rust:unit && pnpm test:donut-sync && pnpm test:sync-e2e", + "test": "pnpm test:themes && pnpm test:tips && pnpm test:window-decorations && pnpm test:cookie-bot-limits && pnpm test:cookie-bot-outcomes && pnpm test:recipes && pnpm test:agent-console && pnpm test:agent-tasks && pnpm test:api-examples && pnpm test:backend-errors && pnpm test:i18n-parity && pnpm test:proxy-string && pnpm test:proxy-type && pnpm test:language-list && pnpm test:proxy-first-hop-claims && pnpm test:profile-search && pnpm test:palette-search && pnpm test:ui-sounds && pnpm test:schedules && pnpm test:team-activity && pnpm test:table-tools && pnpm test:motion && pnpm test:donut-lab && pnpm test:profile-store && pnpm test:licenses && pnpm test:xray-packaging && pnpm test:appimage-metainfo && pnpm test:appimage-system-libs && pnpm test:rust:unit && pnpm test:donut-sync && pnpm test:sync-e2e", "test:themes": "node --test src/lib/themes.test.mjs", "test:tips": "node --test src/lib/tips.test.mjs src/lib/coach-mark.test.mjs", "test:window-decorations": "node --test src/lib/window-decorations.test.mjs", @@ -25,6 +25,7 @@ "test:i18n-parity": "node --test src/lib/i18n-parity.test.mjs", "test:proxy-string": "node --test src/lib/proxy-string.test.mjs", "test:proxy-type": "node --test src/lib/proxy-type.test.mjs", + "test:language-list": "node --test src/lib/language-list.test.mjs", "test:proxy-first-hop-claims": "node --test src/lib/proxy-first-hop-claims.test.mjs", "test:profile-search": "node --test src/lib/profile-search.test.mjs src/lib/profile-search-suggest.test.mjs", "test:palette-search": "node --test src/lib/palette-search.test.mjs src/lib/shortcuts.test.mjs", diff --git a/src/components/wayfern-config-form.tsx b/src/components/wayfern-config-form.tsx index 836bb04a..e79e8f72 100644 --- a/src/components/wayfern-config-form.tsx +++ b/src/components/wayfern-config-form.tsx @@ -39,6 +39,7 @@ import { Textarea } from "@/components/ui/textarea"; import { useCloudAuth } from "@/hooks/use-cloud-auth"; import { translateBackendError } from "@/lib/backend-errors"; import { getEntitlements } from "@/lib/entitlements"; +import { formatLanguageList, parseLanguageList } from "@/lib/language-list"; import type { PersonaField, WayfernConfig, @@ -319,6 +320,10 @@ export function WayfernConfigForm({ } | null>(null); const [deviceAttempt, setDeviceAttempt] = useState(0); const ownEditRef = useRef<{ json: string | undefined } | null>(null); + // The languages text while the field has focus. Most keystrokes of an edit + // leave a list that does not parse yet, so the field shows what was typed and + // stores only complete lists. + const [languagesDraft, setLanguagesDraft] = useState(null); const handleGenerateFingerprint = async () => { if (!profileVersion) return; @@ -1236,24 +1241,17 @@ export function WayfernConfigForm({ { - if (!e.target.value) { - updateFingerprintConfig("languages", undefined); - return; - } - try { - const parsed = JSON.parse(e.target.value); - if (Array.isArray(parsed)) { - updateFingerprintConfig("languages", parsed); - } - } catch { - // Invalid JSON, keep current value + setLanguagesDraft(e.target.value); + const languages = parseLanguageList(e.target.value); + if (languages) { + updateFingerprintConfig("languages", languages); } }} + onBlur={() => setLanguagesDraft(null)} placeholder='["en-US", "en"]' /> diff --git a/src/lib/language-list.test.mjs b/src/lib/language-list.test.mjs new file mode 100644 index 00000000..d5b45a3f --- /dev/null +++ b/src/lib/language-list.test.mjs @@ -0,0 +1,70 @@ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import path from "node:path"; +import test from "node:test"; +import { fileURLToPath } from "node:url"; +import { formatLanguageList, parseLanguageList } from "./language-list.ts"; + +const HERE = path.dirname(fileURLToPath(import.meta.url)); +const FORM = path.join(HERE, "..", "components", "wayfern-config-form.tsx"); +const source = readFileSync(FORM, "utf8"); + +test("a JSON array and a comma list read as the same tags", () => { + const expected = ["en-US", "uk-UA", "uk", "ru"]; + assert.deepEqual(parseLanguageList('["en-US","uk-UA","uk","ru"]'), expected); + assert.deepEqual(parseLanguageList("en-US, uk-UA, uk, ru"), expected); + assert.deepEqual(parseLanguageList("en-US uk-UA\nuk,ru"), expected); +}); + +test("every intermediate text of deleting tags one key at a time is a list", () => { + // Deleting `,"uk-UA","uk","ru"` from the end, one key at a time: no text on + // the way may be unreadable, or the field would refuse the edit. + const shown = '["en-US","uk-UA","uk","ru"]'; + const target = '["en-US"]'; + const kept = '["en-US"'; + let text = shown; + while (text.length > kept.length) { + text = text.slice(0, -1); + const parsed = parseLanguageList(text); + if (parsed !== null) { + assert.ok(parsed.length >= 1 && parsed[0] === "en-US", text); + } + } + assert.deepEqual(parseLanguageList(`${text}]`), ["en-US"]); + assert.deepEqual(parseLanguageList(target), ["en-US"]); +}); + +test("an incomplete tag is not committed", () => { + assert.equal(parseLanguageList('["en-US","r'), null); + assert.equal(parseLanguageList("en-US, de-"), null); + assert.equal(parseLanguageList("en_US"), null); +}); + +test("empty text is the empty list", () => { + assert.deepEqual(parseLanguageList(""), []); + assert.deepEqual(parseLanguageList("[]"), []); +}); + +test("the stored list is shown as JSON, an old string as it is", () => { + assert.equal(formatLanguageList(["en-US", "en"]), '["en-US","en"]'); + assert.equal(formatLanguageList("de-DE,de"), "de-DE,de"); + assert.equal(formatLanguageList(undefined), ""); + assert.deepEqual(parseLanguageList(formatLanguageList(["en-US", "en"])), [ + "en-US", + "en", + ]); +}); + +test("the form keeps what the person types until the field loses focus", () => { + const field = source.slice( + source.indexOf('id="languages"'), + source.indexOf("", source.indexOf('id="languages"')), + ); + // A field whose value is only the stored list throws away every keystroke + // that does not parse, which is most keystrokes of any edit. + assert.match(field, /value=\{\s*languagesDraft \?\?\s*formatLanguageList\(/); + assert.match(field, /setLanguagesDraft\(e\.target\.value\)/); + assert.match(field, /parseLanguageList\(e\.target\.value\)/); + assert.match(field, /onBlur=\{\(\) => setLanguagesDraft\(null\)\}/); + assert.doesNotMatch(field, /JSON\.parse/); +}); diff --git a/src/lib/language-list.ts b/src/lib/language-list.ts new file mode 100644 index 00000000..105b947b --- /dev/null +++ b/src/lib/language-list.ts @@ -0,0 +1,19 @@ +/** A BCP 47 tag as `navigator.languages` carries one: a 2-8 letter language + * subtag, then optional subtags. */ +const LANGUAGE_TAG = /^[A-Za-z]{2,8}(-[A-Za-z0-9]{1,8})*$/; + +/** The text the languages field shows for a stored list. Old profiles store a + * comma-separated string, which is shown as it is. */ +export function formatLanguageList(languages: unknown): string { + if (Array.isArray(languages)) return JSON.stringify(languages); + return typeof languages === "string" ? languages : ""; +} + +/** The tags in what a person typed, or `null` while the text holds a tag that + * is not complete. Commas, spaces, brackets and quotes all separate tags, so a + * JSON array, `en-US, en` and a JSON array with one quote deleted all read as + * the same list. Empty text is the empty list. */ +export function parseLanguageList(text: string): string[] | null { + const tags = text.split(/[\s,[\]"']+/).filter(Boolean); + return tags.every((tag) => LANGUAGE_TAG.test(tag)) ? tags : null; +}