Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerPullRequestMatches.test.ts

108 lines3.3 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import type { ThreadPullRequestLink } from "@t3tools/contracts";
4​
5import {
6 composerPullRequestsFromLinks,
7 filterComposerPullRequestMatches,
8 withLinkedComposerPullRequestsFirst,
9} from "./composerPullRequestMatches.ts";
10​
11const entry = (number: number, updatedAt: string) => ({
12 number,
13 projectId: "p1",
14 repository: "owner/repo",
15 updatedAt,
16});
17​
18describe("filterComposerPullRequestMatches", () => {
19 it("ranks the exact number above newer substring matches", () => {
20 const result = filterComposerPullRequestMatches({
21 entries: [entry(1234, "2026-01-03"), entry(123, "2026-01-01"), entry(1230, "2026-01-02")],
22 projectId: "p1",
23 repository: "owner/repo",
24 query: "123",
25 limit: 10,
26 });
27 expect(result.map((match) => match.number)).toEqual([123, 1234, 1230]);
28 });
29});
30​
31describe("linked pull requests", () => {
32 const link = (
33 repository: string,
34 number: number,
35 overrides: Partial<ThreadPullRequestLink> = {},
36 ): ThreadPullRequestLink => ({
37 host: "github.com",
38 repository,
39 number,
40 url: `https://github.com/${repository}/pull/${number}`,
41 source: "agent",
42 linkedAt: "2026-01-01T00:00:00.000Z",
43 snapshot: {
44 state: "open",
45 title: `${repository} ${number}`,
46 headBranch: "feature",
47 baseBranch: "main",
48 isDraft: false,
49 updatedAt: "2026-01-02T00:00:00.000Z",
50 syncedAt: "2026-01-02T00:00:00.000Z",
51 },
52 stack: null,
53 ...overrides,
54 });
55​
56 it("offers a linked pull request from another repository above the project's own", () => {
57 const result = filterComposerPullRequestMatches({
58 entries: [entry(4, "2026-01-03"), { ...entry(4, "2026-01-04"), repository: "owner/other" }],
59 linked: composerPullRequestsFromLinks([link("owner/repo-b", 4)], "p1"),
60 projectId: "p1",
61 repository: "owner/repo",
62 query: "4",
63 limit: 10,
64 });
65 expect(result.map((match) => `${match.repository}#${match.number}`)).toEqual([
66 "owner/repo-b#4",
67 "owner/repo#4",
68 ]);
69 });
70​
71 it("keeps one row for a linked pull request the project listing already has", () => {
72 const result = filterComposerPullRequestMatches({
73 entries: [entry(4, "2026-01-03")],
74 linked: composerPullRequestsFromLinks([link("Owner/Repo", 4)], "p1"),
75 projectId: "p1",
76 repository: "owner/repo",
77 query: "",
78 limit: 10,
79 });
80 expect(result).toEqual([entry(4, "2026-01-03")]);
81 });
82​
83 it("puts linked pull requests ahead of a text search, before and after it returns", () => {
84 const linked = composerPullRequestsFromLinks(
85 [
86 link("owner/repo", 7, {
87 snapshot: { ...link("owner/repo", 7).snapshot!, title: "Fix login" },
88 }),
89 link("owner/repo", 8),
90 ],
91 "p1",
92 );
93 const searching = withLinkedComposerPullRequestsFirst({
94 matches: [],
95 linked,
96 query: "fix",
97 limit: 10,
98 });
99 expect(searching.map((match) => match.number)).toEqual([7]);
100 const searched = withLinkedComposerPullRequestsFirst({
101 matches: [entry(9, "2026-01-05"), entry(8, "2026-01-03")],
102 linked,
103 query: "fix",
104 limit: 10,
105 });
106 expect(searched).toEqual([linked[0], entry(8, "2026-01-03"), entry(9, "2026-01-05")]);
107 });
108});