Files
PaperClipAI/ui/src/components/CommandPalette.test.tsx
T
606aa4f266 feat(search): filters, sorting, operators & command-palette parity (#9327)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Company search is the primary way operators find issues, comments,
documents, artifacts, agents, and projects across a busy company
> - Search previously supported only a bare text query: no way to narrow
by status/assignee/project/label/date, no sort control, no typed
operators, and weak relevance/snippets meant hunting through noise
> - As companies accumulate tens of thousands of items, unfiltered
single-sort search stops scaling for day-to-day operator workflows
> - This pull request adds a full filtering model (filter bar, chips,
mobile sheet, URL state), sort modes, typed query operators (`status:`,
`assignee:`, `type:`, …) with command-palette parity,
relevance/snippet/deep-link improvements, zero-results recovery, and the
supporting shared validators, backend service work, and DB indexes
> - The benefit is that operators can go from a vague query to the exact
item in a couple of keystrokes, on desktop and mobile, with shareable
filtered-search URLs

## Linked Issues or Issue Description

No existing public GitHub issue; describing the underlying feature
request inline (per feature_request template):

- **Problem:** Company search accepted only a plain text query. Users
could not filter results by status, assignee, project, label, or
recency; could not change result ordering; and got no guidance when
filters emptied the result set.
- **Desired solution:** Structured search filters (UI controls + typed
query operators + URL parameters), selectable sort modes, better
relevance and snippets with exact deep links, and parity between the
search page and the command palette.
- **Alternatives considered:** Client-side filtering of unfiltered
results (does not scale past the fetch limit); a separate "advanced
search" page (splits the surface and duplicates state handling).

Related (not duplicate) PRs found while searching: #4848 (issue search
query planning), #8235 (search rate limiting).

## What Changed

- **Shared contract:** new search filter/sort/count/zero-results types
and validators in `packages/shared` (`validators/search.ts`, types
index).
- **Backend:** `server/src/services/company-search.ts` supports issue
filters, sort modes, per-filter option counts, snippets, artifact
visibility, and zero-results loosen suggestions; single-statement match
replaces per-scope scans and predicates are trigram-index compatible
(~3.7s → ~350ms on a live 14.8k-hit corpus).
- **DB:** migration `0142_company_search_sort_indexes.sql` adds the
supporting indexes.
- **Search page (`ui/src/pages/Search.tsx`):** filter bar, removable
chips, mobile filter sheet with result-count preview, sort menu, URL
round-tripping, zero-results recovery UI.
- **Query operators (`ui/src/lib/search-query-parser.ts`):** typed
operators parsed into filters, operator autocomplete, filter pills.
- **Command palette:** operator-aware parsing and full-search handoff.
- **Stale-operator fix (latest commit):** typed operator filters are no
longer folded into persistent URL-filter state, so deleting a token
(e.g. removing `status:blocked` from the input) actually removes the
filter from subsequent requests; filter-control edits materialize
control state and strip typed tokens so a removed chip cannot resurrect
from the input.

## Verification

- `cd ui && npx vitest run src/pages/Search.test.tsx` — 19 tests
including two new red→green regressions for the stale-operator paths
(both fail on the previous commit, pass now).
- `cd ui && npx vitest run src/components/CommandPalette.test.tsx` and
`cd server && npx vitest run
src/services/company-search-service.test.ts` — operator parity and
backend filter/sort/count coverage.
- `cd ui && npx tsc --noEmit` — clean.
- Manual: open `/search`, type `auth status:blocked`, confirm the status
filter applies; delete `status:blocked`, confirm results are unfiltered
again; drive the same filters from the filter bar/chips/mobile sheet and
confirm the URL round-trips (reload/back/forward preserves state).
- Full end-to-end QA pass (9/9 acceptance checks) against the wireframes
on desktop (1280px) and mobile (390px) with a live API and browser
automation.

## Risks

