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