The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { SourceControlProviderKind } from "@t3tools/contracts"; |
| 2 | import { assert, it, describe } from "@effect/vitest"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import * as Deferred from "effect/Deferred"; |
| 5 | import * as DateTime from "effect/DateTime"; |
| 6 | import * as Duration from "effect/Duration"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Fiber from "effect/Fiber"; |
| 9 | import * as Exit from "effect/Exit"; |
| 10 | import * as FileSystem from "effect/FileSystem"; |
| 11 | import * as Layer from "effect/Layer"; |
| 12 | import * as Option from "effect/Option"; |
| 13 | import * as Path from "effect/Path"; |
| 14 | import * as Queue from "effect/Queue"; |
| 15 | import * as Scope from "effect/Scope"; |
| 16 | import * as Scheduler from "effect/Scheduler"; |
| 17 | import * as Stream from "effect/Stream"; |
| 18 | import * as TestClock from "effect/testing/TestClock"; |
| 19 | import * as Tracer from "effect/Tracer"; |
| 20 | import type { |
| 21 | BackgroundScope, |
| 22 | VcsBranchStatus, |
| 23 | VcsStatusLocalResult, |
| 24 | VcsStatusRemoteResult, |
| 25 | VcsStatusResult, |
| 26 | VcsStatusStreamEvent, |
| 27 | } from "@t3tools/contracts"; |
| 28 | import { GitCommandError, GitManagerError } from "@t3tools/contracts"; |
| 29 | import { applyGitStatusStreamEvent } from "@t3tools/shared/git"; |
| 30 | |
| 31 | import * as VcsAutomaticFetchInterval from "./VcsAutomaticFetchInterval.ts"; |
| 32 | import * as VcsStatusBroadcaster from "./VcsStatusBroadcaster.ts"; |
| 33 | import * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; |
| 34 | import * as GitWorkflowService from "../git/GitWorkflowService.ts"; |
| 35 | import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; |
| 36 | |
| 37 | const TEST_EPOCH = DateTime.makeUnsafe("1970-01-01T00:00:00.000Z"); |
| 38 | |
| 39 | const baseLocalStatus: VcsStatusLocalResult = { |
| 40 | isRepo: true, |
| 41 | sourceControlProvider: { |
| 42 | kind: SourceControlProviderKind.make("github"), |
| 43 | name: "GitHub", |
| 44 | baseUrl: "https://github.com", |
| 45 | }, |
| 46 | hasPrimaryRemote: true, |
| 47 | isDefaultRef: false, |
| 48 | refName: "feature/status-broadcast", |
| 49 | hasWorkingTreeChanges: false, |
| 50 | workingTree: { files: [], insertions: 0, deletions: 0 }, |
| 51 | }; |
| 52 | |
| 53 | const baseRemoteStatus: VcsStatusRemoteResult = { |
| 54 | hasUpstream: true, |
| 55 | aheadCount: 0, |
| 56 | behindCount: 0, |
| 57 | pr: null, |
| 58 | }; |
| 59 | |
| 60 | const remoteStatusWithPr: VcsStatusRemoteResult = { |
| 61 | ...baseRemoteStatus, |
| 62 | pr: { |
| 63 | number: 2978, |
| 64 | title: "[codex] Rewrite client connection architecture", |
| 65 | url: "https://github.com/pingdotgg/t3code/pull/2978", |
| 66 | baseRef: "main", |
| 67 | headRef: "upstream-topic", |
| 68 | state: "open", |
| 69 | }, |
| 70 | }; |
| 71 | |
| 72 | function layerTestFor(state: { |
| 73 | currentLocalStatus: VcsStatusLocalResult; |
| 74 | currentRemoteStatus: VcsStatusRemoteResult | null; |
| 75 | currentDivergence?: Pick<VcsStatusRemoteResult, "aheadCount" | "behindCount">; |
| 76 | localStatusCalls: number; |
| 77 | remoteStatusCalls: number; |
| 78 | localInvalidationCalls: number; |
| 79 | remoteInvalidationCalls: number; |
| 80 | remoteStatusRefreshUpstreamValues?: Array<boolean | undefined>; |
| 81 | localStatusBranchChanges?: Array<boolean>; |
| 82 | refSnapshot?: string | null; |
| 83 | remoteStatusError?: GitManagerError; |
| 84 | backgroundWorkEnabled?: boolean; |
| 85 | automaticFetchInterval?: Effect.Effect<Duration.Duration>; |
| 86 | beforeRemoteStatus?: Effect.Effect<void, GitManagerError>; |
| 87 | beforeLocalStatus?: Effect.Effect<void>; |
| 88 | beforeCurrentBranch?: Effect.Effect<void>; |
| 89 | beforeInvalidate?: Effect.Effect<void>; |
| 90 | branchRead?: () => Effect.Effect<string | null>; |
| 91 | fileSystem?: FileSystem.FileSystem; |
| 92 | pullCurrentBranch?: GitWorkflowService.GitWorkflowService["Service"]["pullCurrentBranch"]; |
| 93 | headPath?: string; |
| 94 | headPathCalls?: number; |
| 95 | failLocalStatus?: boolean; |
| 96 | onLocalStatusFailure?: Effect.Effect<void>; |
| 97 | }) { |
| 98 | return VcsStatusBroadcaster.layer.pipe( |
| 99 | Layer.provideMerge( |
| 100 | state.fileSystem |
| 101 | ? Layer.merge(Layer.succeed(FileSystem.FileSystem, state.fileSystem), Path.layer) |
| 102 | : NodeServices.layer, |
| 103 | ), |
| 104 | Layer.provide(layerBackgroundPolicy(() => state.backgroundWorkEnabled !== false)), |
| 105 | Layer.provide( |
| 106 | Layer.succeed( |
| 107 | VcsAutomaticFetchInterval.VcsAutomaticFetchInterval, |
| 108 | state.automaticFetchInterval ?? Effect.succeed(Duration.seconds(30)), |
| 109 | ), |
| 110 | ), |
| 111 | Layer.provide( |
| 112 | Layer.mock(GitWorkflowService.GitWorkflowService)({ |
| 113 | localStatus: (_input, options) => |
| 114 | Effect.suspend(() => { |
| 115 | state.localStatusCalls += 1; |
| 116 | const includeBranchChanges = options?.includeBranchChanges !== false; |
| 117 | state.localStatusBranchChanges?.push(includeBranchChanges); |
| 118 | const { branchChanges: _branchChanges, ...withoutBranchChanges } = |
| 119 | state.currentLocalStatus; |
| 120 | if (state.failLocalStatus) { |
| 121 | return (state.onLocalStatusFailure ?? Effect.void).pipe( |
| 122 | Effect.andThen( |
| 123 | Effect.fail( |
| 124 | new GitManagerError({ |
| 125 | operation: "VcsStatusBroadcaster.test", |
| 126 | cwd: "/repo", |
| 127 | detail: "index.lock is held", |
| 128 | }), |
| 129 | ), |
| 130 | ), |
| 131 | ); |
| 132 | } |
| 133 | return (state.beforeLocalStatus ?? Effect.void).pipe( |
| 134 | Effect.as(includeBranchChanges ? state.currentLocalStatus : withoutBranchChanges), |
| 135 | ); |
| 136 | }), |
| 137 | repositoryRoot: () => Effect.sync(() => (state.currentLocalStatus.isRepo ? "/repo" : null)), |
| 138 | currentBranch: () => |
| 139 | state.branchRead |
| 140 | ? state.branchRead() |
| 141 | : (state.beforeCurrentBranch ?? Effect.void).pipe( |
| 142 | Effect.andThen(Effect.sync(() => state.currentLocalStatus.refName)), |
| 143 | ), |
| 144 | readRefSnapshot: () => |
| 145 | Effect.sync(() => (state.refSnapshot === undefined ? "refs" : state.refSnapshot)), |
| 146 | upstreamDivergence: () => |
| 147 | Effect.sync(() => |
| 148 | state.currentRemoteStatus?.hasUpstream && state.currentLocalStatus.refName !== null |
| 149 | ? { |
| 150 | refName: state.currentLocalStatus.refName, |
| 151 | aheadCount: (state.currentDivergence ?? state.currentRemoteStatus).aheadCount, |
| 152 | behindCount: (state.currentDivergence ?? state.currentRemoteStatus).behindCount, |
| 153 | } |
| 154 | : null, |
| 155 | ), |
| 156 | remoteStatus: (_input, options) => |
| 157 | Effect.suspend(() => { |
| 158 | return (state.beforeRemoteStatus ?? Effect.void).pipe( |
| 159 | Effect.andThen( |
| 160 | Effect.sync(() => { |
| 161 | state.remoteStatusCalls += 1; |
| 162 | state.remoteStatusRefreshUpstreamValues?.push(options?.refreshUpstream); |
| 163 | return state.currentRemoteStatus; |
| 164 | }), |
| 165 | ), |
| 166 | Effect.tap(() => |
| 167 | state.remoteStatusError ? Effect.fail(state.remoteStatusError) : Effect.void, |
| 168 | ), |
| 169 | ); |
| 170 | }), |
| 171 | invalidateLocalStatus: () => |
| 172 | Effect.sync(() => { |
| 173 | state.localInvalidationCalls += 1; |
| 174 | }).pipe(Effect.andThen(state.beforeInvalidate ?? Effect.void)), |
| 175 | invalidateRemoteStatus: () => |
| 176 | Effect.sync(() => { |
| 177 | state.remoteInvalidationCalls += 1; |
| 178 | }), |
| 179 | invalidateStatus: () => |
| 180 | Effect.sync(() => { |
| 181 | state.localInvalidationCalls += 1; |
| 182 | state.remoteInvalidationCalls += 1; |
| 183 | }), |
| 184 | ...(state.pullCurrentBranch ? { pullCurrentBranch: state.pullCurrentBranch } : {}), |
| 185 | headPath: () => |
| 186 | Effect.sync(() => { |
| 187 | state.headPathCalls = (state.headPathCalls ?? 0) + 1; |
| 188 | return state.headPath ?? null; |
| 189 | }), |
| 190 | }), |
| 191 | ), |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | function layerBackgroundPolicy(shouldRunScopeWork: (scope: BackgroundScope) => boolean) { |
| 196 | return Layer.mock(BackgroundPolicy.BackgroundPolicy)({ |
| 197 | reportClientActivity: () => Effect.void, |
| 198 | removeRpcClient: () => Effect.void, |
| 199 | snapshot: Effect.succeed({ |
| 200 | hostPower: { |
| 201 | source: "unknown", |
| 202 | idle: "unknown", |
| 203 | idleSeconds: null, |
| 204 | locked: "unknown", |
| 205 | suspended: false, |
| 206 | onBattery: "unknown", |
| 207 | lowPowerMode: "unknown", |
| 208 | thermalState: "unknown", |
| 209 | stale: true, |
| 210 | updatedAt: TEST_EPOCH, |
| 211 | }, |
| 212 | leases: [], |
| 213 | activeForegroundLeaseCount: 0, |
| 214 | activeScopeKeys: [], |
| 215 | shouldRunOpportunisticWork: false, |
| 216 | updatedAt: TEST_EPOCH, |
| 217 | }), |
| 218 | shouldRunScopeWork: (scope) => Effect.sync(() => shouldRunScopeWork(scope)), |
| 219 | shouldRunOpportunisticWork: Effect.succeed(true), |
| 220 | }); |
| 221 | } |
| 222 | |
| 223 | describe("VcsStatusBroadcaster", () => { |
| 224 | it.effect.each(["passive subscriptions", "status reads", "local refreshes", "full refreshes"])( |
| 225 | "bounds historical snapshots from %s while retaining recently read entries", |
| 226 | (source) => { |
| 227 | const state = { |
| 228 | currentLocalStatus: baseLocalStatus, |
| 229 | currentRemoteStatus: baseRemoteStatus, |
| 230 | localStatusCalls: 0, |
| 231 | remoteStatusCalls: 0, |
| 232 | localInvalidationCalls: 0, |
| 233 | remoteInvalidationCalls: 0, |
| 234 | }; |
| 235 | |
| 236 | return Effect.gen(function* () { |
| 237 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 238 | yield* broadcaster.getStatus({ cwd: "/hot-repo" }); |
| 239 | for (let index = 0; index < 1_000; index += 1) { |
| 240 | const cwd = `/visited-repo-${index}`; |
| 241 | if (source === "passive subscriptions") { |
| 242 | yield* Stream.runHead(broadcaster.streamStatus({ cwd, includeRemote: false })); |
| 243 | } else if (source === "status reads") { |
| 244 | yield* broadcaster.getStatus({ cwd }); |
| 245 | } else if (source === "local refreshes") { |
| 246 | yield* broadcaster.refreshLocalStatus(cwd); |
| 247 | } else { |
| 248 | yield* broadcaster.refreshStatus(cwd); |
| 249 | } |
| 250 | const localCalls = state.localStatusCalls; |
| 251 | const remoteCalls = state.remoteStatusCalls; |
| 252 | yield* broadcaster.getStatus({ cwd: "/hot-repo" }); |
| 253 | assert.equal(state.localStatusCalls, localCalls); |
| 254 | assert.equal(state.remoteStatusCalls, remoteCalls); |
| 255 | } |
| 256 | |
| 257 | const localCalls = state.localStatusCalls; |
| 258 | yield* Stream.runHead( |
| 259 | broadcaster.streamStatus({ cwd: "/visited-repo-999", includeRemote: false }), |
| 260 | ); |
| 261 | assert.equal(state.localStatusCalls, localCalls); |
| 262 | yield* Stream.runHead( |
| 263 | broadcaster.streamStatus({ cwd: "/visited-repo-0", includeRemote: false }), |
| 264 | ); |
| 265 | assert.equal(state.localStatusCalls, localCalls + 1); |
| 266 | }).pipe(Effect.provide(layerTestFor(state))); |
| 267 | }, |
| 268 | ); |
| 269 | |
| 270 | it.effect("keeps a watched cwd's snapshot until its last subscriber leaves", () => { |
| 271 | const state = { |
| 272 | currentLocalStatus: baseLocalStatus, |
| 273 | currentRemoteStatus: remoteStatusWithPr, |
| 274 | localStatusCalls: 0, |
| 275 | remoteStatusCalls: 0, |
| 276 | localInvalidationCalls: 0, |
| 277 | remoteInvalidationCalls: 0, |
| 278 | backgroundWorkEnabled: false, |
| 279 | automaticFetchInterval: Effect.succeed(Duration.zero), |
| 280 | }; |
| 281 | |
| 282 | return Effect.gen(function* () { |
| 283 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 284 | const subscription = yield* Scope.make(); |
| 285 | const remoteLoaded = yield* Deferred.make<void>(); |
| 286 | yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/active-repo" }), (event) => |
| 287 | event._tag === "remoteUpdated" ? Deferred.succeed(remoteLoaded, undefined) : Effect.void, |
| 288 | ).pipe(Effect.forkIn(subscription)); |
| 289 | yield* Deferred.await(remoteLoaded); |
| 290 | for (let index = 0; index < 1_000; index += 1) { |
| 291 | yield* broadcaster.getStatus({ cwd: `/visited-repo-${index}` }); |
| 292 | } |
| 293 | |
| 294 | // With fetching disabled, nothing would reload a remote dropped from the cache. |
| 295 | const localCalls = state.localStatusCalls; |
| 296 | const remoteCalls = state.remoteStatusCalls; |
| 297 | const snapshot = yield* Stream.runHead(broadcaster.streamStatus({ cwd: "/active-repo" })); |
| 298 | assert.deepStrictEqual(Option.getOrThrow(snapshot), { |
| 299 | _tag: "snapshot", |
| 300 | local: baseLocalStatus, |
| 301 | remote: remoteStatusWithPr, |
| 302 | }); |
| 303 | assert.equal(state.localStatusCalls, localCalls); |
| 304 | assert.equal(state.remoteStatusCalls, remoteCalls); |
| 305 | // Turn completion skips a cwd it has no snapshot for. |
| 306 | const turnEndRemote = { ...remoteStatusWithPr, aheadCount: 1 }; |
| 307 | state.currentRemoteStatus = turnEndRemote; |
| 308 | assert.deepStrictEqual( |
| 309 | yield* broadcaster.refreshPullRequestStatus("/active-repo"), |
| 310 | turnEndRemote, |
| 311 | ); |
| 312 | |
| 313 | yield* Scope.close(subscription, Exit.void); |
| 314 | for (let index = 1_000; index < 2_000; index += 1) { |
| 315 | yield* broadcaster.getStatus({ cwd: `/visited-repo-${index}` }); |
| 316 | } |
| 317 | const releasedCalls = state.localStatusCalls; |
| 318 | yield* broadcaster.getStatus({ cwd: "/active-repo" }); |
| 319 | assert.equal(state.localStatusCalls, releasedCalls + 1); |
| 320 | }).pipe(Effect.provide(layerTestFor(state))); |
| 321 | }); |
| 322 | |
| 323 | it.effect("keeps an unwatched refresh coherent while other directories fill the cache", () => { |
| 324 | const state = { |
| 325 | currentLocalStatus: baseLocalStatus, |
| 326 | currentRemoteStatus: { ...remoteStatusWithPr, aheadCount: 2 }, |
| 327 | localStatusCalls: 0, |
| 328 | remoteStatusCalls: 0, |
| 329 | localInvalidationCalls: 0, |
| 330 | remoteInvalidationCalls: 0, |
| 331 | }; |
| 332 | const policyStarted = Deferred.makeUnsafe<void>(); |
| 333 | const releasePolicy = Deferred.makeUnsafe<void>(); |
| 334 | const layer = layerTestFor(state).pipe( |
| 335 | Layer.provide( |
| 336 | Layer.succeed(VcsStatusBroadcaster.VcsAutoPullPolicy, { |
| 337 | isEnabled: () => |
| 338 | Deferred.succeed(policyStarted, undefined).pipe( |
| 339 | Effect.andThen(Deferred.await(releasePolicy)), |
| 340 | Effect.as(false), |
| 341 | ), |
| 342 | }), |
| 343 | ), |
| 344 | ); |
| 345 | return Effect.gen(function* () { |
| 346 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 347 | const refresh = yield* broadcaster.refreshStatus("/refreshing-repo").pipe(Effect.forkScoped); |
| 348 | yield* Deferred.await(policyStarted); |
| 349 | for (let index = 0; index < 256; index += 1) { |
| 350 | yield* broadcaster.refreshLocalStatus(`/other-repo-${index}`); |
| 351 | } |
| 352 | yield* Deferred.succeed(releasePolicy, undefined); |
| 353 | const result = yield* Fiber.join(refresh); |
| 354 | assert.deepStrictEqual(result, { ...baseLocalStatus, ...state.currentRemoteStatus }); |
| 355 | }).pipe(Effect.provide(layer), Effect.scoped); |
| 356 | }); |
| 357 | |
| 358 | it.effect("trims excess snapshots when the last watched directories are released", () => { |
| 359 | const state = { |
| 360 | currentLocalStatus: baseLocalStatus, |
| 361 | currentRemoteStatus: baseRemoteStatus, |
| 362 | localStatusCalls: 0, |
| 363 | remoteStatusCalls: 0, |
| 364 | localInvalidationCalls: 0, |
| 365 | remoteInvalidationCalls: 0, |
| 366 | backgroundWorkEnabled: false, |
| 367 | }; |
| 368 | return Effect.gen(function* () { |
| 369 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 370 | const scopes = []; |
| 371 | for (let index = 0; index < 257; index += 1) { |
| 372 | const scope = yield* Effect.acquireRelease(Scope.make(), (scope) => |
| 373 | Scope.close(scope, Exit.void), |
| 374 | ); |
| 375 | scopes.push(scope); |
| 376 | const ready = yield* Deferred.make<void>(); |
| 377 | yield* Stream.runForEach( |
| 378 | broadcaster.streamStatus({ cwd: `/watched-repo-${index}`, includeRemote: false }), |
| 379 | () => Deferred.succeed(ready, undefined), |
| 380 | ).pipe(Effect.forkIn(scope)); |
| 381 | yield* Deferred.await(ready); |
| 382 | } |
| 383 | for (const scope of scopes) yield* Scope.close(scope, Exit.void); |
| 384 | const reads = state.localStatusCalls; |
| 385 | // A cache read protects its own cwd before trimming; only teardown can evict this oldest cwd. |
| 386 | yield* Stream.runHead( |
| 387 | broadcaster.streamStatus({ cwd: "/watched-repo-0", includeRemote: false }), |
| 388 | ); |
| 389 | assert.equal(state.localStatusCalls, reads + 1); |
| 390 | }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); |
| 391 | }); |
| 392 | |
| 393 | it.effect("refreshes queued behind a running one share the next git status read", () => { |
| 394 | const state: Parameters<typeof layerTestFor>[0] = { |
| 395 | currentLocalStatus: baseLocalStatus, |
| 396 | currentRemoteStatus: baseRemoteStatus, |
| 397 | localStatusCalls: 0, |
| 398 | remoteStatusCalls: 0, |
| 399 | localInvalidationCalls: 0, |
| 400 | remoteInvalidationCalls: 0, |
| 401 | fileSystem: FileSystem.makeNoop({ realPath: (cwd) => Effect.succeed(cwd) }), |
| 402 | }; |
| 403 | |
| 404 | return Effect.gen(function* () { |
| 405 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 406 | const started = yield* Deferred.make<void>(); |
| 407 | const release = yield* Deferred.make<void>(); |
| 408 | state.beforeLocalStatus = Deferred.succeed(started, undefined).pipe( |
| 409 | Effect.andThen(Deferred.await(release)), |
| 410 | ); |
| 411 | const running = yield* broadcaster |
| 412 | .refreshLocalStatus("/repo") |
| 413 | .pipe(Effect.forkScoped({ startImmediately: true })); |
| 414 | yield* Deferred.await(started); |
| 415 | // Requested while the first read runs, so that read may predate their change. |
| 416 | state.currentLocalStatus = { ...baseLocalStatus, hasWorkingTreeChanges: true }; |
| 417 | const queued = yield* Effect.all( |
| 418 | [broadcaster.refreshLocalStatus("/repo"), broadcaster.refreshLocalStatus("/repo")], |
| 419 | { concurrency: "unbounded" }, |
| 420 | ).pipe(Effect.forkScoped({ startImmediately: true })); |
| 421 | yield* Deferred.succeed(release, undefined); |
| 422 | |
| 423 | assert.isFalse((yield* Fiber.join(running)).hasWorkingTreeChanges); |
| 424 | const [second, third] = yield* Fiber.join(queued); |
| 425 | assert.isTrue(second.hasWorkingTreeChanges); |
| 426 | assert.isTrue(third.hasWorkingTreeChanges); |
| 427 | assert.strictEqual(state.localStatusCalls, 2); |
| 428 | |
| 429 | yield* broadcaster.refreshLocalStatus("/repo"); |
| 430 | assert.strictEqual(state.localStatusCalls, 3); |
| 431 | }).pipe(Effect.provide(layerTestFor(state))); |
| 432 | }); |
| 433 | |
| 434 | it.effect( |
| 435 | "streams fresh branch-only reads, detached HEAD, non-repositories, and detailed demand without mixing results", |
| 436 | () => { |
| 437 | const state: Parameters<typeof layerTestFor>[0] = { |
| 438 | currentLocalStatus: baseLocalStatus, |
| 439 | currentRemoteStatus: null, |
| 440 | localStatusCalls: 0, |
| 441 | remoteStatusCalls: 0, |
| 442 | localInvalidationCalls: 0, |
| 443 | remoteInvalidationCalls: 0, |
| 444 | fileSystem: FileSystem.makeNoop({ |
| 445 | realPath: (cwd) => Effect.succeed(cwd === "/alias" ? "/repo" : cwd), |
| 446 | }), |
| 447 | }; |
| 448 | return Effect.gen(function* () { |
| 449 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 450 | const branches = yield* Stream.toPull(broadcaster.streamBranchStatus({ cwd: "/repo" })); |
| 451 | assert.deepEqual(yield* branches, [{ refName: baseLocalStatus.refName, isRepo: true }]); |
| 452 | assert.equal(state.localStatusCalls, 0); |
| 453 | state.currentLocalStatus = { ...baseLocalStatus, refName: "external" }; |
| 454 | yield* broadcaster.refreshBranchStatus({ cwd: "/repo" }); |
| 455 | assert.deepEqual(yield* branches, [{ refName: "external", isRepo: true }]); |
| 456 | state.currentLocalStatus = { ...baseLocalStatus, refName: null }; |
| 457 | yield* broadcaster.refreshBranchStatus({ cwd: "/repo" }); |
| 458 | assert.deepEqual(yield* branches, [{ refName: null, isRepo: true }]); |
| 459 | state.currentLocalStatus = { ...baseLocalStatus, isRepo: false, refName: null }; |
| 460 | yield* broadcaster.refreshBranchStatus({ cwd: "/repo" }); |
| 461 | assert.deepEqual(yield* branches, [{ refName: null, isRepo: false }]); |
| 462 | assert.equal(state.localStatusCalls, 0); |
| 463 | const detailScope = yield* Scope.make(); |
| 464 | const details = yield* Stream.toPull( |
| 465 | broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }), |
| 466 | ).pipe(Scope.provide(detailScope)); |
| 467 | assert.deepEqual(yield* details, [ |
| 468 | { _tag: "snapshot", local: state.currentLocalStatus, remote: null }, |
| 469 | ]); |
| 470 | const callsWithDetail = state.localStatusCalls; |
| 471 | const extraBranches = yield* Stream.toPull( |
| 472 | broadcaster.streamBranchStatus({ cwd: "/repo" }), |
| 473 | ); |
| 474 | yield* extraBranches; |
| 475 | yield* broadcaster.refreshBranchStatus({ cwd: "/repo" }); |
| 476 | assert.equal(state.localStatusCalls, callsWithDetail); |
| 477 | state.currentLocalStatus = { |
| 478 | ...baseLocalStatus, |
| 479 | refName: "changed", |
| 480 | workingTree: { files: [], insertions: 12, deletions: 3 }, |
| 481 | }; |
| 482 | yield* broadcaster.refreshBranchStatus({ cwd: "/repo" }); |
| 483 | assert.deepEqual(yield* details, [ |
| 484 | { _tag: "localUpdated", local: state.currentLocalStatus }, |
| 485 | ]); |
| 486 | assert.deepEqual(yield* branches, [{ refName: "changed", isRepo: true }]); |
| 487 | const secondScope = yield* Scope.make(); |
| 488 | const secondDetails = yield* Stream.toPull( |
| 489 | broadcaster.streamStatus({ cwd: "/alias", includeRemote: false }), |
| 490 | ).pipe(Scope.provide(secondScope)); |
| 491 | yield* secondDetails; |
| 492 | yield* Scope.close(detailScope, Exit.void); |
| 493 | state.currentLocalStatus = { |
| 494 | ...state.currentLocalStatus, |
| 495 | workingTree: { files: [], insertions: 20, deletions: 4 }, |
| 496 | }; |
| 497 | yield* broadcaster.refreshLocalStatus("/repo"); |
| 498 | assert.deepEqual(yield* secondDetails, [ |
| 499 | { _tag: "localUpdated", local: state.currentLocalStatus }, |
| 500 | ]); |
| 501 | yield* Scope.close(secondScope, Exit.void); |
| 502 | const callsAfterRelease = state.localStatusCalls; |
| 503 | state.currentLocalStatus = { |
| 504 | ...state.currentLocalStatus, |
| 505 | workingTree: { files: [], insertions: 30, deletions: 5 }, |
| 506 | }; |
| 507 | yield* broadcaster.refreshBranchStatus({ cwd: "/repo" }); |
| 508 | assert.equal(state.localStatusCalls, callsAfterRelease); |
| 509 | const reopened = yield* Stream.toPull( |
| 510 | broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }), |
| 511 | ); |
| 512 | assert.deepEqual(yield* reopened, [ |
| 513 | { _tag: "snapshot", local: state.currentLocalStatus, remote: null }, |
| 514 | ]); |
| 515 | state.currentLocalStatus = { |
| 516 | ...baseLocalStatus, |
| 517 | isRepo: false, |
| 518 | refName: null, |
| 519 | }; |
| 520 | yield* broadcaster.refreshBranchStatus({ cwd: "/repo" }); |
| 521 | assert.deepEqual(yield* reopened, [ |
| 522 | { _tag: "localUpdated", local: state.currentLocalStatus }, |
| 523 | ]); |
| 524 | assert.deepEqual(yield* branches, [{ refName: null, isRepo: false }]); |
| 525 | assert.equal(state.remoteStatusCalls, 0); |
| 526 | }).pipe(Effect.provide(layerTestFor(state))); |
| 527 | }, |
| 528 | ); |
| 529 | |
| 530 | it.effect("publishes the new branch before pending detailed status completes", () => { |
| 531 | const state = newState(); |
| 532 | return Effect.gen(function* () { |
| 533 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 534 | const branches = yield* Stream.toPull(broadcaster.streamBranchStatus({ cwd: "/repo" })); |
| 535 | yield* branches; |
| 536 | const reading = yield* Deferred.make<void>(); |
| 537 | const resume = yield* Deferred.make<void>(); |
| 538 | state.beforeLocalStatus = Deferred.succeed(reading, undefined).pipe( |
| 539 | Effect.andThen(Deferred.await(resume)), |
| 540 | ); |
| 541 | const details = yield* Stream.runHead( |
| 542 | broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }), |
| 543 | ).pipe(Effect.forkScoped); |
| 544 | yield* Deferred.await(reading); |
| 545 | state.currentLocalStatus = { ...baseLocalStatus, refName: "feature/next" }; |
| 546 | const update = yield* branches.pipe(Effect.forkScoped({ startImmediately: true })); |
| 547 | yield* broadcaster.refreshBranchStatus({ cwd: "/repo" }); |
| 548 | assert.deepEqual( |
| 549 | update.pollUnsafe(), |
| 550 | Exit.succeed([{ refName: "feature/next", isRepo: true }]), |
| 551 | ); |
| 552 | state.beforeLocalStatus = Effect.void; |
| 553 | yield* Deferred.succeed(resume, undefined); |
| 554 | yield* Fiber.join(details); |
| 555 | yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 556 | state.currentLocalStatus = { ...baseLocalStatus, refName: "feature/latest" }; |
| 557 | yield* broadcaster.refreshBranchStatus({ cwd: "/repo" }); |
| 558 | assert.deepEqual(yield* branches, [{ refName: "feature/latest", isRepo: true }]); |
| 559 | }).pipe(Effect.provide(layerTestFor(state))); |
| 560 | }); |
| 561 | |
| 562 | it.effect("invalidates totals for a refresh arriving after detailed demand closes", () => { |
| 563 | const state = newState(); |
| 564 | return Effect.gen(function* () { |
| 565 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 566 | const detailScope = yield* Scope.make(); |
| 567 | const details = yield* Stream.toPull( |
| 568 | broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }), |
| 569 | ).pipe(Scope.provide(detailScope)); |
| 570 | yield* details; |
| 571 | const reading = yield* Deferred.make<void>(); |
| 572 | const resume = yield* Deferred.make<void>(); |
| 573 | state.currentLocalStatus = { |
| 574 | ...baseLocalStatus, |
| 575 | refName: "feature/next", |
| 576 | workingTree: { files: [], insertions: 1, deletions: 0 }, |
| 577 | }; |
| 578 | state.beforeLocalStatus = Deferred.succeed(reading, undefined).pipe( |
| 579 | Effect.andThen(Deferred.await(resume)), |
| 580 | ); |
| 581 | yield* broadcaster.refreshBranchStatus({ cwd: "/repo" }); |
| 582 | yield* Deferred.await(reading); |
| 583 | yield* Scope.close(detailScope, Exit.void); |
| 584 | state.currentLocalStatus = { |
| 585 | ...state.currentLocalStatus, |
| 586 | workingTree: { files: [], insertions: 2, deletions: 0 }, |
| 587 | }; |
| 588 | const trailing = yield* broadcaster |
| 589 | .refreshBranchStatus({ cwd: "/repo" }) |
| 590 | .pipe(Effect.forkScoped({ startImmediately: true })); |
| 591 | state.beforeLocalStatus = Effect.void; |
| 592 | yield* Deferred.succeed(resume, undefined); |
| 593 | yield* Fiber.join(trailing); |
| 594 | const reopened = yield* Stream.runHead( |
| 595 | broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }), |
| 596 | ); |
| 597 | assert.deepEqual( |
| 598 | reopened, |
| 599 | Option.some({ _tag: "snapshot" as const, local: state.currentLocalStatus, remote: null }), |
| 600 | ); |
| 601 | }).pipe(Effect.provide(layerTestFor(state))); |
| 602 | }); |
| 603 | |
| 604 | it.effect("upgrades an in-flight branch refresh when detailed demand arrives", () => { |
| 605 | const state: Parameters<typeof layerTestFor>[0] = { |
| 606 | currentLocalStatus: baseLocalStatus, |
| 607 | currentRemoteStatus: null, |
| 608 | localStatusCalls: 0, |
| 609 | remoteStatusCalls: 0, |
| 610 | localInvalidationCalls: 0, |
| 611 | remoteInvalidationCalls: 0, |
| 612 | }; |
| 613 | return Effect.gen(function* () { |
| 614 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 615 | const reading = yield* Deferred.make<void>(); |
| 616 | const resume = yield* Deferred.make<void>(); |
| 617 | state.beforeCurrentBranch = Deferred.succeed(reading, undefined).pipe( |
| 618 | Effect.andThen(Deferred.await(resume)), |
| 619 | ); |
| 620 | const refresh = yield* broadcaster |
| 621 | .refreshBranchStatus({ cwd: "/repo" }) |
| 622 | .pipe(Effect.forkScoped); |
| 623 | yield* Deferred.await(reading); |
| 624 | const detailsFiber = yield* Stream.runHead( |
| 625 | broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }), |
| 626 | ).pipe(Effect.forkScoped); |
| 627 | state.currentLocalStatus = { |
| 628 | ...baseLocalStatus, |
| 629 | workingTree: { files: [], insertions: 8, deletions: 2 }, |
| 630 | }; |
| 631 | yield* Deferred.succeed(resume, undefined); |
| 632 | yield* Fiber.join(refresh); |
| 633 | assert.deepEqual( |
| 634 | yield* Fiber.join(detailsFiber), |
| 635 | Option.some({ _tag: "snapshot" as const, local: state.currentLocalStatus, remote: null }), |
| 636 | ); |
| 637 | }).pipe(Effect.provide(layerTestFor(state))); |
| 638 | }); |
| 639 | |
| 640 | it.effect("publishes terminal branch switches and validates cached status reads", () => { |
| 641 | const state: Parameters<typeof layerTestFor>[0] = { |
| 642 | currentLocalStatus: baseLocalStatus, |
| 643 | currentRemoteStatus: remoteStatusWithPr, |
| 644 | localStatusCalls: 0, |
| 645 | remoteStatusCalls: 0, |
| 646 | localInvalidationCalls: 0, |
| 647 | remoteInvalidationCalls: 0, |
| 648 | }; |
| 649 | |
| 650 | return Effect.gen(function* () { |
| 651 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 652 | yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 653 | const branchEvents = yield* broadcaster.streamBranchChanges.pipe( |
| 654 | Stream.take(2), |
| 655 | Stream.runCollect, |
| 656 | Effect.forkScoped({ startImmediately: true }), |
| 657 | ); |
| 658 | const subscribed = yield* Deferred.make<void>(); |
| 659 | const events = yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe( |
| 660 | Stream.tap((event) => |
| 661 | event._tag === "snapshot" ? Deferred.succeed(subscribed, undefined) : Effect.void, |
| 662 | ), |
| 663 | Stream.take(3), |
| 664 | Stream.runCollect, |
| 665 | Effect.forkScoped, |
| 666 | ); |
| 667 | yield* Deferred.await(subscribed); |
| 668 | |
| 669 | state.currentLocalStatus = { ...baseLocalStatus, refName: "main" }; |
| 670 | state.currentRemoteStatus = baseRemoteStatus; |
| 671 | const refreshed = yield* broadcaster.refreshStatus("/repo", { localOnly: true }); |
| 672 | assert.equal(refreshed.refName, "main"); |
| 673 | assert.isNull(refreshed.pr); |
| 674 | assert.equal(state.localInvalidationCalls, 1); |
| 675 | assert.equal(state.remoteStatusCalls, 1); |
| 676 | assert.equal(state.remoteInvalidationCalls, 1); |
| 677 | assert.deepStrictEqual((yield* Fiber.join(events)).slice(1), [ |
| 678 | { _tag: "remoteUpdated", remote: null }, |
| 679 | { _tag: "localUpdated", local: state.currentLocalStatus }, |
| 680 | ]); |
| 681 | assert.isNull((yield* broadcaster.getStatus({ cwd: "/repo" })).pr); |
| 682 | assert.equal(state.remoteStatusCalls, 2); |
| 683 | state.currentLocalStatus = { ...state.currentLocalStatus, hasWorkingTreeChanges: true }; |
| 684 | yield* broadcaster.refreshLocalStatus("/repo"); |
| 685 | state.currentLocalStatus = { ...baseLocalStatus, refName: "union" }; |
| 686 | state.currentRemoteStatus = null; |
| 687 | const queried = yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 688 | assert.equal(queried.refName, "union"); |
| 689 | assert.isNull(queried.pr); |
| 690 | const snapshot = yield* broadcaster |
| 691 | .streamStatus({ cwd: "/repo", includeRemote: false }) |
| 692 | .pipe(Stream.take(1), Stream.runCollect); |
| 693 | assert.deepStrictEqual(snapshot[0], { |
| 694 | _tag: "snapshot", |
| 695 | local: state.currentLocalStatus, |
| 696 | remote: null, |
| 697 | }); |
| 698 | assert.deepStrictEqual(yield* Fiber.join(branchEvents), [ |
| 699 | { cwd: "/repo", refName: "main" }, |
| 700 | { cwd: "/repo", refName: "union" }, |
| 701 | ]); |
| 702 | }).pipe(Effect.provide(layerTestFor(state))); |
| 703 | }); |
| 704 | |
| 705 | it.effect( |
| 706 | "preserves cached remote status when a local-only refresh stays on the same branch", |
| 707 | () => { |
| 708 | const state = { |
| 709 | currentLocalStatus: baseLocalStatus, |
| 710 | currentRemoteStatus: { ...remoteStatusWithPr, aheadCount: 2, behindCount: 3 }, |
| 711 | localStatusCalls: 0, |
| 712 | remoteStatusCalls: 0, |
| 713 | localInvalidationCalls: 0, |
| 714 | remoteInvalidationCalls: 0, |
| 715 | }; |
| 716 | |
| 717 | return Effect.gen(function* () { |
| 718 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 719 | const initial = yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 720 | const refreshed = yield* broadcaster.refreshStatus("/repo", { localOnly: true }); |
| 721 | assert.deepStrictEqual(refreshed, initial); |
| 722 | assert.deepStrictEqual(yield* broadcaster.getStatus({ cwd: "/repo" }), refreshed); |
| 723 | assert.equal(state.localInvalidationCalls, 1); |
| 724 | assert.equal(state.remoteInvalidationCalls, 1); |
| 725 | assert.equal(state.remoteStatusCalls, 1); |
| 726 | }).pipe(Effect.provide(layerTestFor(state))); |
| 727 | }, |
| 728 | ); |
| 729 | |
| 730 | it.effect("a local-only refresh that overlaps the first remote poll keeps its PR", () => { |
| 731 | const state: Parameters<typeof layerTestFor>[0] = { |
| 732 | currentLocalStatus: baseLocalStatus, |
| 733 | currentRemoteStatus: remoteStatusWithPr, |
| 734 | localStatusCalls: 0, |
| 735 | remoteStatusCalls: 0, |
| 736 | localInvalidationCalls: 0, |
| 737 | remoteInvalidationCalls: 0, |
| 738 | }; |
| 739 | |
| 740 | return Effect.gen(function* () { |
| 741 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 742 | const pollStarted = yield* Deferred.make<void>(); |
| 743 | const releasePoll = yield* Deferred.make<void>(); |
| 744 | state.beforeRemoteStatus = Deferred.succeed(pollStarted, undefined).pipe( |
| 745 | Effect.andThen(Deferred.await(releasePoll)), |
| 746 | ); |
| 747 | const localSeen = yield* Deferred.make<void>(); |
| 748 | yield* broadcaster.streamStatus({ cwd: "/repo" }).pipe( |
| 749 | Stream.runForEach((event) => |
| 750 | event._tag === "localUpdated" ? Deferred.succeed(localSeen, undefined) : Effect.void, |
| 751 | ), |
| 752 | Effect.forkScoped, |
| 753 | ); |
| 754 | yield* Deferred.await(pollStarted); |
| 755 | state.beforeRemoteStatus = Effect.void; |
| 756 | |
| 757 | state.currentLocalStatus = { ...baseLocalStatus, hasWorkingTreeChanges: true }; |
| 758 | const refresh = yield* broadcaster |
| 759 | .refreshStatus("/repo", { localOnly: true }) |
| 760 | .pipe(Effect.forkScoped); |
| 761 | yield* Deferred.await(localSeen); |
| 762 | yield* Deferred.succeed(releasePoll, undefined); |
| 763 | yield* Fiber.join(refresh); |
| 764 | |
| 765 | const status = yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 766 | assert.deepStrictEqual(status.pr, remoteStatusWithPr.pr); |
| 767 | assert.equal(state.remoteStatusCalls, 1); |
| 768 | }).pipe(Effect.provide(layerTestFor(state))); |
| 769 | }); |
| 770 | |
| 771 | it.effect( |
| 772 | "a local-only refresh before any remote read leaves the PR refresh able to read", |
| 773 | () => { |
| 774 | const state: Parameters<typeof layerTestFor>[0] = { |
| 775 | currentLocalStatus: baseLocalStatus, |
| 776 | currentRemoteStatus: remoteStatusWithPr, |
| 777 | localStatusCalls: 0, |
| 778 | remoteStatusCalls: 0, |
| 779 | localInvalidationCalls: 0, |
| 780 | remoteInvalidationCalls: 0, |
| 781 | }; |
| 782 | |
| 783 | return Effect.gen(function* () { |
| 784 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 785 | yield* broadcaster.refreshStatus("/repo", { localOnly: true }); |
| 786 | |
| 787 | const remote = yield* broadcaster.refreshPullRequestStatus("/repo"); |
| 788 | assert.deepStrictEqual(remote?.pr, remoteStatusWithPr.pr); |
| 789 | assert.equal(state.remoteStatusCalls, 1); |
| 790 | }).pipe(Effect.provide(layerTestFor(state))); |
| 791 | }, |
| 792 | ); |
| 793 | |
| 794 | it.effect.each([0, 2])( |
| 795 | "refreshes merge divergence while a lookup ends with %s commits behind", |
| 796 | (behindAfterLookup) => { |
| 797 | const state: Parameters<typeof layerTestFor>[0] = { |
| 798 | currentLocalStatus: baseLocalStatus, |
| 799 | currentRemoteStatus: { ...baseRemoteStatus, aheadCount: 2, behindCount: 3 }, |
| 800 | localStatusCalls: 0, |
| 801 | remoteStatusCalls: 0, |
| 802 | localInvalidationCalls: 0, |
| 803 | remoteInvalidationCalls: 0, |
| 804 | backgroundWorkEnabled: false, |
| 805 | }; |
| 806 | return Effect.gen(function* () { |
| 807 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 808 | yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 809 | const started = yield* Deferred.make<void>(); |
| 810 | const release = yield* Deferred.make<void>(); |
| 811 | state.beforeRemoteStatus = Deferred.succeed(started, undefined).pipe( |
| 812 | Effect.andThen(Deferred.await(release)), |
| 813 | ); |
| 814 | const lookup = yield* broadcaster.refreshPullRequestStatus("/repo").pipe(Effect.forkScoped); |
| 815 | yield* Deferred.await(started); |
| 816 | state.currentRemoteStatus = { ...baseRemoteStatus, aheadCount: 4, behindCount: 0 }; |
| 817 | yield* broadcaster.refreshLocalStatus("/repo"); |
| 818 | const status = yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 819 | assert.equal(status.aheadCount, 4); |
| 820 | assert.equal(status.behindCount, 0); |
| 821 | assert.equal(state.remoteStatusCalls, 1); |
| 822 | state.currentDivergence = { aheadCount: 4, behindCount: behindAfterLookup }; |
| 823 | state.currentRemoteStatus = |
| 824 | behindAfterLookup > 0 |
| 825 | ? { ...baseRemoteStatus, ...state.currentDivergence } |
| 826 | : { ...baseRemoteStatus, aheadCount: 2, behindCount: 3 }; |
| 827 | yield* Deferred.succeed(release, undefined); |
| 828 | yield* Fiber.join(lookup); |
| 829 | const afterLookup = yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 830 | assert.equal(afterLookup.aheadCount, 4); |
| 831 | assert.equal(afterLookup.behindCount, behindAfterLookup); |
| 832 | }).pipe(Effect.provide(layerTestFor(state))); |
| 833 | }, |
| 834 | ); |
| 835 | |
| 836 | it.effect("a local-only refresh after a branch switch leaves the PR refresh able to read", () => { |
| 837 | const state: Parameters<typeof layerTestFor>[0] = { |
| 838 | currentLocalStatus: baseLocalStatus, |
| 839 | currentRemoteStatus: remoteStatusWithPr, |
| 840 | localStatusCalls: 0, |
| 841 | remoteStatusCalls: 0, |
| 842 | localInvalidationCalls: 0, |
| 843 | remoteInvalidationCalls: 0, |
| 844 | }; |
| 845 | |
| 846 | return Effect.gen(function* () { |
| 847 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 848 | yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 849 | |
| 850 | state.currentLocalStatus = { ...baseLocalStatus, refName: "feature/next" }; |
| 851 | const nextBranchRemote: VcsStatusRemoteResult = { |
| 852 | ...baseRemoteStatus, |
| 853 | pr: { ...remoteStatusWithPr.pr!, number: 3001, headRef: "feature/next" }, |
| 854 | }; |
| 855 | state.currentRemoteStatus = nextBranchRemote; |
| 856 | yield* broadcaster.refreshStatus("/repo", { localOnly: true }); |
| 857 | |
| 858 | const remote = yield* broadcaster.refreshPullRequestStatus("/repo"); |
| 859 | assert.deepStrictEqual(remote?.pr, nextBranchRemote.pr); |
| 860 | assert.equal(state.remoteStatusCalls, 2); |
| 861 | }).pipe(Effect.provide(layerTestFor(state))); |
| 862 | }); |
| 863 | |
| 864 | it.effect("clears the previous branch's remote status after an in-flight PR refresh", () => { |
| 865 | const remoteReadStarted = Deferred.makeUnsafe<void>(); |
| 866 | const releaseRemoteRead = Deferred.makeUnsafe<void>(); |
| 867 | const state = { |
| 868 | currentLocalStatus: baseLocalStatus, |
| 869 | currentRemoteStatus: remoteStatusWithPr, |
| 870 | localStatusCalls: 0, |
| 871 | remoteStatusCalls: 0, |
| 872 | localInvalidationCalls: 0, |
| 873 | remoteInvalidationCalls: 0, |
| 874 | beforeRemoteStatus: Effect.void, |
| 875 | }; |
| 876 | |
| 877 | return Effect.gen(function* () { |
| 878 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 879 | yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 880 | state.beforeRemoteStatus = Deferred.succeed(remoteReadStarted, undefined).pipe( |
| 881 | Effect.andThen(Deferred.await(releaseRemoteRead)), |
| 882 | ); |
| 883 | const remoteRefresh = yield* broadcaster |
| 884 | .refreshPullRequestStatus("/repo") |
| 885 | .pipe(Effect.forkScoped); |
| 886 | yield* Deferred.await(remoteReadStarted); |
| 887 | state.currentLocalStatus = { ...baseLocalStatus, refName: "main" }; |
| 888 | const localRefresh = yield* broadcaster |
| 889 | .refreshStatus("/repo", { localOnly: true }) |
| 890 | .pipe(Effect.forkScoped); |
| 891 | yield* Deferred.succeed(releaseRemoteRead, undefined); |
| 892 | yield* Fiber.join(remoteRefresh); |
| 893 | yield* Fiber.join(localRefresh); |
| 894 | state.currentRemoteStatus = baseRemoteStatus; |
| 895 | |
| 896 | const status = yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 897 | assert.equal(status.refName, "main"); |
| 898 | assert.isNull(status.pr); |
| 899 | assert.equal(status.aheadCount, 0); |
| 900 | assert.equal(status.behindCount, 0); |
| 901 | assert.equal(state.remoteStatusCalls, 3); |
| 902 | }).pipe(Effect.provide(layerTestFor(state))); |
| 903 | }); |
| 904 | |
| 905 | it.effect.skipIf(!symlinksSupported)( |
| 906 | "automatically pulls an enabled clean default branch when status detects it is behind", |
| 907 | () => { |
| 908 | let remoteStatus: VcsStatusRemoteResult = { ...baseRemoteStatus, behindCount: 2 }; |
| 909 | let pullCalls = 0; |
| 910 | let configuredWorkspaceRoot = ""; |
| 911 | const localStatus: VcsStatusLocalResult = { |
| 912 | ...baseLocalStatus, |
| 913 | isDefaultRef: true, |
| 914 | refName: "main", |
| 915 | }; |
| 916 | const layerTest = VcsStatusBroadcaster.layer.pipe( |
| 917 | Layer.provideMerge(NodeServices.layer), |
| 918 | Layer.provide(layerBackgroundPolicy(() => true)), |
| 919 | Layer.provide( |
| 920 | Layer.succeed(VcsStatusBroadcaster.VcsAutoPullPolicy, { |
| 921 | isEnabled: (cwd) => Effect.succeed(cwd === configuredWorkspaceRoot), |
| 922 | }), |
| 923 | ), |
| 924 | Layer.provide( |
| 925 | Layer.mock(GitWorkflowService.GitWorkflowService)({ |
| 926 | localStatus: () => Effect.succeed(localStatus), |
| 927 | currentBranch: () => Effect.succeed(localStatus.refName), |
| 928 | readRefSnapshot: () => Effect.succeed("refs"), |
| 929 | remoteStatus: () => Effect.succeed(remoteStatus), |
| 930 | invalidateLocalStatus: () => Effect.void, |
| 931 | invalidateRemoteStatus: () => Effect.void, |
| 932 | invalidateStatus: () => Effect.void, |
| 933 | pullCurrentBranch: () => |
| 934 | Effect.sync(() => { |
| 935 | pullCalls += 1; |
| 936 | remoteStatus = { ...remoteStatus, behindCount: 0 }; |
| 937 | return { |
| 938 | status: "pulled" as const, |
| 939 | refName: "main", |
| 940 | upstreamRef: "origin/main", |
| 941 | }; |
| 942 | }), |
| 943 | }), |
| 944 | ), |
| 945 | ); |
| 946 | |
| 947 | return Effect.gen(function* () { |
| 948 | const fileSystem = yield* FileSystem.FileSystem; |
| 949 | const path = yield* Path.Path; |
| 950 | const realDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 951 | prefix: "t3-vcs-auto-pull-real-", |
| 952 | }); |
| 953 | const linkParent = yield* fileSystem.makeTempDirectoryScoped({ |
| 954 | prefix: "t3-vcs-auto-pull-link-", |
| 955 | }); |
| 956 | configuredWorkspaceRoot = path.join(linkParent, "repo-link"); |
| 957 | yield* fileSystem.symlink(realDir, configuredWorkspaceRoot); |
| 958 | |
| 959 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 960 | const status = yield* broadcaster.refreshStatus(configuredWorkspaceRoot); |
| 961 | |
| 962 | assert.equal(pullCalls, 1); |
| 963 | assert.equal(status.behindCount, 0); |
| 964 | }).pipe(Effect.provide(layerTest)); |
| 965 | }, |
| 966 | ); |
| 967 | |
| 968 | it.effect("refreshes a loaded cwd without reusing a previous branch's PR", () => { |
| 969 | const state: Parameters<typeof layerTestFor>[0] = { |
| 970 | currentLocalStatus: baseLocalStatus, |
| 971 | currentRemoteStatus: { ...baseRemoteStatus, aheadCount: 3, behindCount: 5 }, |
| 972 | localStatusCalls: 0, |
| 973 | remoteStatusCalls: 0, |
| 974 | localInvalidationCalls: 0, |
| 975 | remoteInvalidationCalls: 0, |
| 976 | remoteStatusRefreshUpstreamValues: [], |
| 977 | }; |
| 978 | |
| 979 | return Effect.gen(function* () { |
| 980 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 981 | |
| 982 | assert.isNull(yield* broadcaster.refreshPullRequestStatus("/repo")); |
| 983 | assert.equal(state.remoteStatusCalls, 0); |
| 984 | |
| 985 | yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 986 | assert.equal(state.remoteStatusCalls, 1); |
| 987 | |
| 988 | state.backgroundWorkEnabled = false; |
| 989 | state.currentRemoteStatus = { ...remoteStatusWithPr, aheadCount: 7 }; |
| 990 | yield* broadcaster.refreshBranchStatus({ cwd: "/repo" }); |
| 991 | const refreshed = yield* broadcaster.refreshPullRequestStatus("/repo"); |
| 992 | assert.deepStrictEqual(refreshed, state.currentRemoteStatus); |
| 993 | const merged = yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 994 | assert.equal(merged.aheadCount, 7); |
| 995 | assert.equal(merged.behindCount, 0); |
| 996 | assert.equal(state.remoteStatusRefreshUpstreamValues?.at(-1), false); |
| 997 | assert.equal(state.remoteStatusCalls, 2); |
| 998 | assert.equal(state.remoteInvalidationCalls, 2); |
| 999 | |
| 1000 | state.currentLocalStatus = { ...baseLocalStatus, refName: "feature/next" }; |
| 1001 | state.currentRemoteStatus = baseRemoteStatus; |
| 1002 | yield* broadcaster.refreshLocalStatus("/repo"); |
| 1003 | const refreshedBranch = yield* broadcaster.refreshPullRequestStatus("/repo"); |
| 1004 | assert.deepStrictEqual(refreshedBranch, baseRemoteStatus); |
| 1005 | assert.equal(state.remoteStatusCalls, 3); |
| 1006 | const refreshedStatus = yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 1007 | assert.equal(refreshedStatus.refName, "feature/next"); |
| 1008 | assert.isNull(refreshedStatus.pr); |
| 1009 | assert.equal(state.remoteStatusCalls, 3); |
| 1010 | assert.equal(state.remoteInvalidationCalls, 3); |
| 1011 | }).pipe(Effect.provide(layerTestFor(state))); |
| 1012 | }); |
| 1013 | |
| 1014 | it.effect("a poll that started before the turn-end refresh cannot overwrite its PR", () => { |
| 1015 | const releaseFirstPoll = Deferred.makeUnsafe<void>(); |
| 1016 | const firstPollStarted = Deferred.makeUnsafe<void>(); |
| 1017 | let remoteReads = 0; |
| 1018 | const layer = VcsStatusBroadcaster.layer.pipe( |
| 1019 | Layer.provideMerge(NodeServices.layer), |
| 1020 | Layer.provide(layerBackgroundPolicy(() => true)), |
| 1021 | Layer.provide( |
| 1022 | Layer.mock(GitWorkflowService.GitWorkflowService)({ |
| 1023 | localStatus: () => Effect.succeed(baseLocalStatus), |
| 1024 | currentBranch: () => Effect.succeed(baseLocalStatus.refName), |
| 1025 | readRefSnapshot: () => Effect.succeed("refs"), |
| 1026 | remoteStatus: () => |
| 1027 | Effect.gen(function* () { |
| 1028 | remoteReads += 1; |
| 1029 | if (remoteReads === 2) { |
| 1030 | yield* Deferred.succeed(firstPollStarted, undefined); |
| 1031 | yield* Deferred.await(releaseFirstPoll); |
| 1032 | return baseRemoteStatus; |
| 1033 | } |
| 1034 | return remoteReads === 1 ? baseRemoteStatus : remoteStatusWithPr; |
| 1035 | }), |
| 1036 | invalidateLocalStatus: () => Effect.void, |
| 1037 | invalidateRemoteStatus: () => Effect.void, |
| 1038 | invalidateStatus: () => Effect.void, |
| 1039 | }), |
| 1040 | ), |
| 1041 | ); |
| 1042 | |
| 1043 | return Effect.gen(function* () { |
| 1044 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1045 | yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 1046 | |
| 1047 | const poll = yield* broadcaster.refreshStatus("/repo").pipe(Effect.forkScoped); |
| 1048 | yield* Deferred.await(firstPollStarted); |
| 1049 | const refresh = yield* broadcaster.refreshPullRequestStatus("/repo").pipe(Effect.forkScoped); |
| 1050 | yield* Deferred.succeed(releaseFirstPoll, undefined); |
| 1051 | yield* Fiber.join(poll); |
| 1052 | const refreshed = yield* Fiber.join(refresh); |
| 1053 | |
| 1054 | assert.deepStrictEqual(refreshed, remoteStatusWithPr); |
| 1055 | const final = yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 1056 | assert.deepStrictEqual(final.pr, remoteStatusWithPr.pr); |
| 1057 | }).pipe(Effect.provide(layer)); |
| 1058 | }); |
| 1059 | |
| 1060 | it.effect("an initial status read cannot overwrite an explicit refresh", () => { |
| 1061 | const firstReadStarted = Deferred.makeUnsafe<void>(); |
| 1062 | const releaseFirstRead = Deferred.makeUnsafe<void>(); |
| 1063 | let remoteReads = 0; |
| 1064 | const layer = VcsStatusBroadcaster.layer.pipe( |
| 1065 | Layer.provide(FileSystem.layerNoop({ realPath: (path) => Effect.succeed(path) })), |
| 1066 | Layer.provideMerge(NodeServices.layer), |
| 1067 | Layer.provide(layerBackgroundPolicy(() => true)), |
| 1068 | Layer.provide( |
| 1069 | Layer.mock(GitWorkflowService.GitWorkflowService)({ |
| 1070 | localStatus: () => Effect.succeed(baseLocalStatus), |
| 1071 | currentBranch: () => Effect.succeed(baseLocalStatus.refName), |
| 1072 | readRefSnapshot: () => Effect.succeed("refs"), |
| 1073 | remoteStatus: () => |
| 1074 | Effect.gen(function* () { |
| 1075 | remoteReads += 1; |
| 1076 | if (remoteReads === 1) { |
| 1077 | yield* Deferred.succeed(firstReadStarted, undefined); |
| 1078 | yield* Deferred.await(releaseFirstRead); |
| 1079 | return baseRemoteStatus; |
| 1080 | } |
| 1081 | return remoteStatusWithPr; |
| 1082 | }), |
| 1083 | invalidateLocalStatus: () => Effect.void, |
| 1084 | invalidateRemoteStatus: () => Effect.void, |
| 1085 | invalidateStatus: () => Effect.void, |
| 1086 | }), |
| 1087 | ), |
| 1088 | ); |
| 1089 | return Effect.gen(function* () { |
| 1090 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1091 | const initial = yield* broadcaster.getStatus({ cwd: "/repo" }).pipe(Effect.forkScoped); |
| 1092 | yield* Deferred.await(firstReadStarted); |
| 1093 | const refresh = yield* broadcaster.refreshStatus("/repo").pipe(Effect.forkScoped); |
| 1094 | yield* TestClock.adjust(Duration.zero); |
| 1095 | yield* Deferred.succeed(releaseFirstRead, undefined); |
| 1096 | yield* Fiber.join(initial); |
| 1097 | yield* Fiber.join(refresh); |
| 1098 | assert.deepStrictEqual( |
| 1099 | (yield* broadcaster.getStatus({ cwd: "/repo" })).pr, |
| 1100 | remoteStatusWithPr.pr, |
| 1101 | ); |
| 1102 | }).pipe(Effect.provide(layer)); |
| 1103 | }); |
| 1104 | |
| 1105 | it.effect("keeps the published local snapshot when a refresh remote read fails", () => { |
| 1106 | const state = { |
| 1107 | currentLocalStatus: baseLocalStatus, |
| 1108 | currentRemoteStatus: baseRemoteStatus, |
| 1109 | localStatusCalls: 0, |
| 1110 | remoteStatusCalls: 0, |
| 1111 | localInvalidationCalls: 0, |
| 1112 | remoteInvalidationCalls: 0, |
| 1113 | failRemoteStatus: false, |
| 1114 | }; |
| 1115 | const layerTest = VcsStatusBroadcaster.layer.pipe( |
| 1116 | Layer.provideMerge(NodeServices.layer), |
| 1117 | Layer.provide(layerBackgroundPolicy(() => true)), |
| 1118 | Layer.provide( |
| 1119 | Layer.mock(GitWorkflowService.GitWorkflowService)({ |
| 1120 | localStatus: () => |
| 1121 | Effect.sync(() => { |
| 1122 | state.localStatusCalls += 1; |
| 1123 | return state.currentLocalStatus; |
| 1124 | }), |
| 1125 | currentBranch: () => Effect.succeed(state.currentLocalStatus.refName), |
| 1126 | readRefSnapshot: () => Effect.succeed("refs"), |
| 1127 | remoteStatus: () => |
| 1128 | Effect.suspend(() => { |
| 1129 | state.remoteStatusCalls += 1; |
| 1130 | return state.failRemoteStatus |
| 1131 | ? Effect.fail( |
| 1132 | new GitManagerError({ |
| 1133 | operation: "VcsStatusBroadcaster.test", |
| 1134 | cwd: "/repo", |
| 1135 | detail: "remote status failed", |
| 1136 | }), |
| 1137 | ) |
| 1138 | : Effect.succeed(state.currentRemoteStatus); |
| 1139 | }), |
| 1140 | invalidateLocalStatus: () => |
| 1141 | Effect.sync(() => { |
| 1142 | state.localInvalidationCalls += 1; |
| 1143 | }), |
| 1144 | invalidateRemoteStatus: () => |
| 1145 | Effect.sync(() => { |
| 1146 | state.remoteInvalidationCalls += 1; |
| 1147 | }), |
| 1148 | invalidateStatus: () => |
| 1149 | Effect.sync(() => { |
| 1150 | state.localInvalidationCalls += 1; |
| 1151 | state.remoteInvalidationCalls += 1; |
| 1152 | }), |
| 1153 | }), |
| 1154 | ), |
| 1155 | ); |
| 1156 | |
| 1157 | return Effect.gen(function* () { |
| 1158 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1159 | yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 1160 | |
| 1161 | state.currentLocalStatus = { |
| 1162 | ...baseLocalStatus, |
| 1163 | refName: "feature/partial-refresh", |
| 1164 | }; |
| 1165 | state.currentRemoteStatus = { |
| 1166 | ...baseRemoteStatus, |
| 1167 | aheadCount: 3, |
| 1168 | }; |
| 1169 | state.failRemoteStatus = true; |
| 1170 | |
| 1171 | const refreshExit = yield* broadcaster.refreshStatus("/repo").pipe(Effect.exit); |
| 1172 | const snapshot = yield* Stream.runHead( |
| 1173 | broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }), |
| 1174 | ); |
| 1175 | |
| 1176 | assert.isTrue(Exit.isFailure(refreshExit)); |
| 1177 | assert.isTrue(Option.isSome(snapshot)); |
| 1178 | const event = Option.getOrThrow(snapshot); |
| 1179 | assert.equal(event._tag, "snapshot"); |
| 1180 | if (event._tag === "snapshot") { |
| 1181 | assert.equal(event.local.refName, "feature/partial-refresh"); |
| 1182 | assert.isNull(event.remote); |
| 1183 | } |
| 1184 | }).pipe(Effect.provide(layerTest)); |
| 1185 | }); |
| 1186 | |
| 1187 | it.effect.skipIf(!symlinksSupported)( |
| 1188 | "normalizes symlinked CWDs before cache lookup and workflow calls", |
| 1189 | () => { |
| 1190 | const seenCwds: string[] = []; |
| 1191 | const state = { |
| 1192 | currentLocalStatus: baseLocalStatus, |
| 1193 | currentRemoteStatus: baseRemoteStatus, |
| 1194 | localStatusCalls: 0, |
| 1195 | remoteStatusCalls: 0, |
| 1196 | localInvalidationCalls: 0, |
| 1197 | remoteInvalidationCalls: 0, |
| 1198 | }; |
| 1199 | const layerTest = VcsStatusBroadcaster.layer.pipe( |
| 1200 | Layer.provideMerge(NodeServices.layer), |
| 1201 | Layer.provide(layerBackgroundPolicy(() => true)), |
| 1202 | Layer.provide( |
| 1203 | Layer.mock(GitWorkflowService.GitWorkflowService)({ |
| 1204 | localStatus: (input) => |
| 1205 | Effect.sync(() => { |
| 1206 | seenCwds.push(input.cwd); |
| 1207 | state.localStatusCalls += 1; |
| 1208 | return state.currentLocalStatus; |
| 1209 | }), |
| 1210 | currentBranch: () => Effect.succeed(state.currentLocalStatus.refName), |
| 1211 | remoteStatus: (input) => |
| 1212 | Effect.sync(() => { |
| 1213 | seenCwds.push(input.cwd); |
| 1214 | state.remoteStatusCalls += 1; |
| 1215 | return state.currentRemoteStatus; |
| 1216 | }), |
| 1217 | invalidateLocalStatus: () => |
| 1218 | Effect.sync(() => { |
| 1219 | state.localInvalidationCalls += 1; |
| 1220 | }), |
| 1221 | invalidateRemoteStatus: () => |
| 1222 | Effect.sync(() => { |
| 1223 | state.remoteInvalidationCalls += 1; |
| 1224 | }), |
| 1225 | } satisfies Partial<GitWorkflowService.GitWorkflowService["Service"]>), |
| 1226 | ), |
| 1227 | ); |
| 1228 | |
| 1229 | return Effect.gen(function* () { |
| 1230 | const fileSystem = yield* FileSystem.FileSystem; |
| 1231 | const path = yield* Path.Path; |
| 1232 | const realDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1233 | prefix: "t3-vcs-status-real-", |
| 1234 | }); |
| 1235 | const linkParent = yield* fileSystem.makeTempDirectoryScoped({ |
| 1236 | prefix: "t3-vcs-status-link-", |
| 1237 | }); |
| 1238 | const linkDir = path.join(linkParent, "repo-link"); |
| 1239 | yield* fileSystem.symlink(realDir, linkDir); |
| 1240 | const realPath = yield* fileSystem.realPath(realDir); |
| 1241 | |
| 1242 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1243 | yield* broadcaster.getStatus({ cwd: linkDir }); |
| 1244 | yield* broadcaster.getStatus({ cwd: realDir }); |
| 1245 | |
| 1246 | assert.deepStrictEqual(seenCwds, [realPath, realPath]); |
| 1247 | assert.equal(state.localStatusCalls, 1); |
| 1248 | assert.equal(state.remoteStatusCalls, 1); |
| 1249 | }).pipe(Effect.provide(layerTest)); |
| 1250 | }, |
| 1251 | ); |
| 1252 | |
| 1253 | it.effect("publishes a checkout's branch before returning, ahead of the remote read", () => { |
| 1254 | const state: Parameters<typeof layerTestFor>[0] = { |
| 1255 | currentLocalStatus: baseLocalStatus, |
| 1256 | currentRemoteStatus: baseRemoteStatus, |
| 1257 | localStatusCalls: 0, |
| 1258 | remoteStatusCalls: 0, |
| 1259 | localInvalidationCalls: 0, |
| 1260 | remoteInvalidationCalls: 0, |
| 1261 | }; |
| 1262 | |
| 1263 | return Effect.gen(function* () { |
| 1264 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1265 | yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 1266 | const subscribed = yield* Deferred.make<void>(); |
| 1267 | const localPublished = yield* Deferred.make<VcsStatusLocalResult>(); |
| 1268 | const remotePublished = yield* Deferred.make<VcsStatusRemoteResult | null>(); |
| 1269 | yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe( |
| 1270 | Stream.runForEach((event) => { |
| 1271 | if (event._tag === "snapshot") return Deferred.succeed(subscribed, undefined); |
| 1272 | if (event._tag === "localUpdated") return Deferred.succeed(localPublished, event.local); |
| 1273 | return event.remote === null |
| 1274 | ? Effect.void |
| 1275 | : Deferred.succeed(remotePublished, event.remote); |
| 1276 | }), |
| 1277 | Effect.forkScoped, |
| 1278 | ); |
| 1279 | yield* Deferred.await(subscribed); |
| 1280 | |
| 1281 | const releaseRemote = yield* Deferred.make<void>(); |
| 1282 | state.beforeRemoteStatus = Deferred.await(releaseRemote); |
| 1283 | state.currentLocalStatus = { ...baseLocalStatus, refName: "main" }; |
| 1284 | state.currentRemoteStatus = remoteStatusWithPr; |
| 1285 | yield* broadcaster.refreshAfterCheckout("/repo"); |
| 1286 | |
| 1287 | assert.equal((yield* Deferred.await(localPublished)).refName, "main"); |
| 1288 | assert.isFalse(yield* Deferred.isDone(remotePublished)); |
| 1289 | |
| 1290 | yield* Deferred.succeed(releaseRemote, undefined); |
| 1291 | assert.deepStrictEqual(yield* Deferred.await(remotePublished), remoteStatusWithPr); |
| 1292 | assert.equal((yield* broadcaster.getStatus({ cwd: "/repo" })).pr?.number, 2978); |
| 1293 | }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); |
| 1294 | }); |
| 1295 | |
| 1296 | it.effect("a remote poll refreshes Changes totals when divergence counts stay the same", () => { |
| 1297 | const state: Parameters<typeof layerTestFor>[0] = { |
| 1298 | currentLocalStatus: { |
| 1299 | ...baseLocalStatus, |
| 1300 | branchChanges: { baseRef: "origin/main", insertions: 745, deletions: 24 }, |
| 1301 | }, |
| 1302 | currentRemoteStatus: baseRemoteStatus, |
| 1303 | localStatusCalls: 0, |
| 1304 | remoteStatusCalls: 0, |
| 1305 | localInvalidationCalls: 0, |
| 1306 | remoteInvalidationCalls: 0, |
| 1307 | }; |
| 1308 | |
| 1309 | return Effect.gen(function* () { |
| 1310 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1311 | yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 1312 | const subscribed = yield* Deferred.make<void>(); |
| 1313 | const polled = yield* Deferred.make<void>(); |
| 1314 | yield* broadcaster.streamStatus({ cwd: "/repo" }).pipe( |
| 1315 | Stream.runForEach((event) => { |
| 1316 | if (event._tag === "snapshot") return Deferred.succeed(subscribed, undefined); |
| 1317 | if (event._tag === "remoteUpdated") return Deferred.succeed(polled, undefined); |
| 1318 | return Effect.void; |
| 1319 | }), |
| 1320 | Effect.forkScoped, |
| 1321 | ); |
| 1322 | yield* Deferred.await(subscribed); |
| 1323 | state.currentLocalStatus = { |
| 1324 | ...baseLocalStatus, |
| 1325 | branchChanges: { baseRef: "origin/main", insertions: 107, deletions: 4 }, |
| 1326 | }; |
| 1327 | state.currentRemoteStatus = remoteStatusWithPr; |
| 1328 | yield* TestClock.adjust("30 seconds"); |
| 1329 | yield* Deferred.await(polled); |
| 1330 | const status = yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 1331 | assert.deepStrictEqual(status.branchChanges, state.currentLocalStatus.branchChanges); |
| 1332 | }).pipe(Effect.provide(layerTestFor(state))); |
| 1333 | }); |
| 1334 | |
| 1335 | it.effect("reads Changes totals only while a subscriber shows them", () => { |
| 1336 | const branchChanges = { baseRef: "origin/main", insertions: 3, deletions: 1 }; |
| 1337 | const state: Parameters<typeof layerTestFor>[0] = { |
| 1338 | currentLocalStatus: { ...baseLocalStatus, branchChanges }, |
| 1339 | currentRemoteStatus: baseRemoteStatus, |
| 1340 | localStatusCalls: 0, |
| 1341 | remoteStatusCalls: 0, |
| 1342 | localInvalidationCalls: 0, |
| 1343 | remoteInvalidationCalls: 0, |
| 1344 | localStatusBranchChanges: [], |
| 1345 | }; |
| 1346 | |
| 1347 | return Effect.gen(function* () { |
| 1348 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1349 | const paletteEvents = yield* Queue.unbounded<VcsStatusStreamEvent>(); |
| 1350 | yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe( |
| 1351 | Stream.runForEach((event) => Queue.offer(paletteEvents, event)), |
| 1352 | Effect.forkScoped, |
| 1353 | ); |
| 1354 | const paletteSnapshot = yield* Queue.take(paletteEvents); |
| 1355 | assert.strictEqual(paletteSnapshot._tag, "snapshot"); |
| 1356 | if (paletteSnapshot._tag === "snapshot") { |
| 1357 | assert.isUndefined(paletteSnapshot.local.branchChanges); |
| 1358 | } |
| 1359 | yield* broadcaster.refreshLocalStatus("/repo"); |
| 1360 | assert.deepStrictEqual(state.localStatusBranchChanges, [false, false]); |
| 1361 | |
| 1362 | const headerScope = yield* Scope.make(); |
| 1363 | const headerEvents = yield* Queue.unbounded<VcsStatusStreamEvent>(); |
| 1364 | yield* broadcaster.streamStatus({ cwd: "/repo" }).pipe( |
| 1365 | Stream.runForEach((event) => Queue.offer(headerEvents, event)), |
| 1366 | Effect.forkIn(headerScope), |
| 1367 | ); |
| 1368 | const headerSnapshot = yield* Queue.take(headerEvents); |
| 1369 | assert.strictEqual(headerSnapshot._tag, "snapshot"); |
| 1370 | if (headerSnapshot._tag === "snapshot") { |
| 1371 | assert.deepStrictEqual(headerSnapshot.local.branchChanges, branchChanges); |
| 1372 | } |
| 1373 | const paletteUpdate = yield* Queue.take(paletteEvents); |
| 1374 | assert.strictEqual(paletteUpdate._tag, "localUpdated"); |
| 1375 | if (paletteUpdate._tag === "localUpdated") { |
| 1376 | assert.deepStrictEqual(paletteUpdate.local.branchChanges, branchChanges); |
| 1377 | } |
| 1378 | // The header's first remote poll also rereads local status. |
| 1379 | while ((yield* Queue.take(headerEvents))._tag !== "remoteUpdated") {} |
| 1380 | const turnEnd = yield* broadcaster.refreshLocalStatus("/repo"); |
| 1381 | assert.deepStrictEqual(turnEnd.branchChanges, branchChanges); |
| 1382 | |
| 1383 | yield* Scope.close(headerScope, Exit.void); |
| 1384 | yield* broadcaster.refreshLocalStatus("/repo"); |
| 1385 | assert.deepStrictEqual(state.localStatusBranchChanges, [ |
| 1386 | false, |
| 1387 | false, |
| 1388 | true, |
| 1389 | true, |
| 1390 | true, |
| 1391 | false, |
| 1392 | ]); |
| 1393 | }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); |
| 1394 | }); |
| 1395 | |
| 1396 | it.effect("a light refresh that a header joins mid-read publishes Changes totals", () => { |
| 1397 | const branchChanges = { baseRef: "origin/main", insertions: 3, deletions: 1 }; |
| 1398 | const state: Parameters<typeof layerTestFor>[0] = { |
| 1399 | currentLocalStatus: { ...baseLocalStatus, branchChanges }, |
| 1400 | currentRemoteStatus: baseRemoteStatus, |
| 1401 | localStatusCalls: 0, |
| 1402 | remoteStatusCalls: 0, |
| 1403 | localInvalidationCalls: 0, |
| 1404 | remoteInvalidationCalls: 0, |
| 1405 | localStatusBranchChanges: [], |
| 1406 | fileSystem: FileSystem.makeNoop({ realPath: (cwd) => Effect.succeed(cwd) }), |
| 1407 | }; |
| 1408 | |
| 1409 | return Effect.gen(function* () { |
| 1410 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1411 | const subscribeHeader = Effect.fn("subscribeHeader")(function* (scope: Scope.Scope) { |
| 1412 | const events = yield* Queue.unbounded<VcsStatusStreamEvent>(); |
| 1413 | yield* broadcaster.streamStatus({ cwd: "/repo" }).pipe( |
| 1414 | Stream.runForEach((event) => Queue.offer(events, event)), |
| 1415 | Effect.forkIn(scope, { startImmediately: true }), |
| 1416 | ); |
| 1417 | return events; |
| 1418 | }); |
| 1419 | const firstHeader = yield* Scope.make(); |
| 1420 | const firstEvents = yield* subscribeHeader(firstHeader); |
| 1421 | while ((yield* Queue.take(firstEvents))._tag !== "remoteUpdated") {} |
| 1422 | yield* Scope.close(firstHeader, Exit.void); |
| 1423 | |
| 1424 | const readStarted = yield* Deferred.make<void>(); |
| 1425 | const finishRead = yield* Deferred.make<void>(); |
| 1426 | state.beforeLocalStatus = Deferred.succeed(readStarted, undefined).pipe( |
| 1427 | Effect.andThen(Deferred.await(finishRead)), |
| 1428 | ); |
| 1429 | const turnEnd = yield* broadcaster.refreshLocalStatus("/repo").pipe(Effect.forkScoped); |
| 1430 | yield* Deferred.await(readStarted); |
| 1431 | // Starting immediately registers the header's demand before it waits behind the read. |
| 1432 | const secondEvents = yield* subscribeHeader(yield* Scope.make()); |
| 1433 | yield* Deferred.succeed(finishRead, undefined); |
| 1434 | const snapshot = yield* Queue.take(secondEvents); |
| 1435 | assert.strictEqual(snapshot._tag, "snapshot"); |
| 1436 | if (snapshot._tag === "snapshot") { |
| 1437 | assert.deepStrictEqual(snapshot.local.branchChanges, branchChanges); |
| 1438 | } |
| 1439 | |
| 1440 | assert.deepStrictEqual((yield* Fiber.join(turnEnd)).branchChanges, branchChanges); |
| 1441 | assert.deepStrictEqual(state.localStatusBranchChanges?.slice(-2), [false, true]); |
| 1442 | }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); |
| 1443 | }); |
| 1444 | |
| 1445 | it.effect("an explicit refresh reads local totals after the fetch", () => { |
| 1446 | return Effect.gen(function* () { |
| 1447 | const fetchStarted = yield* Deferred.make<void>(); |
| 1448 | const finishFetch = yield* Deferred.make<void>(); |
| 1449 | let local: VcsStatusLocalResult = { |
| 1450 | ...baseLocalStatus, |
| 1451 | branchChanges: { baseRef: "origin/main", insertions: 745, deletions: 24 }, |
| 1452 | }; |
| 1453 | let cachedLocal: VcsStatusLocalResult | undefined; |
| 1454 | let refSnapshot = "before"; |
| 1455 | const readLocalStatus = () => Effect.sync(() => (cachedLocal ??= local)); |
| 1456 | const layerTest = VcsStatusBroadcaster.layer.pipe( |
| 1457 | Layer.provideMerge(NodeServices.layer), |
| 1458 | Layer.provide(layerBackgroundPolicy(() => true)), |
| 1459 | Layer.provide( |
| 1460 | Layer.mock(GitWorkflowService.GitWorkflowService)({ |
| 1461 | localStatus: readLocalStatus, |
| 1462 | invalidateStatus: () => |
| 1463 | Effect.sync(() => { |
| 1464 | cachedLocal = undefined; |
| 1465 | }), |
| 1466 | invalidateLocalStatus: () => |
| 1467 | Effect.sync(() => { |
| 1468 | cachedLocal = undefined; |
| 1469 | }), |
| 1470 | currentBranch: () => Effect.succeed(baseLocalStatus.refName), |
| 1471 | invalidateRemoteStatus: () => Effect.void, |
| 1472 | readRefSnapshot: () => Effect.sync(() => refSnapshot), |
| 1473 | remoteStatus: () => |
| 1474 | Deferred.succeed(fetchStarted, undefined).pipe( |
| 1475 | Effect.andThen(Deferred.await(finishFetch)), |
| 1476 | Effect.as(baseRemoteStatus), |
| 1477 | ), |
| 1478 | }), |
| 1479 | ), |
| 1480 | ); |
| 1481 | yield* Effect.gen(function* () { |
| 1482 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1483 | const refresh = yield* broadcaster.refreshStatus("/repo").pipe(Effect.forkScoped); |
| 1484 | yield* Deferred.await(fetchStarted); |
| 1485 | // Another reader can refill the local cache while fetch is still running. |
| 1486 | yield* readLocalStatus(); |
| 1487 | local = { |
| 1488 | ...baseLocalStatus, |
| 1489 | branchChanges: { baseRef: "origin/main", insertions: 0, deletions: 0 }, |
| 1490 | }; |
| 1491 | refSnapshot = "after"; |
| 1492 | yield* Deferred.succeed(finishFetch, undefined); |
| 1493 | const status = yield* Fiber.join(refresh); |
| 1494 | assert.deepStrictEqual(status.branchChanges, local.branchChanges); |
| 1495 | }).pipe(Effect.provide(layerTest)); |
| 1496 | }); |
| 1497 | }); |
| 1498 | |
| 1499 | it.effect("an explicit refresh re-reads local totals only after refs moved", () => { |
| 1500 | const totals = (insertions: number): VcsStatusLocalResult => ({ |
| 1501 | ...baseLocalStatus, |
| 1502 | branchChanges: { baseRef: "origin/main", insertions, deletions: 0 }, |
| 1503 | }); |
| 1504 | const state: Parameters<typeof layerTestFor>[0] = { |
| 1505 | currentLocalStatus: totals(745), |
| 1506 | currentRemoteStatus: remoteStatusWithPr, |
| 1507 | localStatusCalls: 0, |
| 1508 | remoteStatusCalls: 0, |
| 1509 | localInvalidationCalls: 0, |
| 1510 | remoteInvalidationCalls: 0, |
| 1511 | }; |
| 1512 | return Effect.gen(function* () { |
| 1513 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1514 | const subscribed = yield* Deferred.make<void>(); |
| 1515 | const events = yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe( |
| 1516 | Stream.tap((event) => |
| 1517 | event._tag === "snapshot" ? Deferred.succeed(subscribed, undefined) : Effect.void, |
| 1518 | ), |
| 1519 | Stream.filter((event) => event._tag !== "snapshot"), |
| 1520 | Stream.take(4), |
| 1521 | Stream.runCollect, |
| 1522 | Effect.forkScoped, |
| 1523 | ); |
| 1524 | yield* Deferred.await(subscribed); |
| 1525 | |
| 1526 | state.localStatusCalls = 0; |
| 1527 | yield* broadcaster.refreshStatus("/repo"); |
| 1528 | assert.equal(state.localStatusCalls, 1); |
| 1529 | |
| 1530 | state.localStatusCalls = 0; |
| 1531 | state.currentRemoteStatus = baseRemoteStatus; |
| 1532 | state.beforeRemoteStatus = Effect.sync(() => { |
| 1533 | state.currentLocalStatus = totals(0); |
| 1534 | state.refSnapshot = "moved"; |
| 1535 | }); |
| 1536 | const moved = yield* broadcaster.refreshStatus("/repo"); |
| 1537 | assert.equal(state.localStatusCalls, 2); |
| 1538 | assert.deepStrictEqual(moved.branchChanges, totals(0).branchChanges); |
| 1539 | assert.deepStrictEqual( |
| 1540 | [...(yield* Fiber.join(events))].map((event) => event._tag), |
| 1541 | // The first refresh adds the totals this palette-style subscription skipped. |
| 1542 | ["localUpdated", "remoteUpdated", "remoteUpdated", "localUpdated"], |
| 1543 | ); |
| 1544 | |
| 1545 | state.localStatusCalls = 0; |
| 1546 | state.refSnapshot = null; |
| 1547 | yield* broadcaster.refreshStatus("/repo"); |
| 1548 | assert.equal(state.localStatusCalls, 2); |
| 1549 | }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); |
| 1550 | }); |
| 1551 | |
| 1552 | it.effect("publishes local status while an earlier lookup holds the remote lock", () => { |
| 1553 | const state: Parameters<typeof layerTestFor>[0] = { |
| 1554 | currentLocalStatus: baseLocalStatus, |
| 1555 | currentRemoteStatus: baseRemoteStatus, |
| 1556 | localStatusCalls: 0, |
| 1557 | remoteStatusCalls: 0, |
| 1558 | localInvalidationCalls: 0, |
| 1559 | remoteInvalidationCalls: 0, |
| 1560 | }; |
| 1561 | |
| 1562 | return Effect.gen(function* () { |
| 1563 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1564 | yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 1565 | const subscribed = yield* Deferred.make<void>(); |
| 1566 | const published = yield* Deferred.make<VcsStatusLocalResult>(); |
| 1567 | yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe( |
| 1568 | Stream.runForEach((event) => { |
| 1569 | if (event._tag === "snapshot") return Deferred.succeed(subscribed, undefined); |
| 1570 | if (event._tag === "localUpdated") return Deferred.succeed(published, event.local); |
| 1571 | return Effect.void; |
| 1572 | }), |
| 1573 | Effect.forkScoped, |
| 1574 | ); |
| 1575 | yield* Deferred.await(subscribed); |
| 1576 | const started = yield* Deferred.make<void>(); |
| 1577 | const release = yield* Deferred.make<void>(); |
| 1578 | state.beforeRemoteStatus = Deferred.succeed(started, undefined).pipe( |
| 1579 | Effect.andThen(Deferred.await(release)), |
| 1580 | ); |
| 1581 | const earlier = yield* broadcaster.refreshPullRequestStatus("/repo").pipe(Effect.forkScoped); |
| 1582 | yield* Deferred.await(started); |
| 1583 | state.currentLocalStatus = { ...baseLocalStatus, hasWorkingTreeChanges: true }; |
| 1584 | const completed = yield* Deferred.make<void>(); |
| 1585 | const refresh = yield* broadcaster |
| 1586 | .refreshStatus("/repo") |
| 1587 | .pipe(Effect.ensuring(Deferred.succeed(completed, undefined)), Effect.forkScoped); |
| 1588 | |
| 1589 | assert.deepStrictEqual(yield* Deferred.await(published), state.currentLocalStatus); |
| 1590 | assert.isFalse(yield* Deferred.isDone(completed)); |
| 1591 | assert.isFalse(yield* Deferred.isDone(release)); |
| 1592 | state.currentRemoteStatus = remoteStatusWithPr; |
| 1593 | yield* Deferred.succeed(release, undefined); |
| 1594 | yield* Fiber.join(earlier); |
| 1595 | const status = yield* Fiber.join(refresh); |
| 1596 | assert.isTrue(status.hasWorkingTreeChanges); |
| 1597 | assert.deepStrictEqual(status.pr, remoteStatusWithPr.pr); |
| 1598 | }).pipe(Effect.provide(layerTestFor(state))); |
| 1599 | }); |
| 1600 | |
| 1601 | it.effect("publishes local changes before a remote error and propagates that error", () => { |
| 1602 | const remoteError = new GitManagerError({ |
| 1603 | operation: "VcsStatusBroadcaster.remoteStatus", |
| 1604 | cwd: "/repo", |
| 1605 | detail: "The controlled remote lookup failed.", |
| 1606 | }); |
| 1607 | const state: Parameters<typeof layerTestFor>[0] = { |
| 1608 | currentLocalStatus: baseLocalStatus, |
| 1609 | currentRemoteStatus: remoteStatusWithPr, |
| 1610 | localStatusCalls: 0, |
| 1611 | remoteStatusCalls: 0, |
| 1612 | localInvalidationCalls: 0, |
| 1613 | remoteInvalidationCalls: 0, |
| 1614 | }; |
| 1615 | |
| 1616 | return Effect.gen(function* () { |
| 1617 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1618 | yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 1619 | const subscribed = yield* Deferred.make<void>(); |
| 1620 | const published = yield* Deferred.make<VcsStatusLocalResult>(); |
| 1621 | yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe( |
| 1622 | Stream.runForEach((event) => { |
| 1623 | if (event._tag === "snapshot") return Deferred.succeed(subscribed, undefined); |
| 1624 | if (event._tag === "localUpdated") return Deferred.succeed(published, event.local); |
| 1625 | return Effect.void; |
| 1626 | }), |
| 1627 | Effect.forkScoped, |
| 1628 | ); |
| 1629 | yield* Deferred.await(subscribed); |
| 1630 | const started = yield* Deferred.make<void>(); |
| 1631 | const release = yield* Deferred.make<void>(); |
| 1632 | const completed = yield* Deferred.make<void>(); |
| 1633 | state.beforeRemoteStatus = Deferred.succeed(started, undefined).pipe( |
| 1634 | Effect.andThen(Deferred.await(release)), |
| 1635 | ); |
| 1636 | state.currentLocalStatus = { ...baseLocalStatus, hasWorkingTreeChanges: true }; |
| 1637 | state.remoteStatusError = remoteError; |
| 1638 | const refresh = yield* broadcaster |
| 1639 | .refreshStatus("/repo") |
| 1640 | .pipe( |
| 1641 | Effect.result, |
| 1642 | Effect.ensuring(Deferred.succeed(completed, undefined)), |
| 1643 | Effect.forkScoped, |
| 1644 | ); |
| 1645 | yield* Deferred.await(started); |
| 1646 | assert.isFalse(yield* Deferred.isDone(completed)); |
| 1647 | const pending = Option.getOrThrow( |
| 1648 | yield* Stream.runHead(broadcaster.streamStatus({ cwd: "/repo", includeRemote: false })), |
| 1649 | ); |
| 1650 | assert.equal(pending._tag, "snapshot"); |
| 1651 | if (pending._tag === "snapshot") assert.isTrue(pending.local.hasWorkingTreeChanges); |
| 1652 | yield* Deferred.succeed(release, undefined); |
| 1653 | const result = yield* Fiber.join(refresh); |
| 1654 | assert.equal(result._tag, "Failure"); |
| 1655 | if (result._tag === "Failure") assert.strictEqual(result.failure, remoteError); |
| 1656 | const cached = yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 1657 | assert.isTrue(cached.hasWorkingTreeChanges); |
| 1658 | assert.deepStrictEqual(cached.pr, remoteStatusWithPr.pr); |
| 1659 | assert.deepStrictEqual(yield* Deferred.await(published), state.currentLocalStatus); |
| 1660 | }).pipe(Effect.provide(layerTestFor(state))); |
| 1661 | }); |
| 1662 | |
| 1663 | it.effect.each(["getStatus", "refreshStatus", "autoPull", "autoPullFailure"] as const)( |
| 1664 | "%s keeps response parts consistent when local state changes after the remote cache write", |
| 1665 | (operation) => { |
| 1666 | const state: Parameters<typeof layerTestFor>[0] = { |
| 1667 | currentLocalStatus: baseLocalStatus, |
| 1668 | currentRemoteStatus: { ...remoteStatusWithPr, aheadCount: 3, behindCount: 5 }, |
| 1669 | localStatusCalls: 0, |
| 1670 | remoteStatusCalls: 0, |
| 1671 | localInvalidationCalls: 0, |
| 1672 | remoteInvalidationCalls: 0, |
| 1673 | fileSystem: FileSystem.makeNoop({ realPath: (cwd) => Effect.succeed(cwd) }), |
| 1674 | }; |
| 1675 | const autoPull = operation === "autoPull" || operation === "autoPullFailure"; |
| 1676 | if (autoPull) { |
| 1677 | state.currentLocalStatus = { ...baseLocalStatus, isDefaultRef: true }; |
| 1678 | state.currentRemoteStatus = { ...remoteStatusWithPr, aheadCount: 0, behindCount: 5 }; |
| 1679 | state.pullCurrentBranch = () => |
| 1680 | operation === "autoPullFailure" |
| 1681 | ? Effect.fail( |
| 1682 | new GitCommandError({ |
| 1683 | operation: "test.pull", |
| 1684 | command: "pull", |
| 1685 | cwd: "/repo", |
| 1686 | detail: "Pull failed.", |
| 1687 | }), |
| 1688 | ) |
| 1689 | : Effect.succeed({ |
| 1690 | status: "pulled", |
| 1691 | refName: "feature/status-broadcast", |
| 1692 | upstreamRef: "origin/main", |
| 1693 | }); |
| 1694 | } |
| 1695 | const layerTest = layerTestFor(state).pipe( |
| 1696 | Layer.provide( |
| 1697 | Layer.succeed(VcsStatusBroadcaster.VcsAutoPullPolicy, { |
| 1698 | isEnabled: () => Effect.succeed(autoPull), |
| 1699 | }), |
| 1700 | ), |
| 1701 | ); |
| 1702 | return Effect.gen(function* () { |
| 1703 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1704 | let switched = false; |
| 1705 | let remoteWritten = false; |
| 1706 | const writeCompleted = yield* Deferred.make<void>(); |
| 1707 | const localRefresh = yield* Effect.gen(function* () { |
| 1708 | yield* Deferred.await(writeCompleted); |
| 1709 | state.currentLocalStatus = { ...baseLocalStatus, refName: "feature/next-status" }; |
| 1710 | yield* broadcaster.refreshLocalStatus("/repo"); |
| 1711 | switched = true; |
| 1712 | }).pipe(Effect.forkScoped); |
| 1713 | const scheduler = new Scheduler.MixedScheduler(); |
| 1714 | const shouldYield = scheduler.shouldYield.bind(scheduler); |
| 1715 | scheduler.shouldYield = (fiber) => (remoteWritten && !switched) || shouldYield(fiber); |
| 1716 | const tracer = Tracer.make({ |
| 1717 | span: (options) => { |
| 1718 | const span = new Tracer.NativeSpan(options); |
| 1719 | const end = span.end.bind(span); |
| 1720 | span.end = (time, exit) => { |
| 1721 | end(time, exit); |
| 1722 | if ( |
| 1723 | options.name !== "VcsStatusBroadcaster.updateCachedRemoteStatus" || |
| 1724 | remoteWritten |
| 1725 | ) { |
| 1726 | return; |
| 1727 | } |
| 1728 | // Yield this reader after its write until the local refresh finishes. |
| 1729 | remoteWritten = true; |
| 1730 | Deferred.doneUnsafe(writeCompleted, Effect.void); |
| 1731 | }; |
| 1732 | return span; |
| 1733 | }, |
| 1734 | }); |
| 1735 | const status = yield* ( |
| 1736 | operation === "getStatus" |
| 1737 | ? broadcaster.getStatus({ cwd: "/repo" }) |
| 1738 | : broadcaster.refreshStatus("/repo") |
| 1739 | ).pipe(Effect.withTracer(tracer), Effect.provideService(Scheduler.Scheduler, scheduler)); |
| 1740 | yield* Fiber.join(localRefresh); |
| 1741 | assert.isTrue(switched); |
| 1742 | assert.equal(status.refName, "feature/next-status"); |
| 1743 | assert.isNull(status.pr); |
| 1744 | assert.equal(status.aheadCount, 0); |
| 1745 | assert.equal(status.behindCount, 0); |
| 1746 | }).pipe(Effect.provide(layerTest)); |
| 1747 | }, |
| 1748 | ); |
| 1749 | |
| 1750 | it.effect("recovers a discarded branch lookup on the next turn-end refresh", () => { |
| 1751 | const state: Parameters<typeof layerTestFor>[0] = { |
| 1752 | currentLocalStatus: baseLocalStatus, |
| 1753 | currentRemoteStatus: { ...remoteStatusWithPr, aheadCount: 3, behindCount: 5 }, |
| 1754 | localStatusCalls: 0, |
| 1755 | remoteStatusCalls: 0, |
| 1756 | localInvalidationCalls: 0, |
| 1757 | remoteInvalidationCalls: 0, |
| 1758 | }; |
| 1759 | |
| 1760 | return Effect.gen(function* () { |
| 1761 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1762 | const started = yield* Deferred.make<void>(); |
| 1763 | const release = yield* Deferred.make<void>(); |
| 1764 | state.beforeRemoteStatus = Deferred.succeed(started, undefined).pipe( |
| 1765 | Effect.andThen(Deferred.await(release)), |
| 1766 | ); |
| 1767 | const lookup = yield* broadcaster.getStatus({ cwd: "/repo" }).pipe(Effect.forkScoped); |
| 1768 | yield* Deferred.await(started); |
| 1769 | state.currentLocalStatus = { ...baseLocalStatus, refName: "feature/next-status" }; |
| 1770 | yield* Deferred.succeed(release, undefined); |
| 1771 | const discarded = yield* Fiber.join(lookup); |
| 1772 | assert.equal(discarded.refName, state.currentLocalStatus.refName); |
| 1773 | assert.isNull(discarded.pr); |
| 1774 | assert.equal(discarded.aheadCount, 0); |
| 1775 | assert.equal(discarded.behindCount, 0); |
| 1776 | const snapshot = Option.getOrThrow( |
| 1777 | yield* Stream.runHead(broadcaster.streamStatus({ cwd: "/repo", includeRemote: false })), |
| 1778 | ); |
| 1779 | assert.equal(snapshot._tag, "snapshot"); |
| 1780 | if (snapshot._tag === "snapshot") assert.isNull(snapshot.remote); |
| 1781 | |
| 1782 | state.beforeRemoteStatus = Effect.void; |
| 1783 | state.currentRemoteStatus = { |
| 1784 | ...baseRemoteStatus, |
| 1785 | aheadCount: 2, |
| 1786 | pr: { ...remoteStatusWithPr.pr!, number: 2979, headRef: "feature/next-status" }, |
| 1787 | }; |
| 1788 | state.backgroundWorkEnabled = false; |
| 1789 | const recovered = yield* broadcaster.refreshPullRequestStatus("/repo"); |
| 1790 | assert.deepStrictEqual(recovered, state.currentRemoteStatus); |
| 1791 | const cached = yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 1792 | assert.equal(cached.refName, state.currentLocalStatus.refName); |
| 1793 | assert.deepStrictEqual(cached.pr, state.currentRemoteStatus.pr); |
| 1794 | assert.equal(cached.aheadCount, 2); |
| 1795 | assert.equal(state.remoteStatusCalls, 2); |
| 1796 | }).pipe(Effect.provide(layerTestFor(state))); |
| 1797 | }); |
| 1798 | |
| 1799 | it.effect("a terminal checkout publishes the new branch without waiting for a poll", () => |
| 1800 | Effect.gen(function* () { |
| 1801 | const fs = yield* FileSystem.FileSystem; |
| 1802 | const path = yield* Path.Path; |
| 1803 | const gitDir = yield* fs.makeTempDirectoryScoped({ prefix: "vcs-head-watch-" }); |
| 1804 | const headPath = path.join(gitDir, "HEAD"); |
| 1805 | yield* fs.writeFileString(headPath, "ref: refs/heads/main\n"); |
| 1806 | const state = { |
| 1807 | currentLocalStatus: { ...baseLocalStatus, isDefaultRef: true, refName: "main" }, |
| 1808 | currentRemoteStatus: remoteStatusWithPr as VcsStatusRemoteResult | null, |
| 1809 | localStatusCalls: 0, |
| 1810 | remoteStatusCalls: 0, |
| 1811 | localInvalidationCalls: 0, |
| 1812 | remoteInvalidationCalls: 0, |
| 1813 | remoteStatusRefreshUpstreamValues: [] as Array<boolean | undefined>, |
| 1814 | headPath, |
| 1815 | }; |
| 1816 | |
| 1817 | yield* Effect.gen(function* () { |
| 1818 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1819 | yield* broadcaster.getStatus({ cwd: "/repo" }); |
| 1820 | const clientStates: Array<VcsStatusResult> = []; |
| 1821 | const subscribed = yield* Deferred.make<void>(); |
| 1822 | const checkedOut = yield* Deferred.make<void>(); |
| 1823 | yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) => |
| 1824 | Effect.gen(function* () { |
| 1825 | const next = applyGitStatusStreamEvent(clientStates.at(-1) ?? null, event); |
| 1826 | clientStates.push(next); |
| 1827 | yield* Deferred.succeed(subscribed, undefined); |
| 1828 | // The refresh ends with the new branch's remote, after its local update. |
| 1829 | if (event._tag === "remoteUpdated" && event.remote && next.refName === "repro") { |
| 1830 | yield* Deferred.succeed(checkedOut, undefined); |
| 1831 | } |
| 1832 | }), |
| 1833 | ).pipe(Effect.forkScoped); |
| 1834 | yield* Deferred.await(subscribed); |
| 1835 | |
| 1836 | // What `git switch -c repro` does to HEAD, with the test clock never advanced. |
| 1837 | state.currentLocalStatus = { ...baseLocalStatus, isDefaultRef: false, refName: "repro" }; |
| 1838 | state.currentRemoteStatus = { ...baseRemoteStatus, hasUpstream: false }; |
| 1839 | const lockPath = path.join(gitDir, "HEAD.lock"); |
| 1840 | yield* fs.writeFileString(lockPath, "ref: refs/heads/repro\n"); |
| 1841 | yield* fs.rename(lockPath, headPath); |
| 1842 | yield* Deferred.await(checkedOut); |
| 1843 | |
| 1844 | assert.deepStrictEqual( |
| 1845 | clientStates.filter((status) => status.refName === "repro").map((status) => status.pr), |
| 1846 | [null, null], |
| 1847 | ); |
| 1848 | // A checkout must not trigger a network fetch. |
| 1849 | assert.deepStrictEqual(state.remoteStatusRefreshUpstreamValues.slice(1), [false]); |
| 1850 | }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); |
| 1851 | }).pipe(Effect.provide(NodeServices.layer)), |
| 1852 | ); |
| 1853 | |
| 1854 | it.effect("a terminal checkout updates branch-only subscribers without full status", () => |
| 1855 | Effect.gen(function* () { |
| 1856 | const fs = yield* FileSystem.FileSystem; |
| 1857 | const path = yield* Path.Path; |
| 1858 | const gitDir = yield* fs.makeTempDirectoryScoped({ prefix: "vcs-head-watch-" }); |
| 1859 | const headPath = path.join(gitDir, "HEAD"); |
| 1860 | yield* fs.writeFileString(headPath, "ref: refs/heads/main\n"); |
| 1861 | const state = { |
| 1862 | currentLocalStatus: { ...baseLocalStatus, refName: "main" }, |
| 1863 | currentRemoteStatus: null, |
| 1864 | localStatusCalls: 0, |
| 1865 | remoteStatusCalls: 0, |
| 1866 | localInvalidationCalls: 0, |
| 1867 | remoteInvalidationCalls: 0, |
| 1868 | headPath, |
| 1869 | }; |
| 1870 | |
| 1871 | yield* Effect.gen(function* () { |
| 1872 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1873 | const branches = yield* Stream.toPull(broadcaster.streamBranchStatus({ cwd: "/repo" })); |
| 1874 | assert.deepEqual(yield* branches, [{ refName: "main", isRepo: true }]); |
| 1875 | |
| 1876 | state.currentLocalStatus = { ...baseLocalStatus, refName: "repro" }; |
| 1877 | const lockPath = path.join(gitDir, "HEAD.lock"); |
| 1878 | yield* fs.writeFileString(lockPath, "ref: refs/heads/repro\n"); |
| 1879 | yield* fs.rename(lockPath, headPath); |
| 1880 | |
| 1881 | assert.deepEqual(yield* branches, [{ refName: "repro", isRepo: true }]); |
| 1882 | assert.equal(state.localStatusCalls, 0); |
| 1883 | }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); |
| 1884 | }).pipe(Effect.provide(NodeServices.layer)), |
| 1885 | ); |
| 1886 | |
| 1887 | it.effect("a terminal checkout updates the branch before pending detailed status completes", () => |
| 1888 | Effect.gen(function* () { |
| 1889 | const fs = yield* FileSystem.FileSystem; |
| 1890 | const path = yield* Path.Path; |
| 1891 | const gitDir = yield* fs.makeTempDirectoryScoped({ prefix: "vcs-head-watch-" }); |
| 1892 | const headPath = path.join(gitDir, "HEAD"); |
| 1893 | yield* fs.writeFileString(headPath, "ref: refs/heads/main\n"); |
| 1894 | const state: Parameters<typeof layerTestFor>[0] = { |
| 1895 | currentLocalStatus: { ...baseLocalStatus, refName: "main" }, |
| 1896 | currentRemoteStatus: null, |
| 1897 | localStatusCalls: 0, |
| 1898 | remoteStatusCalls: 0, |
| 1899 | localInvalidationCalls: 0, |
| 1900 | remoteInvalidationCalls: 0, |
| 1901 | headPath, |
| 1902 | }; |
| 1903 | |
| 1904 | yield* Effect.gen(function* () { |
| 1905 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1906 | const branches = yield* Stream.toPull(broadcaster.streamBranchStatus({ cwd: "/repo" })); |
| 1907 | yield* branches; |
| 1908 | const details = yield* Stream.toPull( |
| 1909 | broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }), |
| 1910 | ); |
| 1911 | yield* details; |
| 1912 | |
| 1913 | const resume = yield* Deferred.make<void>(); |
| 1914 | state.beforeLocalStatus = Deferred.await(resume); |
| 1915 | state.currentLocalStatus = { ...baseLocalStatus, refName: "repro" }; |
| 1916 | const lockPath = path.join(gitDir, "HEAD.lock"); |
| 1917 | yield* fs.writeFileString(lockPath, "ref: refs/heads/repro\n"); |
| 1918 | yield* fs.rename(lockPath, headPath); |
| 1919 | |
| 1920 | assert.deepEqual(yield* branches, [{ refName: "repro", isRepo: true }]); |
| 1921 | state.currentLocalStatus = { ...baseLocalStatus, refName: "second" }; |
| 1922 | yield* fs.writeFileString(lockPath, "ref: refs/heads/second\n"); |
| 1923 | yield* fs.rename(lockPath, headPath); |
| 1924 | assert.deepEqual(yield* branches, [{ refName: "second", isRepo: true }]); |
| 1925 | yield* Deferred.succeed(resume, undefined); |
| 1926 | }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); |
| 1927 | }).pipe(Effect.provide(NodeServices.layer)), |
| 1928 | ); |
| 1929 | |
| 1930 | it.effect("a checkout read that fails while index.lock is held retries until it succeeds", () => |
| 1931 | Effect.gen(function* () { |
| 1932 | const events = yield* Queue.unbounded<FileSystem.WatchEvent>(); |
| 1933 | const headPath = "/repo/.git/HEAD"; |
| 1934 | const failed = yield* Deferred.make<void>(); |
| 1935 | const state: Parameters<typeof layerTestFor>[0] = { |
| 1936 | currentLocalStatus: { ...baseLocalStatus, isDefaultRef: true, refName: "main" }, |
| 1937 | currentRemoteStatus: null, |
| 1938 | localStatusCalls: 0, |
| 1939 | remoteStatusCalls: 0, |
| 1940 | localInvalidationCalls: 0, |
| 1941 | remoteInvalidationCalls: 0, |
| 1942 | headPath, |
| 1943 | fileSystem: FileSystem.makeNoop({ |
| 1944 | realPath: (target) => Effect.succeed(target), |
| 1945 | exists: () => Effect.succeed(true), |
| 1946 | watch: (_target, options) => |
| 1947 | Stream.fromQueue(events).pipe(Stream.onStart(Effect.sync(() => options?.onReady?.()))), |
| 1948 | }), |
| 1949 | failLocalStatus: false, |
| 1950 | onLocalStatusFailure: Deferred.succeed(failed, undefined).pipe(Effect.asVoid), |
| 1951 | }; |
| 1952 | |
| 1953 | yield* Effect.gen(function* () { |
| 1954 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 1955 | const subscribed = yield* Deferred.make<void>(); |
| 1956 | const checkedOut = yield* Deferred.make<void>(); |
| 1957 | let clientState: VcsStatusResult | null = null; |
| 1958 | yield* Stream.runForEach( |
| 1959 | broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }), |
| 1960 | (event) => |
| 1961 | Effect.gen(function* () { |
| 1962 | clientState = applyGitStatusStreamEvent(clientState, event); |
| 1963 | yield* Deferred.succeed(subscribed, undefined); |
| 1964 | if (clientState.refName === "repro") yield* Deferred.succeed(checkedOut, undefined); |
| 1965 | }), |
| 1966 | ).pipe(Effect.forkScoped); |
| 1967 | yield* Deferred.await(subscribed); |
| 1968 | |
| 1969 | // A terminal commit holds index.lock across the checkout, so the HEAD event's read fails. |
| 1970 | state.failLocalStatus = true; |
| 1971 | state.currentLocalStatus = { ...baseLocalStatus, isDefaultRef: false, refName: "repro" }; |
| 1972 | yield* Queue.offer(events, { _tag: "Update", path: "HEAD" }); |
| 1973 | yield* Deferred.await(failed); |
| 1974 | const failedCalls = state.localStatusCalls; |
| 1975 | |
| 1976 | // Removing index.lock moves no HEAD, and no remote poll runs for this subscriber. |
| 1977 | state.failLocalStatus = false; |
| 1978 | yield* TestClock.adjust(Duration.seconds(30)); |
| 1979 | yield* Deferred.await(checkedOut); |
| 1980 | assert.strictEqual(state.localStatusCalls, failedCalls + 1); |
| 1981 | }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); |
| 1982 | }), |
| 1983 | ); |
| 1984 | |
| 1985 | it.effect("subscribers share one HEAD watch that ends with the last of them", () => |
| 1986 | Effect.gen(function* () { |
| 1987 | const fs = yield* FileSystem.FileSystem; |
| 1988 | const path = yield* Path.Path; |
| 1989 | const gitDir = yield* fs.makeTempDirectoryScoped({ prefix: "vcs-head-watch-" }); |
| 1990 | const headPath = path.join(gitDir, "HEAD"); |
| 1991 | yield* fs.writeFileString(headPath, "ref: refs/heads/main\n"); |
| 1992 | const state = { |
| 1993 | currentLocalStatus: baseLocalStatus, |
| 1994 | currentRemoteStatus: baseRemoteStatus as VcsStatusRemoteResult | null, |
| 1995 | localStatusCalls: 0, |
| 1996 | remoteStatusCalls: 0, |
| 1997 | localInvalidationCalls: 0, |
| 1998 | remoteInvalidationCalls: 0, |
| 1999 | headPath, |
| 2000 | headPathCalls: 0, |
| 2001 | }; |
| 2002 | |
| 2003 | yield* Effect.gen(function* () { |
| 2004 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 2005 | const subscribe = Effect.gen(function* () { |
| 2006 | const received = yield* Deferred.make<void>(); |
| 2007 | const fiber = yield* Stream.runForEach( |
| 2008 | broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }), |
| 2009 | () => Deferred.succeed(received, undefined), |
| 2010 | ).pipe(Effect.forkScoped); |
| 2011 | yield* Deferred.await(received); |
| 2012 | return fiber; |
| 2013 | }); |
| 2014 | |
| 2015 | const first = yield* subscribe; |
| 2016 | const second = yield* subscribe; |
| 2017 | assert.strictEqual(state.headPathCalls, 1); |
| 2018 | |
| 2019 | yield* Fiber.interrupt(first); |
| 2020 | yield* Fiber.interrupt(second); |
| 2021 | yield* subscribe; |
| 2022 | assert.strictEqual(state.headPathCalls, 2); |
| 2023 | }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); |
| 2024 | }).pipe(Effect.provide(NodeServices.layer)), |
| 2025 | ); |
| 2026 | |
| 2027 | it.effect("a HEAD watch that ended restarts for the next subscriber and keeps earlier ones", () => |
| 2028 | Effect.gen(function* () { |
| 2029 | const fs = yield* FileSystem.FileSystem; |
| 2030 | const path = yield* Path.Path; |
| 2031 | const gitDir = yield* fs.makeTempDirectoryScoped({ prefix: "vcs-head-watch-" }); |
| 2032 | const headPath = path.join(gitDir, "HEAD"); |
| 2033 | yield* fs.writeFileString(headPath, "ref: refs/heads/main\n"); |
| 2034 | const state: Parameters<typeof layerTestFor>[0] = { |
| 2035 | currentLocalStatus: { ...baseLocalStatus, isDefaultRef: true, refName: "main" }, |
| 2036 | currentRemoteStatus: null, |
| 2037 | localStatusCalls: 0, |
| 2038 | remoteStatusCalls: 0, |
| 2039 | localInvalidationCalls: 0, |
| 2040 | remoteInvalidationCalls: 0, |
| 2041 | headPathCalls: 0, |
| 2042 | }; |
| 2043 | |
| 2044 | yield* Effect.gen(function* () { |
| 2045 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 2046 | const subscribe = Effect.gen(function* () { |
| 2047 | const subscribed = yield* Deferred.make<void>(); |
| 2048 | const refs = new Map<string, Deferred.Deferred<void>>(); |
| 2049 | const sawRef = (refName: string) => { |
| 2050 | const existing = refs.get(refName); |
| 2051 | if (existing) return existing; |
| 2052 | const created = Deferred.makeUnsafe<void>(); |
| 2053 | refs.set(refName, created); |
| 2054 | return created; |
| 2055 | }; |
| 2056 | let clientState: VcsStatusResult | null = null; |
| 2057 | const fiber = yield* Stream.runForEach( |
| 2058 | broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }), |
| 2059 | (event) => |
| 2060 | Effect.gen(function* () { |
| 2061 | clientState = applyGitStatusStreamEvent(clientState, event); |
| 2062 | yield* Deferred.succeed(subscribed, undefined); |
| 2063 | yield* Deferred.succeed(sawRef(clientState.refName ?? ""), undefined); |
| 2064 | }), |
| 2065 | ).pipe(Effect.forkScoped); |
| 2066 | yield* Deferred.await(subscribed); |
| 2067 | return { fiber, sawRef }; |
| 2068 | }); |
| 2069 | const checkout = (refName: string) => |
| 2070 | Effect.gen(function* () { |
| 2071 | state.currentLocalStatus = { ...baseLocalStatus, isDefaultRef: false, refName }; |
| 2072 | const lockPath = path.join(gitDir, "HEAD.lock"); |
| 2073 | yield* fs.writeFileString(lockPath, `ref: refs/heads/${refName}\n`); |
| 2074 | yield* fs.rename(lockPath, headPath); |
| 2075 | }); |
| 2076 | |
| 2077 | // The repository has no HEAD yet, so the first watch ends at once. |
| 2078 | const first = yield* subscribe; |
| 2079 | state.headPath = headPath; |
| 2080 | const second = yield* subscribe; |
| 2081 | assert.strictEqual(state.headPathCalls, 2); |
| 2082 | |
| 2083 | yield* checkout("one"); |
| 2084 | yield* Deferred.await(first.sawRef("one")); |
| 2085 | yield* Deferred.await(second.sawRef("one")); |
| 2086 | |
| 2087 | // The replacement belongs to both subscribers, so the second leaving keeps it. |
| 2088 | yield* Fiber.interrupt(second.fiber); |
| 2089 | yield* checkout("two"); |
| 2090 | yield* Deferred.await(first.sawRef("two")); |
| 2091 | }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); |
| 2092 | }).pipe(Effect.provide(NodeServices.layer)), |
| 2093 | ); |
| 2094 | |
| 2095 | it.effect("a HEAD watch ends when its git directory is removed", () => |
| 2096 | Effect.gen(function* () { |
| 2097 | const gitDir = "/repo/.git/worktrees/feature"; |
| 2098 | const events = yield* Queue.unbounded<FileSystem.WatchEvent>(); |
| 2099 | const closed = yield* Deferred.make<void>(); |
| 2100 | let gitDirExists = true; |
| 2101 | const state: Parameters<typeof layerTestFor>[0] = { |
| 2102 | currentLocalStatus: { ...baseLocalStatus, refName: "main" }, |
| 2103 | currentRemoteStatus: null, |
| 2104 | localStatusCalls: 0, |
| 2105 | remoteStatusCalls: 0, |
| 2106 | localInvalidationCalls: 0, |
| 2107 | remoteInvalidationCalls: 0, |
| 2108 | headPath: `${gitDir}/HEAD`, |
| 2109 | fileSystem: FileSystem.makeNoop({ |
| 2110 | realPath: (target) => Effect.succeed(target), |
| 2111 | exists: (target) => Effect.succeed(target !== gitDir || gitDirExists), |
| 2112 | watch: (_target, options) => |
| 2113 | Stream.fromQueue(events).pipe( |
| 2114 | Stream.onStart(Effect.sync(() => options?.onReady?.())), |
| 2115 | Stream.ensuring(Deferred.succeed(closed, undefined)), |
| 2116 | ), |
| 2117 | }), |
| 2118 | }; |
| 2119 | |
| 2120 | yield* Effect.gen(function* () { |
| 2121 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 2122 | const branches = yield* Stream.toPull(broadcaster.streamBranchStatus({ cwd: "/repo" })); |
| 2123 | assert.deepEqual(yield* branches, [{ refName: "main", isRepo: true }]); |
| 2124 | |
| 2125 | // Every checkout removes HEAD.lock, which leaves the watch running. |
| 2126 | state.currentLocalStatus = { ...baseLocalStatus, refName: "other" }; |
| 2127 | yield* Queue.offer(events, { _tag: "Remove", path: "HEAD.lock" }); |
| 2128 | yield* Queue.offer(events, { _tag: "Update", path: "HEAD" }); |
| 2129 | assert.deepEqual(yield* branches, [{ refName: "other", isRepo: true }]); |
| 2130 | |
| 2131 | // `git worktree remove` deletes the watched directory; Windows keeps reporting it. |
| 2132 | gitDirExists = false; |
| 2133 | yield* Queue.offer(events, { _tag: "Remove", path: gitDir }); |
| 2134 | yield* Deferred.await(closed); |
| 2135 | }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); |
| 2136 | }), |
| 2137 | ); |
| 2138 | |
| 2139 | it.effect("releases remote demand while passive observers remain", () => { |
| 2140 | const state = { |
| 2141 | currentLocalStatus: baseLocalStatus, |
| 2142 | currentRemoteStatus: baseRemoteStatus, |
| 2143 | localStatusCalls: 0, |
| 2144 | remoteStatusCalls: 0, |
| 2145 | localInvalidationCalls: 0, |
| 2146 | remoteInvalidationCalls: 0, |
| 2147 | }; |
| 2148 | let remoteInterruptedDeferred: Deferred.Deferred<void, never> | null = null; |
| 2149 | let remoteStartedDeferred: Deferred.Deferred<void, never> | null = null; |
| 2150 | const layerTest = VcsStatusBroadcaster.layer.pipe( |
| 2151 | Layer.provideMerge(NodeServices.layer), |
| 2152 | Layer.provide(layerBackgroundPolicy(() => true)), |
| 2153 | Layer.provide( |
| 2154 | Layer.mock(GitWorkflowService.GitWorkflowService)({ |
| 2155 | localStatus: () => |
| 2156 | Effect.sync(() => { |
| 2157 | state.localStatusCalls += 1; |
| 2158 | return state.currentLocalStatus; |
| 2159 | }), |
| 2160 | currentBranch: () => Effect.succeed(state.currentLocalStatus.refName), |
| 2161 | remoteStatus: () => |
| 2162 | Effect.sync(() => { |
| 2163 | state.remoteStatusCalls += 1; |
| 2164 | }).pipe( |
| 2165 | Effect.andThen( |
| 2166 | remoteStartedDeferred |
| 2167 | ? Deferred.succeed(remoteStartedDeferred, undefined).pipe(Effect.ignore) |
| 2168 | : Effect.void, |
| 2169 | ), |
| 2170 | Effect.andThen(Effect.never as Effect.Effect<VcsStatusRemoteResult | null, never>), |
| 2171 | Effect.onInterrupt(() => |
| 2172 | remoteInterruptedDeferred |
| 2173 | ? Deferred.succeed(remoteInterruptedDeferred, undefined).pipe(Effect.ignore) |
| 2174 | : Effect.void, |
| 2175 | ), |
| 2176 | ), |
| 2177 | invalidateLocalStatus: () => |
| 2178 | Effect.sync(() => { |
| 2179 | state.localInvalidationCalls += 1; |
| 2180 | }), |
| 2181 | invalidateRemoteStatus: () => |
| 2182 | Effect.sync(() => { |
| 2183 | state.remoteInvalidationCalls += 1; |
| 2184 | }), |
| 2185 | } satisfies Partial<GitWorkflowService.GitWorkflowService["Service"]>), |
| 2186 | ), |
| 2187 | ); |
| 2188 | |
| 2189 | return Effect.gen(function* () { |
| 2190 | const remoteInterrupted = yield* Deferred.make<void>(); |
| 2191 | const remoteStarted = yield* Deferred.make<void>(); |
| 2192 | remoteInterruptedDeferred = remoteInterrupted; |
| 2193 | remoteStartedDeferred = remoteStarted; |
| 2194 | |
| 2195 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 2196 | const firstSnapshot = yield* Deferred.make<VcsStatusStreamEvent>(); |
| 2197 | const secondSnapshot = yield* Deferred.make<VcsStatusStreamEvent>(); |
| 2198 | const firstScope = yield* Scope.make(); |
| 2199 | const secondScope = yield* Scope.make(); |
| 2200 | const passiveSnapshot = yield* Deferred.make<VcsStatusStreamEvent>(); |
| 2201 | yield* Stream.runForEach( |
| 2202 | broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }), |
| 2203 | (event) => |
| 2204 | event._tag === "snapshot" |
| 2205 | ? Deferred.succeed(passiveSnapshot, event).pipe(Effect.ignore) |
| 2206 | : Effect.void, |
| 2207 | ).pipe(Effect.forkScoped); |
| 2208 | yield* Deferred.await(passiveSnapshot); |
| 2209 | assert.equal(state.remoteStatusCalls, 0); |
| 2210 | yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) => |
| 2211 | event._tag === "snapshot" |
| 2212 | ? Deferred.succeed(firstSnapshot, event).pipe(Effect.ignore) |
| 2213 | : Effect.void, |
| 2214 | ).pipe(Effect.forkIn(firstScope)); |
| 2215 | yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) => |
| 2216 | event._tag === "snapshot" |
| 2217 | ? Deferred.succeed(secondSnapshot, event).pipe(Effect.ignore) |
| 2218 | : Effect.void, |
| 2219 | ).pipe(Effect.forkIn(secondScope)); |
| 2220 | |
| 2221 | yield* Deferred.await(firstSnapshot); |
| 2222 | yield* Deferred.await(secondSnapshot); |
| 2223 | yield* Deferred.await(remoteStarted); |
| 2224 | |
| 2225 | assert.equal(state.remoteStatusCalls, 1); |
| 2226 | |
| 2227 | yield* Scope.close(firstScope, Exit.void); |
| 2228 | assert.isTrue(Option.isNone(yield* Deferred.poll(remoteInterrupted))); |
| 2229 | |
| 2230 | yield* Scope.close(secondScope, Exit.void).pipe(Effect.forkScoped); |
| 2231 | yield* Deferred.await(remoteInterrupted); |
| 2232 | assert.isTrue(Option.isSome(yield* Deferred.poll(remoteInterrupted))); |
| 2233 | }).pipe(Effect.provide(layerTest)); |
| 2234 | }); |
| 2235 | |
| 2236 | it.effect("refreshes requested during a refresh share one follow-up refresh", () => { |
| 2237 | const state: Parameters<typeof layerTestFor>[0] = { |
| 2238 | currentLocalStatus: baseLocalStatus, |
| 2239 | currentRemoteStatus: baseRemoteStatus, |
| 2240 | localStatusCalls: 0, |
| 2241 | remoteStatusCalls: 0, |
| 2242 | localInvalidationCalls: 0, |
| 2243 | remoteInvalidationCalls: 0, |
| 2244 | }; |
| 2245 | |
| 2246 | return Effect.gen(function* () { |
| 2247 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 2248 | const fetchStarted = yield* Deferred.make<void>(); |
| 2249 | const finishFetch = yield* Deferred.make<void>(); |
| 2250 | state.beforeRemoteStatus = Deferred.succeed(fetchStarted, undefined).pipe( |
| 2251 | Effect.andThen(Deferred.await(finishFetch)), |
| 2252 | ); |
| 2253 | const first = yield* broadcaster.refreshStatus("/repo").pipe(Effect.forkScoped); |
| 2254 | yield* Deferred.await(fetchStarted); |
| 2255 | // Every read after the first sees a PR, so a caller that reused the |
| 2256 | // first refresh would get a status from before its own request. |
| 2257 | state.beforeRemoteStatus = Effect.sync(() => { |
| 2258 | state.currentRemoteStatus = remoteStatusWithPr; |
| 2259 | }); |
| 2260 | const later = yield* Effect.all( |
| 2261 | [broadcaster.refreshStatus("/repo"), broadcaster.refreshStatus("/repo")], |
| 2262 | { concurrency: "unbounded" }, |
| 2263 | ).pipe(Effect.forkScoped({ startImmediately: true })); |
| 2264 | yield* Deferred.succeed(finishFetch, undefined); |
| 2265 | |
| 2266 | assert.isNull((yield* Fiber.join(first)).pr); |
| 2267 | const [second, third] = yield* Fiber.join(later); |
| 2268 | assert.deepStrictEqual(second.pr, remoteStatusWithPr.pr); |
| 2269 | assert.deepStrictEqual(third.pr, remoteStatusWithPr.pr); |
| 2270 | assert.equal(state.remoteStatusCalls, 2); |
| 2271 | |
| 2272 | yield* broadcaster.refreshStatus("/repo"); |
| 2273 | assert.equal(state.remoteStatusCalls, 3); |
| 2274 | }).pipe(Effect.provide(layerTestFor(state))); |
| 2275 | }); |
| 2276 | |
| 2277 | it.effect("a failed refresh releases interrupted waiters and runs the shared follow-up", () => { |
| 2278 | const state: Parameters<typeof layerTestFor>[0] = { |
| 2279 | currentLocalStatus: baseLocalStatus, |
| 2280 | currentRemoteStatus: remoteStatusWithPr, |
| 2281 | localStatusCalls: 0, |
| 2282 | remoteStatusCalls: 0, |
| 2283 | localInvalidationCalls: 0, |
| 2284 | remoteInvalidationCalls: 0, |
| 2285 | }; |
| 2286 | |
| 2287 | return Effect.gen(function* () { |
| 2288 | const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 2289 | const started = yield* Deferred.make<void>(); |
| 2290 | const release = yield* Deferred.make<void>(); |
| 2291 | state.beforeRemoteStatus = Deferred.succeed(started, undefined).pipe( |
| 2292 | Effect.andThen(Deferred.await(release)), |
| 2293 | Effect.andThen( |
| 2294 | Effect.fail( |
| 2295 | new GitManagerError({ |
| 2296 | operation: "VcsStatusBroadcaster.test", |
| 2297 | cwd: "/repo", |
| 2298 | detail: "remote status failed", |
| 2299 | }), |
| 2300 | ), |
| 2301 | ), |
| 2302 | ); |
| 2303 | const first = yield* broadcaster.refreshStatus("/repo").pipe(Effect.exit, Effect.forkScoped); |
| 2304 | yield* Deferred.await(started); |
| 2305 | const interrupted = yield* broadcaster |
| 2306 | .refreshStatus("/repo") |
| 2307 | .pipe(Effect.forkScoped({ startImmediately: true })); |
| 2308 | const remaining = yield* broadcaster |
| 2309 | .refreshStatus("/repo") |
| 2310 | .pipe(Effect.forkScoped({ startImmediately: true })); |
| 2311 | yield* Fiber.interrupt(interrupted); |
| 2312 | state.beforeRemoteStatus = Effect.void; |
| 2313 | yield* Deferred.succeed(release, undefined); |
| 2314 | assert.isTrue(Exit.isFailure(yield* Fiber.join(first))); |
| 2315 | assert.deepStrictEqual((yield* Fiber.join(remaining)).pr, remoteStatusWithPr.pr); |
| 2316 | assert.equal(state.remoteStatusCalls, 1); |
| 2317 | yield* broadcaster.refreshStatus("/repo"); |
| 2318 | assert.equal(state.remoteStatusCalls, 2); |
| 2319 | }).pipe(Effect.provide(layerTestFor(state))); |
| 2320 | }); |
| 2321 | }); |
| 2322 | |
| 2323 | const newState = (): Parameters<typeof layerTestFor>[0] => ({ |
| 2324 | fileSystem: FileSystem.makeNoop({ realPath: (cwd) => Effect.succeed(cwd) }), |
| 2325 | currentLocalStatus: baseLocalStatus, |
| 2326 | currentRemoteStatus: null, |
| 2327 | localStatusCalls: 0, |
| 2328 | remoteStatusCalls: 0, |
| 2329 | localInvalidationCalls: 0, |
| 2330 | remoteInvalidationCalls: 0, |
| 2331 | }); |
| 2332 | it.effect("detail join during invalidation must get fresh totals", () => { |
| 2333 | const state = newState(); |
| 2334 | return Effect.gen(function* () { |
| 2335 | const b = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 2336 | const oldScope = yield* Scope.make(); |
| 2337 | const old = yield* Stream.toPull(b.streamStatus({ cwd: "/repo", includeRemote: false })).pipe( |
| 2338 | Scope.provide(oldScope), |
| 2339 | ); |
| 2340 | yield* old; |
| 2341 | yield* Scope.close(oldScope, Exit.void); |
| 2342 | state.currentLocalStatus = { |
| 2343 | ...baseLocalStatus, |
| 2344 | workingTree: { files: [], insertions: 42, deletions: 0 }, |
| 2345 | }; |
| 2346 | const entered = yield* Deferred.make<void>(); |
| 2347 | const resume = yield* Deferred.make<void>(); |
| 2348 | state.beforeInvalidate = Deferred.succeed(entered, undefined).pipe( |
| 2349 | Effect.andThen(Deferred.await(resume)), |
| 2350 | ); |
| 2351 | const refresh = yield* b.refreshBranchStatus({ cwd: "/repo" }).pipe(Effect.forkScoped); |
| 2352 | yield* Deferred.await(entered); |
| 2353 | const snapshotFiber = yield* Stream.runHead( |
| 2354 | b.streamStatus({ cwd: "/repo", includeRemote: false }), |
| 2355 | ).pipe(Effect.forkScoped({ startImmediately: true })); |
| 2356 | yield* Deferred.succeed(resume, undefined); |
| 2357 | yield* Fiber.join(refresh); |
| 2358 | assert.deepEqual( |
| 2359 | yield* Fiber.join(snapshotFiber), |
| 2360 | Option.some({ _tag: "snapshot" as const, local: state.currentLocalStatus, remote: null }), |
| 2361 | ); |
| 2362 | }).pipe(Effect.provide(layerTestFor(state))); |
| 2363 | }); |
| 2364 | it.effect("serializes branch reads with detailed refresh publication", () => { |
| 2365 | const state = newState(); |
| 2366 | return Effect.gen(function* () { |
| 2367 | const b = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 2368 | const entered = yield* Deferred.make<void>(); |
| 2369 | const resume = yield* Deferred.make<void>(); |
| 2370 | state.branchRead = () => |
| 2371 | Deferred.succeed(entered, undefined).pipe( |
| 2372 | Effect.andThen(Deferred.await(resume)), |
| 2373 | Effect.as("old"), |
| 2374 | ); |
| 2375 | const older = yield* b.refreshBranchStatus({ cwd: "/repo" }).pipe(Effect.forkScoped); |
| 2376 | yield* Deferred.await(entered); |
| 2377 | state.currentLocalStatus = { ...baseLocalStatus, refName: "new" }; |
| 2378 | const newer = yield* b |
| 2379 | .refreshLocalStatus("/repo") |
| 2380 | .pipe(Effect.forkScoped({ startImmediately: true })); |
| 2381 | yield* Deferred.succeed(resume, undefined); |
| 2382 | yield* Fiber.join(older); |
| 2383 | yield* Fiber.join(newer); |
| 2384 | state.branchRead = () => Effect.succeed("new"); |
| 2385 | const result = yield* b |
| 2386 | .streamStatus({ cwd: "/repo", includeRemote: false }) |
| 2387 | .pipe(Stream.runHead, Effect.flatMap(Effect.fromOption)); |
| 2388 | assert.equal(result._tag === "snapshot" && result.local.refName, "new"); |
| 2389 | assert.equal(state.localStatusCalls, 1); |
| 2390 | }).pipe(Effect.provide(layerTestFor(state))); |
| 2391 | }); |
| 2392 | |
| 2393 | it.effect("publishes overlapping branch reads in checkout order", () => { |
| 2394 | const state = newState(); |
| 2395 | return Effect.gen(function* () { |
| 2396 | const b = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 2397 | const branches = yield* Stream.toPull(b.streamBranchStatus({ cwd: "/repo" })); |
| 2398 | yield* branches; |
| 2399 | const entered = yield* Deferred.make<void>(); |
| 2400 | const resume = yield* Deferred.make<void>(); |
| 2401 | let reads = 0; |
| 2402 | state.branchRead = () => |
| 2403 | ++reads === 1 |
| 2404 | ? Deferred.succeed(entered, undefined).pipe( |
| 2405 | Effect.andThen(Deferred.await(resume)), |
| 2406 | Effect.as("old"), |
| 2407 | ) |
| 2408 | : Effect.succeed("new"); |
| 2409 | const older = yield* b.refreshBranchStatus({ cwd: "/repo" }).pipe(Effect.forkScoped); |
| 2410 | yield* Deferred.await(entered); |
| 2411 | const newer = yield* b |
| 2412 | .refreshBranchStatus({ cwd: "/repo" }) |
| 2413 | .pipe(Effect.forkScoped({ startImmediately: true })); |
| 2414 | yield* Deferred.succeed(resume, undefined); |
| 2415 | yield* Fiber.join(older); |
| 2416 | yield* Fiber.join(newer); |
| 2417 | assert.deepEqual(yield* branches, [ |
| 2418 | { refName: "old", isRepo: true }, |
| 2419 | { refName: "new", isRepo: true }, |
| 2420 | ]); |
| 2421 | }).pipe(Effect.provide(layerTestFor(state))); |
| 2422 | }); |
| 2423 | |
| 2424 | it.effect("does not replay older queued branches after a new subscriber snapshot", () => { |
| 2425 | const state = newState(); |
| 2426 | return Effect.gen(function* () { |
| 2427 | const b = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; |
| 2428 | const entered = yield* Deferred.make<void>(); |
| 2429 | const resume = yield* Deferred.make<void>(); |
| 2430 | const values = yield* Queue.unbounded<VcsBranchStatus>(); |
| 2431 | let reads = 0; |
| 2432 | state.branchRead = () => |
| 2433 | ++reads === 1 |
| 2434 | ? Deferred.succeed(entered, undefined).pipe( |
| 2435 | Effect.andThen(Deferred.await(resume)), |
| 2436 | Effect.as("old"), |
| 2437 | ) |
| 2438 | : Effect.succeed("new"); |
| 2439 | const older = yield* b.refreshBranchStatus({ cwd: "/repo" }).pipe(Effect.forkScoped); |
| 2440 | yield* Deferred.await(entered); |
| 2441 | const subscriber = yield* b.streamBranchStatus({ cwd: "/repo" }).pipe( |
| 2442 | Stream.tap((value) => Queue.offer(values, value)), |
| 2443 | Stream.take(2), |
| 2444 | Stream.runDrain, |
| 2445 | Effect.forkScoped({ startImmediately: true }), |
| 2446 | ); |
| 2447 | yield* Deferred.succeed(resume, undefined); |
| 2448 | yield* Fiber.join(older); |
| 2449 | assert.deepEqual(yield* Queue.take(values), { refName: "new", isRepo: true }); |
| 2450 | state.branchRead = () => Effect.succeed("latest"); |
| 2451 | yield* b.refreshBranchStatus({ cwd: "/repo" }); |
| 2452 | assert.deepEqual(yield* Queue.take(values), { refName: "latest", isRepo: true }); |
| 2453 | yield* Fiber.join(subscriber); |
| 2454 | }).pipe(Effect.provide(layerTestFor(state))); |
| 2455 | }); |