Files
PaperClipAI/ui/src/lib/search-query-parser.test.ts
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

144 lines
4.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
applySearchOperatorSuggestion,
buildSearchPathFromQuery,
parseSearchQuery,
readSearchFiltersFromParams,
searchOperatorSuggestions,
} from "./search-query-parser";
const context = {
currentUserId: "user-1",
agents: [
{ id: "agent-1", name: "Codex Coder", urlKey: "codex-coder" },
{ id: "agent-2", name: "QA" },
],
projects: [
{ id: "11111111-1111-4111-8111-111111111111", name: "Paperclip App", urlKey: "paperclip-app" },
],
labels: [
{ id: "22222222-2222-4222-8222-222222222222", name: "bug" },
],
};
describe("parseSearchQuery", () => {
it("parses status operators", () => {
expect(parseSearchQuery("status:todo auth", context)).toMatchObject({
query: "auth",
filters: { status: ["todo"] },
pills: [{ key: "status", value: "todo", label: "status:todo" }],
});
});
it("parses assignee:me to the current user", () => {
expect(parseSearchQuery("assignee:me", context).filters).toEqual({
assigneeUserId: "user-1",
});
});
it("parses assignee names including quoted multi-word names", () => {
expect(parseSearchQuery("assignee:\"Codex Coder\" crash", context)).toMatchObject({
query: "crash",
filters: { assigneeAgentId: "agent-1" },
pills: [{ key: "assignee", value: "Codex Coder", label: "assignee:Codex Coder" }],
});
});
it("parses project names", () => {
expect(parseSearchQuery("project:paperclip-app", context).filters).toEqual({
projectId: "11111111-1111-4111-8111-111111111111",
});
});
it("parses label names", () => {
expect(parseSearchQuery("label:bug", context).filters).toEqual({
labelId: "22222222-2222-4222-8222-222222222222",
});
});
it("parses priority operators", () => {
expect(parseSearchQuery("priority:high", context).filters).toEqual({
priority: ["high"],
});
});
it("parses updated:>7d as updatedWithin", () => {
expect(parseSearchQuery("updated:>7d", context).filters).toEqual({
updatedWithin: "7d",
});
});
it("parses is:open quick filters", () => {
expect(parseSearchQuery("is:open", context).filters).toEqual({
status: ["backlog", "todo", "in_progress", "in_review", "blocked"],
});
});
it("preserves quoted phrases in free text", () => {
expect(parseSearchQuery("\"auth flake\" status:blocked", context)).toMatchObject({
query: "\"auth flake\"",
filters: { status: ["blocked"] },
});
});
it("parses mixed free text and multiple operators", () => {
expect(parseSearchQuery("auth status:in_progress priority:critical project:paperclip-app", context)).toMatchObject({
query: "auth",
filters: {
status: ["in_progress"],
priority: ["critical"],
projectId: "11111111-1111-4111-8111-111111111111",
},
});
});
it("falls unknown operators through to plain text", () => {
expect(parseSearchQuery("owner:me auth", context)).toMatchObject({
query: "owner:me auth",
filters: {},
pills: [],
});
});
it("falls malformed values through to plain text", () => {
expect(parseSearchQuery("status:notreal updated:>soon priority:urgent", context)).toMatchObject({
query: "status:notreal updated:>soon priority:urgent",
filters: {},
pills: [],
});
});
});
describe("search query URLs", () => {
it("builds /search paths with parsed filters", () => {
expect(buildSearchPathFromQuery("auth status:todo updated:>7d", context)).toBe(
"/search?q=auth&status=todo&updatedWithin=7d",
);
});
it("reads filter params back from URLSearchParams", () => {
const filters = readSearchFiltersFromParams(
new URLSearchParams("q=auth&status=todo&status=blocked&priority=high&updatedWithin=7d"),
);
expect(filters).toEqual({
status: ["todo", "blocked"],
priority: ["high"],
updatedWithin: "7d",
});
});
});
describe("search operator suggestions", () => {
it("suggests syntax for the current partial token", () => {
expect(searchOperatorSuggestions("auth sta").map((suggestion) => suggestion.token)).toEqual([
"status:todo",
"status:blocked",
]);
});
it("replaces only the current token when applying a suggestion", () => {
expect(applySearchOperatorSuggestion("auth sta", "status:todo")).toBe("auth status:todo");
expect(applySearchOperatorSuggestion("", "assignee:me")).toBe("assignee:me");
});
});