Files
paperclip/ui/src/components/CommandPalette.test.tsx
T
Dotta 320fd5d23b Add full company search page (#5293)
## Thinking Path

> - Paperclip orchestrates AI agents for zero-human companies.
> - Operators need to find work, documents, agents, projects, comments,
and activity across a company without jumping through separate surfaces.
> - The existing Command-K flow was useful for fast navigation but not
enough for deeper company-wide discovery.
> - Search also needs company-scoped backend contracts, query cost
controls, and indexed document matching so it stays safe as company data
grows.
> - This pull request adds a full company search API and a dedicated
board search page that Command-K can hand off to.
> - The benefit is a single searchable control-plane surface with richer
result context, recents, highlights, and test coverage across server and
UI behavior.

## What Changed

- Added a company-scoped search endpoint/service with query validation,
rate limiting, text matching, fuzzy title matching, and result typing
shared through `@paperclipai/shared`.
- Added idempotent search migrations for document search indexes and
fuzzy matching support.
- Added the full `/companies/:companyKey/search` UI, search result row
components, highlighted snippets, recent searches, and sidebar/Command-K
handoff.
- Added Storybook coverage for search surfaces and Vitest coverage for
server search behavior, rate limiting, route generation, Command-K
behavior, and the search page.
- Addressed Greptile findings by renaming the no-match SQL helper,
applying search pagination after cross-type merge sorting, and
lazy-initializing the default search service so unrelated route-test
mocks do not need to know about it.
- Merged current `public-gh/master` and renumbered the search migrations
behind upstream `0078_white_darwin`: search indexes are now
`0079_company_search_document_indexes` and fuzzy matching is
`0080_company_search_fuzzystrmatch`.

## Verification

- `git fetch public-gh master`
- `git diff --check public-gh/master...HEAD`
- `git diff --name-only public-gh/master...HEAD | rg '^pnpm-lock\.yaml$'
|| true` produced no output before opening the PR.
- `pnpm run preflight:workspace-links && pnpm exec vitest run
server/src/__tests__/company-search-service.test.ts
server/src/__tests__/company-search-rate-limit-routes.test.ts
ui/src/pages/Search.test.tsx ui/src/components/CommandPalette.test.tsx
ui/src/lib/company-routes.test.ts` passed: 5 files, 25 tests.
- `pnpm --filter @paperclipai/shared typecheck && pnpm --filter
@paperclipai/db typecheck && pnpm --filter @paperclipai/server typecheck
&& pnpm --filter @paperclipai/ui typecheck` passed.
- `pnpm exec vitest run
server/src/__tests__/company-search-service.test.ts
server/src/__tests__/company-search-rate-limit-routes.test.ts && pnpm
--filter @paperclipai/server typecheck` passed after Greptile pagination
fixes.
- `pnpm exec vitest run
server/src/__tests__/issue-agent-mutation-ownership-routes.test.ts
server/src/__tests__/company-search-rate-limit-routes.test.ts
server/src/__tests__/company-search-service.test.ts && pnpm --filter
@paperclipai/server typecheck` passed after the CI mock fix.
- After resolving the migration conflict with current
`public-gh/master`: `pnpm --filter @paperclipai/db typecheck && pnpm
exec vitest run server/src/__tests__/company-search-service.test.ts
server/src/__tests__/company-search-rate-limit-routes.test.ts && pnpm
--filter @paperclipai/server typecheck` passed.
- DB migration numbering check passed as part of `@paperclipai/db`
typecheck.
- UI states are covered by the added Storybook stories in
`ui/storybook/stories/search.stories.tsx`.
- GitHub reports the PR merge state as `CLEAN` on head `18e54fa8`.
- GitHub PR checks are green on head `18e54fa8`: policy, verify,
serialized server shards 1/4 through 4/4, e2e, canary dry run, Snyk, and
Greptile Review.

## Risks

- Search ranking and snippets are new user-facing behavior, so reviewers
should check whether result ordering feels right on real company data.
- Search touches broad company data, so company scoping and query
cost/rate-limit behavior should be reviewed carefully.
- The migrations add search indexes/extensions; they are idempotent with
`IF NOT EXISTS` for users who may have applied an earlier branch
migration number.

> ROADMAP.md checked. This PR adds a focused board search surface and
does not duplicate an open roadmap item.

## Model Used

- OpenAI Codex, GPT-5 coding agent, tool-enabled shell/git/GitHub CLI
session with medium reasoning effort. Existing branch commits were
produced across prior agent sessions; this packaging pass verified,
opened the PR, addressed Greptile findings, resolved migration conflicts
after upstream PRs landed, and got PR checks green.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] If this change affects the UI, I have included before/after
screenshots
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-06 06:32:37 -05:00

