Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import * as Json from "./tanStackSourceJson.ts";
4
5const HOST = "source.tanstack.com";
6const run = (fields: Partial<Json.SourceCheckRun> & { readonly name: string }) => ({
7 view: "full",
8 status: "completed",
9 conclusion: "success",
10 startedAt: 1,
11 completedAt: 2,
12 summary: null,
13 ...fields,
14});
15
16describe("toChecks", () => {
17 // Shapes from `GET /check-runs?sha=` on TanStack/ai: Source CI jobs carry a job id, checks
18 // synced from GitHub a url and a source.
19 const job: Json.SourceCiJob = {
20 runId: "b524c952-431d-885f-255c-bf2fbf8addb2",
21 workflow: "pr",
22 key: "test (shard: 1)",
23 runUrl: `https://${HOST}/@TanStack/ai/ci/b524c952-431d-885f-255c-bf2fbf8addb2`,
24 };
25
26 it("names Source CI jobs by workflow, links their log, and keeps a re-run's newest result", () => {
27 const checks = Json.toChecks({
28 runs: [
29 run({ jobId: "old", name: "test (shard: 1)", conclusion: "failure", startedAt: 1 }),
30 // A job Source CI has not reported a run for yet still shows, under its own name.
31 run({ jobId: "unread", name: "lint", conclusion: "failure", startedAt: 0 }),
32 run({ jobId: "new", name: "test (shard: 1)", startedAt: 5, summary: "### Passed\nmore" }),
33 run({
34 jobId: "",
35 name: "CodeRabbit",
36 status: "in_progress",
37 conclusion: null,
38 url: "https://coderabbit.ai",
39 source: "github",
40 }),
41 ],
42 jobs: new Map([
43 ["old", job],
44 ["new", job],
45 ]),
46 required: new Set(["pr/test (shard: 1)"]),
47 });
48 expect(checks).toEqual(
49 expect.arrayContaining([
50 {
51 name: "pr/test (shard: 1)",
52 status: "success",
53 description: "Passed",
54 url: `${job.runUrl}?job=test+%28shard%3A+1%29`,
55 required: true,
56 },
57 { name: "CodeRabbit", status: "pending", description: null, url: "https://coderabbit.ai" },
58 { name: "lint", status: "failure", description: null, url: null },
59 ]),
60 );
61 expect(checks).toHaveLength(3);
62 expect(Json.checksStateOf(checks)).toBe("failing");
63 expect(Json.parseCiJobUrl(checks.find((check) => check.required)!.url, HOST)).toEqual({
64 runId: job.runId,
65 job: job.key,
66 });
67 expect(Json.parseCiJobUrl("https://coderabbit.ai", HOST)).toBeNull();
68 });
69});
70
71describe("toReviewComment", () => {
72 it("anchors added and right-side lines on new, deleted and left-side lines on old", () => {
73 const draft = (position: Parameters<typeof Json.toReviewComment>[0]["position"]) =>
74 Json.toReviewComment({ path: "a.ts", position, body: "x" }, "sha");
75 expect(draft({ kind: "added", newLine: 4 })).toMatchObject({ side: "new", line: 4 });
76 expect(draft({ kind: "deleted", oldLine: 3 })).toMatchObject({ side: "old", line: 3 });
77 expect(draft({ kind: "context", oldLine: 3, newLine: 5, side: "left" })).toMatchObject({
78 side: "old",
79 line: 3,
80 });
81 expect(
82 Json.toReviewComment(
83 {
84 path: "a.ts",
85 startPosition: { kind: "added", newLine: 2 },
86 position: { kind: "added", newLine: 4 },
87 body: "x",
88 },
89 "sha",
90 ),
91 ).toEqual({ path: "a.ts", side: "new", line: 4, startLine: 2, commitId: "sha", body: "x" });
92 });
93});
94
95describe("patchSections", () => {
96 it("splits a unified patch by the path each section changes", () => {
97 const patch =
98 "diff --git a/a.ts b/a.ts\n--- a/a.ts\n+++ b/a.ts\n@@ -1 +1 @@\n-a\n+b\n" +
99 "diff --git a/old.ts b/new.ts\nsimilarity index 100%\n";
100 const sections = Json.patchSections(patch);
101 expect([...sections.keys()]).toEqual(["a.ts", "new.ts"]);
102 expect(sections.get("new.ts")).toBe("diff --git a/old.ts b/new.ts\nsimilarity index 100%\n");
103 });
104});
105
106describe("toActivity", () => {
107 it("tells conversation comments, line comments and reviews apart, and hides held ones", () => {
108 const activity = Json.toActivity({
109 timeline: {
110 timeline: [
111 { id: 1, comment: "hi", author: "alice", at: 1_000, moderation: "visible" },
112 { id: 2, comment: null, author: "spam", at: 2_000, moderation: "quarantined" },
113 { id: 3, comment: "LGTM", author: "bob", at: 3_000, review: "approved" },
114 { id: 4, comment: "nit", author: "bob", at: 3_000, path: "a.ts", reviewId: 3 },
115 ],
116 next: null,
117 },
118 threads: [
119 {
120 id: 4,
121 path: "a.ts",
122 side: "old",
123 line: 2,
124 outdated: false,
125 resolved: true,
126 comments: [{ id: 4, comment: "nit", author: "bob", at: 3_000 }],
127 },
128 ],
129 commits: {
130 baseSha: null,
131 headSha: null,
132 mergeBaseSha: null,
133 available: true,
134 commitsTruncated: false,
135 commits: [{ sha: "abc", title: "fix", author: { name: "Bob", timestamp: 3 } }],
136 },
137 host: HOST,
138 repository: "TanStack/ai",
139 number: 9,
140 });
141 expect(
142 activity.comments.map((comment) => [comment.id, comment.kind, comment.reviewState]),
143 ).toEqual([
144 ["1", "issue-comment", null],
145 ["3", "review", "approved"],
146 ["4", "review-comment", null],
147 ]);
148 expect(activity.comments[0]?.url).toBe(`https://${HOST}/@TanStack/ai/pull/9#comment-1`);
149 expect(Json.pullUrl(HOST, "%40TanStack/ai", 9)).toBe(`https://${HOST}/@TanStack/ai/pull/9`);
150 expect(activity.reviewThreads).toMatchObject([
151 { id: "4", path: "a.ts", line: 2, side: "left", isResolved: true },
152 ]);
153 expect(activity.commits).toEqual([
154 { oid: "abc", messageHeadline: "fix", committedDate: "1970-01-01T00:00:03.000Z" },
155 ]);
156 });
157});