Repository navigation
Expand file tree
/
Copy pathNativeTelemetryClient.test.ts
More file actions
140 lines (129 loc) · 4.36 KB
/
Copy pathNativeTelemetryClient.test.ts
File metadata and controls
140 lines (129 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import type { HostPowerSnapshot } from "@t3tools/contracts";
import { describe, expect, it } from "@effect/vitest";
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 Ref from "effect/Ref";
import * as Semaphore from "effect/Semaphore";
import {
commitCollectionControlUpdate,
synchronizeCollectionControlOnStart,
} from "./NativeTelemetryClient.ts";
const basePower: HostPowerSnapshot = {
source: "electron-main",
idle: "false",
idleSeconds: 0,
locked: "false",
suspended: false,
onBattery: "false",
lowPowerMode: "false",
thermalState: "nominal",
stale: false,
updatedAt: DateTime.makeUnsafe("2026-06-17T12:00:00.000Z"),
};
describe("commitCollectionControlUpdate", () => {
it.effect("retains desired demand and retries unapplied sidecar state", () =>
Effect.gen(function* () {
const initial = {
hostPower: basePower,
liveSubscriberCount: 0,
sampleIntervalMs: 5_000,
};
const desired = yield* Ref.make(initial);
const applied = yield* Ref.make(initial);
const failure = new Error("sidecar write failed");
const receivedStates: Array<readonly [number, number]> = [];
const received = yield* commitCollectionControlUpdate(
desired,
applied,
(current) => ({
...current,
liveSubscriberCount: 1,
sampleIntervalMs: 1_000,
}),
(previous, next) => {
receivedStates.push([previous.sampleIntervalMs, next.sampleIntervalMs]);
return Effect.fail(failure);
},
).pipe(Effect.flip);
expect(received).toBe(failure);
expect(yield* Ref.get(desired)).toEqual({
...initial,
liveSubscriberCount: 1,
sampleIntervalMs: 1_000,
});
expect(yield* Ref.get(applied)).toEqual(initial);
yield* commitCollectionControlUpdate(
desired,
applied,
(current) => current,
(previous, next) => {
receivedStates.push([previous.sampleIntervalMs, next.sampleIntervalMs]);
return Effect.void;
},
);
expect(receivedStates).toEqual([
[5_000, 1_000],
[5_000, 1_000],
]);
expect(yield* Ref.get(applied)).toEqual(yield* Ref.get(desired));
}),
);
it.effect("serializes startup synchronization with runtime control updates", () =>
Effect.gen(function* () {
const initial = {
hostPower: basePower,
liveSubscriberCount: 0,
sampleIntervalMs: 5_000,
};
const desired = yield* Ref.make(initial);
const applied = yield* Ref.make(initial);
const ready = yield* Ref.make(false);
const mutex = yield* Semaphore.make(1);
const startupApplying = yield* Deferred.make<void>();
const releaseStartup = yield* Deferred.make<void>();
const appliedIntervals: Array<number> = [];
const startupFiber = yield* synchronizeCollectionControlOnStart(
mutex,
desired,
applied,
(control) =>
Effect.sync(() => {
appliedIntervals.push(control.sampleIntervalMs);
}).pipe(
Effect.andThen(Deferred.succeed(startupApplying, undefined)),
Effect.andThen(Deferred.await(releaseStartup)),
Effect.asVoid,
),
Ref.set(ready, true),
).pipe(Effect.forkChild);
yield* Deferred.await(startupApplying);
const updateFiber = yield* mutex
.withPermits(1)(
commitCollectionControlUpdate(
desired,
applied,
(current) => ({
...current,
liveSubscriberCount: 1,
sampleIntervalMs: 1_000,
}),
(_previous, next) =>
Effect.gen(function* () {
expect(yield* Ref.get(ready)).toBe(true);
appliedIntervals.push(next.sampleIntervalMs);
}),
),
)
.pipe(Effect.forkChild);
yield* Effect.yieldNow;
expect(yield* Ref.get(desired)).toEqual(initial);
yield* Deferred.succeed(releaseStartup, undefined);
yield* Fiber.join(startupFiber);
yield* Fiber.join(updateFiber);
expect(appliedIntervals).toEqual([5_000, 1_000]);
expect(yield* Ref.get(applied)).toEqual(yield* Ref.get(desired));
}),
);
});