Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

NativeTelemetryClient.test.ts

200 lines7.0 KB

1import type { HostPowerSnapshot } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as DateTime from "effect/DateTime";
4import * as Deferred from "effect/Deferred";
5import * as Effect from "effect/Effect";
6import * as Fiber from "effect/Fiber";
7import * as Ref from "effect/Ref";
8import * as Semaphore from "effect/Semaphore";
9​
10import {
11 canCommandNativeTelemetrySidecar,
12 canRequestNativeTelemetryRetry,
13 commitCollectionControlUpdate,
14 retainRecentNativeTelemetryFailures,
15 resolveNativeSampleIntervalMs,
16 synchronizeCollectionControlOnStart,
17} from "./NativeTelemetryClient.ts";
18​
19const basePower: HostPowerSnapshot = {
20 source: "electron-main",
21 idle: "false",
22 idleSeconds: 0,
23 locked: "false",
24 suspended: false,
25 onBattery: "false",
26 lowPowerMode: "false",
27 thermalState: "nominal",
28 stale: false,
29 updatedAt: DateTime.makeUnsafe("2026-06-17T12:00:00.000Z"),
30};
31​
32describe("resolveNativeSampleIntervalMs", () => {
33 it("keeps a recovery cadence while suspended and backs off under host constraints", () => {
34 expect(resolveNativeSampleIntervalMs({ ...basePower, suspended: true }, 1)).toBe(15_000);
35 expect(resolveNativeSampleIntervalMs({ ...basePower, locked: "true" }, 1)).toBe(15_000);
36 expect(resolveNativeSampleIntervalMs({ ...basePower, lowPowerMode: "true" }, 1)).toBe(15_000);
37 expect(resolveNativeSampleIntervalMs({ ...basePower, thermalState: "critical" }, 1)).toBe(
38 15_000,
39 );
40 expect(resolveNativeSampleIntervalMs({ ...basePower, onBattery: "true" }, 1)).toBe(5_000);
41 });
42​
43 it("slows background telemetry and serves live diagnostics at 1Hz", () => {
44 const unknown: HostPowerSnapshot = {
45 ...basePower,
46 source: "unknown",
47 stale: true,
48 };
49 expect(resolveNativeSampleIntervalMs(unknown, 0)).toBe(5_000);
50 expect(resolveNativeSampleIntervalMs(unknown, 1)).toBe(1_000);
51 expect(
52 resolveNativeSampleIntervalMs(
53 { ...basePower, stale: true, locked: "true", suspended: true },
54 0,
55 ),
56 ).toBe(5_000);
57 expect(resolveNativeSampleIntervalMs(basePower, 0)).toBe(5_000);
58 expect(resolveNativeSampleIntervalMs(basePower, 1)).toBe(1_000);
59 });
60});
61​
62describe("canRequestNativeTelemetryRetry", () => {
63 it("only accepts retry while the supervisor is waiting without a live sidecar", () => {
64 expect(canRequestNativeTelemetryRetry("degraded", false)).toBe(true);
65 expect(canRequestNativeTelemetryRetry("unavailable", false)).toBe(true);
66 expect(canRequestNativeTelemetryRetry("degraded", true)).toBe(false);
67 expect(canRequestNativeTelemetryRetry("healthy", false)).toBe(false);
68 expect(canRequestNativeTelemetryRetry("starting", false)).toBe(false);
69 });
70});
71​
72describe("canCommandNativeTelemetrySidecar", () => {
73 it("keeps on-demand recovery commands available while a live sidecar is degraded", () => {
74 expect(canCommandNativeTelemetrySidecar("healthy", true)).toBe(true);
75 expect(canCommandNativeTelemetrySidecar("degraded", true)).toBe(true);
76 expect(canCommandNativeTelemetrySidecar("unavailable", true)).toBe(false);
77 expect(canCommandNativeTelemetrySidecar("degraded", false)).toBe(false);
78 });
79});
80​
81describe("retainRecentNativeTelemetryFailures", () => {
82 it("expires old failures so an isolated crash restarts from the initial backoff", () => {
83 expect(retainRecentNativeTelemetryFailures([0, 30_000], 90_001)).toEqual([]);
84 expect(retainRecentNativeTelemetryFailures([30_000, 60_000], 90_000)).toEqual([30_000, 60_000]);
85 });
86});
87​
88describe("commitCollectionControlUpdate", () => {
89 it.effect("retains desired demand and retries unapplied sidecar state", () =>
90 Effect.gen(function* () {
91 const initial = {
92 hostPower: basePower,
93 liveSubscriberCount: 0,
94 sampleIntervalMs: 5_000,
95 };
96 const desired = yield* Ref.make(initial);
97 const applied = yield* Ref.make(initial);
98 const failure = new Error("sidecar write failed");
99 const receivedStates: Array<readonly [number, number]> = [];
100​
101 const received = yield* commitCollectionControlUpdate(
102 desired,
103 applied,
104 (current) => ({
105 ...current,
106 liveSubscriberCount: 1,
107 sampleIntervalMs: 1_000,
108 }),
109 (previous, next) => {
110 receivedStates.push([previous.sampleIntervalMs, next.sampleIntervalMs]);
111 return Effect.fail(failure);
112 },
113 ).pipe(Effect.flip);
114​
115 expect(received).toBe(failure);
116 expect(yield* Ref.get(desired)).toEqual({
117 ...initial,
118 liveSubscriberCount: 1,
119 sampleIntervalMs: 1_000,
120 });
121 expect(yield* Ref.get(applied)).toEqual(initial);
122​
123 yield* commitCollectionControlUpdate(
124 desired,
125 applied,
126 (current) => current,
127 (previous, next) => {
128 receivedStates.push([previous.sampleIntervalMs, next.sampleIntervalMs]);
129 return Effect.void;
130 },
131 );
132 expect(receivedStates).toEqual([
133 [5_000, 1_000],
134 [5_000, 1_000],
135 ]);
136 expect(yield* Ref.get(applied)).toEqual(yield* Ref.get(desired));
137 }),
138 );
139​
140 it.effect("serializes startup synchronization with runtime control updates", () =>
141 Effect.gen(function* () {
142 const initial = {
143 hostPower: basePower,
144 liveSubscriberCount: 0,
145 sampleIntervalMs: 5_000,
146 };
147 const desired = yield* Ref.make(initial);
148 const applied = yield* Ref.make(initial);
149 const ready = yield* Ref.make(false);
150 const mutex = yield* Semaphore.make(1);
151 const startupApplying = yield* Deferred.make<void>();
152 const releaseStartup = yield* Deferred.make<void>();
153 const appliedIntervals: Array<number> = [];
154​
155 const startupFiber = yield* synchronizeCollectionControlOnStart(
156 mutex,
157 desired,
158 applied,
159 (control) =>
160 Effect.sync(() => {
161 appliedIntervals.push(control.sampleIntervalMs);
162 }).pipe(
163 Effect.andThen(Deferred.succeed(startupApplying, undefined)),
164 Effect.andThen(Deferred.await(releaseStartup)),
165 Effect.asVoid,
166 ),
167 Ref.set(ready, true),
168 ).pipe(Effect.forkChild);
169 yield* Deferred.await(startupApplying);
170​
171 const updateFiber = yield* mutex
172 .withPermits(1)(
173 commitCollectionControlUpdate(
174 desired,
175 applied,
176 (current) => ({
177 ...current,
178 liveSubscriberCount: 1,
179 sampleIntervalMs: 1_000,
180 }),
181 (_previous, next) =>
182 Effect.gen(function* () {
183 expect(yield* Ref.get(ready)).toBe(true);
184 appliedIntervals.push(next.sampleIntervalMs);
185 }),
186 ),
187 )
188 .pipe(Effect.forkChild);
189 yield* Effect.yieldNow;
190 expect(yield* Ref.get(desired)).toEqual(initial);
191​
192 yield* Deferred.succeed(releaseStartup, undefined);
193 yield* Fiber.join(startupFiber);
194 yield* Fiber.join(updateFiber);
195​
196 expect(appliedIntervals).toEqual([5_000, 1_000]);
197 expect(yield* Ref.get(applied)).toEqual(yield* Ref.get(desired));
198 }),
199 );
200});