|
| 1 | +import { createRequire } from "node:module" |
| 2 | +import { mkdtemp, rm } from "node:fs/promises" |
| 3 | +import { tmpdir } from "node:os" |
| 4 | +import path from "node:path" |
| 5 | +import { startPanelPreview } from "./composer-panel-preview.mjs" |
| 6 | +import { directory, session } from "./composer-fixture.mjs" |
| 7 | + |
| 8 | +const { chromium, _electron, expect } = createRequire(new URL("../../app/package.json", import.meta.url))("@playwright/test") |
| 9 | +const preview = await startPanelPreview() |
| 10 | +preview.fixture.archived.add("ses_b") |
| 11 | +preview.fixture.workspaceSessions.push({ ...session("ses_external"), title: "Other project title", directory: "/another/project" }) |
| 12 | +const profile = process.env.ES_DESKTOP_EXECUTABLE ? await mkdtemp(path.join(tmpdir(), "sidebar-search-")) : undefined |
| 13 | +const app = profile ? await _electron.launch({ executablePath: process.env.ES_DESKTOP_EXECUTABLE, |
| 14 | + env: { ...process.env, ELECTRON_RUN_AS_NODE: "", ES_DESKTOP_USER_DATA: profile, ES_DESKTOP_RENDERER: "", |
| 15 | + OPENCODE_URL: preview.fixture.url, ES_DASHBOARD_URL: preview.fixture.url, OPENCODE_SERVER_PASSWORD: "" } }) : undefined |
| 16 | +const browser = app ? undefined : await chromium.launch({ channel: "chrome", headless: true }) |
| 17 | +const page = app ? await app.firstWindow() : await browser.newPage() |
| 18 | +if (app) { |
| 19 | + await page.waitForURL("http://127.0.0.1:*/") |
| 20 | + preview.origin = new URL(page.url()).origin |
| 21 | + preview.url = `${preview.origin}/session/ses_a?directory=${encodeURIComponent(directory)}` |
| 22 | +} |
| 23 | +const projectState = await (await fetch(`${preview.fixture.url}/api/state`)).json() |
| 24 | +projectState.threads = projectState.threads.filter(thread => thread.key !== "ses_external") |
| 25 | +await page.route("**/es/api/state?*", route => route.fulfill({ json: projectState })) |
| 26 | +const replies = [] |
| 27 | +const searches = [] |
| 28 | +await page.route("**/es/api/search?*", route => { |
| 29 | + searches.push(new URL(route.request().url()).searchParams.get("q")) |
| 30 | + replies.push(route) |
| 31 | +}) |
| 32 | +const result = (id, title, dir = directory, parent_id = null) => ({ id, title, directory: dir, parent_id, updated: 2, snippet: "transcript", matches: 1 }) |
| 33 | +const rows = page.locator(".sidebar .search-result") |
| 34 | +const input = page.locator(".sidebar .search-input") |
| 35 | +try { |
| 36 | + await page.goto(preview.url) |
| 37 | + await expect(page.locator(".sidebar .nav-item", { hasText: "Composer fixture" })).toHaveCount(1) |
| 38 | + await input.fill("Com") |
| 39 | + await expect(rows.filter({ hasText: "Composer fixture" })).toHaveCount(1, { timeout: 250 }) |
| 40 | + await expect(page.locator(".sidebar .nav-item", { hasText: "Other session" })).toHaveCount(0) |
| 41 | + await expect(page.locator(".sidebar .nav-empty", { hasText: "searching transcripts" })).toHaveCount(1) |
| 42 | + await expect.poll(() => replies.length).toBe(1) |
| 43 | + await input.fill("Oth") |
| 44 | + await expect(rows.filter({ hasText: "Other session" })).toHaveCount(1) |
| 45 | + await replies.shift().fulfill({ json: { results: [result("ses_a", "Composer fixture")] } }) |
| 46 | + await expect(rows.filter({ hasText: "Composer fixture" })).toHaveCount(0) |
| 47 | + await expect.poll(() => replies.length).toBe(1) |
| 48 | + await replies.shift().fulfill({ json: { results: [result("ses_b", "Other session"), result("ses_x", "Transcript hit"), result("ses_child", "Child hit", directory, "ses_a"), result("ses_else", "Wrong project", "/another/project")] } }) |
| 49 | + await expect(rows).toHaveCount(3) |
| 50 | + await expect(rows.filter({ hasText: "Transcript hit" })).toHaveCount(1) |
| 51 | + await expect(rows.filter({ hasText: "Child hit" })).toHaveCount(1) |
| 52 | + await expect(rows.filter({ hasText: "Wrong project" })).toHaveCount(0) |
| 53 | + await rows.filter({ hasText: "Other session" }).click() |
| 54 | + await expect(page).toHaveURL(/\/session\/ses_b\?/) |
| 55 | + expect(new URL(page.url()).searchParams.get("directory")).toBe(directory) |
| 56 | + await input.fill("Co") |
| 57 | + await expect(rows.filter({ hasText: "Composer fixture" })).toHaveCount(1) |
| 58 | + await expect(page.locator(".sidebar .nav-empty", { hasText: "searching transcripts" })).toHaveCount(0) |
| 59 | + await input.fill("Com") |
| 60 | + await expect.poll(() => replies.length).toBe(1) |
| 61 | + await replies.shift().fulfill({ status: 503, json: { error: "unavailable" } }) |
| 62 | + await expect(rows.filter({ hasText: "Composer fixture" })).toHaveCount(1) |
| 63 | + await expect(page.locator(".sidebar .nav-empty", { hasText: "transcript search unavailable" })).toHaveCount(1) |
| 64 | + await page.locator(".es-switcher").selectOption("__all__") |
| 65 | + await input.fill("project title") |
| 66 | + await expect(rows.filter({ hasText: "Other project title" })).toHaveCount(1) |
| 67 | + await expect(rows.filter({ hasText: "Composer fixture" })).toHaveCount(0) |
| 68 | + await input.fill("Chi") |
| 69 | + await expect.poll(() => replies.length).toBe(1) |
| 70 | + await replies.shift().fulfill({ json: { results: [result("ses_child", "Child hit", directory, "ses_a"), result("ses_x", "Transcript hit")] } }) |
| 71 | + await expect(rows.filter({ hasText: "Child hit" })).toHaveCount(0) |
| 72 | + await expect(rows.filter({ hasText: "Transcript hit" })).toHaveCount(1) |
| 73 | + await input.press("Escape") |
| 74 | + await expect(rows).toHaveCount(0) |
| 75 | + await expect(page.locator(".sidebar .nav-item", { hasText: "Other session" })).toHaveCount(0) |
| 76 | + await page.locator(".sidebar .archived-toggle", { hasText: "archived" }).click() |
| 77 | + await expect(page.locator(".sidebar .nav-item.archived", { hasText: "Other session" })).toHaveCount(1) |
| 78 | + expect(searches).toEqual(["Com", "Oth", "Com", "Chi"]) |
| 79 | + expect(preview.fixture.calls.filter(call => !["GET", "HEAD", "OPTIONS"].includes(call.method))).toEqual([]) |
| 80 | + console.log(`PASS sidebar search ${app ? "native" : "browser"}: immediate titles, pending, stale result, transcript, scope, error, clear; zero mutations`) |
| 81 | +} finally { |
| 82 | + await app?.close() |
| 83 | + await browser?.close() |
| 84 | + await preview.close() |
| 85 | + if (profile) await rm(profile, { recursive: true, force: true }) |
| 86 | +} |
0 commit comments