The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { assert, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Fiber from "effect/Fiber"; |
| 4 | import * as Option from "effect/Option"; |
| 5 | import * as Redacted from "effect/Redacted"; |
| 6 | import * as Stream from "effect/Stream"; |
| 7 | import * as TestClock from "effect/testing/TestClock"; |
| 8 | import { HttpClient, HttpClientResponse } from "effect/http"; |
| 9 | |
| 10 | import * as GitHubPullRequestEvents from "./gitHubPullRequestEvents.ts"; |
| 11 | import { GitHubApiNotFoundError, GitHubApiResponseError } from "./GitHubRequestPolicy.ts"; |
| 12 | import * as GitHubRest from "./GitHubRest.ts"; |
| 13 | |
| 14 | const target = { host: "github.com", repository: "pingdotgg/t3code", number: 15 }; |
| 15 | |
| 16 | const sse = (...frames: ReadonlyArray<readonly [string, string]>) => |
| 17 | frames.map(([event, data]) => `event: ${event}\ndata: ${data}\n\n`).join(""); |
| 18 | |
| 19 | function fakeRest( |
| 20 | subscribe: ( |
| 21 | count: number, |
| 22 | ) => Effect.Effect<string, GitHubApiResponseError | GitHubApiNotFoundError>, |
| 23 | signedIn = true, |
| 24 | ) { |
| 25 | const posts: Array<unknown> = []; |
| 26 | const deletes: Array<string> = []; |
| 27 | const rest = GitHubRest.GitHubRest.of({ |
| 28 | rest: (input) => |
| 29 | input.method === "DELETE" |
| 30 | ? Effect.sync(() => { |
| 31 | deletes.push(input.path); |
| 32 | return { status: 204, headers: {}, body: "", truncated: false, invalidUtf8: false }; |
| 33 | }) |
| 34 | : Effect.suspend(() => { |
| 35 | posts.push(input.body); |
| 36 | return subscribe(posts.length); |
| 37 | }).pipe( |
| 38 | Effect.map((body) => ({ |
| 39 | status: 201, |
| 40 | headers: {}, |
| 41 | body, |
| 42 | truncated: false, |
| 43 | invalidUtf8: false, |
| 44 | })), |
| 45 | ), |
| 46 | credential: (_host, options) => |
| 47 | Effect.succeed( |
| 48 | options?.personal === true |
| 49 | ? { token: Redacted.make("user-token"), fingerprint: "user", source: "gh" as const } |
| 50 | : { token: Redacted.make("app-token"), fingerprint: "app", source: "app" as const }, |
| 51 | ), |
| 52 | appCredential: () => |
| 53 | Effect.succeed( |
| 54 | signedIn |
| 55 | ? Option.some({ |
| 56 | token: Redacted.make("app-token"), |
| 57 | fingerprint: "app", |
| 58 | source: "app" as const, |
| 59 | }) |
| 60 | : Option.none(), |
| 61 | ), |
| 62 | }); |
| 63 | return { rest, posts, deletes }; |
| 64 | } |
| 65 | |
| 66 | const eventsOf = (rest: GitHubRest.GitHubRest["Service"], httpClient: HttpClient.HttpClient) => |
| 67 | GitHubPullRequestEvents.make.pipe( |
| 68 | Effect.provideService(GitHubRest.GitHubRest, rest), |
| 69 | Effect.provideService(HttpClient.HttpClient, httpClient), |
| 70 | ); |
| 71 | |
| 72 | const subscription = (id: number) => |
| 73 | Effect.succeed( |
| 74 | JSON.stringify({ id, events_url: `https://api.github.com/event-subscriptions/${id}/events` }), |
| 75 | ); |
| 76 | |
| 77 | it.effect("follows a pull request across a refused app token, reconnects, and renewals", () => |
| 78 | Effect.gen(function* () { |
| 79 | const { rest, posts, deletes } = fakeRest(subscription); |
| 80 | const connections: Array<string> = []; |
| 81 | const answers = [ |
| 82 | // The app is not installed on this repository; the user's own token reads it. |
| 83 | () => new Response(null, { status: 404 }), |
| 84 | () => |
| 85 | new Response( |
| 86 | sse( |
| 87 | ["com.github.pull_request.head.changed.v1", "{}"], |
| 88 | ["github.stream.end", '{"outcome":"reconnect","reason":"shutdown"}'], |
| 89 | ), |
| 90 | ), |
| 91 | () => |
| 92 | new Response( |
| 93 | sse(["github.stream.end", '{"outcome":"subscription_ended","reason":"expired"}']), |
| 94 | ), |
| 95 | () => new Response(": heartbeat\n\n"), |
| 96 | ]; |
| 97 | const httpClient = HttpClient.make((request) => |
| 98 | Effect.sync(() => { |
| 99 | connections.push(`${request.url} ${request.headers.authorization}`); |
| 100 | return HttpClientResponse.fromWeb(request, answers[connections.length - 1]!()); |
| 101 | }), |
| 102 | ); |
| 103 | |
| 104 | const events = yield* eventsOf(rest, httpClient); |
| 105 | const fiber = yield* events(target).pipe(Stream.take(4), Stream.runCount, Effect.forkChild); |
| 106 | yield* TestClock.adjust("5 minutes"); |
| 107 | // An open per connection plus the head change. |
| 108 | assert.equal(yield* Fiber.join(fiber), 4); |
| 109 | |
| 110 | assert.deepEqual(connections, [ |
| 111 | "https://api.github.com/event-subscriptions/1/events Bearer app-token", |
| 112 | "https://api.github.com/event-subscriptions/1/events Bearer user-token", |
| 113 | "https://api.github.com/event-subscriptions/1/events Bearer user-token", |
| 114 | "https://api.github.com/event-subscriptions/2/events Bearer app-token", |
| 115 | ]); |
| 116 | assert.deepEqual(posts[0], { |
| 117 | resource_url: "https://github.com/pingdotgg/t3code/pull/15", |
| 118 | event_types: [ |
| 119 | "com.github.pull_request.review.submitted.v1", |
| 120 | "com.github.pull_request.head.changed.v1", |
| 121 | "com.github.pull_request.state.changed.v1", |
| 122 | "com.github.pull_request.merge_conflict.v1", |
| 123 | "com.github.pull_request.check_suite.status.changed.v1", |
| 124 | ], |
| 125 | }); |
| 126 | assert.deepEqual(deletes, ["event-subscriptions/1", "event-subscriptions/2"]); |
| 127 | }), |
| 128 | ); |
| 129 | |
| 130 | it.effect("gives up on a silent stream and deletes its subscription", () => |
| 131 | Effect.gen(function* () { |
| 132 | const { rest, posts, deletes } = fakeRest(subscription); |
| 133 | let connections = 0; |
| 134 | // Answers, then never sends a byte, not even a keep-alive. |
| 135 | const httpClient = HttpClient.make((request) => |
| 136 | Effect.sync(() => { |
| 137 | connections += 1; |
| 138 | return HttpClientResponse.fromWeb(request, new Response(new ReadableStream())); |
| 139 | }), |
| 140 | ); |
| 141 | const events = yield* eventsOf(rest, httpClient); |
| 142 | const fiber = yield* events(target).pipe(Stream.runCount, Effect.forkChild); |
| 143 | yield* TestClock.adjust("15 minutes"); |
| 144 | |
| 145 | assert.equal(yield* Fiber.join(fiber), 7, "one catch-up read per connection"); |
| 146 | assert.equal(connections, 7); |
| 147 | assert.equal(posts.length, 1); |
| 148 | assert.deepEqual(deletes, ["event-subscriptions/1"]); |
| 149 | }), |
| 150 | ); |
| 151 | |
| 152 | it.effect("leaves users without the app sign-in, other hosts, and refusals to polling", () => |
| 153 | Effect.gen(function* () { |
| 154 | const httpClient = HttpClient.make(() => Effect.die("no stream without a subscription")); |
| 155 | const signedOut = fakeRest(subscription, false); |
| 156 | assert.equal(yield* Stream.runCount((yield* eventsOf(signedOut.rest, httpClient))(target)), 0); |
| 157 | assert.equal(signedOut.posts.length, 0); |
| 158 | |
| 159 | const signedIn = fakeRest(subscription); |
| 160 | const enterprise = { ...target, host: "ghe.example.com" }; |
| 161 | assert.equal( |
| 162 | yield* Stream.runCount((yield* eventsOf(signedIn.rest, httpClient))(enterprise)), |
| 163 | 0, |
| 164 | ); |
| 165 | assert.equal(signedIn.posts.length, 0); |
| 166 | |
| 167 | const refused = fakeRest(() => |
| 168 | Effect.fail( |
| 169 | new GitHubApiResponseError({ |
| 170 | host: "github.com", |
| 171 | operation: "subscribePullRequestEvents", |
| 172 | status: 403, |
| 173 | }), |
| 174 | ), |
| 175 | ); |
| 176 | const events = yield* eventsOf(refused.rest, httpClient); |
| 177 | assert.equal(yield* Stream.runCount(events(target)), 0); |
| 178 | assert.equal(yield* Stream.runCount(events({ ...target, number: 16 })), 0); |
| 179 | assert.equal(refused.posts.length, 1, "a refused account is not asked again for an hour"); |
| 180 | yield* TestClock.adjust("61 minutes"); |
| 181 | assert.equal(yield* Stream.runCount(events(target)), 0); |
| 182 | assert.equal(refused.posts.length, 2); |
| 183 | |
| 184 | const missing = fakeRest(() => |
| 185 | Effect.fail( |
| 186 | new GitHubApiNotFoundError({ host: "github.com", operation: "subscribePullRequestEvents" }), |
| 187 | ), |
| 188 | ); |
| 189 | const missingEvents = yield* eventsOf(missing.rest, httpClient); |
| 190 | assert.equal(yield* Stream.runCount(missingEvents(target)), 0); |
| 191 | assert.equal(yield* Stream.runCount(missingEvents(target)), 0); |
| 192 | assert.equal(missing.posts.length, 1, "a pull request it cannot see is not asked again"); |
| 193 | assert.equal(yield* Stream.runCount(missingEvents({ ...target, number: 16 })), 0); |
| 194 | assert.equal(missing.posts.length, 2, "other pull requests on the host still subscribe"); |
| 195 | }), |
| 196 | ); |