- Additive migration (indexes only, no data rewrites) — safe to roll
forward; index creation cost is paid once at migrate time.
- Search request shape gains optional parameters only; old clients keep
working.
- Behavioral shift: filter-control edits now strip typed operator tokens
from the query text (their values persist as filter state) — deliberate,
so removed filters stay removed.
- Ranking changes alter result ordering for existing queries; covered by
service tests and the QA pass.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Claude Fable 5 (`claude-fable-5`, Anthropic, extended thinking + tool
use) — stale-operator-filter fix, regression tests, PR preparation.
- GPT-5 Codex (`codex_local` adapter) and Claude Opus 4.6
(`claude-opus-4-6`) — earlier implementation phases (backend contract,
filter UI, operators, ranking) under agent orchestration.

## 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 searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [ ] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
— pre-existing branch name retained to avoid closing/reopening the PR
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes (no
user-facing docs affected)
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green (pending re-run on latest commit)
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
(pending re-review of the stale-filter fix)
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-09 19:32:58 -05:00

502 lines
15 KiB
TypeScript

// @vitest-environment jsdom
import type { KeyboardEventHandler, ReactNode } from "react";
import { flushSync } from "react-dom";
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";
import { queryKeys } from "../lib/queryKeys";
function act(callback: () => void | Promise<void>) {
let result: void | Promise<void> | undefined;
flushSync(() => {
result = callback();
});
return result;
}
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(),
listLabels: vi.fn(),
}));
const mockAgentsApi = vi.hoisted(() => ({
list: vi.fn(),
}));
const mockProjectsApi = vi.hoisted(() => ({
list: vi.fn(),
}));
const mockInstanceSettingsApi = vi.hoisted(() => ({
getExperimental: vi.fn(),
}));
const mockAuthApi = vi.hoisted(() => ({
getSession: 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(),
}));
const locationState = vi.hoisted(() => ({
location: { pathname: "/", search: "", hash: "" },
}));
vi.mock("@/lib/router", () => ({
useNavigate: () => navigateState.navigate,
useLocation: () => locationState.location,
}));
vi.mock("../api/issues", () => ({
issuesApi: mockIssuesApi,
}));
vi.mock("../api/agents", () => ({
agentsApi: mockAgentsApi,
}));
vi.mock("../api/projects", () => ({
projectsApi: mockProjectsApi,
}));
vi.mock("../api/instanceSettings", () => ({
instanceSettingsApi: mockInstanceSettingsApi,
}));
vi.mock("../api/auth", () => ({
authApi: mockAuthApi,
}));
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,
seedQueryClient?: (queryClient: QueryClient) => void,
) {
const root = createRoot(container);
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
});
seedQueryClient?.(queryClient);
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();
mockIssuesApi.listLabels.mockReset();
mockAgentsApi.list.mockReset();
mockProjectsApi.list.mockReset();
mockInstanceSettingsApi.getExperimental.mockReset();
mockAuthApi.getSession.mockReset();
navigateState.navigate.mockReset();
locationState.location.pathname = "/";
locationState.location.search = "";
locationState.location.hash = "";
mockIssuesApi.list.mockResolvedValue([]);
mockIssuesApi.listLabels.mockResolvedValue([]);
mockAgentsApi.list.mockResolvedValue([]);
mockProjectsApi.list.mockResolvedValue([]);
mockInstanceSettingsApi.getExperimental.mockResolvedValue({
enableExperimentalFileViewer: false,
});
mockAuthApi.getSession.mockResolvedValue({ user: { id: "user-1" }, session: { userId: "user-1" } });
});
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("hides the issue file viewer command by default", async () => {
locationState.location.pathname = "/issues/PAP-1";
const { root } = renderWithQueryClient(<CommandPalette />, container);
act(() => {
document.dispatchEvent(new KeyboardEvent("keydown", { key: "k", metaKey: true, bubbles: true }));
});
await waitForAssertion(() => {
expect(container.textContent).toContain("Create new task");
});
expect(container.textContent).not.toContain("Open file in this issue");
act(() => {
root.unmount();
});
});
it("shows the issue file viewer command when the experimental flag is enabled", async () => {
locationState.location.pathname = "/issues/PAP-1";
mockInstanceSettingsApi.getExperimental.mockResolvedValue({
enableExperimentalFileViewer: true,
});
const { root } = renderWithQueryClient(
<CommandPalette />,
container,
(queryClient) => {
queryClient.setQueryData(queryKeys.instance.experimentalSettings, {
enableExperimentalFileViewer: true,
});
},
);
act(() => {
document.dispatchEvent(new KeyboardEvent("keydown", { key: "k", metaKey: true, bubbles: true }));
});
await waitForAssertion(() => {
expect(container.textContent).toContain("Open file in this issue");
});
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+flake");
});
act(() => {
root.unmount();
});
});
it("promotes matching projects above the Tasks group when typing", async () => {
const projects = [
{ id: "p1", urlKey: "mobile", name: "Mobile App", description: "iOS client", archivedAt: null },
{ id: "p2", urlKey: "billing", name: "Billing Service", description: null, archivedAt: null },
];
mockProjectsApi.list.mockResolvedValue(projects);
mockIssuesApi.list.mockImplementation((_companyId: string, opts?: { q?: string }) =>
Promise.resolve(opts?.q ? [{ id: "i1", identifier: "ENG-9", title: "Fix login" }] : []),
);
const { root } = renderWithQueryClient(<CommandPalette />, container, (queryClient) => {
// Seed the caches so the already-loaded data is available synchronously —
// this harness's flush model doesn't reliably propagate fresh async fetches.
queryClient.setQueryData(queryKeys.projects.list("company-1"), projects);
queryClient.setQueryData(queryKeys.issues.search("company-1", "mob", undefined, 10), [
{ id: "i1", identifier: "ENG-9", title: "Fix login" },
]);
});
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, "mob");
input.dispatchEvent(new Event("input", { bubbles: true }));
});
await waitForAssertion(() => {
const match = container.querySelector('button[data-testid="command-project-match"]');
expect(match).not.toBeNull();
expect(match!.textContent).toContain("Mobile App");
});
// Non-matching project is excluded from the typeahead results.
expect(container.textContent).not.toContain("Billing Service");
// The promoted project renders above the fold — before the Tasks group.
await waitForAssertion(() => {
const text = container.textContent ?? "";
expect(text).toContain("Fix login");
expect(text.indexOf("Mobile App")).toBeLessThan(text.indexOf("Fix login"));
});
// Selecting the promoted project navigates to its URL.
act(() => {
container
.querySelector('button[data-testid="command-project-match"]')!
.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
await waitForAssertion(() => {
expect(navigateState.navigate).toHaveBeenCalledWith("/projects/mobile");
});
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();
});
});
it("renders quick-filter chips and inserts them into the palette query", async () => {
const { root } = renderWithQueryClient(<CommandPalette />, container);
act(() => {
document.dispatchEvent(new KeyboardEvent("keydown", { key: "k", metaKey: true, bubbles: true }));
});
await waitForAssertion(() => {
const chips = Array.from(container.querySelectorAll('button[data-testid="command-filter-chip"]'));
expect(chips.map((chip) => chip.textContent)).toEqual(
expect.arrayContaining([
expect.stringContaining("assignee:me"),
expect.stringContaining("is:open"),
expect.stringContaining("updated:>7d"),
]),
);
});
act(() => {
container.querySelector('button[data-testid="command-filter-chip"]')!.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
const input = container.querySelector('input[aria-label="Command search"]') as HTMLInputElement;
await waitForAssertion(() => {
expect(input.value).toBe("assignee:me");
});
act(() => {
root.unmount();
});
});
it("parses operators for lightweight issue search but keeps filters for command-enter handoff", 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, "auth status:blocked updated:>7d");
input.dispatchEvent(new Event("input", { bubbles: true }));
});
await waitForAssertion(() => {
expect(mockIssuesApi.list).toHaveBeenCalledWith("company-1", {
q: "auth",
limit: 10,
includeRoutineExecutions: true,
});
});
act(() => {
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", metaKey: true, bubbles: true }));
});
await waitForAssertion(() => {
expect(navigateState.navigate).toHaveBeenCalledWith("/search?q=auth&status=blocked&updatedWithin=7d");
});
act(() => {
root.unmount();
});
});
});