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(ai): pin the localized prompts and their placeholders

davbio133e979c3f7c
diff --git a/src/core/capture/ai/__tests__/description.test.ts b/src/core/capture/ai/__tests__/description.test.ts
new file mode 100644
index 0000000..eb376d3
--- /dev/null
+++ b/src/core/capture/ai/__tests__/description.test.ts
@@ -0,0 +1,63 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const { generateTextMock } = vi.hoisted(() => ({ generateTextMock: vi.fn() }));
+
+vi.mock('ai', () => ({ generateText: generateTextMock }));
+
+vi.mock('../provider', () => ({ createModel: () => ({ id: 'test-model' }) }));
+
+vi.mock('@/lib/browser-api', () => ({
+ localStorage: { get: vi.fn().mockResolvedValue({ aiLanguage: 'en' }) },
+}));
+
+import { localStorage } from '@/lib/browser-api';
+import type { DOMContext } from '../../dom/context';
+import { getAIDescription } from '../description';
+
+function makeContext(overrides: Partial<DOMContext> = {}): DOMContext {
+ return {
+ page: { title: 'Settings', path: '/settings' },
+ container: null,
+ heading: null,
+ siblings: [],
+ target: { tag: 'button', role: null, name: 'Update profile', value: null, action: 'click' },
+ ...overrides,
+ };
+}
+
+async function promptFor(aiLanguage: string, context = makeContext()): Promise<string> {
+ vi.mocked(localStorage.get).mockResolvedValue({ aiLanguage });
+ await getAIDescription(context, 'openai', 'gpt-4o-mini', 'key');
+ return generateTextMock.mock.calls[0][0].prompt as string;
+}
+
+describe('getAIDescription', () => {
+ beforeEach(() => {
+ generateTextMock.mockReset();
+ generateTextMock.mockResolvedValue({ text: 'Click Update profile' });
+ vi.mocked(localStorage.get).mockResolvedValue({ aiLanguage: 'en' });
+ });
+
+ it('leaves no placeholder unfilled', async () => {
+ expect(await promptFor('en')).not.toMatch(/\{\{\w+\}\}/);
+ });
+
+ it('gives the model examples in the configured language', async () => {
+ const prompt = await promptFor('fr');
+ expect(prompt).toContain('Cliquer sur le bouton Submit');
+ expect(prompt).not.toContain('Click the Submit button');
+ });
+
+ it('writes the language instruction in that language too', async () => {
+ expect(await promptFor('de')).toContain('WICHTIG');
+ });
+
+ it('appends no language instruction for English', async () => {
+ expect(await promptFor('en')).not.toContain('IMPORTANT: Write the output in');
+ });
+
+ it('does not let page text act as a replacement pattern', async () => {
+ const prompt = await promptFor('en', makeContext({ page: { title: 'Save $& and $` now', path: '/x' } }));
+ expect(prompt).toContain('Save $& and $` now');
+ });
+});
diff --git a/src/core/capture/ai/__tests__/examples.test.ts b/src/core/capture/ai/__tests__/examples.test.ts
new file mode 100644
index 0000000..dbf4e78
--- /dev/null
+++ b/src/core/capture/ai/__tests__/examples.test.ts
@@ -0,0 +1,70 @@
+import { describe, expect, it } from 'vitest';
+import { formatExamples, resolveExamples } from '../examples';
+import { AI_LANGUAGES } from '../prompts';
+
+const TITLE_LIMIT = 60;
+
+describe('resolveExamples', () => {
+ it('has its own examples for every offered language, not the English fallback', () => {
+ const english = resolveExamples('en');
+ for (const { code } of AI_LANGUAGES) {
+ const examples = resolveExamples(code);
+ expect(examples.steps.length).toBeGreaterThan(0);
+ expect(examples.titles.length).toBeGreaterThan(0);
+ expect(examples.descriptions.length).toBeGreaterThan(0);
+ if (code !== 'en') expect(examples).not.toBe(english);
+ }
+ });
+
+ it('matches a regional locale to its base language', () => {
+ expect(resolveExamples('fr-CA')).toBe(resolveExamples('fr'));
+ expect(resolveExamples('pt')).toBe(resolveExamples('pt-BR'));
+ });
+
+ it('falls back to English for an unknown locale', () => {
+ expect(resolveExamples('sv')).toBe(resolveExamples('en'));
+ });
+
+ it('keeps every title example under the limit the prompt states', () => {
+ for (const { code } of AI_LANGUAGES) {
+ for (const title of resolveExamples(code).titles) {
+ expect(title.length).toBeLessThan(TITLE_LIMIT);
+ }
+ }
+ });
+
+ it('leaves UI labels and product names untranslated, so the examples teach the rule', () => {
+ for (const { code } of AI_LANGUAGES) {
+ const { steps, titles } = resolveExamples(code);
+ expect(steps.join(' ')).toContain('Submit');
+ expect(steps.join(' ')).toContain('Settings');
+ expect(titles.join(' ')).toContain('claude-code');
+ expect(titles.join(' ')).toContain('Workday');
+ }
+ });
+
+ it('quotes values with straight quotes, which is what redaction matches on', () => {
+ for (const { code } of AI_LANGUAGES) {
+ const text = resolveExamples(code).steps.join(' ');
+ expect(text).not.toMatch(/[“”«»]/);
+ }
+ });
+});
+
+describe('formatExamples', () => {
+ it('renders one bullet per line', () => {
+ expect(formatExamples(['One', 'Two'])).toBe('- One\n- Two');
+ });
+
+ it('does not wrap a line that already quotes a value', () => {
+ expect(formatExamples(['Select "Admin" from the Role dropdown'])).toBe('- Select "Admin" from the Role dropdown');
+ });
+
+ it('keeps every rendered example free of nested quote collisions', () => {
+ for (const { code } of AI_LANGUAGES) {
+ for (const line of formatExamples(resolveExamples(code).steps).split('\n')) {
+ expect(line).not.toMatch(/^- ".*".*"/);
+ }
+ }
+ });
+});
diff --git a/src/core/capture/ai/__tests__/meta.test.ts b/src/core/capture/ai/__tests__/meta.test.ts
index 2ffee00..2fd7eed 100644
--- a/src/core/capture/ai/__tests__/meta.test.ts
+++ b/src/core/capture/ai/__tests__/meta.test.ts
@@ -17,6 +17,7 @@ vi.mock('@/lib/browser-api', () => ({
localStorage: { get: vi.fn().mockResolvedValue({ aiLanguage: 'en' }) },
}));
+import { localStorage } from '@/lib/browser-api';
import { generateGuideMeta, parseGuideMeta } from '../meta';
const steps = [{ description: 'Click Directory', url: 'https://admin.okta.com/users' }];
@@ -194,3 +195,37 @@ describe('parseGuideMeta', () => {
expect(parseGuideMeta(' ')).toBeNull();
});
});
+
+describe('guide meta prompt', () => {
+ beforeEach(() => {
+ generateObjectMock.mockReset();
+ generateObjectMock.mockResolvedValue({ object: { title: 'A title', description: 'A description.' } });
+ });
+
+ async function promptFor(aiLanguage: string): Promise<string> {
+ vi.mocked(localStorage.get).mockResolvedValue({ aiLanguage });
+ await generateGuideMeta(steps, 'openai', 'gpt-4o-mini', 'key');
+ return generateObjectMock.mock.calls[0][0].prompt as string;
+ }
+
+ it('leaves no placeholder unfilled', async () => {
+ expect(await promptFor('en')).not.toMatch(/\{\{\w+\}\}/);
+ });
+
+ it('gives the model examples in the configured language', async () => {
+ const prompt = await promptFor('fr');
+ expect(prompt).toContain('Configurer les notifications Slack');
+ expect(prompt).not.toContain('Configure Slack Notification Preferences');
+ });
+
+ it('follows the description language, not the UI language', async () => {
+ const prompt = await promptFor('de');
+ expect(prompt).toContain('Spesenabrechnung in Workday einreichen');
+ });
+
+ it('does not let step text act as a replacement pattern', async () => {
+ vi.mocked(localStorage.get).mockResolvedValue({ aiLanguage: 'en' });
+ await generateGuideMeta([{ description: 'Click $& then $`', url: 'https://example.com' }], 'openai', 'm', 'key');
+ expect(generateObjectMock.mock.calls[0][0].prompt).toContain('Click $& then $`');
+ });
+});
diff --git a/src/core/capture/ai/__tests__/prompt-render.test.ts b/src/core/capture/ai/__tests__/prompt-render.test.ts
new file mode 100644
index 0000000..a0abbac
--- /dev/null
+++ b/src/core/capture/ai/__tests__/prompt-render.test.ts
@@ -0,0 +1,119 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const { generateTextMock, generateObjectMock } = vi.hoisted(() => ({
+ generateTextMock: vi.fn(),
+ generateObjectMock: vi.fn(),
+}));
+
+vi.mock('ai', () => ({
+ generateText: generateTextMock,
+ generateObject: generateObjectMock,
+ jsonSchema: (schema: unknown) => schema,
+}));
+
+vi.mock('../provider', () => ({ createModel: () => ({ id: 'test-model' }) }));
+
+vi.mock('@/lib/browser-api', () => ({
+ localStorage: { get: vi.fn().mockResolvedValue({ aiLanguage: 'en' }) },
+}));
+
+import { localStorage } from '@/lib/browser-api';
+import type { DOMContext } from '../../dom/context';
+import { getAIDescription } from '../description';
+import { resolveExamples } from '../examples';
+import { generateGuideMeta } from '../meta';
+import { AI_LANGUAGES, type AILanguageCode } from '../prompts';
+
+const CONTEXT: DOMContext = {
+ page: { title: 'Settings', path: '/settings' },
+ container: null,
+ heading: null,
+ siblings: [],
+ target: { tag: 'button', role: null, name: 'Update profile', value: null, action: 'click' },
+};
+
+const STEPS = [{ description: 'Click Directory', url: 'https://admin.okta.com/users' }];
+
+async function stepPrompt(aiLanguage: string): Promise<string> {
+ vi.mocked(localStorage.get).mockResolvedValue({ aiLanguage });
+ generateTextMock.mockReset();
+ generateTextMock.mockResolvedValue({ text: 'ok' });
+ await getAIDescription(CONTEXT, 'openai', 'gpt-4o-mini', 'key');
+ return generateTextMock.mock.calls[0][0].prompt as string;
+}
+
+async function metaPrompt(aiLanguage: string): Promise<string> {
+ vi.mocked(localStorage.get).mockResolvedValue({ aiLanguage });
+ generateObjectMock.mockReset();
+ generateObjectMock.mockResolvedValue({ object: { title: 'T', description: 'D' } });
+ await generateGuideMeta(STEPS, 'openai', 'gpt-4o-mini', 'key');
+ return generateObjectMock.mock.calls[0][0].prompt as string;
+}
+
+const CODES = AI_LANGUAGES.map((l) => l.code) as readonly AILanguageCode[];
+
+const EXPECTED: Record<AILanguageCode, { step: string; title: string }> = {
+ en: { step: 'Click the Submit button', title: 'Configure Slack Notification Preferences' },
+ es: { step: 'Hacer clic en el botón Submit', title: 'Configurar las notificaciones de Slack' },
+ fr: { step: 'Cliquer sur le bouton Submit', title: 'Configurer les notifications Slack' },
+ de: { step: 'Auf die Schaltfläche Submit klicken', title: 'Slack-Benachrichtigungen konfigurieren' },
+ 'pt-BR': { step: 'Clicar no botão Submit', title: 'Configurar as notificações do Slack' },
+ 'zh-CN': { step: '点击 Submit 按钮', title: '配置 Slack 通知偏好' },
+};
+
+describe.each(CODES)('rendered prompts for %s', (code) => {
+ beforeEach(() => {
+ vi.mocked(localStorage.get).mockResolvedValue({ aiLanguage: code });
+ });
+
+ it('fills every placeholder in both prompts', async () => {
+ expect(await stepPrompt(code)).not.toMatch(/\{\{\w+\}\}/);
+ expect(await metaPrompt(code)).not.toMatch(/\{\{\w+\}\}/);
+ });
+
+ it('carries this language own examples', async () => {
+ expect(await stepPrompt(code)).toContain(EXPECTED[code].step);
+ expect(await metaPrompt(code)).toContain(EXPECTED[code].title);
+ });
+
+ it('renders every example bullet without a nested quote collision', async () => {
+ for (const prompt of [await stepPrompt(code), await metaPrompt(code)]) {
+ for (const line of prompt.split('\n')) {
+ if (line.startsWith('- ')) expect(line).not.toMatch(/^- ".*".*"/);
+ }
+ }
+ });
+
+ it('keeps UI labels and product names untranslated in the examples', async () => {
+ const step = await stepPrompt(code);
+ expect(step).toContain('Submit');
+ expect(step).toContain('Settings');
+ const meta = await metaPrompt(code);
+ expect(meta).toContain('claude-code');
+ expect(meta).toContain('Workday');
+ });
+
+ it('states the never-translate rule in both prompts', async () => {
+ expect(await stepPrompt(code)).toContain('Never translate');
+ expect(await metaPrompt(code)).toContain('Never translate');
+ });
+
+ it('appends a native-language instruction, or none at all for English', async () => {
+ const step = await stepPrompt(code);
+ if (code === 'en') {
+ expect(step).not.toContain('IMPORTANT: Write the output in');
+ expect(step).not.toContain('IMPORTANTE');
+ } else {
+ expect(step).not.toContain('Write the output in');
+ expect(await metaPrompt(code)).not.toContain('Write the output in');
+ }
+ });
+
+ it('never leaks another language examples', async () => {
+ const step = await stepPrompt(code);
+ for (const other of CODES) {
+ if (other === code) continue;
+ expect(step).not.toContain(resolveExamples(other).steps[0]);
+ }
+ });
+});
diff --git a/src/core/capture/ai/__tests__/prompts.test.ts b/src/core/capture/ai/__tests__/prompts.test.ts
index c895d88..276e74e 100644
--- a/src/core/capture/ai/__tests__/prompts.test.ts
+++ b/src/core/capture/ai/__tests__/prompts.test.ts
@@ -25,31 +25,48 @@ describe('getLanguageSuffix', () => {
expect(getLanguageSuffix('en-US')).toBe('');
});
- it('returns Spanish suffix for es', () => {
- expect(getLanguageSuffix('es')).toContain('Spanish');
+ it('writes the Spanish instruction in Spanish', () => {
+ expect(getLanguageSuffix('es')).toContain('en español');
});
- it('returns French suffix for fr', () => {
- expect(getLanguageSuffix('fr')).toContain('French');
+ it('writes the French instruction in French', () => {
+ expect(getLanguageSuffix('fr')).toContain('en français');
});
- it('returns Chinese suffix for zh-CN', () => {
- expect(getLanguageSuffix('zh-CN')).toContain('Chinese');
+ it('writes the Chinese instruction in Chinese', () => {
+ expect(getLanguageSuffix('zh-CN')).toContain('请用中文输出');
});
- it('returns Brazilian Portuguese suffix for pt-BR', () => {
- expect(getLanguageSuffix('pt-BR')).toContain('Brazilian Portuguese');
+ it('writes the Brazilian Portuguese instruction in Portuguese', () => {
+ expect(getLanguageSuffix('pt-BR')).toContain('português do Brasil');
});
- it('returns the locale code for unknown languages', () => {
- expect(getLanguageSuffix('sv')).toContain('sv');
+ it('matches a regional locale to its base language', () => {
+ expect(getLanguageSuffix('fr-CA')).toBe(getLanguageSuffix('fr'));
});
- it('includes IMPORTANT instruction', () => {
- const suffix = getLanguageSuffix('es');
- expect(suffix).toContain('IMPORTANT');
+ it('falls back to the English instruction and the locale code for unknown languages', () => {
+ const suffix = getLanguageSuffix('sv');
+ expect(suffix).toContain('sv');
expect(suffix).toContain('Write the output in');
});
+
+ it('writes every offered language its own instruction, never the English fallback', () => {
+ for (const { code } of AI_LANGUAGES) {
+ if (code === 'en') continue;
+ expect(getLanguageSuffix(code)).not.toContain('Write the output in');
+ }
+ });
+
+ it('writes the German instruction in German', () => {
+ expect(getLanguageSuffix('de')).toContain('auf Deutsch');
+ });
+
+ it('tells every language to leave UI labels alone', () => {
+ expect(getLanguageSuffix('es')).toContain('No traduzcas');
+ expect(getLanguageSuffix('de')).toContain('Übersetze keine');
+ expect(getLanguageSuffix('sv')).toContain('Never translate');
+ });
});
describe('AI_LANGUAGES', () => {
diff --git a/src/core/capture/ai/__tests__/rewrite.test.ts b/src/core/capture/ai/__tests__/rewrite.test.ts
index 7abd71b..a99b036 100644
--- a/src/core/capture/ai/__tests__/rewrite.test.ts
+++ b/src/core/capture/ai/__tests__/rewrite.test.ts
@@ -52,7 +52,7 @@ describe('buildRewritePrompt', () => {
});
it('appends a language suffix for non-English locales', () => {
- expect(buildRewritePrompt('Click Save', 'Make it shorter.', 'fr')).toContain('French');
+ expect(buildRewritePrompt('Click Save', 'Make it shorter.', 'fr')).toContain('en français');
});
it('appends nothing for English', () => {