|
| 1 | +import { test, expect, type Page } from '@playwright/test'; |
| 2 | +import fs from 'node:fs/promises'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import crypto from 'node:crypto'; |
| 6 | +import Database from 'better-sqlite3'; |
| 7 | + |
| 8 | +function getDbPath() { |
| 9 | + const dataDir = process.env.CLAUDE_GUI_DATA_DIR || path.join(os.homedir(), '.codepilot'); |
| 10 | + return path.join(dataDir, 'codepilot.db'); |
| 11 | +} |
| 12 | + |
| 13 | +function addMessage(sessionId: string, role: 'user' | 'assistant', content: string) { |
| 14 | + const db = new Database(getDbPath()); |
| 15 | + try { |
| 16 | + const id = crypto.randomBytes(16).toString('hex'); |
| 17 | + const now = new Date().toISOString().replace('T', ' ').split('.')[0]; |
| 18 | + db.prepare( |
| 19 | + 'INSERT INTO messages (id, session_id, role, content, created_at, token_usage) VALUES (?, ?, ?, ?, ?, ?)' |
| 20 | + ).run(id, sessionId, role, content, now, null); |
| 21 | + db.prepare('UPDATE chat_sessions SET updated_at = ? WHERE id = ?').run(now, sessionId); |
| 22 | + } finally { |
| 23 | + db.close(); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +async function createSession(page: Page, title: string, workingDirectory: string) { |
| 28 | + const res = await page.request.post('/api/chat/sessions', { |
| 29 | + data: { title, working_directory: workingDirectory }, |
| 30 | + }); |
| 31 | + expect(res.ok()).toBeTruthy(); |
| 32 | + const data = await res.json(); |
| 33 | + return data.session.id as string; |
| 34 | +} |
| 35 | + |
| 36 | +test.describe('Global Search modes UX', () => { |
| 37 | + test('supports all/session/message/file modes and keyboard open', async ({ page }) => { |
| 38 | + const suffix = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; |
| 39 | + const rootA = path.join(os.tmpdir(), `codepilot-search-modes-a-${suffix}`); |
| 40 | + const rootB = path.join(os.tmpdir(), `codepilot-search-modes-b-${suffix}`); |
| 41 | + const fileNameA = `alpha-${suffix}.ts`; |
| 42 | + const filePathA = path.join(rootA, 'src', fileNameA); |
| 43 | + const sessionTitleA = `Search Session Alpha ${suffix}`; |
| 44 | + const sessionTitleB = `Search Session Beta ${suffix}`; |
| 45 | + const messageTokenA = `message-token-alpha-${suffix}`; |
| 46 | + const messageTokenB = `message-token-beta-${suffix}`; |
| 47 | + |
| 48 | + await fs.mkdir(path.dirname(filePathA), { recursive: true }); |
| 49 | + await fs.mkdir(rootB, { recursive: true }); |
| 50 | + await fs.writeFile(filePathA, 'export const alpha = true;\n', 'utf8'); |
| 51 | + |
| 52 | + const sessionA = await createSession(page, sessionTitleA, rootA); |
| 53 | + const sessionB = await createSession(page, sessionTitleB, rootB); |
| 54 | + addMessage(sessionA, 'user', `User says ${messageTokenA}`); |
| 55 | + addMessage(sessionB, 'assistant', `Assistant says ${messageTokenB}`); |
| 56 | + |
| 57 | + const searchInput = page.locator( |
| 58 | + 'input[data-slot="command-input"], input[placeholder*="Search"], input[placeholder*="搜索"]' |
| 59 | + ).first(); |
| 60 | + |
| 61 | + try { |
| 62 | + await page.goto(`/chat/${sessionA}`); |
| 63 | + |
| 64 | + // Open global search from the sidebar trigger (language-agnostic fallback). |
| 65 | + await page.getByRole('button', { name: /(搜索会话|Search sessions|Search)/i }).first().click(); |
| 66 | + await expect(searchInput).toBeVisible({ timeout: 10_000 }); |
| 67 | + |
| 68 | + // Default all-mode can find sessions, messages and files. |
| 69 | + await searchInput.fill(suffix); |
| 70 | + await expect(page.getByText(sessionTitleA).first()).toBeVisible(); |
| 71 | + await expect(page.getByText(fileNameA).first()).toBeVisible(); |
| 72 | + await expect(page.getByText(messageTokenA).first()).toBeVisible(); |
| 73 | + |
| 74 | + // session: prefix narrows to session result. |
| 75 | + await searchInput.fill(`session:${sessionTitleA}`); |
| 76 | + await expect(page.getByText(sessionTitleA).first()).toBeVisible(); |
| 77 | + await expect(page.getByText(fileNameA)).toHaveCount(0); |
| 78 | + |
| 79 | + // message: prefix narrows to message snippets and supports navigation to target session. |
| 80 | + await searchInput.fill(`message:${messageTokenB}`); |
| 81 | + await expect(page.getByText(messageTokenB)).toBeVisible({ timeout: 10_000 }); |
| 82 | + await page.getByText(messageTokenB).first().click(); |
| 83 | + await expect(page).toHaveURL(new RegExp(`/chat/${sessionB}\\?message=`), { timeout: 10_000 }); |
| 84 | + |
| 85 | + // Re-open and verify file: prefix still works in the same UX flow. |
| 86 | + await page.getByRole('button', { name: /(搜索会话|Search sessions|Search)/i }).first().click(); |
| 87 | + await expect(searchInput).toBeVisible({ timeout: 10_000 }); |
| 88 | + await searchInput.fill(`file:${fileNameA}`); |
| 89 | + await expect(page.getByText(/(Searching in|当前搜索范围)/)).toBeVisible({ timeout: 10_000 }); |
| 90 | + await expect(page.getByText('file:')).toBeVisible({ timeout: 10_000 }); |
| 91 | + await expect(page.getByText(fileNameA)).toBeVisible({ timeout: 10_000 }); |
| 92 | + await page.getByText(fileNameA).first().click(); |
| 93 | + await expect(page).toHaveURL(new RegExp(`/chat/${sessionA}\\?file=`), { timeout: 10_000 }); |
| 94 | + } finally { |
| 95 | + await page.request.delete(`/api/chat/sessions/${sessionA}`, { timeout: 5_000 }).catch(() => {}); |
| 96 | + await page.request.delete(`/api/chat/sessions/${sessionB}`, { timeout: 5_000 }).catch(() => {}); |
| 97 | + await fs.rm(rootA, { recursive: true, force: true }); |
| 98 | + await fs.rm(rootB, { recursive: true, force: true }); |
| 99 | + } |
| 100 | + }); |
| 101 | +}); |
0 commit comments