Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as HttpClient from "effect/http/HttpClient";
4
5import * as TanStackSourceApi from "./TanStackSourceApi.ts";
6import * as Credentials from "./TanStackSourceCredentials.ts";
7import * as TanStackSourcePullRequestProvider from "./TanStackSourcePullRequestProvider.ts";
8
9const HOST = "source.tanstack.com";
10
11/**
12 * Shaped like `GET /pulls/{n}` of a private pull request: its head is `owner:branch` in the
13 * companion repository, and Source has stored no file or line counts for it.
14 */
15const PRIVATE_PULL = {
16 number: 42,
17 title: "Fix the thing",
18 state: "open",
19 visibility: "private",
20 draft: false,
21 author: "bob",
22 head: "acme:fix/thing",
23 createdAt: 1_000,
24 body: null,
25 base: "main",
26 headRepo: "acme/web.private",
27 headSha: "c".repeat(40),
28 baseSha: "b".repeat(40),
29 mergeCommitSha: null,
30 mergedAt: null,
31 mergedBy: null,
32 mergeable: null,
33 changedFiles: null,
34 changedLines: null,
35};
36
37const FILES = {
38 baseSha: "b".repeat(40),
39 headSha: "c".repeat(40),
40 mergeBaseSha: "b".repeat(40),
41 available: true,
42 truncated: false,
43 files: [
44 { path: "a.ts", status: "modified", additions: 3, deletions: 1 },
45 { path: "b.ts", status: "added", additions: 4, deletions: 0 },
46 ],
47};
48
49const RESPONSES: Record<string, unknown> = {
50 "/repos/acme/web/pulls/42": PRIVATE_PULL,
51 "/repos/acme/web/pulls/42/files": FILES,
52 "/repos/acme/web/pulls/42/checks": { checks: [], upToDate: null, mayMerge: true },
53 "/repos/acme/web/pulls/42/stack": { members: [] },
54 [`/repos/acme/web/check-runs?sha=${"c".repeat(40)}`]: [],
55 "/repos/acme/web/ci/runs?pull=42&limit=30": { items: [], next: null },
56 "/repos/acme/web/issues/42": {
57 timeline: [
58 { id: 1, comment: "Looks good", author: "alice", at: 2_000 },
59 { event: "head_updated", actor: "bob", at: 3_000 },
60 ],
61 next: null,
62 },
63 "/repos/acme/web/pulls/42/comments": [],
64 "/repos/acme/web/pulls/42/commits": {
65 baseSha: null,
66 headSha: null,
67 mergeBaseSha: null,
68 available: true,
69 commits: [],
70 },
71 "/user": { username: "alice" },
72};
73
74const api = TanStackSourceApi.TanStackSourceApi.of({
75 request: (input) =>
76 input.path in RESPONSES
77 ? Effect.succeed(JSON.stringify(RESPONSES[input.path]))
78 : Effect.die(`unexpected ${input.path}`),
79 response: () => Effect.die("unexpected response read"),
80});
81
82const provider = TanStackSourcePullRequestProvider.make.pipe(
83 Effect.provideService(TanStackSourceApi.TanStackSourceApi, api),
84 Effect.provideService(
85 Credentials.TanStackSourceCredentials,
86 Credentials.TanStackSourceCredentials.of({
87 get: () => Effect.die("unexpected credential read"),
88 invalidate: () => Effect.void,
89 }),
90 ),
91 Effect.provideService(
92 HttpClient.HttpClient,
93 HttpClient.make(() => Effect.die("unexpected http request")),
94 ),
95);
96
97describe("TanStackSourcePullRequestProvider.getChangeRequest", () => {
98 // A client without this host's definition percent-encodes the owner's `@`; stored links
99 // keep the web's own spelling.
100 it.effect.each(["acme/web", "@acme/web", "%40acme/web"])(
101 "reads a private pull request of %s",
102 (repository) =>
103 Effect.gen(function* () {
104 const detail = yield* (yield* provider).getChangeRequest({
105 host: HOST,
106 repository,
107 number: 42,
108 cwd: "/repo",
109 });
110 assert.strictEqual(detail.url, `https://${HOST}/@acme/web/pull/42`);
111 assert.strictEqual(detail.visibility, "private");
112 assert.strictEqual(detail.headBranch, "fix/thing");
113 assert.strictEqual(detail.headRepositoryNameWithOwner, "acme/web.private");
114 // Source stores no counts for it, so they come from its files.
115 assert.deepStrictEqual(
116 [detail.changedFiles, detail.additions, detail.deletions],
117 [2, 7, 1],
118 );
119 }),
120 );
121});
122
123describe("TanStackSourcePullRequestProvider.getChangeRequestActivity", () => {
124 it.effect("reads the comments of a timeline that also holds events", () =>
125 Effect.gen(function* () {
126 const activity = yield* (yield* provider).getChangeRequestActivity({
127 host: HOST,
128 repository: "acme/web",
129 number: 42,
130 cwd: "/repo",
131 });
132 assert.deepStrictEqual(
133 activity.comments.map((comment) => comment.body),
134 ["Looks good"],
135 );
136 }),
137 );
138});