Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import type { ThreadPullRequestKey, ThreadPullRequestLink } from "@t3tools/contracts";
4
5import {
6 composerPullRequestsFromLinks,
7 filterComposerPullRequestMatches,
8 withLinkedComposerPullRequestsFirst,
9} from "./composerPullRequestMatches.ts";
10
11const keyOf = (entry: ThreadPullRequestKey) =>
12 `${entry.host.trim().toLowerCase()}/${entry.repository.trim().toLowerCase()}#${entry.number}`;
13
14const entry = (number: number, updatedAt: string) => ({
15 number,
16 host: "github.com",
17 projectId: "p1",
18 repository: "owner/repo",
19 updatedAt,
20});
21
22describe("filterComposerPullRequestMatches", () => {
23 it("ranks the exact number above newer substring matches", () => {
24 const result = filterComposerPullRequestMatches({
25 keyOf,
26 entries: [entry(1234, "2026-01-03"), entry(123, "2026-01-01"), entry(1230, "2026-01-02")],
27 projectId: "p1",
28 repository: "owner/repo",
29 query: "123",
30 limit: 10,
31 });
32 expect(result.map((match) => match.number)).toEqual([123, 1234, 1230]);
33 });
34
35 it("orders by update instant when timestamps carry different UTC offsets", () => {
36 const result = filterComposerPullRequestMatches({
37 keyOf,
38 entries: [entry(401, "2026-10-25T02:50:00+02:00"), entry(499, "2026-10-25T02:10:00+01:00")],
39 projectId: "p1",
40 repository: "owner/repo",
41 query: "4",
42 limit: 1,
43 });
44 expect(result.map((match) => match.number)).toEqual([499]);
45 });
46
47 it("matches a zero-padded numeric query to its exact pull request ahead of substring matches", () => {
48 const result = filterComposerPullRequestMatches({
49 keyOf,
50 entries: [entry(10042, "2026-01-03"), entry(42, "2026-01-01"), entry(420, "2026-01-04")],
51 projectId: "p1",
52 repository: "owner/repo",
53 query: "0042",
54 limit: 10,
55 });
56 expect(result.map((match) => match.number)).toEqual([42, 10042]);
57 });
58});
59
60describe("linked pull requests", () => {
61 const link = (
62 repository: string,
63 number: number,
64 overrides: Partial<ThreadPullRequestLink> = {},
65 ): ThreadPullRequestLink => ({
66 host: "github.com",
67 repository,
68 number,
69 url: `https://github.com/${repository}/pull/${number}`,
70 source: "agent",
71 linkedAt: "2026-01-01T00:00:00.000Z",
72 snapshot: {
73 state: "open",
74 title: `${repository} ${number}`,
75 headBranch: "feature",
76 baseBranch: "main",
77 isDraft: false,
78 updatedAt: "2026-01-02T00:00:00.000Z",
79 syncedAt: "2026-01-02T00:00:00.000Z",
80 },
81 stack: null,
82 ...overrides,
83 });
84
85 it("offers a linked pull request from another repository above the project's own", () => {
86 const result = filterComposerPullRequestMatches({
87 keyOf,
88 entries: [entry(4, "2026-01-03"), { ...entry(4, "2026-01-04"), repository: "owner/other" }],
89 linked: composerPullRequestsFromLinks([link("owner/repo-b", 4)], "p1"),
90 projectId: "p1",
91 repository: "owner/repo",
92 query: "4",
93 limit: 10,
94 });
95 expect(result.map((match) => `${match.repository}#${match.number}`)).toEqual([
96 "owner/repo-b#4",
97 "owner/repo#4",
98 ]);
99 });
100
101 it("matches linked pull requests for zero-padded numeric queries", () => {
102 const result = filterComposerPullRequestMatches({
103 keyOf,
104 entries: [entry(42, "2026-01-03")],
105 linked: composerPullRequestsFromLinks([link("owner/repo-b", 42)], "p1"),
106 projectId: "p1",
107 repository: "owner/repo",
108 query: "0042",
109 limit: 10,
110 });
111 expect(result.map((match) => `${match.repository}#${match.number}`)).toEqual([
112 "owner/repo-b#42",
113 "owner/repo#42",
114 ]);
115 });
116
117 it("keeps linked reviews with the same repository and number on different hosts", () => {
118 const github = link("wireshark/wireshark", 1);
119 const gitlab = link("wireshark/wireshark", 1, {
120 host: "gitlab.com",
121 url: "https://gitlab.com/wireshark/wireshark/-/merge_requests/1",
122 });
123 const result = filterComposerPullRequestMatches({
124 keyOf,
125 entries: [],
126 linked: composerPullRequestsFromLinks([github, gitlab], "p1"),
127 projectId: "p1",
128 repository: "wireshark/wireshark",
129 query: "1",
130 limit: 10,
131 });
132 expect(result.map((match) => match.url)).toEqual([github.url, gitlab.url]);
133 });
134
135 it("keeps one row for a linked pull request the project listing already has", () => {
136 const result = filterComposerPullRequestMatches({
137 keyOf,
138 entries: [entry(4, "2026-01-03")],
139 linked: composerPullRequestsFromLinks([link("Owner/Repo", 4)], "p1"),
140 projectId: "p1",
141 repository: "owner/repo",
142 query: "",
143 limit: 10,
144 });
145 expect(result).toEqual([entry(4, "2026-01-03")]);
146 });
147
148 it("puts linked pull requests ahead of a text search, before and after it returns", () => {
149 const linked = composerPullRequestsFromLinks(
150 [
151 link("owner/repo", 7, {
152 snapshot: { ...link("owner/repo", 7).snapshot!, title: "Fix login" },
153 }),
154 link("owner/repo", 8),
155 link("owner/repo", 8, {
156 host: "gitlab.com",
157 url: "https://gitlab.com/owner/repo/-/merge_requests/8",
158 snapshot: { ...link("owner/repo", 8).snapshot!, title: "Fix CI" },
159 }),
160 ],
161 "p1",
162 );
163 const searching = withLinkedComposerPullRequestsFirst({
164 keyOf,
165 matches: [],
166 linked,
167 query: "fix",
168 limit: 10,
169 });
170 expect(searching.map((match) => match.number)).toEqual([7, 8]);
171 const searched = withLinkedComposerPullRequestsFirst({
172 keyOf,
173 matches: [entry(9, "2026-01-05"), entry(8, "2026-01-03")],
174 linked,
175 query: "fix",
176 limit: 10,
177 });
178 expect(searched).toEqual([
179 linked[0],
180 entry(8, "2026-01-03"),
181 linked[2],
182 entry(9, "2026-01-05"),
183 ]);
184 });
185
186 it("matches linked pull requests across NFC and NFD Unicode forms", () => {
187 const linked = composerPullRequestsFromLinks(
188 [
189 link("owner/repo", 7, {
190 snapshot: { ...link("owner/repo", 7).snapshot!, title: "Fix caf\u00e9" },
191 }),
192 ],
193 "p1",
194 );
195 expect(
196 withLinkedComposerPullRequestsFirst({
197 keyOf,
198 matches: [],
199 linked,
200 query: "cafe\u0301",
201 limit: 10,
202 }).map((match) => match.number),
203 ).toEqual([7]);
204 });
205
206 it("offers a linked Greek-titled pull request when the query uses medial sigma", () => {
207 const review = link("owner/other", 7);
208 const linked = composerPullRequestsFromLinks(
209 [{ ...review, snapshot: { ...review.snapshot!, title: "Έλεγχος σύνδεσης" } }],
210 "p1",
211 );
212
213 expect(
214 withLinkedComposerPullRequestsFirst({
215 keyOf,
216 matches: [],
217 linked,
218 query: "έλεγχοσ",
219 limit: 10,
220 }).map((match) => match.title),
221 ).toEqual(["Έλεγχος σύνδεσης"]);
222 });
223});