import { test, expect, type Page } from './fixtures/test-fixtures' import { TEST_SEARCH, TEST_USER_LOGIN } from './helpers/test-data' /** * Search E2E tests. * * Covers: * - Cmd+K / Ctrl+K palette opens and focuses an input * - Search page URL receives ?q= query parameter * - All four tabs render (repos, people, posts, hashtags) * - Switching tabs changes the results view * - Clicking a user result navigates to their profile * - Clicking a repo result navigates to the repo page * - Nav search bar submits to /explore/repos?q= * - Pasting a GitHub URL in the nav bar navigates to the repo page * * Network strategy: * - "mocked" suite — page.route() intercepts so no live backend needed * - "live" suite — requires E2E_USER_EMAIL / E2E_USER_PASSWORD */ // --------------------------------------------------------------------------- // Mock data & helpers // --------------------------------------------------------------------------- const MOCK_USER = { id: 'search-user-1', username: 'reactdev', email: 'reactdev@gitgram.test', avatar: null, } const MOCK_SEARCH_RESULTS = { users: [ { id: 'u1', username: 'reactdev', fullName: 'React Dev', avatar: null, followersCount: 42 }, ], repos: [ { id: 'r1', owner: 'facebook', name: 'react', fullName: 'facebook/react', description: 'The library for web and native UIs.', stars: 228000, language: 'TypeScript', }, ], posts: [ { id: 'p1', content: 'Hello from the search test', likesCount: 1, user: { username: 'reactdev' } }, ], hashtags: [ { id: 'h1', name: 'javascript', postsCount: 500 }, ], } async function mockAuthApis(page: Page) { await page.route('**/socket.io/**', (route) => route.abort()) await page.route('**/api/v1/auth/me', (route) => route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ user: MOCK_USER }), }), ) await page.route('**/api/v1/auth/login', (route) => route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ user: MOCK_USER }), }), ) } async function mockSearchApis(page: Page) { await page.route('**/api/v1/search**', async (route) => { const url = new URL(route.request().url()) const type = url.searchParams.get('type') ?? 'all' const body: Record = {} if (type === 'users' || type === 'all') body.users = MOCK_SEARCH_RESULTS.users if (type === 'repos' || type === 'all') body.repos = MOCK_SEARCH_RESULTS.repos if (type === 'posts' || type === 'all') body.posts = MOCK_SEARCH_RESULTS.posts if (type === 'hashtags' || type === 'all') body.hashtags = MOCK_SEARCH_RESULTS.hashtags await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(body), }) }) // Also handle the explore/repos search endpoint variant await page.route('**/api/v1/repositories/search**', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ repositories: MOCK_SEARCH_RESULTS.repos }), }) }) } async function injectAuthAndNavigate(page: Page, path: string) { await page.addInitScript((user) => { window.localStorage.setItem('user', JSON.stringify(user)) }, MOCK_USER) await page.goto(path) } // --------------------------------------------------------------------------- // Keyboard shortcut (Cmd+K / Ctrl+K) — mocked // --------------------------------------------------------------------------- test.describe('Search keyboard shortcut (mocked)', () => { test.beforeEach(async ({ page }) => { await page.context().clearCookies() await mockAuthApis(page) await mockSearchApis(page) await injectAuthAndNavigate(page, '/') }) test('Cmd+K / Ctrl+K focuses a search input', async ({ page }) => { const isMac = process.platform === 'darwin' await page.keyboard.press(isMac ? 'Meta+k' : 'Control+k') // Either the nav search input is focused or a modal palette input appears await page.waitForTimeout(300) const activeTag = await page.evaluate( () => document.activeElement?.tagName.toLowerCase() ?? '', ) const isInputFocused = activeTag === 'input' || activeTag === 'textarea' expect(isInputFocused).toBe(true) }) test('Cmd+K shortcut does not navigate away from the page', async ({ page }) => { const isMac = process.platform === 'darwin' await page.keyboard.press(isMac ? 'Meta+k' : 'Control+k') await page.waitForTimeout(300) // Should remain on the current page (home) expect(page.url()).not.toContain('/login') }) }) // --------------------------------------------------------------------------- // Search page structure — mocked // --------------------------------------------------------------------------- test.describe('Search page structure (mocked)', () => { test.beforeEach(async ({ page }) => { await page.context().clearCookies() await mockAuthApis(page) await mockSearchApis(page) await injectAuthAndNavigate(page, '/search') }) test('renders all four search tabs', async ({ searchPage }) => { await expect(searchPage.reposTab).toBeVisible({ timeout: 8_000 }) await expect(searchPage.peopleTab).toBeVisible({ timeout: 8_000 }) await expect(searchPage.postsTab).toBeVisible({ timeout: 8_000 }) await expect(searchPage.hashtagsTab).toBeVisible({ timeout: 8_000 }) }) test('search input is focusable', async ({ searchPage }) => { await searchPage.searchInput.click() await expect(searchPage.searchInput).toBeFocused() }) test('URL updates with ?q= after searching', async ({ searchPage, page }) => { await searchPage.search('react') await expect(page).toHaveURL(/[?&]q=react/, { timeout: 5_000 }) }) }) // --------------------------------------------------------------------------- // Tab switching — mocked // --------------------------------------------------------------------------- test.describe('Search tab switching (mocked)', () => { test.beforeEach(async ({ page }) => { await page.context().clearCookies() await mockAuthApis(page) await mockSearchApis(page) await injectAuthAndNavigate(page, '/search?q=react') }) test('switches to People tab and shows user result', async ({ searchPage, page }) => { await searchPage.switchTab('people') // After switching, should show a user result or an empty state const userResult = page.locator('[data-testid^="user-card-"], [data-testid^="search-user-"]').first() const hasResult = await userResult.isVisible({ timeout: 5_000 }).catch(() => false) if (hasResult) { await expect(userResult).toBeVisible() } else { // Acceptable: empty state message const content = await page.textContent('body') expect(content).toBeTruthy() } }) test('switches to Repos tab and shows repo result', async ({ searchPage, page }) => { await searchPage.switchTab('repos') const repoResult = page .locator('[data-testid^="repo-card-"], [data-testid^="search-repo-"]') .first() const hasResult = await repoResult.isVisible({ timeout: 5_000 }).catch(() => false) if (hasResult) { await expect(repoResult).toBeVisible() } else { const content = await page.textContent('body') expect(content).toBeTruthy() } }) test('switches to Posts tab', async ({ searchPage, page }) => { await searchPage.switchTab('posts') await expect(searchPage.postsTab).toBeVisible() const content = await page.textContent('body') expect(content).toBeTruthy() }) test('switches to Hashtags tab', async ({ searchPage, page }) => { await searchPage.switchTab('hashtags') await expect(searchPage.hashtagsTab).toBeVisible() const content = await page.textContent('body') expect(content).toBeTruthy() }) }) // --------------------------------------------------------------------------- // Search result navigation — mocked // --------------------------------------------------------------------------- test.describe('Search result navigation (mocked)', () => { test.beforeEach(async ({ page }) => { await page.context().clearCookies() await mockAuthApis(page) await mockSearchApis(page) await injectAuthAndNavigate(page, '/search?q=react') }) test('clicking a user result navigates to their profile page', async ({ searchPage, page, }) => { await searchPage.switchTab('people') await page.waitForTimeout(1_000) const profileLink = page .locator('a[href^="/"]') .filter({ hasText: /reactdev/i }) .first() const hasLink = await profileLink.isVisible({ timeout: 5_000 }).catch(() => false) if (!hasLink) { test.skip(true, 'No user result link found — skipping navigation test') } const href = await profileLink.getAttribute('href') await profileLink.click() if (href) { await page.waitForURL(href, { timeout: 10_000 }) await expect(page).toHaveURL(href) } }) test('clicking a repo result navigates to its page', async ({ searchPage, page, }) => { await searchPage.switchTab('repos') await page.waitForTimeout(1_000) const repoLink = page .locator('a[href*="/facebook/react"]') .first() const hasLink = await repoLink.isVisible({ timeout: 5_000 }).catch(() => false) if (!hasLink) { test.skip(true, 'No repo result link found — skipping navigation test') } await repoLink.click() await page.waitForURL(/\/facebook\/react/, { timeout: 10_000 }) expect(page.url()).toContain('react') }) }) // --------------------------------------------------------------------------- // Nav search bar integration — mocked // --------------------------------------------------------------------------- test.describe('Nav search bar integration (mocked)', () => { test.beforeEach(async ({ page }) => { await page.context().clearCookies() await mockAuthApis(page) await mockSearchApis(page) await injectAuthAndNavigate(page, '/') }) test('submits to /explore/repos with q= param', async ({ homePage, page }) => { await homePage.expectAuthenticated() await homePage.search(TEST_SEARCH.repoQuery) await page.waitForURL(/explore\/repos\?q=/, { timeout: 8_000 }) expect(page.url()).toContain(TEST_SEARCH.repoQuery) }) test('pasting a GitHub URL navigates to the repo page', async ({ homePage, page }) => { await homePage.expectAuthenticated() await homePage.search('https://github.com/facebook/react') await page.waitForURL(/\/facebook\/react/, { timeout: 8_000 }) expect(page.url()).toContain('facebook') expect(page.url()).toContain('react') }) }) // --------------------------------------------------------------------------- // Live integration suite (requires seeded backend) // --------------------------------------------------------------------------- test.describe('Search (live integration)', () => { test.beforeEach(async ({ authPage, page }) => { await page.context().clearCookies() await authPage.goto() await authPage.login(TEST_USER_LOGIN.email, TEST_USER_LOGIN.password) await page.waitForURL(/^\/(feed|$)/, { timeout: 12_000 }) }) test('search page loads without application errors', async ({ searchPage, page }) => { await searchPage.goto() await expect(page.locator('body')).not.toContainText('Application error') await expect(searchPage.reposTab).toBeVisible({ timeout: 8_000 }) }) test('Cmd+K focuses an input on the home page', async ({ homePage, page }) => { await homePage.goto() const isMac = process.platform === 'darwin' await page.keyboard.press(isMac ? 'Meta+k' : 'Control+k') await page.waitForTimeout(300) const activeTag = await page.evaluate( () => document.activeElement?.tagName.toLowerCase() ?? '', ) const isInputFocused = activeTag === 'input' || activeTag === 'textarea' expect(isInputFocused).toBe(true) }) test('shows user results when searching by username', async ({ searchPage, page }) => { await searchPage.goto() await searchPage.switchTab('people') await searchPage.search(TEST_SEARCH.userQuery) await page.waitForTimeout(2_000) await expect(searchPage.peopleTab).toBeVisible() const content = await page.textContent('body') expect(content).toBeTruthy() }) test('shows repo results when searching by keyword', async ({ searchPage, page }) => { await searchPage.goto() await searchPage.switchTab('repos') await searchPage.search(TEST_SEARCH.repoQuery) await page.waitForTimeout(2_000) await expect(searchPage.reposTab).toBeVisible() const content = await page.textContent('body') expect(content).toBeTruthy() }) })