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

composerPullRequestMatches.test.ts

119 lines3.7 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 it("orders by update instant when timestamps carry different UTC offsets", () => {
31 const result = filterComposerPullRequestMatches({
32 entries: [entry(401, "2026-10-25T02:50:00+02:00"), entry(499, "2026-10-25T02:10:00+01:00")],
33 projectId: "p1",
34 repository: "owner/repo",
35 query: "4",
36 limit: 1,
37 });
38 expect(result.map((match) => match.number)).toEqual([499]);
39 });
40});
41​
42describe("linked pull requests", () => {
43 const link = (
44 repository: string,
45 number: number,
46 overrides: Partial<ThreadPullRequestLink> = {},
47 ): ThreadPullRequestLink => ({
48 host: "github.com",
49 repository,
50 number,
51 url: `https://github.com/${repository}/pull/${number}`,
52 source: "agent",
53 linkedAt: "2026-01-01T00:00:00.000Z",
54 snapshot: {
55 state: "open",
56 title: `${repository} ${number}`,
57 headBranch: "feature",
58 baseBranch: "main",
59 isDraft: false,
60 updatedAt: "2026-01-02T00:00:00.000Z",
61 syncedAt: "2026-01-02T00:00:00.000Z",
62 },
63 stack: null,
64 ...overrides,
65 });
66​
67 it("offers a linked pull request from another repository above the project's own", () => {
68 const result = filterComposerPullRequestMatches({
69 entries: [entry(4, "2026-01-03"), { ...entry(4, "2026-01-04"), repository: "owner/other" }],
70 linked: composerPullRequestsFromLinks([link("owner/repo-b", 4)], "p1"),
71 projectId: "p1",
72 repository: "owner/repo",
73 query: "4",
74 limit: 10,
75 });
76 expect(result.map((match) => `${match.repository}#${match.number}`)).toEqual([
77 "owner/repo-b#4",
78 "owner/repo#4",
79 ]);
80 });
81​
82 it("keeps one row for a linked pull request the project listing already has", () => {
83 const result = filterComposerPullRequestMatches({
84 entries: [entry(4, "2026-01-03")],
85 linked: composerPullRequestsFromLinks([link("Owner/Repo", 4)], "p1"),
86 projectId: "p1",
87 repository: "owner/repo",
88 query: "",
89 limit: 10,
90 });
91 expect(result).toEqual([entry(4, "2026-01-03")]);
92 });
93​
94 it("puts linked pull requests ahead of a text search, before and after it returns", () => {
95 const linked = composerPullRequestsFromLinks(
96 [
97 link("owner/repo", 7, {
98 snapshot: { ...link("owner/repo", 7).snapshot!, title: "Fix login" },
99 }),
100 link("owner/repo", 8),
101 ],
102 "p1",
103 );
104 const searching = withLinkedComposerPullRequestsFirst({
105 matches: [],
106 linked,
107 query: "fix",
108 limit: 10,
109 });
110 expect(searching.map((match) => match.number)).toEqual([7]);
111 const searched = withLinkedComposerPullRequestsFirst({
112 matches: [entry(9, "2026-01-05"), entry(8, "2026-01-03")],
113 linked,
114 query: "fix",
115 limit: 10,
116 });
117 expect(searched).toEqual([linked[0], entry(8, "2026-01-03"), entry(9, "2026-01-05")]);
118 });
119});