Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 191 additions & 0 deletions apps/server/src/background/BackgroundPolicy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
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),
setDemandActive: () => Effect.void,
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" },
),
),
),
);
});
Loading
Loading