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

pullRequestDiffHttp.test.ts

117 lines3.8 KB

1import { EnvironmentId, ProjectId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Option from "effect/Option";
5​
6import { PrimaryConnectionTarget, type PreparedConnection } from "../connection/model.ts";
7import * as RpcHttp from "../rpc/http.ts";
8import {
9 fetchEnvironmentPullRequestDiff,
10 PullRequestDiffCredentialRejectedError,
11} from "./pullRequestDiffHttp.ts";
12​
13const TARGET = new PrimaryConnectionTarget({
14 environmentId: EnvironmentId.make("environment-1"),
15 label: "Test environment",
16 httpBaseUrl: "https://environment.example.test/base",
17 wsBaseUrl: "wss://environment.example.test",
18});
19​
20const PREPARED: PreparedConnection = {
21 environmentId: TARGET.environmentId,
22 label: TARGET.label,
23 httpBaseUrl: TARGET.httpBaseUrl,
24 socketUrl: "wss://environment.example.test/ws",
25 httpAuthorization: null,
26 target: TARGET,
27};
28​
29describe("fetchEnvironmentPullRequestDiff", () => {
30 it.effect("posts the diff input to the prepared environment over authenticated HTTP", () =>
31 Effect.gen(function* () {
32 const calls: Array<readonly [RequestInfo | URL, RequestInit]> = [];
33 const fetchFn = ((request, init) => {
34 calls.push([request, init ?? {}]);
35 return Promise.resolve(
36 Response.json({
37 patch: "diff --git a/file.ts b/file.ts",
38 truncated: false,
39 nextCursor: null,
40 }),
41 );
42 }) satisfies typeof fetch;
43​
44 const result = yield* fetchEnvironmentPullRequestDiff({
45 prepared: PREPARED,
46 signer: Option.none(),
47 diff: {
48 projectId: ProjectId.make("project-1"),
49 repository: "owner/repository",
50 number: 42,
51 cursor: "next-page",
52 },
53 }).pipe(Effect.provide(RpcHttp.layerRemoteHttpClient(fetchFn)));
54​
55 expect(result).toEqual({
56 patch: "diff --git a/file.ts b/file.ts",
57 truncated: false,
58 nextCursor: null,
59 });
60 expect(calls).toHaveLength(1);
61 const [request, init] = calls[0]!;
62 expect(String(request)).toBe("https://environment.example.test/api/pull-requests/diff");
63 expect(init.method).toBe("POST");
64 expect(init.credentials).toBe("include");
65​
66 const body =
67 typeof init.body === "string"
68 ? init.body
69 : init.body instanceof Uint8Array
70 ? new TextDecoder().decode(init.body)
71 : "";
72 expect(JSON.parse(body)).toEqual({
73 projectId: "project-1",
74 repository: "owner/repository",
75 number: 42,
76 cursor: "next-page",
77 });
78 }),
79 );
80​
81 it.effect("gives rejected diff sessions a recovery action", () =>
82 Effect.gen(function* () {
83 const fetchFn = (() =>
84 Promise.resolve(
85 Response.json(
86 {
87 _tag: "EnvironmentAuthInvalidError",
88 code: "auth_invalid",
89 reason: "invalid_credential",
90 traceId: "trace-auth-test",
91 },
92 { status: 401 },
93 ),
94 )) satisfies typeof fetch;
95​
96 const error = yield* fetchEnvironmentPullRequestDiff({
97 prepared: PREPARED,
98 signer: Option.none(),
99 diff: {
100 projectId: ProjectId.make("project-1"),
101 repository: "owner/repository",
102 number: 42,
103 },
104 }).pipe(Effect.provide(RpcHttp.layerRemoteHttpClient(fetchFn)), Effect.flip);
105​
106 expect(error).toBeInstanceOf(PullRequestDiffCredentialRejectedError);
107 expect(error).toMatchObject({
108 repository: "owner/repository",
109 number: 42,
110 traceId: "trace-auth-test",
111 });
112 expect(error.message).toBe(
113 "This environment session is no longer valid (invalid_credential). Refresh the page or quit and reopen T3 Code.",
114 );
115 }),
116 );
117});