280 lines
7.6 KiB
TypeScript

// @vitest-environment jsdom
import { act } from "react";
import type { KeyboardEventHandler, ReactNode } from "react";
import { createRoot } from "react-dom/client";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { CommandPalette } from "./CommandPalette";
const companyState = vi.hoisted(() => ({
selectedCompanyId: "company-1",
}));
const dialogState = vi.hoisted(() => ({
openNewIssue: vi.fn(),
openNewAgent: vi.fn(),
}));
const sidebarState = vi.hoisted(() => ({
isMobile: false,
setSidebarOpen: vi.fn(),
}));
const mockIssuesApi = vi.hoisted(() => ({
list: vi.fn(),
}));
const mockAgentsApi = vi.hoisted(() => ({
list: vi.fn(),
}));
const mockProjectsApi = vi.hoisted(() => ({
list: vi.fn(),
}));
vi.mock("../context/CompanyContext", () => ({
useCompany: () => companyState,
}));
vi.mock("../context/DialogContext", () => ({
useDialog: () => dialogState,
useDialogActions: () => dialogState,
}));
vi.mock("../context/SidebarContext", () => ({
useSidebar: () => sidebarState,
}));
const navigateState = vi.hoisted(() => ({
navigate: vi.fn(),
}));
vi.mock("@/lib/router", () => ({
useNavigate: () => navigateState.navigate,
}));
vi.mock("../api/issues", () => ({
issuesApi: mockIssuesApi,
}));
vi.mock("../api/agents", () => ({
agentsApi: mockAgentsApi,
}));
vi.mock("../api/projects", () => ({
projectsApi: mockProjectsApi,
}));
vi.mock("./Identity", () => ({
Identity: ({ name }: { name: string }) => <span>{name}</span>,
}));
vi.mock("@/components/ui/command", () => ({
CommandDialog: ({ open, children }: { open: boolean; children: ReactNode }) => (open ? <div>{children}</div> : null),
CommandEmpty: ({ children }: { children: ReactNode }) => <div>{children}</div>,
CommandGroup: ({ children }: { children: ReactNode }) => <div>{children}</div>,
CommandInput: ({
value,
onValueChange,
onKeyDown,
}: {
value: string;
onValueChange: (value: string) => void;
onKeyDown?: KeyboardEventHandler<HTMLInputElement>;
}) => (
<div>
<input
aria-label="Command search"
value={value}
onChange={(event) => onValueChange(event.currentTarget.value)}
onKeyDown={onKeyDown}
/>
<button type="button" aria-label="Set query" onClick={() => onValueChange("pull/3303")} />
</div>
),
CommandItem: ({
children,
onSelect,
"data-testid": testId,
}: {
children: ReactNode;
onSelect?: () => void;
"data-testid"?: string;
}) => (
<button data-testid={testId} onClick={onSelect}>
{children}
</button>
),
CommandList: ({ children }: { children: ReactNode }) => <div>{children}</div>,
CommandSeparator: () => <hr />,
}));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
async function flush() {
await act(async () => {
await Promise.resolve();
});
}
async function waitForAssertion(assertion: () => void, attempts = 20) {
let lastError: unknown;
for (let attempt = 0; attempt < attempts; attempt += 1) {
try {
assertion();
return;
} catch (error) {
lastError = error;
await flush();
}
}
throw lastError;
}
function renderWithQueryClient(node: ReactNode, container: HTMLDivElement) {
const root = createRoot(container);
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
});
act(() => {
root.render(
<QueryClientProvider client={queryClient}>
{node}
</QueryClientProvider>,
);
});
return { root, queryClient };
}
describe("CommandPalette", () => {
let container: HTMLDivElement;
beforeEach(() => {
container = document.createElement("div");
document.body.appendChild(container);
dialogState.openNewIssue.mockReset();
dialogState.openNewAgent.mockReset();
sidebarState.setSidebarOpen.mockReset();
mockIssuesApi.list.mockReset();
mockAgentsApi.list.mockReset();
mockProjectsApi.list.mockReset();
navigateState.navigate.mockReset();
mockIssuesApi.list.mockResolvedValue([]);
mockAgentsApi.list.mockResolvedValue([]);
mockProjectsApi.list.mockResolvedValue([]);
});
afterEach(() => {
container.remove();
});
it("includes routine execution issues in search queries", async () => {
const { root } = renderWithQueryClient(<CommandPalette />, container);
act(() => {
document.dispatchEvent(new KeyboardEvent("keydown", { key: "k", metaKey: true, bubbles: true }));
});
const setQueryButton = container.querySelector('button[aria-label="Set query"]');
expect(setQueryButton).not.toBeNull();
act(() => {
setQueryButton!.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
await waitForAssertion(() => {
expect(mockIssuesApi.list).toHaveBeenCalledWith("company-1", {
q: "pull/3303",
limit: 10,
includeRoutineExecutions: true,
});
});
act(() => {
root.unmount();
});
});
it("offers a Search-all command when the query is non-empty and routes Enter to /search when no issues match", async () => {
mockIssuesApi.list.mockResolvedValue([]);
const { root } = renderWithQueryClient(<CommandPalette />, container);
act(() => {
document.dispatchEvent(new KeyboardEvent("keydown", { key: "k", metaKey: true, bubbles: true }));
});
const input = container.querySelector('input[aria-label="Command search"]') as HTMLInputElement;
expect(input).not.toBeNull();
act(() => {
const nativeSetter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!;
nativeSetter.call(input, "auth flake");
input.dispatchEvent(new Event("input", { bubbles: true }));
});
await waitForAssertion(() => {
const searchAllButton = container.querySelector(
'button[data-testid="command-search-all"]',
) as HTMLButtonElement | null;
expect(searchAllButton).not.toBeNull();
expect(searchAllButton!.textContent).toContain("auth flake");
});
act(() => {
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
});
await waitForAssertion(() => {
expect(navigateState.navigate).toHaveBeenCalledWith("/search?q=auth%20flake");
});
act(() => {
root.unmount();
});
});
it("navigates to /search when the user clicks the Search-all command", async () => {
mockIssuesApi.list.mockResolvedValue([]);
const { root } = renderWithQueryClient(<CommandPalette />, container);
act(() => {
document.dispatchEvent(new KeyboardEvent("keydown", { key: "k", metaKey: true, bubbles: true }));
});
const input = container.querySelector('input[aria-label="Command search"]') as HTMLInputElement;
act(() => {
const nativeSetter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!;
nativeSetter.call(input, "deflake");
input.dispatchEvent(new Event("input", { bubbles: true }));
});
let searchAllButton: HTMLButtonElement | null = null;
await waitForAssertion(() => {
searchAllButton = container.querySelector(
'button[data-testid="command-search-all"]',
) as HTMLButtonElement | null;
expect(searchAllButton).not.toBeNull();
});
act(() => {
searchAllButton!.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
await waitForAssertion(() => {
expect(navigateState.navigate).toHaveBeenCalledWith("/search?q=deflake");
});
act(() => {
root.unmount();
});
});
});