Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots 📖

test(i18n): share one locale reader between the locale tests

davbio6cd516470a85
diff --git a/src/locales/__tests__/coverage.test.ts b/src/locales/__tests__/coverage.test.ts
index c7e9bbd..0e979c9 100644
--- a/src/locales/__tests__/coverage.test.ts
+++ b/src/locales/__tests__/coverage.test.ts
@@ -1,31 +1,11 @@
-import { readFileSync } from 'node:fs';
-import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
-
-function localeKeys(path: string): string[] {
- const keys: string[] = [];
- let section = '';
-
- for (const line of readFileSync(join(process.cwd(), path), 'utf8').replace(/\r\n/g, '\n').split('\n')) {
- const top = /^([\w-]+):/.exec(line);
- if (top) {
- section = top[1];
- continue;
- }
-
- const nested = /^ {2}([\w-]+):/.exec(line);
- if (nested && section) keys.push(`${section}.${nested[1]}`);
- }
-
- return keys.sort();
-}
-
-const LOCALES = ['zh-CN', 'es', 'fr', 'de', 'pt-BR'] as const;
+import { LOCALES, localeKeys } from './read-locale';
describe('locale coverage', () => {
for (const locale of LOCALES) {
+ if (locale === 'en') continue;
it(`${locale} matches the English message keys`, () => {
- expect(localeKeys(`src/locales/${locale}.yml`)).toEqual(localeKeys('src/locales/en.yml'));
+ expect(localeKeys(locale)).toEqual(localeKeys('en'));
});
}
});
diff --git a/src/locales/__tests__/read-locale.ts b/src/locales/__tests__/read-locale.ts
new file mode 100644
index 0000000..92ebaf1
--- /dev/null
+++ b/src/locales/__tests__/read-locale.ts
@@ -0,0 +1,59 @@
+import { readFileSync } from 'node:fs';
+import { join } from 'node:path';
+
+export const LOCALES = ['en', 'zh-CN', 'es', 'fr', 'de', 'pt-BR'] as const;
+
+export type Locale = (typeof LOCALES)[number];
+
+function lines(locale: string): string[] {
+ return readFileSync(join(process.cwd(), `src/locales/${locale}.yml`), 'utf8')
+ .replace(/\r\n/g, '\n')
+ .split('\n');
+}
+
+function unquote(raw: string): string {
+ const value = raw.trim();
+ if (value.startsWith("'") && value.endsWith("'")) return value.slice(1, -1).replace(/''/g, "'");
+ if (value.startsWith('"') && value.endsWith('"')) return value.slice(1, -1).replace(/\\"/g, '"');
+ return value;
+}
+
+export function localeKeys(locale: string): string[] {
+ const keys: string[] = [];
+ let section = '';
+
+ for (const line of lines(locale)) {
+ const top = /^([\w-]+):/.exec(line);
+ if (top) {
+ section = top[1];
+ continue;
+ }
+
+ const nested = /^ {2}([\w-]+):/.exec(line);
+ if (nested && section) keys.push(`${section}.${nested[1]}`);
+ }
+
+ return keys.sort();
+}
+
+export function localeMessage(locale: string, path: string): string {
+ const [wanted, key] = path.split('.');
+ let section = '';
+
+ for (const line of lines(locale)) {
+ const top = /^([\w-]+):/.exec(line);
+ if (top) section = top[1];
+ if (section !== wanted) continue;
+
+ const match = new RegExp(`^ {2}${key}: (.*)$`).exec(line);
+ if (match) return unquote(match[1]);
+ }
+
+ throw new Error(`${path} missing from ${locale}.yml`);
+}
+
+export function renderMessage(locale: string, path: string, substitutions: string[] = []): string {
+ return localeMessage(locale, path).replace(/\$(?:\$|(\d))/g, (match, index) =>
+ index === undefined ? '$' : (substitutions[Number(index) - 1] ?? match),
+ );
+}
diff --git a/src/locales/__tests__/step-strings.test.ts b/src/locales/__tests__/step-strings.test.ts
index 928a287..32965ec 100644
--- a/src/locales/__tests__/step-strings.test.ts
+++ b/src/locales/__tests__/step-strings.test.ts
@@ -1,35 +1,10 @@
-import { readFileSync } from 'node:fs';
-import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
+import { unwrapQuotes } from '@/core/capture/ai/text';
import { SCRUB_PLACEHOLDER, scrubValues, typedValues } from '@/core/transfer/scrub';
-
-const LOCALES = ['en', 'zh-CN', 'es', 'fr', 'de', 'pt-BR'] as const;
-
-function stepMessage(locale: string, key: string): string {
- const lines = readFileSync(join(process.cwd(), `src/locales/${locale}.yml`), 'utf8')
- .replace(/\r\n/g, '\n')
- .split('\n');
-
- let inSteps = false;
- for (const line of lines) {
- if (/^[\w-]+:/.test(line)) inSteps = line.startsWith('steps:');
- if (!inSteps) continue;
- const match = new RegExp(`^ {2}${key}: (.*)$`).exec(line);
- if (!match) continue;
- const raw = match[1].trim();
- if (raw.startsWith("'") && raw.endsWith("'")) return raw.slice(1, -1).replace(/''/g, "'");
- if (raw.startsWith('"') && raw.endsWith('"')) return raw.slice(1, -1).replace(/\\"/g, '"');
- return raw;
- }
- throw new Error(`steps.${key} missing from ${locale}.yml`);
-}
-
-function substitute(message: string, args: string[]): string {
- return message.replace(/\$(\d)/g, (_match, index) => args[Number(index) - 1] ?? '');
-}
+import { LOCALES, renderMessage } from './read-locale';
function describeTyping(locale: string, value: string, label: string): string {
- return substitute(stepMessage(locale, 'typeValueInto'), [value, label]);
+ return renderMessage(locale, 'steps.typeValueInto', [value, label]);
}
describe('localized typing step descriptions', () => {
@@ -44,7 +19,7 @@ describe('localized typing step descriptions', () => {
});
it.each(LOCALES)('%s clears a field without echoing any value', (locale) => {
- const cleared = substitute(stepMessage(locale, 'clearField'), ['Password']);
+ const cleared = renderMessage(locale, 'steps.clearField', ['Password']);
expect(cleared).toContain('Password');
expect(cleared).not.toContain('"');
});
@@ -69,3 +44,21 @@ describe('bundle redaction over localized descriptions', () => {
});
}
});
+
+describe('bundle redaction over AI-written descriptions', () => {
+ const shapes = [
+ { name: 'a sentence opening with the quoted value', text: '"$1" in das Feld $2 eingeben' },
+ { name: 'a sentence closing with the quoted value', text: '在 $2 字段中输入 "$1"' },
+ { name: 'a sentence quoting the value mid-way', text: 'Type "$1" in $2' },
+ { name: 'a description the model wrapped whole', text: '"Type "$1" in $2"' },
+ ];
+
+ for (const { name, text } of shapes) {
+ it(`redacts a short value in ${name}`, () => {
+ const written = unwrapQuotes(text.replace('$1', 'abc').replace('$2', 'Email'));
+ const scrubbed = scrubValues(written, typedValues([{ inputValue: 'abc' }]));
+ expect(scrubbed).not.toContain('abc');
+ expect(scrubbed).toContain(SCRUB_PLACEHOLDER);
+ });
+ }
+});