The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { vi } from "vite-plus/test"; |
| 2 | import { |
| 3 | EnvironmentId, |
| 4 | AuthSourceControlWriteScope, |
| 5 | WS_METHODS, |
| 6 | VcsProcessExitError, |
| 7 | type VcsListRefsInput, |
| 8 | type VcsListRefsResult, |
| 9 | type VcsStatusResult, |
| 10 | type ServerConfig, |
| 11 | } from "@t3tools/contracts"; |
| 12 | import { describe, expect, it } from "@effect/vitest"; |
| 13 | import * as Deferred from "effect/Deferred"; |
| 14 | import * as Effect from "effect/Effect"; |
| 15 | import * as Exit from "effect/Exit"; |
| 16 | import * as Fiber from "effect/Fiber"; |
| 17 | import * as Layer from "effect/Layer"; |
| 18 | import * as Logger from "effect/Logger"; |
| 19 | import * as Option from "effect/Option"; |
| 20 | import * as Queue from "effect/Queue"; |
| 21 | import * as Ref from "effect/Ref"; |
| 22 | import * as Stream from "effect/Stream"; |
| 23 | import * as SubscriptionRef from "effect/SubscriptionRef"; |
| 24 | import * as TestClock from "effect/testing/TestClock"; |
| 25 | import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity"; |
| 26 | |
| 27 | import { |
| 28 | AVAILABLE_CONNECTION_STATE, |
| 29 | PrimaryConnectionTarget, |
| 30 | type PreparedConnection, |
| 31 | type SupervisorConnectionState, |
| 32 | } from "../connection/model.ts"; |
| 33 | import * as EnvironmentRegistry from "../connection/registry.ts"; |
| 34 | import * as EnvironmentSupervisor from "../connection/supervisor.ts"; |
| 35 | import * as Persistence from "../platform/persistence.ts"; |
| 36 | import { EnvironmentRpcUnavailableError } from "../rpc/client.ts"; |
| 37 | import type { WsRpcProtocolClient } from "../rpc/protocol.ts"; |
| 38 | import type { RpcSession } from "../rpc/session.ts"; |
| 39 | import { createGitEnvironmentAtoms } from "./git.ts"; |
| 40 | |
| 41 | import { |
| 42 | commitVcsRefsRefresh, |
| 43 | createVcsEnvironmentAtoms, |
| 44 | makeCachedVcsRefsChanges, |
| 45 | resolveQuickAction, |
| 46 | } from "./vcs.ts"; |
| 47 | import { |
| 48 | invalidateCachedVcsRefs, |
| 49 | invalidateVcsRefs, |
| 50 | vcsRefsCacheStateAtom, |
| 51 | } from "./vcsRefInvalidation.ts"; |
| 52 | |
| 53 | const TARGET = new PrimaryConnectionTarget({ |
| 54 | environmentId: EnvironmentId.make("environment-1"), |
| 55 | label: "Test environment", |
| 56 | httpBaseUrl: "https://environment.example.test", |
| 57 | wsBaseUrl: "wss://environment.example.test", |
| 58 | }); |
| 59 | |
| 60 | const CONNECTED_CONNECTION_STATE: SupervisorConnectionState = { |
| 61 | ...AVAILABLE_CONNECTION_STATE, |
| 62 | desired: true, |
| 63 | network: "online", |
| 64 | phase: "connected", |
| 65 | attempt: 1, |
| 66 | generation: 1, |
| 67 | }; |
| 68 | |
| 69 | const CACHED_REFS: VcsListRefsResult = { |
| 70 | refs: [ |
| 71 | { |
| 72 | name: "main", |
| 73 | current: true, |
| 74 | isDefault: true, |
| 75 | worktreePath: "/repo", |
| 76 | }, |
| 77 | ], |
| 78 | isRepo: true, |
| 79 | hasPrimaryRemote: true, |
| 80 | nextCursor: null, |
| 81 | totalCount: 1, |
| 82 | }; |
| 83 | |
| 84 | const LIVE_REFS: VcsListRefsResult = { |
| 85 | ...CACHED_REFS, |
| 86 | refs: [ |
| 87 | { |
| 88 | name: "release", |
| 89 | current: true, |
| 90 | isDefault: true, |
| 91 | worktreePath: "/repo", |
| 92 | }, |
| 93 | ], |
| 94 | }; |
| 95 | |
| 96 | function session(client: WsRpcProtocolClient): RpcSession { |
| 97 | return { |
| 98 | client, |
| 99 | initialConfig: Effect.never, |
| 100 | subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input), |
| 101 | ready: Effect.void, |
| 102 | probe: Effect.void, |
| 103 | inboundFrames: Effect.succeed(0), |
| 104 | closed: Effect.never, |
| 105 | }; |
| 106 | } |
| 107 | |
| 108 | function cacheWithRefs( |
| 109 | refs: Option.Option<VcsListRefsResult>, |
| 110 | overrides: Partial<Persistence.EnvironmentCacheStore["Service"]> = {}, |
| 111 | ) { |
| 112 | return Persistence.EnvironmentCacheStore.of({ |
| 113 | loadShell: () => Effect.succeedNone, |
| 114 | saveShell: () => Effect.void, |
| 115 | loadThread: () => Effect.succeedNone, |
| 116 | saveThread: () => Effect.void, |
| 117 | removeThread: () => Effect.void, |
| 118 | loadServerConfig: () => Effect.succeedNone, |
| 119 | saveServerConfig: () => Effect.void, |
| 120 | loadVcsRefs: () => Effect.succeed(refs), |
| 121 | saveVcsRefs: () => Effect.void, |
| 122 | removeVcsRefs: () => Effect.void, |
| 123 | clearVcsRefs: () => Effect.void, |
| 124 | clear: () => Effect.void, |
| 125 | ...overrides, |
| 126 | }); |
| 127 | } |
| 128 | |
| 129 | describe("branch subscriptions", () => { |
| 130 | it.effect.each([true, false])( |
| 131 | "shares and releases branch streams across session replacement (capability %s)", |
| 132 | (supported) => |
| 133 | Effect.gen(function* () { |
| 134 | const released = yield* Queue.unbounded<string>(); |
| 135 | let subscriptions = 0; |
| 136 | const clientFor = (refName: string, branchSupported: boolean) => |
| 137 | ({ |
| 138 | [branchSupported ? WS_METHODS.subscribeVcsBranch : WS_METHODS.subscribeVcsStatus]: |
| 139 | () => { |
| 140 | subscriptions += 1; |
| 141 | return Stream.concat( |
| 142 | Stream.make( |
| 143 | branchSupported |
| 144 | ? { refName, isRepo: true } |
| 145 | : { _tag: "snapshot", local: status({ refName }), remote: null }, |
| 146 | ), |
| 147 | Stream.never, |
| 148 | ).pipe(Stream.ensuring(Queue.offer(released, refName))); |
| 149 | }, |
| 150 | }) as unknown as WsRpcProtocolClient; |
| 151 | const branchSession = (refName: string, branchSupported: boolean): RpcSession => ({ |
| 152 | ...session(clientFor(refName, branchSupported)), |
| 153 | initialConfig: Effect.succeed({ |
| 154 | environment: { capabilities: branchSupported ? { vcsBranchStatus: true } : {} }, |
| 155 | } as ServerConfig), |
| 156 | }); |
| 157 | const activeSession = yield* SubscriptionRef.make( |
| 158 | Option.some(branchSession("main", supported)), |
| 159 | ); |
| 160 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 161 | target: TARGET, |
| 162 | state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE), |
| 163 | session: activeSession, |
| 164 | prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()), |
| 165 | connect: Effect.void, |
| 166 | disconnect: Effect.void, |
| 167 | retryNow: Effect.void, |
| 168 | }); |
| 169 | const environmentRegistry = EnvironmentRegistry.EnvironmentRegistry.of({ |
| 170 | followStream: (_environmentId, stream) => |
| 171 | Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 172 | } as EnvironmentRegistry.EnvironmentRegistry["Service"]); |
| 173 | const runtime = Atom.runtime( |
| 174 | Layer.merge( |
| 175 | Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry), |
| 176 | Layer.succeed(Persistence.EnvironmentCacheStore, cacheWithRefs(Option.none())), |
| 177 | ), |
| 178 | ); |
| 179 | const atoms = createVcsEnvironmentAtoms(runtime); |
| 180 | const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (value) => |
| 181 | Effect.sync(() => value.dispose()), |
| 182 | ); |
| 183 | const target = { environmentId: TARGET.environmentId, input: { cwd: "/repo" } }; |
| 184 | const first = yield* Stream.toPull( |
| 185 | AtomRegistry.toStreamResult(registry, atoms.branch(target)), |
| 186 | ); |
| 187 | expect(yield* first).toEqual([{ refName: "main", isRepo: true }]); |
| 188 | const second = yield* Stream.toPull( |
| 189 | AtomRegistry.toStreamResult(registry, atoms.branch(target)), |
| 190 | ); |
| 191 | expect(yield* second).toEqual([{ refName: "main", isRepo: true }]); |
| 192 | expect(subscriptions).toBe(1); |
| 193 | yield* SubscriptionRef.set( |
| 194 | activeSession, |
| 195 | Option.some(branchSession("external", !supported)), |
| 196 | ); |
| 197 | expect(yield* Queue.take(released)).toBe("main"); |
| 198 | expect(yield* first).toEqual([{ refName: "external", isRepo: true }]); |
| 199 | expect(yield* second).toEqual([{ refName: "external", isRepo: true }]); |
| 200 | registry.dispose(); |
| 201 | expect(yield* Queue.take(released)).toBe("external"); |
| 202 | }), |
| 203 | ); |
| 204 | }); |
| 205 | |
| 206 | describe("cached VCS refs", () => { |
| 207 | it("invalidates all ref streams in the mutated environment", () => { |
| 208 | const registry = AtomRegistry.make(); |
| 209 | const environment = { |
| 210 | environmentId: TARGET.environmentId, |
| 211 | }; |
| 212 | const otherEnvironment = { |
| 213 | environmentId: EnvironmentId.make("environment-2"), |
| 214 | }; |
| 215 | |
| 216 | expect(registry.get(vcsRefsCacheStateAtom(environment))).toEqual({ |
| 217 | revision: 0, |
| 218 | persistedCacheReadable: true, |
| 219 | }); |
| 220 | expect(registry.get(vcsRefsCacheStateAtom(otherEnvironment))).toEqual({ |
| 221 | revision: 0, |
| 222 | persistedCacheReadable: true, |
| 223 | }); |
| 224 | |
| 225 | invalidateVcsRefs(registry, environment); |
| 226 | |
| 227 | expect(registry.get(vcsRefsCacheStateAtom(environment))).toEqual({ |
| 228 | revision: 1, |
| 229 | persistedCacheReadable: true, |
| 230 | }); |
| 231 | expect(registry.get(vcsRefsCacheStateAtom(otherEnvironment))).toEqual({ |
| 232 | revision: 0, |
| 233 | persistedCacheReadable: true, |
| 234 | }); |
| 235 | registry.dispose(); |
| 236 | }); |
| 237 | |
| 238 | it.effect("bypasses the server ref snapshot when a mounted list is invalidated", () => |
| 239 | Effect.gen(function* () { |
| 240 | const requests = yield* Queue.unbounded<VcsListRefsInput>(); |
| 241 | const client = { |
| 242 | [WS_METHODS.vcsListRefs]: (input: VcsListRefsInput) => |
| 243 | Effect.sync(() => { |
| 244 | Queue.offerUnsafe(requests, input); |
| 245 | return LIVE_REFS; |
| 246 | }), |
| 247 | } as unknown as WsRpcProtocolClient; |
| 248 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 249 | target: TARGET, |
| 250 | state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE), |
| 251 | session: yield* SubscriptionRef.make(Option.some(session(client))), |
| 252 | prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()), |
| 253 | connect: Effect.void, |
| 254 | disconnect: Effect.void, |
| 255 | retryNow: Effect.void, |
| 256 | } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); |
| 257 | const followStream: EnvironmentRegistry.EnvironmentRegistry["Service"]["followStream"] = ( |
| 258 | _environmentId, |
| 259 | stream, |
| 260 | ) => Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor); |
| 261 | const environmentRegistry = EnvironmentRegistry.EnvironmentRegistry.of({ |
| 262 | followStream, |
| 263 | } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]); |
| 264 | const runtime = Atom.runtime( |
| 265 | Layer.merge( |
| 266 | Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry), |
| 267 | Layer.succeed(Persistence.EnvironmentCacheStore, cacheWithRefs(Option.none())), |
| 268 | ), |
| 269 | ); |
| 270 | const atoms = createVcsEnvironmentAtoms(runtime); |
| 271 | const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) => |
| 272 | Effect.sync(() => registry.dispose()), |
| 273 | ); |
| 274 | yield* Effect.acquireRelease( |
| 275 | Effect.sync(() => |
| 276 | registry.mount( |
| 277 | atoms.listRefs({ |
| 278 | environmentId: TARGET.environmentId, |
| 279 | input: { cwd: "/repo", limit: 100 }, |
| 280 | }), |
| 281 | ), |
| 282 | ), |
| 283 | (unmount) => Effect.sync(unmount), |
| 284 | ); |
| 285 | |
| 286 | expect(yield* Queue.take(requests)).toEqual({ cwd: "/repo", limit: 100 }); |
| 287 | |
| 288 | invalidateVcsRefs(registry, TARGET); |
| 289 | |
| 290 | expect(yield* Queue.take(requests)).toEqual({ cwd: "/repo", limit: 100, refresh: true }); |
| 291 | |
| 292 | yield* Effect.acquireRelease( |
| 293 | Effect.sync(() => |
| 294 | registry.mount( |
| 295 | atoms.listRefs({ |
| 296 | environmentId: TARGET.environmentId, |
| 297 | input: { cwd: "/other-repo", limit: 100 }, |
| 298 | }), |
| 299 | ), |
| 300 | ), |
| 301 | (unmount) => Effect.sync(unmount), |
| 302 | ); |
| 303 | expect(yield* Queue.take(requests)).toEqual({ |
| 304 | cwd: "/other-repo", |
| 305 | limit: 100, |
| 306 | refresh: true, |
| 307 | }); |
| 308 | |
| 309 | yield* Effect.acquireRelease( |
| 310 | Effect.sync(() => |
| 311 | registry.mount( |
| 312 | atoms.listRefs({ |
| 313 | environmentId: TARGET.environmentId, |
| 314 | input: { cwd: "/other-repo", query: "release", limit: 100 }, |
| 315 | }), |
| 316 | ), |
| 317 | ), |
| 318 | (unmount) => Effect.sync(unmount), |
| 319 | ); |
| 320 | expect(yield* Queue.take(requests)).toEqual({ |
| 321 | cwd: "/other-repo", |
| 322 | query: "release", |
| 323 | limit: 100, |
| 324 | }); |
| 325 | }), |
| 326 | ); |
| 327 | |
| 328 | it.effect("does not repersist a refresh superseded by ref invalidation", () => |
| 329 | Effect.gen(function* () { |
| 330 | const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) => |
| 331 | Effect.sync(() => registry.dispose()), |
| 332 | ); |
| 333 | const saved = yield* Ref.make<ReadonlyArray<VcsListRefsResult>>([]); |
| 334 | const clears = yield* Ref.make(0); |
| 335 | const revisionsObservedDuringClear = yield* Ref.make<ReadonlyArray<number>>([]); |
| 336 | const cache = cacheWithRefs(Option.none(), { |
| 337 | saveVcsRefs: (_environmentId, _cwd, refs) => |
| 338 | Ref.update(saved, (current) => [...current, refs]), |
| 339 | clearVcsRefs: () => |
| 340 | Effect.all([ |
| 341 | Ref.update(clears, (count) => count + 1), |
| 342 | Ref.update(revisionsObservedDuringClear, (current) => [ |
| 343 | ...current, |
| 344 | registry.get(vcsRefsCacheStateAtom(TARGET)).revision, |
| 345 | ]), |
| 346 | ]).pipe(Effect.asVoid), |
| 347 | }); |
| 348 | |
| 349 | yield* invalidateCachedVcsRefs(registry, { |
| 350 | environmentId: TARGET.environmentId, |
| 351 | cwd: "/repo-worktree", |
| 352 | }).pipe(Effect.provideService(Persistence.EnvironmentCacheStore, cache)); |
| 353 | |
| 354 | expect( |
| 355 | yield* commitVcsRefsRefresh(registry, cache, { |
| 356 | environmentId: TARGET.environmentId, |
| 357 | cwd: "/repo", |
| 358 | refs: CACHED_REFS, |
| 359 | expectedRevision: 0, |
| 360 | persist: true, |
| 361 | }), |
| 362 | ).toBe(false); |
| 363 | |
| 364 | expect(yield* Ref.get(saved)).toEqual([]); |
| 365 | expect(yield* Ref.get(clears)).toBe(1); |
| 366 | expect(yield* Ref.get(revisionsObservedDuringClear)).toEqual([0]); |
| 367 | expect(registry.get(vcsRefsCacheStateAtom(TARGET))).toEqual({ |
| 368 | revision: 1, |
| 369 | persistedCacheReadable: true, |
| 370 | }); |
| 371 | |
| 372 | expect( |
| 373 | yield* commitVcsRefsRefresh(registry, cache, { |
| 374 | environmentId: TARGET.environmentId, |
| 375 | cwd: "/repo", |
| 376 | refs: LIVE_REFS, |
| 377 | expectedRevision: 1, |
| 378 | persist: true, |
| 379 | }), |
| 380 | ).toBe(true); |
| 381 | expect(yield* Ref.get(saved)).toEqual([LIVE_REFS]); |
| 382 | }), |
| 383 | ); |
| 384 | |
| 385 | it.effect("invalidates persisted refs when ref-affecting commands settle", () => |
| 386 | Effect.gen(function* () { |
| 387 | const expectedError = new EnvironmentRpcUnavailableError({ |
| 388 | environmentId: TARGET.environmentId, |
| 389 | message: "pull failed after fetching refs", |
| 390 | }); |
| 391 | const client = { |
| 392 | [WS_METHODS.vcsPull]: () => Effect.fail(expectedError), |
| 393 | [WS_METHODS.vcsRefreshStatus]: () => Effect.void, |
| 394 | [WS_METHODS.gitPreparePullRequestThread]: () => |
| 395 | Effect.succeed({ branch: "feature/pr", worktreePath: null }), |
| 396 | } as unknown as WsRpcProtocolClient; |
| 397 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 398 | target: TARGET, |
| 399 | state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE), |
| 400 | session: yield* SubscriptionRef.make(Option.some(session(client))), |
| 401 | prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()), |
| 402 | connect: Effect.void, |
| 403 | disconnect: Effect.void, |
| 404 | retryNow: Effect.void, |
| 405 | } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); |
| 406 | const run: EnvironmentRegistry.EnvironmentRegistry["Service"]["run"] = ( |
| 407 | _environmentId, |
| 408 | effect, |
| 409 | ) => Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor); |
| 410 | const environmentRegistry = EnvironmentRegistry.EnvironmentRegistry.of({ |
| 411 | run, |
| 412 | } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]); |
| 413 | const clears = yield* Ref.make(0); |
| 414 | const runtime = Atom.runtime( |
| 415 | Layer.merge( |
| 416 | Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry), |
| 417 | Layer.succeed( |
| 418 | Persistence.EnvironmentCacheStore, |
| 419 | cacheWithRefs(Option.none(), { |
| 420 | clearVcsRefs: () => Ref.update(clears, (count) => count + 1), |
| 421 | }), |
| 422 | ), |
| 423 | ), |
| 424 | ); |
| 425 | const atoms = createVcsEnvironmentAtoms(runtime); |
| 426 | const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) => |
| 427 | Effect.sync(() => registry.dispose()), |
| 428 | ); |
| 429 | |
| 430 | const result = yield* Effect.exit( |
| 431 | atoms.pull.run(registry, { |
| 432 | environmentId: TARGET.environmentId, |
| 433 | input: { cwd: "/repo" }, |
| 434 | }), |
| 435 | ); |
| 436 | |
| 437 | expect(Exit.isFailure(result)).toBe(true); |
| 438 | expect(yield* Ref.get(clears)).toBe(1); |
| 439 | expect(registry.get(vcsRefsCacheStateAtom(TARGET)).revision).toBe(1); |
| 440 | |
| 441 | const refreshResult = yield* Effect.exit( |
| 442 | atoms.refreshStatus.run(registry, { |
| 443 | environmentId: TARGET.environmentId, |
| 444 | input: { cwd: "/repo" }, |
| 445 | }), |
| 446 | ); |
| 447 | |
| 448 | expect(Exit.isSuccess(refreshResult)).toBe(true); |
| 449 | expect(yield* Ref.get(clears)).toBe(2); |
| 450 | expect(registry.get(vcsRefsCacheStateAtom(TARGET)).revision).toBe(2); |
| 451 | |
| 452 | const prepareResult = yield* Effect.exit( |
| 453 | createGitEnvironmentAtoms(runtime).preparePullRequestThread.run(registry, { |
| 454 | environmentId: TARGET.environmentId, |
| 455 | input: { cwd: "/repo", reference: "42", mode: "local" }, |
| 456 | }), |
| 457 | ); |
| 458 | |
| 459 | expect(Exit.isSuccess(prepareResult)).toBe(true); |
| 460 | expect(yield* Ref.get(clears)).toBe(3); |
| 461 | expect(registry.get(vcsRefsCacheStateAtom(TARGET)).revision).toBe(3); |
| 462 | }), |
| 463 | ); |
| 464 | |
| 465 | it.effect("a checkout does not wait behind a picker's local status refresh", () => |
| 466 | Effect.gen(function* () { |
| 467 | const refreshStarted = yield* Deferred.make<void>(); |
| 468 | const releaseRefresh = yield* Deferred.make<void>(); |
| 469 | const client = { |
| 470 | [WS_METHODS.vcsRefreshStatus]: () => |
| 471 | Deferred.succeed(refreshStarted, undefined).pipe( |
| 472 | Effect.andThen(Deferred.await(releaseRefresh)), |
| 473 | ), |
| 474 | [WS_METHODS.vcsSwitchRef]: () => Effect.succeed({ refName: "feature" }), |
| 475 | } as unknown as WsRpcProtocolClient; |
| 476 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 477 | target: TARGET, |
| 478 | state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE), |
| 479 | session: yield* SubscriptionRef.make(Option.some(session(client))), |
| 480 | prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()), |
| 481 | connect: Effect.void, |
| 482 | disconnect: Effect.void, |
| 483 | retryNow: Effect.void, |
| 484 | } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); |
| 485 | const run: EnvironmentRegistry.EnvironmentRegistry["Service"]["run"] = ( |
| 486 | _environmentId, |
| 487 | effect, |
| 488 | ) => Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor); |
| 489 | const runtime = Atom.runtime( |
| 490 | Layer.merge( |
| 491 | Layer.succeed( |
| 492 | EnvironmentRegistry.EnvironmentRegistry, |
| 493 | EnvironmentRegistry.EnvironmentRegistry.of({ |
| 494 | run, |
| 495 | } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]), |
| 496 | ), |
| 497 | Layer.succeed(Persistence.EnvironmentCacheStore, cacheWithRefs(Option.none())), |
| 498 | ), |
| 499 | ); |
| 500 | const atoms = createVcsEnvironmentAtoms(runtime); |
| 501 | const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) => |
| 502 | Effect.sync(() => registry.dispose()), |
| 503 | ); |
| 504 | const target = { environmentId: TARGET.environmentId, input: { cwd: "/repo" } }; |
| 505 | |
| 506 | const refresh = yield* Effect.forkChild( |
| 507 | Effect.exit( |
| 508 | atoms.refreshLocalStatusForPicker.run(registry, { |
| 509 | ...target, |
| 510 | input: { cwd: "/repo", localOnly: true }, |
| 511 | }), |
| 512 | ), |
| 513 | { startImmediately: true }, |
| 514 | ); |
| 515 | yield* Deferred.await(refreshStarted); |
| 516 | const checkout = yield* Effect.exit( |
| 517 | atoms.switchRef.run(registry, { |
| 518 | ...target, |
| 519 | input: { cwd: "/repo", refName: "feature" }, |
| 520 | }), |
| 521 | ); |
| 522 | |
| 523 | expect(Exit.isSuccess(checkout)).toBe(true); |
| 524 | yield* Deferred.succeed(releaseRefresh, undefined); |
| 525 | expect(Exit.isSuccess(yield* Fiber.join(refresh))).toBe(true); |
| 526 | }), |
| 527 | ); |
| 528 | |
| 529 | it.effect("suppresses persisted snapshots after an environment-wide clear fails", () => |
| 530 | Effect.gen(function* () { |
| 531 | const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) => |
| 532 | Effect.sync(() => registry.dispose()), |
| 533 | ); |
| 534 | const persisted = yield* Ref.make<Option.Option<VcsListRefsResult>>(Option.some(CACHED_REFS)); |
| 535 | const loads = yield* Ref.make(0); |
| 536 | const clearAttempts = yield* Ref.make(0); |
| 537 | const cache = cacheWithRefs(Option.none(), { |
| 538 | loadVcsRefs: () => |
| 539 | Ref.update(loads, (count) => count + 1).pipe(Effect.andThen(Ref.get(persisted))), |
| 540 | saveVcsRefs: (_environmentId, _cwd, refs) => Ref.set(persisted, Option.some(refs)), |
| 541 | clearVcsRefs: () => |
| 542 | Ref.updateAndGet(clearAttempts, (count) => count + 1).pipe( |
| 543 | Effect.flatMap((attempt) => |
| 544 | attempt === 1 |
| 545 | ? Effect.fail( |
| 546 | new Persistence.ConnectionPersistenceError({ |
| 547 | operation: "clear-vcs-refs", |
| 548 | message: "storage unavailable", |
| 549 | }), |
| 550 | ) |
| 551 | : Ref.set(persisted, Option.none()), |
| 552 | ), |
| 553 | ), |
| 554 | }); |
| 555 | |
| 556 | yield* invalidateCachedVcsRefs(registry, { |
| 557 | environmentId: TARGET.environmentId, |
| 558 | cwd: "/repo", |
| 559 | }).pipe(Effect.provideService(Persistence.EnvironmentCacheStore, cache)); |
| 560 | |
| 561 | const state = registry.get(vcsRefsCacheStateAtom(TARGET)); |
| 562 | expect(state).toEqual({ |
| 563 | revision: 1, |
| 564 | persistedCacheReadable: false, |
| 565 | }); |
| 566 | |
| 567 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 568 | target: TARGET, |
| 569 | state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE), |
| 570 | session: yield* SubscriptionRef.make(Option.none<RpcSession>()), |
| 571 | prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()), |
| 572 | connect: Effect.void, |
| 573 | disconnect: Effect.void, |
| 574 | retryNow: Effect.void, |
| 575 | } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); |
| 576 | const refs = yield* Stream.unwrap( |
| 577 | makeCachedVcsRefsChanges( |
| 578 | { cwd: "/repo", limit: 100 }, |
| 579 | state.revision, |
| 580 | registry, |
| 581 | state.persistedCacheReadable, |
| 582 | ).pipe( |
| 583 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 584 | Effect.provideService(Persistence.EnvironmentCacheStore, cache), |
| 585 | ), |
| 586 | ).pipe(Stream.runHead, Effect.forkChild({ startImmediately: true })); |
| 587 | |
| 588 | yield* Effect.yieldNow; |
| 589 | expect(yield* Ref.get(loads)).toBe(0); |
| 590 | yield* Fiber.interrupt(refs); |
| 591 | expect(registry.get(vcsRefsCacheStateAtom(TARGET))).toEqual(state); |
| 592 | |
| 593 | expect( |
| 594 | yield* commitVcsRefsRefresh(registry, cache, { |
| 595 | environmentId: TARGET.environmentId, |
| 596 | cwd: "/repo", |
| 597 | refs: LIVE_REFS, |
| 598 | expectedRevision: state.revision, |
| 599 | persist: true, |
| 600 | }), |
| 601 | ).toBe(true); |
| 602 | const recoveredState = registry.get(vcsRefsCacheStateAtom(TARGET)); |
| 603 | expect(recoveredState).toEqual({ |
| 604 | revision: 1, |
| 605 | persistedCacheReadable: true, |
| 606 | }); |
| 607 | expect(yield* Ref.get(clearAttempts)).toBe(2); |
| 608 | |
| 609 | const recoveredRefs = yield* Stream.unwrap( |
| 610 | makeCachedVcsRefsChanges( |
| 611 | { cwd: "/repo", limit: 100 }, |
| 612 | recoveredState.revision, |
| 613 | registry, |
| 614 | recoveredState.persistedCacheReadable, |
| 615 | ).pipe( |
| 616 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 617 | Effect.provideService(Persistence.EnvironmentCacheStore, cache), |
| 618 | ), |
| 619 | ).pipe(Stream.runHead); |
| 620 | |
| 621 | expect(Option.getOrThrow(recoveredRefs)).toEqual(LIVE_REFS); |
| 622 | expect(yield* Ref.get(loads)).toBe(1); |
| 623 | }), |
| 624 | ); |
| 625 | |
| 626 | it.effect("loads an unfiltered branch list without a connection", () => |
| 627 | Effect.gen(function* () { |
| 628 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 629 | target: TARGET, |
| 630 | state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE), |
| 631 | session: yield* SubscriptionRef.make(Option.none<RpcSession>()), |
| 632 | prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()), |
| 633 | connect: Effect.void, |
| 634 | disconnect: Effect.void, |
| 635 | retryNow: Effect.void, |
| 636 | } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); |
| 637 | const refs = yield* Stream.unwrap( |
| 638 | makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe( |
| 639 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 640 | Effect.provideService( |
| 641 | Persistence.EnvironmentCacheStore, |
| 642 | cacheWithRefs(Option.some(CACHED_REFS)), |
| 643 | ), |
| 644 | ), |
| 645 | ).pipe(Stream.runHead); |
| 646 | |
| 647 | expect(Option.getOrThrow(refs)).toEqual(CACHED_REFS); |
| 648 | }), |
| 649 | ); |
| 650 | |
| 651 | it.effect("waits silently for a session while the connection phase is still connected", () => |
| 652 | Effect.gen(function* () { |
| 653 | const logs: unknown[] = []; |
| 654 | const logger = Logger.make(({ message }) => logs.push(message)); |
| 655 | const liveSession = yield* SubscriptionRef.make(Option.none<RpcSession>()); |
| 656 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 657 | target: TARGET, |
| 658 | state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE), |
| 659 | session: liveSession, |
| 660 | prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()), |
| 661 | connect: Effect.void, |
| 662 | disconnect: Effect.void, |
| 663 | retryNow: Effect.void, |
| 664 | } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); |
| 665 | const refs = yield* Stream.unwrap( |
| 666 | makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe( |
| 667 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 668 | Effect.provideService( |
| 669 | Persistence.EnvironmentCacheStore, |
| 670 | cacheWithRefs(Option.some(CACHED_REFS)), |
| 671 | ), |
| 672 | ), |
| 673 | ).pipe( |
| 674 | Stream.take(2), |
| 675 | Stream.runCollect, |
| 676 | Effect.provide(Logger.layer([logger], { mergeWithExisting: false })), |
| 677 | Effect.forkChild({ startImmediately: true }), |
| 678 | ); |
| 679 | |
| 680 | yield* TestClock.adjust("1 minute"); |
| 681 | expect(logs).toEqual([]); |
| 682 | |
| 683 | const client = { |
| 684 | [WS_METHODS.vcsListRefs]: () => Effect.succeed(LIVE_REFS), |
| 685 | } as unknown as WsRpcProtocolClient; |
| 686 | yield* SubscriptionRef.set(liveSession, Option.some(session(client))); |
| 687 | expect(yield* Fiber.join(refs)).toEqual([CACHED_REFS, LIVE_REFS]); |
| 688 | expect(logs).toEqual([]); |
| 689 | }), |
| 690 | ); |
| 691 | |
| 692 | it.effect("cancels an in-flight refresh and restarts when the RPC session changes", () => |
| 693 | Effect.gen(function* () { |
| 694 | const started = yield* Deferred.make<void>(); |
| 695 | const interrupted = yield* Deferred.make<void>(); |
| 696 | const calls = yield* Ref.make(0); |
| 697 | const interruptions = yield* Ref.make(0); |
| 698 | const client = { |
| 699 | [WS_METHODS.vcsListRefs]: () => |
| 700 | Ref.updateAndGet(calls, (count) => count + 1).pipe( |
| 701 | Effect.flatMap((count) => |
| 702 | count === 1 |
| 703 | ? Deferred.succeed(started, undefined).pipe( |
| 704 | Effect.andThen(Effect.never), |
| 705 | Effect.onInterrupt(() => |
| 706 | Ref.update(interruptions, (interruptions) => interruptions + 1).pipe( |
| 707 | Effect.andThen(Deferred.succeed(interrupted, undefined)), |
| 708 | ), |
| 709 | ), |
| 710 | ) |
| 711 | : Effect.succeed(LIVE_REFS), |
| 712 | ), |
| 713 | ), |
| 714 | } as unknown as WsRpcProtocolClient; |
| 715 | const liveSession = yield* SubscriptionRef.make(Option.some(session(client))); |
| 716 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 717 | target: TARGET, |
| 718 | state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE), |
| 719 | session: liveSession, |
| 720 | prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()), |
| 721 | connect: Effect.void, |
| 722 | disconnect: Effect.void, |
| 723 | retryNow: Effect.void, |
| 724 | } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); |
| 725 | |
| 726 | const fiber = yield* Stream.unwrap( |
| 727 | makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe( |
| 728 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 729 | Effect.provideService(Persistence.EnvironmentCacheStore, cacheWithRefs(Option.none())), |
| 730 | ), |
| 731 | ).pipe(Stream.runHead, Effect.forkChild); |
| 732 | |
| 733 | yield* Deferred.await(started); |
| 734 | yield* SubscriptionRef.set(liveSession, Option.none()); |
| 735 | yield* Deferred.await(interrupted); |
| 736 | expect(yield* Ref.get(interruptions)).toBe(1); |
| 737 | |
| 738 | yield* SubscriptionRef.set(liveSession, Option.some(session(client))); |
| 739 | expect(Option.getOrThrow(yield* Fiber.join(fiber))).toEqual(LIVE_REFS); |
| 740 | expect(yield* Ref.get(calls)).toBe(2); |
| 741 | }), |
| 742 | ); |
| 743 | |
| 744 | it.effect("retries Git failures and cancels pending retries when the session is removed", () => |
| 745 | Effect.gen(function* () { |
| 746 | const calls = yield* Ref.make(0); |
| 747 | const warnings = yield* Queue.unbounded<unknown>(); |
| 748 | const logger = Logger.make(({ message }) => Queue.offerUnsafe(warnings, message)); |
| 749 | const failure = new VcsProcessExitError({ |
| 750 | operation: "listRefs", |
| 751 | command: "git", |
| 752 | cwd: "/repo", |
| 753 | exitCode: 128, |
| 754 | detail: "Could not read refs.", |
| 755 | }); |
| 756 | const client = { |
| 757 | [WS_METHODS.vcsListRefs]: () => |
| 758 | Ref.updateAndGet(calls, (count) => count + 1).pipe( |
| 759 | Effect.flatMap((count) => |
| 760 | count < 3 ? Effect.fail(failure) : Effect.succeed(LIVE_REFS), |
| 761 | ), |
| 762 | ), |
| 763 | } as unknown as WsRpcProtocolClient; |
| 764 | const liveSession = yield* SubscriptionRef.make(Option.some(session(client))); |
| 765 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 766 | target: TARGET, |
| 767 | state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE), |
| 768 | session: liveSession, |
| 769 | prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()), |
| 770 | connect: Effect.void, |
| 771 | disconnect: Effect.void, |
| 772 | retryNow: Effect.void, |
| 773 | } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); |
| 774 | const refs = yield* Stream.unwrap( |
| 775 | makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe( |
| 776 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 777 | Effect.provideService( |
| 778 | Persistence.EnvironmentCacheStore, |
| 779 | cacheWithRefs(Option.some(CACHED_REFS)), |
| 780 | ), |
| 781 | ), |
| 782 | ).pipe( |
| 783 | Stream.take(2), |
| 784 | Stream.runCollect, |
| 785 | Effect.provide(Logger.layer([logger], { mergeWithExisting: false })), |
| 786 | Effect.forkChild({ startImmediately: true }), |
| 787 | ); |
| 788 | |
| 789 | yield* Queue.take(warnings); |
| 790 | expect(yield* Ref.get(calls)).toBe(1); |
| 791 | yield* TestClock.adjust("1 second"); |
| 792 | expect(yield* Ref.get(calls)).toBe(2); |
| 793 | expect(yield* Queue.size(warnings)).toBe(1); |
| 794 | |
| 795 | yield* SubscriptionRef.set(liveSession, Option.none()); |
| 796 | yield* TestClock.adjust("1 minute"); |
| 797 | expect(yield* Ref.get(calls)).toBe(2); |
| 798 | expect(yield* Queue.size(warnings)).toBe(1); |
| 799 | |
| 800 | yield* SubscriptionRef.set(liveSession, Option.some(session(client))); |
| 801 | expect(yield* Fiber.join(refs)).toEqual([CACHED_REFS, LIVE_REFS]); |
| 802 | expect(yield* Ref.get(calls)).toBe(3); |
| 803 | }), |
| 804 | ); |
| 805 | |
| 806 | it.effect("emits persisted refs before a live refresh", () => |
| 807 | Effect.gen(function* () { |
| 808 | const client = { |
| 809 | [WS_METHODS.vcsListRefs]: () => Effect.succeed(LIVE_REFS), |
| 810 | } as unknown as WsRpcProtocolClient; |
| 811 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 812 | target: TARGET, |
| 813 | state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE), |
| 814 | session: yield* SubscriptionRef.make(Option.some(session(client))), |
| 815 | prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()), |
| 816 | connect: Effect.void, |
| 817 | disconnect: Effect.void, |
| 818 | retryNow: Effect.void, |
| 819 | } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); |
| 820 | |
| 821 | const refs = yield* Stream.unwrap( |
| 822 | makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe( |
| 823 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 824 | Effect.provideService( |
| 825 | Persistence.EnvironmentCacheStore, |
| 826 | cacheWithRefs(Option.some(CACHED_REFS)), |
| 827 | ), |
| 828 | ), |
| 829 | ).pipe(Stream.take(2), Stream.runCollect); |
| 830 | |
| 831 | expect(refs).toEqual([CACHED_REFS, LIVE_REFS]); |
| 832 | }), |
| 833 | ); |
| 834 | }); |
| 835 | |
| 836 | vi.mock("./session.ts", () => ({ |
| 837 | createEnvironmentSessionAtoms: () => ({ sessionStateAtom: grantedSessions }), |
| 838 | })); |
| 839 | const grantedSessions = Atom.family((_id: EnvironmentId) => |
| 840 | Atom.make( |
| 841 | AsyncResult.success({ |
| 842 | authenticated: true, |
| 843 | auth: { |
| 844 | policy: "remote-reachable", |
| 845 | bootstrapMethods: [], |
| 846 | sessionMethods: [], |
| 847 | sessionCookieName: "test", |
| 848 | }, |
| 849 | scopes: [AuthSourceControlWriteScope], |
| 850 | }), |
| 851 | ), |
| 852 | ); |
| 853 | |
| 854 | function status(overrides: Partial<VcsStatusResult> = {}): VcsStatusResult { |
| 855 | return { |
| 856 | isRepo: true, |
| 857 | hasPrimaryRemote: true, |
| 858 | isDefaultRef: false, |
| 859 | refName: "feature/test", |
| 860 | hasWorkingTreeChanges: false, |
| 861 | workingTree: { |
| 862 | files: [], |
| 863 | insertions: 0, |
| 864 | deletions: 0, |
| 865 | }, |
| 866 | hasUpstream: true, |
| 867 | aheadCount: 0, |
| 868 | behindCount: 0, |
| 869 | pr: null, |
| 870 | ...overrides, |
| 871 | }; |
| 872 | } |
| 873 | |
| 874 | describe("resolveQuickAction", () => { |
| 875 | it("commits local changes without pushing when the branch is behind upstream", () => { |
| 876 | const behind = status({ hasWorkingTreeChanges: true, behindCount: 2 }); |
| 877 | const commit = { label: "Commit", disabled: false, kind: "run_action", action: "commit" }; |
| 878 | expect(resolveQuickAction(behind, false)).toEqual(commit); |
| 879 | expect( |
| 880 | resolveQuickAction( |
| 881 | { |
| 882 | ...behind, |
| 883 | pr: { |
| 884 | number: 13, |
| 885 | title: "Open PR", |
| 886 | url: "https://example.com/pr/13", |
| 887 | baseRef: "main", |
| 888 | headRef: "feature/test", |
| 889 | state: "open", |
| 890 | }, |
| 891 | }, |
| 892 | false, |
| 893 | ), |
| 894 | ).toEqual(commit); |
| 895 | }); |
| 896 | }); |