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

pullRequests.test.ts

163 lines6.3 KB

1import { EnvironmentId, ProjectId, WS_METHODS } from "@t3tools/contracts";
2import { expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Latch from "effect/Latch";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as PubSub from "effect/PubSub";
8import * as Stream from "effect/Stream";
9import * as SubscriptionRef from "effect/SubscriptionRef";
10import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity";
11​
12import {
13 AVAILABLE_CONNECTION_STATE,
14 PrimaryConnectionTarget,
15 type PreparedConnection,
16 type SupervisorConnectionState,
17} from "../connection/model.ts";
18import * as EnvironmentRegistry from "../connection/registry.ts";
19import * as EnvironmentSupervisor from "../connection/supervisor.ts";
20import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
21import type { RpcSession } from "../rpc/session.ts";
22import { createPullRequestEnvironmentAtoms } from "./pullRequests.ts";
23import { PullRequestDiffLoader } from "./pullRequestDiffHttp.ts";
24import { executeAtomQuery } from "./runtime.ts";
25​
26const TARGET = new PrimaryConnectionTarget({
27 environmentId: EnvironmentId.make("environment-1"),
28 label: "Test environment",
29 httpBaseUrl: "https://environment.example.test",
30 wsBaseUrl: "wss://environment.example.test",
31});
32​
33function session(client: WsRpcProtocolClient): RpcSession {
34 return {
35 client,
36 initialConfig: Effect.never,
37 subscribeServerConfig: (input) => client.subscribeServerConfig(input),
38 ready: Effect.void,
39 probe: Effect.void,
40 closed: Effect.never,
41 };
43​
44it.effect("refreshes pull request activity after a comment is updated", () =>
45 Effect.scoped(
46 Effect.gen(function* () {
47 const refreshEvents = yield* PubSub.unbounded<number>();
48 let commentBody = "old comment";
49 const client = {
50 [WS_METHODS.pullRequestsSubscribeRefreshes]: () => Stream.fromPubSub(refreshEvents),
51 [WS_METHODS.pullRequestsActivity]: () =>
52 Effect.succeed({
53 author: null,
54 reviewers: [],
55 comments: [
56 {
57 id: "comment-1",
58 kind: "issue-comment",
59 author: null,
60 body: commentBody,
61 createdAt: "2026-08-24T00:00:00Z",
62 url: null,
63 path: null,
64 reviewState: null,
65 reactions: [],
66 },
67 ],
68 commentCount: 1,
69 commentsTruncated: false,
70 reviewThreads: [],
71 commits: [],
72 reactions: [],
73 }),
74 [WS_METHODS.pullRequestsUpdateComment]: (input: { readonly body: string }) =>
75 Effect.sync(() => {
76 commentBody = input.body;
77 }),
78 } as unknown as WsRpcProtocolClient;
79 const connectionState: SupervisorConnectionState = {
80 ...AVAILABLE_CONNECTION_STATE,
81 desired: true,
82 network: "online",
83 phase: "connected",
84 attempt: 1,
85 generation: 1,
86 };
87 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
88 target: TARGET,
89 state: yield* SubscriptionRef.make(connectionState),
90 session: yield* SubscriptionRef.make(Option.some(session(client))),
91 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
92 connect: Effect.void,
93 disconnect: Effect.void,
94 retryNow: Effect.void,
95 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
96 const environmentRegistry = EnvironmentRegistry.EnvironmentRegistry.of({
97 run: (_environmentId, effect) =>
98 Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
99 runStream: (_environmentId, stream) =>
100 Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
101 followStream: (_environmentId, stream) =>
102 Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
103 } as EnvironmentRegistry.EnvironmentRegistry["Service"]);
104 const runtime = Atom.runtime(
105 Layer.merge(
106 Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),
107 Layer.succeed(
108 PullRequestDiffLoader,
109 PullRequestDiffLoader.of({ load: () => Effect.die("unused") }),
110 ),
111 ),
112 );
113 const atoms = createPullRequestEnvironmentAtoms(runtime);
114 const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) =>
115 Effect.sync(() => registry.dispose()),
116 );
117 const reference = {
118 projectId: ProjectId.make("project-1"),
119 repository: "acme/web",
120 number: 1,
121 } as const;
122 const activity = atoms.activity({ environmentId: TARGET.environmentId, input: reference });
123 const unmount = registry.mount(activity);
124 yield* Effect.addFinalizer(() => Effect.sync(unmount));
125​
126 const initial = yield* Effect.promise(() => executeAtomQuery(registry, activity));
127 expect(AsyncResult.isSuccess(initial)).toBe(true);
128 if (!AsyncResult.isSuccess(initial)) {
129 return yield* Effect.die("activity did not load");
130 }
131 expect(initial.value.comments[0]?.body).toBe("old comment");
132​
133 const update = yield* Effect.promise(() =>
134 atoms.updateComment.run(registry, {
135 environmentId: TARGET.environmentId,
136 input: { ...reference, commentId: "comment-1", kind: "issue-comment", body: "updated" },
137 }),
138 );
139​
140 expect(AsyncResult.isSuccess(update)).toBe(true);
141 expect(
142 (yield* AtomRegistry.getResult(registry, activity, { suspendOnWaiting: true })).comments[0]
143 ?.body,
144 ).toBe("updated");
145 const refreshed = Latch.makeUnsafe();
146 const stop = registry.subscribe(activity, (result) => {
147 if (AsyncResult.isSuccess(result) && result.value.comments[0]?.body === "after turn") {
148 refreshed.openUnsafe();
149 }
150 });
151 yield* Effect.addFinalizer(() => Effect.sync(stop));
152​
153 commentBody = "after turn";
154 yield* PubSub.publish(refreshEvents, 1);
155 yield* refreshed.await;
156​
157 expect(
158 (yield* AtomRegistry.getResult(registry, activity, { suspendOnWaiting: true })).comments[0]
159 ?.body,
160 ).toBe("after turn");
161 }),
162 ),
163);