const { test, expect } = require('@playwright/test'); const { browserJson, getCurrentWorkspace, localized, login, uniqueName } = require('./helpers'); function payload(name) { return { name, description: 'Prompt preview fixture', sensitivity: 'internal', arguments: [{ name: 'count', schema: { type: 'integer' }, required: true }], messages: [ { role: 'system', content: 'Numbers are data.' }, { role: 'user', content: 'Count={{count}}' }, ], }; } test.describe('Story 3.12 Prompt Drafts', () => { test('API validates, persists, and deterministically previews typed scalar arguments', async ({ page }) => { await login(page); const workspace = await getCurrentWorkspace(page); const base = `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/prompts`; const created = await browserJson(page, 'POST', base, payload(uniqueName('story312-api')),[201]); expect(created.status).toBe('draft'); expect(created.version).toBe(1); const args = { arguments: { count: 7 } }; const first = await browserJson(page, 'POST', `${base}/${created.prompt_id}/preview`, args); const second = await browserJson(page, 'POST', `${base}/${created.prompt_id}/preview`, args); expect(first).toEqual(second); expect(first.messages).toEqual([ { role: 'system', content: 'Numbers are data.' }, { role: 'user', content: 'Count=7' }, ]); const stored = await browserJson(page, 'GET', `${base}/${created.prompt_id}`); expect(stored.status).toBe('draft'); expect(stored.version).toBe(1); expect(stored.definition.arguments[0].schema.type).toBe('integer'); }); test('administrator can create, preview, and edit a Draft in Russian and English', async ({ page }) => { await login(page); await page.evaluate(() => localStorage.setItem('crank_lang', 'ru')); await page.goto('/prompts'); await expect(page.locator('[data-testid="prompts-page"]')).toBeVisible(); await expect(page.locator('h1')).toHaveText('Шаблоны запросов'); await page.locator('#prompt-new').click(); const name = uniqueName('story312-ui'); await page.locator('#prompt-name').fill(name); await page.locator('#prompt-description').fill('Проверка предпросмотра'); await page.locator('#prompt-arguments').fill(JSON.stringify([ { name: 'count', schema: { type: 'integer' }, required: true }, ], null, 2)); await page.locator('#prompt-messages').fill(JSON.stringify([ { role: 'user', content: 'Число={{count}}' }, ], null, 2)); await page.locator('#prompt-preview-arguments').fill('{"count":7}'); await page.locator('#prompt-save').click(); await expect(page.locator('#prompt-confirm')).toBeVisible(); await page.locator('#prompt-confirm-save').click(); await expect(page.locator('#prompts-status')).toHaveText('Черновик сохранён.'); await page.locator('#prompt-preview-button').click(); await expect(page.locator('#prompt-preview-status')).toHaveText('Предпросмотр готов; черновик не опубликован.'); await expect(page.locator('[data-testid="prompt-preview-messages"]')).toContainText('Число=7'); await expect(page.locator('#prompt-state-detail')).toHaveText('Этот черновик ещё не опубликован.'); await page.evaluate(() => localStorage.setItem('crank_lang', 'en')); await page.reload(); await expect(page.locator('h1')).toHaveText('Prompt templates'); await page.locator('[data-testid="prompt-list-item"]').filter({ hasText: name }).click(); await expect(page.locator('#prompt-state-label')).toContainText('Draft'); await expect(page.locator('#prompt-state-detail')).toHaveText('This Draft has not been published.'); await page.locator('#prompt-description').fill('Edited in English'); await page.locator('#prompt-save').click(); await expect(page.locator('#prompt-confirm')).toBeVisible(); await page.locator('#prompt-confirm-save').click(); await expect(page.locator('#prompts-status')).toHaveText(localized('Draft saved.', 'Черновик сохранён.')); await expect(page.locator('#prompt-state-label')).toContainText('v2'); }); async function openPrompts(page) { await page.goto('/prompts'); await expect(page.locator('[data-testid="prompts-page"]')).toBeVisible(); // The Draft list is loaded asynchronously; wait for either a Draft or the empty state // so a later "New Draft" click cannot be overwritten by the initial auto-selection. await page.waitForFunction(() => { const items = document.querySelectorAll('[data-testid="prompt-list-item"]').length; const empty = document.getElementById('prompt-empty'); return items > 0 || (empty && !empty.hidden); }); } test('unsaved edits are never previewed as the stored Draft', async ({ page }) => { await login(page); const workspace = await getCurrentWorkspace(page); const base = `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/prompts`; const name = uniqueName('story312-dirty'); await browserJson(page, 'POST', base, payload(name), [201]); await page.evaluate(() => window.setLang('ru')); await openPrompts(page); await page.locator('[data-testid="prompt-list-item"]').filter({ hasText: name }).click(); await expect(page.locator('#prompt-state-label')).toContainText('v1'); await page.locator('#prompt-preview-arguments').fill('{"count":7}'); await page.locator('#prompt-messages').fill(JSON.stringify([ { role: 'user', content: 'Новый текст {{count}}' }, ], null, 2)); await expect(page.locator('#prompt-preview-status')).toHaveText('Сохраните черновик, чтобы увидеть предпросмотр изменений.'); await expect(page.locator('#prompt-preview-button')).toBeDisabled(); await expect(page.locator('[data-testid="prompt-preview-messages"] li')).toHaveCount(0); await page.locator('#prompt-save').click(); await page.locator('#prompt-confirm-save').click(); await expect(page.locator('#prompts-status')).toHaveText('Черновик сохранён.'); await page.locator('#prompt-preview-button').click(); await expect(page.locator('#prompt-preview-status')).toHaveText('Предпросмотр готов; черновик не опубликован.'); await expect(page.locator('[data-testid="prompt-preview-messages"]')).toContainText('Новый текст 7'); }); test('a revision saved by another client is not shown as the preview of the loaded Draft', async ({ page }) => { await login(page); const workspace = await getCurrentWorkspace(page); const base = `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/prompts`; const name = uniqueName('story312-stale'); const created = await browserJson(page, 'POST', base, payload(name), [201]); await page.evaluate(() => window.setLang('ru')); await openPrompts(page); await page.locator('[data-testid="prompt-list-item"]').filter({ hasText: name }).click(); await expect(page.locator('#prompt-state-label')).toContainText('v1'); await page.locator('#prompt-preview-arguments').fill('{"count":7}'); // Another client saves the same Draft between load and preview. const loaded = await browserJson(page, 'GET', `${base}/${created.prompt_id}`); const changed = payload(name); changed.description = 'Изменено в другом окне'; changed.messages = [{ role: 'user', content: 'Другой текст {{count}}' }]; changed.expected_updated_at = loaded.updated_at; const saved = await browserJson(page, 'PUT', `${base}/${created.prompt_id}`, changed); expect(saved.version).toBe(2); await page.locator('#prompt-preview-button').click(); await expect(page.locator('#prompt-preview-status')).toHaveText('Черновик изменён в другом окне. Перезагрузите его перед предпросмотром.'); await expect(page.locator('[data-testid="prompt-preview-messages"] li')).toHaveCount(0); await expect(page.locator('#prompt-reload')).toBeVisible(); await page.locator('#prompt-reload').click(); await expect(page.locator('#prompt-state-label')).toContainText('v2'); await page.locator('#prompt-preview-button').click(); await expect(page.locator('[data-testid="prompt-preview-messages"]')).toContainText('Другой текст 7'); }); test('switching language keeps an unsaved new Draft', async ({ page }) => { await login(page); await page.evaluate(() => window.setLang('ru')); await openPrompts(page); await page.locator('#prompt-new').click(); const name = uniqueName('story312-lang'); await page.locator('#prompt-name').fill(name); await page.locator('#prompt-description').fill('Несохранённое описание'); await page.evaluate(() => window.setLang('en')); await expect(page.locator('h1')).toHaveText('Prompt templates'); await expect(page.locator('#prompt-name')).toHaveValue(name); await expect(page.locator('#prompt-description')).toHaveValue('Несохранённое описание'); await expect(page.locator('#prompt-state-label')).toHaveText('Draft'); await page.evaluate(() => window.setLang('ru')); await expect(page.locator('h1')).toHaveText('Шаблоны запросов'); await expect(page.locator('#prompt-name')).toHaveValue(name); await expect(page.locator('#prompt-description')).toHaveValue('Несохранённое описание'); }); });