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 * as RpcHttp from "../rpc/http.ts";
8import { fetchEnvironmentPullRequestDiff } from "./pullRequestDiffHttp.ts";
9
10const TARGET = new PrimaryConnectionTarget({
11 environmentId: EnvironmentId.make("environment-1"),
12 label: "Test environment",
13 httpBaseUrl: "https://environment.example.test/base",
14 wsBaseUrl: "wss://environment.example.test",
15});
16
17const PREPARED: PreparedConnection = {
18 environmentId: TARGET.environmentId,
19 label: TARGET.label,
20 httpBaseUrl: TARGET.httpBaseUrl,
21 socketUrl: "wss://environment.example.test/ws",
22 httpAuthorization: null,
23 target: TARGET,
24};
25
26describe("fetchEnvironmentPullRequestDiff", () => {
27 it.effect("posts the diff input to the prepared environment over authenticated HTTP", () =>
28 Effect.gen(function* () {
29 const calls: Array<readonly [RequestInfo | URL, RequestInit]> = [];
30 const fetchFn = ((request, init) => {
31 calls.push([request, init ?? {}]);
32 return Promise.resolve(
33 Response.json({
34 patch: "diff --git a/file.ts b/file.ts",
35 truncated: false,
36 nextCursor: null,
37 }),
38 );
39 }) satisfies typeof fetch;
40
41 const result = yield* fetchEnvironmentPullRequestDiff({
42 prepared: PREPARED,
43 signer: Option.none(),
44 diff: {
45 projectId: ProjectId.make("project-1"),
46 repository: "owner/repository",
47 number: 42,
48 cursor: "next-page",
49 },
50 }).pipe(Effect.provide(RpcHttp.layerRemoteHttpClient(fetchFn)));
51
52 expect(result).toEqual({
53 patch: "diff --git a/file.ts b/file.ts",
54 truncated: false,
55 nextCursor: null,
56 });
57 expect(calls).toHaveLength(1);
58 const [request, init] = calls[0]!;
59 expect(String(request)).toBe("https://environment.example.test/api/pull-requests/diff");
60 expect(init.method).toBe("POST");
61 expect(init.credentials).toBe("include");
62
63 const body =
64 typeof init.body === "string"
65 ? init.body
66 : init.body instanceof Uint8Array
67 ? new TextDecoder().decode(init.body)
68 : "";
69 expect(JSON.parse(body)).toEqual({
70 projectId: "project-1",
71 repository: "owner/repository",
72 number: 42,
73 cursor: "next-page",
74 });
75 }),
76 );
77});