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