Open History to see the last commit
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 { remoteHttpClientLayer } 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(remoteHttpClientLayer(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 // The assertion deliberately inspects the serialized wire body rather than decoding a
73 // domain value for use in application code.
74 // @effect-diagnostics-next-line preferSchemaOverJson:off
75 expect(JSON.parse(body)).toEqual({
76 projectId: "project-1",
77 repository: "owner/repository",
78 number: 42,
79 cursor: "next-page",
80 });
81 }),
82 );
83
84 it.effect("gives rejected diff sessions a recovery action", () =>
85 Effect.gen(function* () {
86 const fetchFn = (() =>
87 Promise.resolve(
88 Response.json(
89 {
90 _tag: "EnvironmentAuthInvalidError",
91 code: "auth_invalid",
92 reason: "invalid_credential",
93 traceId: "trace-auth-test",
94 },
95 { status: 401 },
96 ),
97 )) satisfies typeof fetch;
98
99 const error = yield* fetchEnvironmentPullRequestDiff({
100 prepared: PREPARED,
101 signer: Option.none(),
102 diff: {
103 projectId: ProjectId.make("project-1"),
104 repository: "owner/repository",
105 number: 42,
106 },
107 }).pipe(Effect.provide(remoteHttpClientLayer(fetchFn)), Effect.flip);
108
109 expect(error).toBeInstanceOf(PullRequestDiffCredentialRejectedError);
110 expect(error).toMatchObject({
111 repository: "owner/repository",
112 number: 42,
113 traceId: "trace-auth-test",
114 });
115 expect(error.message).toBe(
116 "This environment session is no longer valid (invalid_credential). Refresh the page or quit and reopen T3 Code.",
117 );
118 }),
119 );
120});