import { assert, describe, it } from "@effect/vitest"; import { AuthSessionId, RpcClientId, type HostPowerSnapshot, type ClientActivityReportInput, } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; import * as PubSub from "effect/PubSub"; import * as Ref from "effect/Ref"; import * as Stream from "effect/Stream"; import { ServerSettingsService } from "../serverSettings.ts"; import * as BackgroundPolicy from "./BackgroundPolicy.ts"; import * as HostPowerMonitor from "./HostPowerMonitor.ts"; const TEST_NOW = DateTime.makeUnsafe("2026-05-13T00:00:00.000Z"); const nominalHostPower: HostPowerSnapshot = { source: "unknown", idle: "unknown", idleSeconds: null, locked: "unknown", suspended: false, onBattery: "unknown", lowPowerMode: "unknown", thermalState: "unknown", stale: true, updatedAt: TEST_NOW, }; const constrainedHostPower: HostPowerSnapshot = { ...nominalHostPower, lowPowerMode: "true", stale: false, }; function makeReport(overrides: Partial = {}): ClientActivityReportInput { return { clientId: "client-1", clientKind: "web", visible: true, focused: true, recentlyInteracted: true, scopes: [{ type: "vcs-status", cwd: "/repo" }], ttlMs: 45_000, observedAt: TEST_NOW, ...overrides, }; } function makeLayer( hostPower: HostPowerSnapshot, settingsOverrides: Parameters[0] = {}, ) { const hostLayer = Layer.effect( HostPowerMonitor.HostPowerMonitor, Effect.gen(function* () { const changes = yield* PubSub.sliding(1); let snapshot = hostPower; return HostPowerMonitor.HostPowerMonitor.of({ snapshot: Effect.sync(() => snapshot), report: (next) => Effect.sync(() => { snapshot = next; }).pipe(Effect.andThen(PubSub.publish(changes, next)), Effect.asVoid), streamChanges: Stream.fromPubSub(changes), }); }), ); return BackgroundPolicy.layer.pipe( Layer.provide(Layer.merge(hostLayer, ServerSettingsService.layerTest(settingsOverrides))), ); } describe("BackgroundPolicy", () => { it.effect("records foreground scoped client demand", () => Effect.gen(function* () { const policy = yield* BackgroundPolicy.BackgroundPolicy; yield* policy.reportClientActivity( AuthSessionId.make("session-1"), RpcClientId.make(1), makeReport(), ); const snapshot = yield* policy.snapshot; assert.equal(snapshot.activeForegroundLeaseCount, 1); assert.deepStrictEqual(snapshot.activeScopeKeys, ["vcs-status:/repo"]); assert.equal(snapshot.shouldRunOpportunisticWork, true); assert.equal(yield* policy.hasDemand({ type: "vcs-status", cwd: "/repo" }), true); assert.equal(yield* policy.hasDemand({ type: "vcs-status", cwd: "/other" }), false); assert.equal(yield* policy.shouldRunScopeWork({ type: "vcs-status", cwd: "/repo" }), true); assert.equal(yield* policy.shouldRunScopeWork({ type: "vcs-status", cwd: "/other" }), false); }).pipe(Effect.provide(makeLayer(nominalHostPower))), ); it.effect("removes all leases for a disconnected websocket connection", () => Effect.gen(function* () { const policy = yield* BackgroundPolicy.BackgroundPolicy; yield* policy.reportClientActivity( AuthSessionId.make("session-1"), RpcClientId.make(1), makeReport(), ); yield* policy.removeRpcClient(AuthSessionId.make("session-1"), RpcClientId.make(1)); const snapshot = yield* policy.snapshot; assert.equal(snapshot.activeForegroundLeaseCount, 0); assert.deepStrictEqual(snapshot.activeScopeKeys, []); assert.equal(snapshot.shouldRunOpportunisticWork, false); }).pipe(Effect.provide(makeLayer(nominalHostPower))), ); it.effect("keeps leases from another session when rpc client ids are reused", () => Effect.gen(function* () { const policy = yield* BackgroundPolicy.BackgroundPolicy; const rpcClientId = RpcClientId.make(1); yield* policy.reportClientActivity( AuthSessionId.make("session-1"), rpcClientId, makeReport({ clientId: "client-1" }), ); yield* policy.reportClientActivity( AuthSessionId.make("session-2"), rpcClientId, makeReport({ clientId: "client-2" }), ); yield* policy.removeRpcClient(AuthSessionId.make("session-1"), rpcClientId); const snapshot = yield* policy.snapshot; assert.equal(snapshot.activeForegroundLeaseCount, 1); assert.equal(snapshot.leases[0]?.sessionId, AuthSessionId.make("session-2")); assert.equal(snapshot.leases[0]?.clientId, "client-2"); }).pipe(Effect.provide(makeLayer(nominalHostPower))), ); it.effect("serializes lease mutation publications", () => Effect.gen(function* () { const firstSnapshotStarted = yield* Deferred.make(); const releaseFirstSnapshot = yield* Deferred.make(); const snapshotReads = yield* Ref.make(0); const hostLayer = Layer.succeed( HostPowerMonitor.HostPowerMonitor, HostPowerMonitor.HostPowerMonitor.of({ snapshot: Ref.updateAndGet(snapshotReads, (count) => count + 1).pipe( Effect.flatMap((count) => count === 1 ? Deferred.succeed(firstSnapshotStarted, undefined).pipe( Effect.andThen(Deferred.await(releaseFirstSnapshot)), Effect.as(nominalHostPower), ) : Effect.succeed(nominalHostPower), ), ), report: () => Effect.void, streamChanges: Stream.empty, }), ); const layer = BackgroundPolicy.layer.pipe( Layer.provide(Layer.merge(hostLayer, ServerSettingsService.layerTest())), ); yield* Effect.gen(function* () { const policy = yield* BackgroundPolicy.BackgroundPolicy; const updatesFiber = yield* policy.streamChanges.pipe( Stream.take(2), Stream.runCollect, Effect.forkChild, ); yield* Effect.yieldNow; const reportFiber = yield* policy .reportClientActivity(AuthSessionId.make("session-1"), RpcClientId.make(1), makeReport()) .pipe(Effect.forkChild); yield* Deferred.await(firstSnapshotStarted); const removeFiber = yield* policy .removeRpcClient(AuthSessionId.make("session-1"), RpcClientId.make(1)) .pipe(Effect.forkChild); yield* Effect.yieldNow; yield* Deferred.succeed(releaseFirstSnapshot, undefined); yield* Fiber.join(reportFiber); yield* Fiber.join(removeFiber); const updates = Array.from(yield* Fiber.join(updatesFiber)); assert.equal(updates.at(-1)?.activeForegroundLeaseCount, 0); assert.deepStrictEqual(updates.at(-1)?.activeScopeKeys, []); }).pipe(Effect.provide(layer)); }), ); it.effect("bounds client-id churn for one websocket connection", () => Effect.gen(function* () { const policy = yield* BackgroundPolicy.BackgroundPolicy; const sessionId = AuthSessionId.make("session-1"); const rpcClientId = RpcClientId.make(1); for ( let index = 0; index <= BackgroundPolicy.MAX_CLIENT_ACTIVITY_LEASES_PER_RPC_CLIENT; index += 1 ) { yield* policy.reportClientActivity( sessionId, rpcClientId, makeReport({ clientId: `client-${index}` }), ); } const snapshot = yield* policy.snapshot; assert.equal( snapshot.leases.length, BackgroundPolicy.MAX_CLIENT_ACTIVITY_LEASES_PER_RPC_CLIENT, ); assert.isTrue( snapshot.leases.some( (lease) => lease.clientId === `client-${BackgroundPolicy.MAX_CLIENT_ACTIVITY_LEASES_PER_RPC_CLIENT}`, ), ); }).pipe(Effect.provide(makeLayer(nominalHostPower))), ); it.effect("host low power mode disables opportunistic work without dropping scoped demand", () => Effect.gen(function* () { const policy = yield* BackgroundPolicy.BackgroundPolicy; yield* policy.reportClientActivity( AuthSessionId.make("session-1"), RpcClientId.make(1), makeReport(), ); const snapshot = yield* policy.snapshot; assert.equal(snapshot.activeForegroundLeaseCount, 1); assert.deepStrictEqual(snapshot.activeScopeKeys, ["vcs-status:/repo"]); assert.equal(snapshot.shouldRunOpportunisticWork, false); assert.equal(yield* policy.hasDemand({ type: "vcs-status", cwd: "/repo" }), true); assert.equal(yield* policy.shouldRunScopeWork({ type: "vcs-status", cwd: "/repo" }), false); }).pipe(Effect.provide(makeLayer(constrainedHostPower))), ); it.effect("host suspension disables scoped and opportunistic work", () => Effect.gen(function* () { const policy = yield* BackgroundPolicy.BackgroundPolicy; yield* policy.reportClientActivity( AuthSessionId.make("session-1"), RpcClientId.make(1), makeReport(), ); const snapshot = yield* policy.snapshot; assert.equal(snapshot.activeForegroundLeaseCount, 1); assert.equal(snapshot.shouldRunOpportunisticWork, false); assert.equal(yield* policy.hasDemand({ type: "vcs-status", cwd: "/repo" }), true); assert.equal(yield* policy.shouldRunScopeWork({ type: "vcs-status", cwd: "/repo" }), false); }).pipe( Effect.provide( makeLayer({ ...nominalHostPower, suspended: true, stale: false, }), ), ), ); it.effect("keeps background demand visible while preventing scoped work", () => Effect.gen(function* () { const policy = yield* BackgroundPolicy.BackgroundPolicy; yield* policy.reportClientActivity( AuthSessionId.make("session-1"), RpcClientId.make(1), makeReport({ focused: false, visible: false }), ); const snapshot = yield* policy.snapshot; assert.equal(snapshot.activeForegroundLeaseCount, 0); assert.deepStrictEqual(snapshot.activeScopeKeys, ["vcs-status:/repo"]); assert.equal(yield* policy.hasDemand({ type: "vcs-status", cwd: "/repo" }), true); assert.equal(yield* policy.shouldRunScopeWork({ type: "vcs-status", cwd: "/repo" }), false); }).pipe(Effect.provide(makeLayer(nominalHostPower))), ); it.effect("keeps scoped work active after a recently used visible window loses focus", () => Effect.gen(function* () { const policy = yield* BackgroundPolicy.BackgroundPolicy; yield* policy.reportClientActivity( AuthSessionId.make("session-1"), RpcClientId.make(1), makeReport({ focused: false, recentlyInteracted: true }), ); const snapshot = yield* policy.snapshot; assert.equal(snapshot.activeForegroundLeaseCount, 1); assert.equal(snapshot.shouldRunOpportunisticWork, true); assert.equal(yield* policy.shouldRunScopeWork({ type: "vcs-status", cwd: "/repo" }), true); }).pipe(Effect.provide(makeLayer(nominalHostPower))), ); it.effect("pauses scoped work for a visible unfocused client without recent interaction", () => Effect.gen(function* () { const policy = yield* BackgroundPolicy.BackgroundPolicy; yield* policy.reportClientActivity( AuthSessionId.make("session-1"), RpcClientId.make(1), makeReport({ focused: false, recentlyInteracted: false }), ); const snapshot = yield* policy.snapshot; assert.equal(snapshot.activeForegroundLeaseCount, 0); assert.equal(snapshot.shouldRunOpportunisticWork, false); assert.equal(yield* policy.shouldRunScopeWork({ type: "vcs-status", cwd: "/repo" }), false); }).pipe(Effect.provide(makeLayer(nominalHostPower))), ); it.effect( "performance profile allows background scoped work while a scoped lease is active", () => Effect.gen(function* () { const policy = yield* BackgroundPolicy.BackgroundPolicy; yield* policy.reportClientActivity( AuthSessionId.make("session-1"), RpcClientId.make(1), makeReport({ focused: false, visible: false }), ); assert.equal(yield* policy.shouldRunScopeWork({ type: "vcs-status", cwd: "/repo" }), true); }).pipe( Effect.provide(makeLayer(nominalHostPower, { backgroundActivityProfile: "performance" })), ), ); it.effect("battery saver profile pauses scoped work on battery", () => Effect.gen(function* () { const policy = yield* BackgroundPolicy.BackgroundPolicy; yield* policy.reportClientActivity( AuthSessionId.make("session-1"), RpcClientId.make(1), makeReport(), ); assert.equal(yield* policy.shouldRunScopeWork({ type: "vcs-status", cwd: "/repo" }), false); }).pipe( Effect.provide( makeLayer( { ...nominalHostPower, onBattery: "true", stale: false, }, { backgroundActivityProfile: "battery-saver" }, ), ), ), ); it.effect("does not gate work on stale host power values", () => Effect.gen(function* () { const policy = yield* BackgroundPolicy.BackgroundPolicy; yield* policy.reportClientActivity( AuthSessionId.make("session-1"), RpcClientId.make(1), makeReport(), ); assert.equal(yield* policy.shouldRunScopeWork({ type: "vcs-status", cwd: "/repo" }), true); }).pipe( Effect.provide( makeLayer( { ...nominalHostPower, locked: "true", onBattery: "true", lowPowerMode: "true", thermalState: "critical", stale: true, }, { backgroundActivityProfile: "battery-saver" }, ), ), ), ); });