| 1 | import { EnvironmentId, WS_METHODS } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "@effect/vitest"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | |
| 5 | import { |
| 6 | observeBackgroundActivitySubscription, |
| 7 | retainedBackgroundScopes, |
| 8 | wasRecentlyInteracted, |
| 9 | } from "./backgroundActivityReporter.ts"; |
| 10 | |
| 11 | describe("wasRecentlyInteracted", () => { |
| 12 | it("expires interaction independently of window focus", () => { |
| 13 | expect(wasRecentlyInteracted(10_000, 55_000)).toBe(true); |
| 14 | expect(wasRecentlyInteracted(10_000, 55_001)).toBe(false); |
| 15 | }); |
| 16 | |
| 17 | it("rejects future timestamps", () => { |
| 18 | expect(wasRecentlyInteracted(10_001, 10_000)).toBe(false); |
| 19 | }); |
| 20 | |
| 21 | it.effect("retains an observed subscription until its returned finalizer runs", () => |
| 22 | Effect.gen(function* () { |
| 23 | const environmentId = EnvironmentId.make("environment-observation-test"); |
| 24 | const scope = { type: "vcs-status" as const, cwd: "/repo" }; |
| 25 | const releasePassive = yield* observeBackgroundActivitySubscription({ |
| 26 | environmentId, |
| 27 | method: WS_METHODS.subscribeVcsStatus, |
| 28 | input: { cwd: scope.cwd, includeRemote: false }, |
| 29 | }); |
| 30 | expect(retainedBackgroundScopes(environmentId)).toEqual([]); |
| 31 | const release = yield* observeBackgroundActivitySubscription({ |
| 32 | environmentId, |
| 33 | method: WS_METHODS.subscribeVcsStatus, |
| 34 | input: { cwd: scope.cwd }, |
| 35 | }); |
| 36 | |
| 37 | expect(retainedBackgroundScopes(environmentId)).toEqual([scope]); |
| 38 | |
| 39 | yield* releasePassive; |
| 40 | expect(retainedBackgroundScopes(environmentId)).toEqual([scope]); |
| 41 | |
| 42 | yield* release; |
| 43 | expect(retainedBackgroundScopes(environmentId)).toEqual([]); |
| 44 | }), |
| 45 | ); |
| 46 | |
| 47 | it.effect("keeps delimiter-containing environment and scope values distinct", () => |
| 48 | Effect.gen(function* () { |
| 49 | const firstEnvironmentId = EnvironmentId.make("a"); |
| 50 | const secondEnvironmentId = EnvironmentId.make("a:vcs-status:b"); |
| 51 | const releaseFirst = yield* observeBackgroundActivitySubscription({ |
| 52 | environmentId: firstEnvironmentId, |
| 53 | method: WS_METHODS.subscribeVcsStatus, |
| 54 | input: { cwd: "b:vcs-status:c" }, |
| 55 | }); |
| 56 | const releaseSecond = yield* observeBackgroundActivitySubscription({ |
| 57 | environmentId: secondEnvironmentId, |
| 58 | method: WS_METHODS.subscribeVcsStatus, |
| 59 | input: { cwd: "c" }, |
| 60 | }); |
| 61 | |
| 62 | expect(retainedBackgroundScopes(firstEnvironmentId)).toEqual([ |
| 63 | { type: "vcs-status", cwd: "b:vcs-status:c" }, |
| 64 | ]); |
| 65 | expect(retainedBackgroundScopes(secondEnvironmentId)).toEqual([ |
| 66 | { type: "vcs-status", cwd: "c" }, |
| 67 | ]); |
| 68 | |
| 69 | yield* Effect.all([releaseFirst, releaseSecond]); |
| 70 | }), |
| 71 | ); |
| 72 | }); |