217 lines · 7.4 KB · TypeScript
RawDownload
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 Effect from "effect/Effect";import * as Layer from "effect/Layer";import * as PubSub from "effect/PubSub";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> = {}): 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<typeof ServerSettingsService.layerTest>[0] = {},) {  const hostLayer = Layer.effect(    HostPowerMonitor.HostPowerMonitor,    Effect.gen(function* () {      const changes = yield* PubSub.sliding<HostPowerSnapshot>(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(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("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("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(    "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" },        ),      ),    ),  );});