Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/api/src/postgres/operations-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class OperationsRepository {
CASE WHEN word_similarity($2,j.name) >= word_similarity($2,j.slug) THEN j.name ELSE j.slug END match_text,
GREATEST(word_similarity($2,j.name),word_similarity($2,j.slug)) score,
CASE WHEN lower(j.name)=$2 OR lower(j.slug)=$2 THEN 1 ELSE 2 END category,j.id::text stable_key
FROM app.jobs j WHERE j.tenant_id=$1 AND (j.name <% $2 OR j.slug <% $2)
FROM app.jobs j WHERE j.tenant_id=$1 AND ($2 <% j.name OR $2 <% j.slug)
UNION ALL
SELECT 'run',r.id::text,coalesce(nullif(r.run_name,''),nullif(r.issue_title,''),'Run '||left(r.id::text,8)),r.state,'/job-runs/'||r.id,r.id::text,
coalesce(nullif(r.run_name,''),nullif(r.issue_title,''),'Run '||left(r.id::text,8)),
Expand All @@ -32,7 +32,7 @@ export class OperationsRepository {
ELSE coalesce(nullif(r.issue_title,''),r.issue_id) END match_text,
GREATEST(similarity(r.issue_id,$2),word_similarity($2,r.run_name),word_similarity($2,r.issue_title)) score,
CASE WHEN lower(r.issue_id)=$2 THEN 0 WHEN lower(r.run_name)=$2 OR lower(r.issue_title)=$2 THEN 1 ELSE 2 END category,r.id::text stable_key
FROM app.runs r WHERE r.tenant_id=$1 AND (r.issue_id % $2 OR r.run_name <% $2 OR r.issue_title <% $2)
FROM app.runs r WHERE r.tenant_id=$1 AND (r.issue_id % $2 OR $2 <% r.run_name OR $2 <% r.issue_title)
) found ORDER BY category,score DESC,kind,stable_key LIMIT 100`, [this.tenantId, needle.toLowerCase()]);
return { items: rows.rows.map(row => ({
kind: row.kind, id: row.id, title: row.title, subtitle: row.subtitle, url: row.url,
Expand Down
18 changes: 17 additions & 1 deletion packages/api/test/search.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { rankField, sessionExcerpt } from "../src/search";
import { scrubSession } from "../src/session-scrubber";
import { OperationsRepository } from "../src/postgres/operations-repository";

describe("global search", () => {
it("finds a hyphenated identifier in a longer run name within the tenant", async () => {
const query = vi.fn(async () => ({ rows: [{
kind: "run", id: "run-id", title: "GEN-2310: PR #222: fix failed Test and validate Workers check (450b8cc6)",
subtitle: "succeeded", url: "/job-runs/run-id", source_id: "run-id", source_label: "GEN-2310: PR #222: fix failed Test and validate Workers check (450b8cc6)",
match_text: "GEN-2310: PR #222: fix failed Test and validate Workers check (450b8cc6)",
}] }));
const repository = new OperationsRepository({ pool: { query } } as any, "authorized-tenant");

await expect(repository.search("GEN-2310")).resolves.toMatchObject({ items: [{ kind: "run", id: "run-id" }] });
const [sql, values] = query.mock.calls[0] as unknown as [string, unknown[]];
expect(sql).toContain("$2 <% r.run_name");
expect(sql).toContain("r.tenant_id=$1");
expect(values).toEqual(["authorized-tenant", "gen-2310"]);
});

it("ranks exact metadata before contiguous and session matches", () => {
expect(rankField("Dispatch after GEN-2098 update", "gen-2098", "run_name")?.score).toBe(1);
expect(rankField("GEN-2098", "gen-2098", "issue_id")?.score).toBe(0);
Expand Down
Loading