Open History to see the last commit
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 Duration from "effect/Duration";
6import * as TestClock from "effect/testing/TestClock";
7import * as Effect from "effect/Effect";
8import * as Fiber from "effect/Fiber";
9import * as Ref from "effect/Ref";
10import * as Semaphore from "effect/Semaphore";
11
12import {
13 requestProcessTable,
14 commitCollectionControlUpdate,
15 synchronizeCollectionControlOnStart,
16} from "./NativeTelemetryClient.ts";
17
18const basePower: HostPowerSnapshot = {
19 source: "electron-main",
20 idle: "false",
21 idleSeconds: 0,
22 locked: "false",
23 suspended: false,
24 onBattery: "false",
25 lowPowerMode: "false",
26 thermalState: "nominal",
27 stale: false,
28 updatedAt: DateTime.makeUnsafe("2026-06-17T12:00:00.000Z"),
29};
30
31describe("commitCollectionControlUpdate", () => {
32 it.effect("retains desired demand and retries unapplied sidecar state", () =>
33 Effect.gen(function* () {
34 const initial = {
35 hostPower: basePower,
36 liveSubscriberCount: 0,
37 sampleIntervalMs: 5_000,
38 };
39 const desired = yield* Ref.make(initial);
40 const applied = yield* Ref.make(initial);
41 const failure = new Error("sidecar write failed");
42 const receivedStates: Array<readonly [number, number]> = [];
43
44 const received = yield* commitCollectionControlUpdate(
45 desired,
46 applied,
47 (current) => ({
48 ...current,
49 liveSubscriberCount: 1,
50 sampleIntervalMs: 1_000,
51 }),
52 (previous, next) => {
53 receivedStates.push([previous.sampleIntervalMs, next.sampleIntervalMs]);
54 return Effect.fail(failure);
55 },
56 ).pipe(Effect.flip);
57
58 expect(received).toBe(failure);
59 expect(yield* Ref.get(desired)).toEqual({
60 ...initial,
61 liveSubscriberCount: 1,
62 sampleIntervalMs: 1_000,
63 });
64 expect(yield* Ref.get(applied)).toEqual(initial);
65
66 yield* commitCollectionControlUpdate(
67 desired,
68 applied,
69 (current) => current,
70 (previous, next) => {
71 receivedStates.push([previous.sampleIntervalMs, next.sampleIntervalMs]);
72 return Effect.void;
73 },
74 );
75 expect(receivedStates).toEqual([
76 [5_000, 1_000],
77 [5_000, 1_000],
78 ]);
79 expect(yield* Ref.get(applied)).toEqual(yield* Ref.get(desired));
80 }),
81 );
82
83 it.effect("serializes startup synchronization with runtime control updates", () =>
84 Effect.gen(function* () {
85 const initial = {
86 hostPower: basePower,
87 liveSubscriberCount: 0,
88 sampleIntervalMs: 5_000,
89 };
90 const desired = yield* Ref.make(initial);
91 const applied = yield* Ref.make(initial);
92 const ready = yield* Ref.make(false);
93 const mutex = yield* Semaphore.make(1);
94 const startupApplying = yield* Deferred.make<void>();
95 const releaseStartup = yield* Deferred.make<void>();
96 const appliedIntervals: Array<number> = [];
97
98 const startupFiber = yield* synchronizeCollectionControlOnStart(
99 mutex,
100 desired,
101 applied,
102 (control) =>
103 Effect.sync(() => {
104 appliedIntervals.push(control.sampleIntervalMs);
105 }).pipe(
106 Effect.andThen(Deferred.succeed(startupApplying, undefined)),
107 Effect.andThen(Deferred.await(releaseStartup)),
108 Effect.asVoid,
109 ),
110 Ref.set(ready, true),
111 ).pipe(Effect.forkChild);
112 yield* Deferred.await(startupApplying);
113
114 const updateFiber = yield* mutex
115 .withPermits(1)(
116 commitCollectionControlUpdate(
117 desired,
118 applied,
119 (current) => ({
120 ...current,
121 liveSubscriberCount: 1,
122 sampleIntervalMs: 1_000,
123 }),
124 (_previous, next) =>
125 Effect.gen(function* () {
126 expect(yield* Ref.get(ready)).toBe(true);
127 appliedIntervals.push(next.sampleIntervalMs);
128 }),
129 ),
130 )
131 .pipe(Effect.forkChild);
132 yield* Effect.yieldNow;
133 expect(yield* Ref.get(desired)).toEqual(initial);
134
135 yield* Deferred.succeed(releaseStartup, undefined);
136 yield* Fiber.join(startupFiber);
137 yield* Fiber.join(updateFiber);
138
139 expect(appliedIntervals).toEqual([5_000, 1_000]);
140 expect(yield* Ref.get(applied)).toEqual(yield* Ref.get(desired));
141 }),
142 );
143});
144
145it.effect("bounds a process-table write that outlasts the deadline", () =>
146 Effect.gen(function* () {
147 const response = yield* Deferred.make<void>();
148 const writing = yield* Deferred.make<void>();
149 const fiber = yield* requestProcessTable(
150 Deferred.succeed(writing, undefined).pipe(Effect.andThen(Effect.sleep(300))),
151 response,
152 Duration.millis(250),
153 ).pipe(Effect.flip, Effect.forkChild);
154 yield* Deferred.await(writing);
155 yield* TestClock.adjust(250);
156 expect(fiber.pollUnsafe()?._tag).toBe("Success");
157 expect((yield* Fiber.join(fiber)).timeoutMs).toBe(250);
158 }),
159);