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

composerPullRequestMatches.test.ts

117 lines3.4 KB

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