Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3
4import * as TanStackSourceApi from "./TanStackSourceApi.ts";
5import * as TanStackSourceIssueTracker from "./TanStackSourceIssueTracker.ts";
6
7const target = { cwd: "/repo", host: "source.tanstack.com", scope: "TanStack/ai" };
8
9function withApi(routes: Readonly<Record<string, unknown>>) {
10 const paths: Array<string> = [];
11 const writes: Array<readonly [string, string, unknown]> = [];
12 const request = (input: TanStackSourceApi.TanStackSourceApiRequest) => {
13 paths.push(input.path);
14 if (input.method !== undefined && input.method !== "GET") {
15 writes.push([input.method, input.path, input.body]);
16 return Effect.succeed("{}");
17 }
18 const body = routes[input.path];
19 return body === undefined
20 ? Effect.die(`unexpected ${input.path}`)
21 : Effect.succeed(JSON.stringify(body));
22 };
23 const api = TanStackSourceApi.TanStackSourceApi.of({
24 request,
25 response: (input) =>
26 request(input).pipe(Effect.map((text) => ({ status: 200, headers: {}, text }))),
27 });
28 return {
29 paths,
30 writes,
31 tracker: TanStackSourceIssueTracker.make.pipe(
32 Effect.provideService(TanStackSourceApi.TanStackSourceApi, api),
33 ),
34 };
35}
36
37const row = (number: number, state: string, createdAt: number) => ({
38 number,
39 title: `Issue ${number}`,
40 state,
41 author: "alice",
42 labels: ["bug"],
43 assignees: ["bob"],
44 comments: 1,
45 createdAt,
46});
47
48describe("TanStackSourceIssueTracker", () => {
49 it.effect("lists open and closed issues together, newest first, under one cursor each", () => {
50 const { tracker, paths } = withApi({
51 "/repos/TanStack/ai/issues?state=open": { items: [row(3, "open", 30)], next: "o2" },
52 "/repos/TanStack/ai/issues?state=closed": { items: [row(4, "closed", 40)], next: null },
53 "/repos/TanStack/ai/issues?state=open&cursor=o2": {
54 items: [row(1, "open", 10)],
55 next: null,
56 },
57 });
58 return Effect.gen(function* () {
59 const issues = yield* tracker;
60 const first = yield* issues.listIssues({
61 ...target,
62 involvement: "all",
63 state: "all",
64 limit: 2,
65 sort: "created",
66 });
67 assert.deepStrictEqual(
68 first.items.map((issue) => [issue.key, issue.state.category]),
69 [
70 ["4", "closed"],
71 ["3", "open"],
72 ],
73 );
74 assert.strictEqual(first.items[0]?.url, "https://source.tanstack.com/@TanStack/ai/issues/4");
75 assert.isNotNull(first.nextCursor);
76 const second = yield* issues.listIssues({
77 ...target,
78 involvement: "all",
79 state: "all",
80 limit: 2,
81 sort: "created",
82 cursor: first.nextCursor!,
83 });
84 assert.deepStrictEqual(
85 second.items.map((issue) => issue.key),
86 ["1"],
87 );
88 assert.isNull(second.nextCursor);
89 // The closed listing had ended, so it is not read again.
90 assert.strictEqual(paths.filter((path) => path.includes("state=closed")).length, 1);
91 });
92 });
93
94 it.effect("reads comments, events and the pull requests that fix it, hiding held entries", () => {
95 const { tracker } = withApi({
96 "/user": { username: "alice" },
97 "/repos/TanStack/ai/issues/2219": {
98 ...row(2219, "open", 1_000),
99 // The issue's own count, which a first timeline page may not reach.
100 comments: 12,
101 kind: "issue",
102 body: "Spec changed.",
103 assignees: [{ username: "bob", note: null }],
104 timeline: [
105 { event: "labeled", actor: "alice", at: 2_000 },
106 { event: "linked_from", actor: "bob", at: 3_000 },
107 { id: 7, comment: "On it.", author: "bob", at: 4_000, moderation: "visible" },
108 { id: 8, comment: null, author: "spam", at: 5_000, moderation: "quarantined" },
109 ],
110 // Older conversation carries on past this first page.
111 next: "t2",
112 fixedBy: [{ number: 2221, title: "fix(ai-mcp): follow the spec", state: "open" }],
113 },
114 });
115 return Effect.gen(function* () {
116 const issues = yield* tracker;
117 const issue = yield* issues.getIssue({ ...target, key: "2219" });
118 assert.strictEqual(issue.body, "Spec changed.");
119 assert.strictEqual(issue.viewer, "alice");
120 assert.deepStrictEqual(
121 issue.assignees.map((actor) => actor.login),
122 ["bob"],
123 );
124 assert.deepStrictEqual(
125 issue.events.map((event) => event.kind),
126 ["labeled", "referenced"],
127 );
128 assert.deepStrictEqual(
129 issue.comments.map((comment) => [comment.id, comment.body]),
130 [["7", "On it."]],
131 );
132 assert.strictEqual(issue.updatedAt, "1970-01-01T00:00:05.000Z");
133 assert.strictEqual(issue.commentCount, 12);
134 assert.strictEqual(issue.commentsCursor, "t2");
135 assert.deepStrictEqual(issue.linkedPullRequests, [
136 {
137 repository: "TanStack/ai",
138 number: 2221,
139 title: "fix(ai-mcp): follow the spec",
140 url: "https://source.tanstack.com/@TanStack/ai/pull/2221",
141 state: "open",
142 isDraft: false,
143 closesIssue: true,
144 },
145 ]);
146 });
147 });
148
149 it.effect("keeps an explicit assignee while also requiring the viewer to be assigned", () => {
150 const { tracker, paths } = withApi({
151 "/repos/TanStack/ai/issues?assignee=bob&state=open": {
152 items: [
153 { ...row(2, "open", 20), assignees: ["bob"] },
154 { ...row(1, "open", 10), assignees: ["bob", "Alice"] },
155 ],
156 next: null,
157 },
158 });
159 return Effect.gen(function* () {
160 const issues = yield* tracker;
161 const page = yield* issues.listIssues({
162 ...target,
163 involvement: "assigned",
164 viewer: "alice",
165 assignee: "bob",
166 state: "open",
167 limit: 50,
168 sort: "created",
169 });
170 assert.deepStrictEqual(
171 page.items.map((issue) => issue.key),
172 ["1"],
173 );
174 assert.deepStrictEqual(paths, ["/repos/TanStack/ai/issues?assignee=bob&state=open"]);
175 });
176 });
177
178 it.effect("writes each changed field on its own route, clearing labels and assignees", () => {
179 const { tracker, writes } = withApi({});
180 return Effect.gen(function* () {
181 const issues = yield* tracker;
182 yield* issues.update!({ ...target, key: "7", title: "New title", labels: [], assignees: [] });
183 yield* issues.setState!({ ...target, key: "7", state: "Closed" });
184 assert.deepStrictEqual(writes, [
185 ["PATCH", "/repos/TanStack/ai/issues/7", { title: "New title" }],
186 ["PUT", "/repos/TanStack/ai/issues/7/labels", { labels: [] }],
187 ["PUT", "/repos/TanStack/ai/issues/7/assignees", { assignees: [] }],
188 ["PATCH", "/repos/TanStack/ai/issues/7", { state: "closed" }],
189 ]);
190 });
191 });
192
193 it.effect("sends nothing for a key that is not an issue number", () => {
194 const { tracker, paths } = withApi({ "/user": { username: "alice" } });
195 return Effect.gen(function* () {
196 const issues = yield* tracker;
197 for (const key of ["7/labels", "7/../8", "7?state=open", "0", "x"]) {
198 const issue = { ...target, key };
199 const exits = yield* Effect.forEach(
200 [
201 issues.getIssue(issue),
202 issues.getComments({ ...issue, cursor: "t2" }),
203 issues.comment!({ ...issue, body: "Hello." }),
204 issues.update!({ ...issue, title: "New title", labels: [], assignees: [] }),
205 issues.setState!({ ...issue, state: "Closed" }),
206 ],
207 (effect) => Effect.exit(effect),
208 );
209 assert.deepStrictEqual(
210 exits.map((exit) => exit._tag),
211 ["Failure", "Failure", "Failure", "Failure", "Failure"],
212 key,
213 );
214 }
215 // Reading an issue asks for the viewer alongside it.
216 assert.deepStrictEqual(
217 paths.filter((path) => path !== "/user"),
218 [],
219 );
220 });
221 });
222});