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

ResourceTelemetry.test.ts

647 lines23.3 KB

1import type {
2 DesktopHostTelemetrySnapshot,
3 ResourceMonitorProcessSample,
4 ResourceMonitorSnapshotEvent,
5} from "@t3tools/contracts";
6import { describe, expect, it } from "@effect/vitest";
7import * as DateTime from "effect/DateTime";
8import * as Deferred from "effect/Deferred";
9import * as Duration from "effect/Duration";
10import * as Effect from "effect/Effect";
11import * as Fiber from "effect/Fiber";
12import * as Layer from "effect/Layer";
13import * as Option from "effect/Option";
14import * as PubSub from "effect/PubSub";
15import * as Ref from "effect/Ref";
16import * as Stream from "effect/Stream";
17import * as TestClock from "effect/testing/TestClock";
18​
19import * as DesktopTelemetryReceiver from "./DesktopTelemetryReceiver.ts";
20import * as NativeTelemetryClient from "./NativeTelemetryClient.ts";
21import * as ResourceAttribution from "./ResourceAttribution.ts";
22import * as ResourceTelemetry from "./ResourceTelemetry.ts";
23​
24function processSample(
25 input: Partial<ResourceMonitorProcessSample> &
26 Pick<ResourceMonitorProcessSample, "pid" | "ppid" | "startTimeMs">,
27): ResourceMonitorProcessSample {
28 return {
29 runTimeMs: 1_000,
30 name: `process-${input.pid}`,
31 command: `process-${input.pid}`,
32 status: "Running",
33 cpuPercent: 0,
34 cpuTimeMs: 0,
35 residentBytes: 1_024,
36 virtualBytes: 2_048,
37 ioReadBytes: 0,
38 ioWriteBytes: 0,
39 ioSemantics: "storage",
40 ...input,
41 };
43​
44function nativeSnapshot(input: {
45 readonly sequence: number;
46 readonly sampledAtUnixMs: number;
47 readonly childCpuTimeMs: number;
48 readonly childWriteBytes: number;
49 readonly externalProcesses?: ResourceMonitorSnapshotEvent["externalProcesses"];
50}): ResourceMonitorSnapshotEvent {
51 const processes = [
52 processSample({
53 pid: process.pid,
54 ppid: 1,
55 startTimeMs: 100,
56 cpuTimeMs: input.sequence * 10,
57 }),
58 processSample({
59 pid: 4_242,
60 ppid: process.pid,
61 startTimeMs: 200,
62 name: "codex",
63 command: "codex app-server",
64 cpuTimeMs: input.childCpuTimeMs,
65 ioWriteBytes: input.childWriteBytes,
66 }),
67 processSample({
68 pid: 5_000,
69 ppid: 1,
70 startTimeMs: 300,
71 name: "electron",
72 command: "electron",
73 cpuTimeMs: input.sequence * 20,
74 }),
75 processSample({
76 pid: 9_000,
77 ppid: process.pid,
78 startTimeMs: 400,
79 name: "t3-resource-monitor",
80 command: "t3-resource-monitor",
81 cpuTimeMs: input.sequence * 5,
82 }),
83 ];
84 return {
85 version: 3,
86 type: "snapshot",
87 sequence: input.sequence,
88 sampledAtUnixMs: input.sampledAtUnixMs,
89 collectionDurationMicros: 300,
90 scannedProcessCount: 80,
91 retainedProcessCount: processes.length,
92 inaccessibleProcessCount: 1,
93 ...(input.externalProcesses === undefined
94 ? {}
95 : { externalProcesses: input.externalProcesses }),
96 processes,
97 };
99​
100function nativeGeneration(
101 snapshot: ResourceMonitorSnapshotEvent,
102 generation: number,
103): NativeTelemetryClient.NativeTelemetrySnapshot {
104 return { generation, snapshot };
106​
107function desktopSnapshot(sampledAtUnixMs: number): DesktopHostTelemetrySnapshot {
108 const sampledAt = DateTime.makeUnsafe(sampledAtUnixMs);
109 return {
110 version: 1,
111 type: "desktopTelemetry",
112 sequence: 1,
113 sampledAtUnixMs,
114 electronPid: 5_000,
115 power: {
116 source: "electron-main",
117 idle: "false",
118 idleSeconds: 2,
119 locked: "false",
120 suspended: false,
121 onBattery: "true",
122 lowPowerMode: "unknown",
123 thermalState: "fair",
124 stale: false,
125 updatedAt: sampledAt,
126 },
127 speedLimitPercent: Option.some(90),
128 electronProcesses: [
129 {
130 pid: 5_000,
131 creationTimeMs: 300,
132 type: "Browser",
133 name: "electron",
134 cpuPercent: 2,
135 cumulativeCpuSeconds: 0.02,
136 idleWakeupsPerSecond: 3,
137 workingSetBytes: 4_096,
138 peakWorkingSetBytes: 8_192,
139 },
140 ],
141 };
143​
144describe("ResourceTelemetry", () => {
145 it.effect("enables live native and Electron collection only while changes are retained", () =>
146 Effect.gen(function* () {
147 const sampledAtUnixMs = DateTime.toEpochMillis(yield* DateTime.now);
148 const sample = nativeSnapshot({
149 sequence: 1,
150 sampledAtUnixMs,
151 childCpuTimeMs: 100,
152 childWriteBytes: 1_000,
153 });
154 const demandChanges = yield* Ref.make<ReadonlyArray<boolean>>([]);
155 const nativeLayer = NativeTelemetryClient.layerTest({
156 sampleNow: Effect.succeed(nativeGeneration(sample, 0)),
157 health: Effect.succeed({
158 status: "healthy",
159 hello: Option.none(),
160 lastSampleAt: Option.none(),
161 lastError: Option.none(),
162 restartCount: 0,
163 sampleIntervalMs: 1_000,
164 }),
165 });
166 const desktopLayer = DesktopTelemetryReceiver.layerTest({
167 latest: Effect.succeedSome(desktopSnapshot(sampledAtUnixMs)),
168 setDiagnosticsDemand: (enabled) =>
169 Ref.update(demandChanges, (changes) => [...changes, enabled]),
170 });
171 const telemetryLayer = ResourceTelemetry.layer.pipe(
172 Layer.provide(Layer.mergeAll(nativeLayer, desktopLayer, ResourceAttribution.layer)),
173 );
174​
175 const live = yield* Stream.runHead(
176 Effect.gen(function* () {
177 const telemetry = yield* ResourceTelemetry.ResourceTelemetry;
178 return telemetry.changes;
179 }).pipe(Stream.unwrap),
180 ).pipe(Effect.provide(telemetryLayer));
181​
182 expect(Option.isSome(live)).toBe(true);
183 expect(yield* Ref.get(demandChanges)).toEqual([true, false]);
184 }),
185 );
186​
187 it.effect("releases live demand when acquisition is interrupted by an idle collector", () =>
188 Effect.gen(function* () {
189 const sampledAtUnixMs = DateTime.toEpochMillis(yield* DateTime.now);
190 const demandChanges = yield* Ref.make<ReadonlyArray<boolean>>([]);
191 const nativeLayer = NativeTelemetryClient.layerTest({
192 sampleNow: Effect.never,
193 health: Effect.succeed({
194 status: "healthy",
195 hello: Option.none(),
196 lastSampleAt: Option.none(),
197 lastError: Option.none(),
198 restartCount: 0,
199 sampleIntervalMs: 1_000,
200 }),
201 });
202 const desktopLayer = DesktopTelemetryReceiver.layerTest({
203 latest: Effect.succeedSome(desktopSnapshot(sampledAtUnixMs)),
204 setDiagnosticsDemand: (enabled) =>
205 Ref.update(demandChanges, (changes) => [...changes, enabled]),
206 });
207 const telemetryLayer = ResourceTelemetry.layer.pipe(
208 Layer.provide(Layer.mergeAll(nativeLayer, desktopLayer, ResourceAttribution.layer)),
209 );
210​
211 const resultFiber = yield* Stream.runHead(
212 Effect.gen(function* () {
213 const telemetry = yield* ResourceTelemetry.ResourceTelemetry;
214 return telemetry.changes;
215 }).pipe(Stream.unwrap),
216 ).pipe(Effect.timeoutOption("10 millis"), Effect.provide(telemetryLayer), Effect.forkChild);
217 yield* Effect.yieldNow;
218 yield* TestClock.adjust("10 millis");
219 const result = yield* Fiber.join(resultFiber);
220​
221 expect(Option.isNone(result)).toBe(true);
222 expect(yield* Ref.get(demandChanges)).toEqual([true, false]);
223 }),
224 );
225​
226 it.effect(
227 "attributes an initial Electron root from the identity recorded by the native snapshot",
228 () =>
229 Effect.gen(function* () {
230 const sampledAtUnixMs = DateTime.toEpochMillis(yield* DateTime.now);
231 const sample = nativeSnapshot({
232 sequence: 1,
233 sampledAtUnixMs,
234 childCpuTimeMs: 100,
235 childWriteBytes: 1_000,
236 externalProcesses: [{ pid: 5_000, startTimeMs: 300 }],
237 });
238 const desktop = {
239 ...desktopSnapshot(sampledAtUnixMs),
240 electronProcesses: [],
241 };
242 const nativeLayer = NativeTelemetryClient.layerTest({
243 sampleNow: Effect.succeed(nativeGeneration(sample, 0)),
244 health: Effect.succeed({
245 status: "healthy",
246 hello: Option.none(),
247 lastSampleAt: Option.none(),
248 lastError: Option.none(),
249 restartCount: 0,
250 sampleIntervalMs: 1_000,
251 }),
252 });
253 const desktopLayer = DesktopTelemetryReceiver.layerTest({
254 latest: Effect.succeedSome(desktop),
255 });
256 const telemetryLayer = ResourceTelemetry.layer.pipe(
257 Layer.provide(Layer.mergeAll(nativeLayer, desktopLayer, ResourceAttribution.layer)),
258 );
259​
260 const snapshot = yield* Effect.gen(function* () {
261 const telemetry = yield* ResourceTelemetry.ResourceTelemetry;
262 return yield* telemetry.refresh;
263 }).pipe(Effect.provide(telemetryLayer));
264​
265 expect(snapshot.processes.find((entry) => entry.identity.pid === 5_000)?.category).toBe(
266 "electron-main",
267 );
268 expect(snapshot.groups.electron.processStarts).toBe(1);
269 expect(snapshot.groups.backend.processStarts).toBe(3);
270 }),
271 );
272​
273 it.effect("rejects buffered snapshots from an earlier sidecar generation", () =>
274 Effect.scoped(
275 Effect.gen(function* () {
276 const startedAt = DateTime.toEpochMillis(yield* DateTime.now);
277 const stale = nativeSnapshot({
278 sequence: 100,
279 sampledAtUnixMs: startedAt + 1_000,
280 childCpuTimeMs: 100,
281 childWriteBytes: 1_000,
282 });
283 const current = nativeSnapshot({
284 sequence: 1,
285 sampledAtUnixMs: startedAt + 2_000,
286 childCpuTimeMs: 200,
287 childWriteBytes: 2_000,
288 });
289 const nativeSnapshots =
290 yield* PubSub.unbounded<NativeTelemetryClient.NativeTelemetrySnapshot>();
291 const nativeLayer = NativeTelemetryClient.layerTest({
292 snapshots: Stream.fromPubSub(nativeSnapshots),
293 sampleNow: Effect.never,
294 health: Effect.succeed({
295 status: "healthy",
296 hello: Option.none(),
297 lastSampleAt: Option.none(),
298 lastError: Option.none(),
299 restartCount: 1,
300 sampleIntervalMs: 1_000,
301 }),
302 });
303 const telemetryLayer = ResourceTelemetry.layer.pipe(
304 Layer.provide(
305 Layer.mergeAll(
306 nativeLayer,
307 DesktopTelemetryReceiver.layerTest(),
308 ResourceAttribution.layer,
309 ),
310 ),
311 );
312​
313 yield* Effect.gen(function* () {
314 const telemetry = yield* ResourceTelemetry.ResourceTelemetry;
315 const subscription = yield* telemetry.subscribe;
316 const nextSnapshot = yield* Stream.runHead(subscription.changes).pipe(Effect.forkChild);
317​
318 yield* PubSub.publish(nativeSnapshots, nativeGeneration(stale, 0));
319 yield* Effect.yieldNow;
320 expect((yield* telemetry.latest).health.scannedProcessCount).toBe(0);
321​
322 yield* PubSub.publish(nativeSnapshots, nativeGeneration(current, 1));
323 const received = yield* Fiber.join(nextSnapshot);
324​
325 expect(Option.isSome(received)).toBe(true);
326 expect(DateTime.toEpochMillis(Option.getOrThrow(received).readAt)).toBe(
327 current.sampledAtUnixMs,
328 );
329 }).pipe(Effect.provide(telemetryLayer));
330 }),
331 ),
332 );
333​
334 it.effect("retains desktop health changes while aggregate telemetry initializes", () =>
335 Effect.scoped(
336 Effect.gen(function* () {
337 const health = yield* Ref.make<DesktopTelemetryReceiver.DesktopTelemetryReceiverHealth>({
338 status: "starting",
339 lastSampleAt: Option.none(),
340 lastError: Option.none(),
341 });
342 const healthChanges =
343 yield* PubSub.sliding<DesktopTelemetryReceiver.DesktopTelemetryReceiverHealth>(4);
344 const healthSubscribed = yield* Deferred.make<void>();
345 const finishDesktopSnapshot = yield* Deferred.make<void>();
346 const desktopLayer = DesktopTelemetryReceiver.layerTest({
347 health: Ref.get(health),
348 subscribeHealth: Effect.gen(function* () {
349 const subscription = yield* PubSub.subscribe(healthChanges);
350 const latest = yield* Ref.get(health);
351 yield* Deferred.succeed(healthSubscribed, undefined);
352 return {
353 latest,
354 changes: Stream.fromSubscription(subscription),
355 };
356 }),
357 subscribe: Deferred.await(finishDesktopSnapshot).pipe(
358 Effect.as({
359 latest: Option.none(),
360 changes: Stream.empty,
361 }),
362 ),
363 });
364 const telemetryLayer = ResourceTelemetry.layer.pipe(
365 Layer.provide(
366 Layer.mergeAll(
367 NativeTelemetryClient.layerTest(),
368 desktopLayer,
369 ResourceAttribution.layer,
370 ),
371 ),
372 );
373 const resultFiber = yield* Effect.gen(function* () {
374 const telemetry = yield* ResourceTelemetry.ResourceTelemetry;
375 while (true) {
376 const snapshot = yield* telemetry.latest;
377 if (snapshot.health.desktop.status === "degraded") return snapshot;
378 yield* Effect.yieldNow;
379 }
380 }).pipe(Effect.provide(telemetryLayer), Effect.timeout("1 second"), Effect.forkChild);
381​
382 yield* Deferred.await(healthSubscribed);
383 const degraded: DesktopTelemetryReceiver.DesktopTelemetryReceiverHealth = {
384 status: "degraded",
385 lastSampleAt: Option.none(),
386 lastError: Option.some("desktop telemetry failed"),
387 };
388 yield* Ref.set(health, degraded);
389 yield* PubSub.publish(healthChanges, degraded);
390 yield* Deferred.succeed(finishDesktopSnapshot, undefined);
391​
392 const snapshot = yield* Fiber.join(resultFiber);
393 expect(snapshot.health.desktop.status).toBe("degraded");
394 expect(Option.getOrNull(snapshot.health.desktop.lastError)).toBe(
395 "desktop telemetry failed",
396 );
397 }),
398 ),
399 );
400​
401 it.effect("atomically subscribes while a desktop update is being rebuilt", () =>
402 Effect.scoped(
403 Effect.gen(function* () {
404 const startedAt = DateTime.toEpochMillis(yield* DateTime.now);
405 const nextDesktopSnapshot = desktopSnapshot(startedAt + 1_000);
406 const desktopChanges = yield* PubSub.unbounded<DesktopHostTelemetrySnapshot>();
407 const rebuildStarted = yield* Deferred.make<void>();
408 const finishRebuild = yield* Deferred.make<void>();
409 const attributionReads = yield* Ref.make(0);
410 const attributionLayer = Layer.succeed(
411 ResourceAttribution.ResourceAttribution,
412 ResourceAttribution.ResourceAttribution.of({
413 record: () => Effect.void,
414 snapshot: Ref.updateAndGet(attributionReads, (count) => count + 1).pipe(
415 Effect.flatMap((count) =>
416 count === 1
417 ? Effect.void
418 : Deferred.succeed(rebuildStarted, undefined).pipe(
419 Effect.andThen(Deferred.await(finishRebuild)),
420 ),
421 ),
422 Effect.andThen(
423 Effect.succeed({
424 readAt: DateTime.makeUnsafe(startedAt),
425 entries: [],
426 }),
427 ),
428 ),
429 }),
430 );
431 const nativeLayer = NativeTelemetryClient.layerTest({
432 sampleNow: Effect.never,
433 health: Effect.succeed({
434 status: "healthy",
435 hello: Option.none(),
436 lastSampleAt: Option.none(),
437 lastError: Option.none(),
438 restartCount: 0,
439 sampleIntervalMs: 1_000,
440 }),
441 });
442 const desktopLayer = DesktopTelemetryReceiver.layerTest({
443 changes: Stream.fromPubSub(desktopChanges),
444 });
445 const telemetryLayer = ResourceTelemetry.layer.pipe(
446 Layer.provide(Layer.mergeAll(nativeLayer, desktopLayer, attributionLayer)),
447 );
448​
449 yield* Effect.gen(function* () {
450 const telemetry = yield* ResourceTelemetry.ResourceTelemetry;
451 yield* Effect.yieldNow;
452 yield* PubSub.publish(desktopChanges, nextDesktopSnapshot);
453 yield* Deferred.await(rebuildStarted);
454​
455 const subscriptionFiber = yield* telemetry.subscribe.pipe(Effect.forkChild);
456 yield* Effect.yieldNow;
457 yield* Deferred.succeed(finishRebuild, undefined);
458 const subscription = yield* Fiber.join(subscriptionFiber);
459​
460 expect(DateTime.toEpochMillis(subscription.latest.readAt)).toBe(
461 nextDesktopSnapshot.sampledAtUnixMs,
462 );
463 expect(subscription.latest.power).toEqual(nextDesktopSnapshot.power);
464 }).pipe(Effect.provide(telemetryLayer));
465 }),
466 ),
467 );
468​
469 it.effect("combines native, Electron, attribution, retry, and history data", () =>
470 Effect.gen(function* () {
471 const startedAt = DateTime.toEpochMillis(yield* DateTime.now);
472 const samples = [
473 nativeSnapshot({
474 sequence: 1,
475 sampledAtUnixMs: startedAt,
476 childCpuTimeMs: 100,
477 childWriteBytes: 1_000,
478 }),
479 nativeSnapshot({
480 sequence: 2,
481 sampledAtUnixMs: startedAt + 1_000,
482 childCpuTimeMs: 350,
483 childWriteBytes: 5_000,
484 }),
485 nativeSnapshot({
486 sequence: 1,
487 sampledAtUnixMs: startedAt + 2_000,
488 childCpuTimeMs: 500,
489 childWriteBytes: 7_000,
490 }),
491 ] as const;
492 const sampleIndex = yield* Ref.make(0);
493 const externalProcesses = yield* Ref.make<
494 ReadonlyArray<{ readonly pid: number; readonly startTimeMs?: number }>
495 >([]);
496 const retryCount = yield* Ref.make(0);
497 const nativeHealth = yield* Ref.make<NativeTelemetryClient.NativeTelemetryClientHealth>({
498 status: "healthy",
499 hello: Option.some({
500 version: 3,
501 type: "hello",
502 sidecarVersion: "0.1.0",
503 sidecarPid: 9_000,
504 platform: "test",
505 arch: "test",
506 capabilities: {
507 cumulativeCpuTime: true,
508 currentCpuPercent: true,
509 residentMemory: true,
510 virtualMemory: true,
511 ioBytes: true,
512 processStartTime: true,
513 processTree: true,
514 },
515 }),
516 lastSampleAt: Option.some(DateTime.makeUnsafe(startedAt)),
517 lastError: Option.none(),
518 restartCount: 2,
519 sampleIntervalMs: 1_000,
520 });
521 const nativeHealthChanges =
522 yield* PubSub.sliding<NativeTelemetryClient.NativeTelemetryClientHealth>(4);
523 const nativeLayer = NativeTelemetryClient.layerTest({
524 setExternalProcesses: (processes) => Ref.set(externalProcesses, processes),
525 readHistory: () => Effect.succeed(samples.slice(0, 2)),
526 sampleNow: Ref.modify(sampleIndex, (index) => {
527 const sampleIndex = Math.min(index, samples.length - 1);
528 return [nativeGeneration(samples[sampleIndex]!, sampleIndex === 2 ? 3 : 2), index + 1];
529 }),
530 retry: Ref.updateAndGet(retryCount, (count) => count + 1).pipe(Effect.as(true)),
531 health: Ref.get(nativeHealth),
532 subscribeHealth: Effect.gen(function* () {
533 const subscription = yield* PubSub.subscribe(nativeHealthChanges);
534 const latest = yield* Ref.get(nativeHealth);
535 return {
536 latest,
537 changes: Stream.fromSubscription(subscription),
538 };
539 }),
540 });
541 const desktopLayer = DesktopTelemetryReceiver.layerTest({
542 latest: Effect.succeedSome(desktopSnapshot(startedAt)),
543 health: Effect.succeed({
544 status: "healthy",
545 lastSampleAt: Option.some(DateTime.makeUnsafe(startedAt)),
546 lastError: Option.none(),
547 }),
548 });
549 const attributionLayer = ResourceAttribution.layer;
550 const dependencies = Layer.mergeAll(nativeLayer, desktopLayer, attributionLayer);
551 const telemetryLayer = ResourceTelemetry.layer.pipe(Layer.provide(dependencies));
552 const layer = Layer.mergeAll(dependencies, telemetryLayer);
553​
554 yield* Effect.gen(function* () {
555 const telemetry = yield* ResourceTelemetry.ResourceTelemetry;
556 const attribution = yield* ResourceAttribution.ResourceAttribution;
557​
558 expect(yield* Ref.get(externalProcesses)).toEqual([{ pid: 5_000, startTimeMs: 300 }]);
559​
560 yield* attribution.record({
561 component: "provider-event-log",
562 operation: "append",
563 logicalWriteBytes: 512,
564 count: 2,
565 durationMs: 4,
566 });
567 const first = yield* telemetry.refresh;
568 expect(first.groups.backend.processCount).toBe(2);
569 expect(first.groups.electron.processCount).toBe(1);
570 expect(first.groups.monitor.processCount).toBe(1);
571 expect(first.power.onBattery).toBe("true");
572 expect(Option.getOrNull(first.speedLimitPercent)).toBe(90);
573 expect(first.attribution.entries).toEqual([
574 {
575 component: "provider-event-log",
576 operation: "append",
577 logicalReadBytes: 0,
578 logicalWriteBytes: 512,
579 count: 2,
580 durationMs: 4,
581 },
582 ]);
583​
584 yield* TestClock.adjust(Duration.seconds(1));
585 const second = yield* telemetry.refresh;
586 const codex = second.processes.find((entry) => entry.identity.pid === 4_242);
587 expect(codex?.cpuPercent).toBe(25);
588 expect(codex?.ioWriteBytesPerSecond).toBe(4_000);
589 expect(second.groups.backend.ioWriteBytes).toBe(4_000);
590 expect(second.health.collectionDurationMicros).toBe(300);
591 expect(second.health.scannedProcessCount).toBe(80);
592 expect(second.health.inaccessibleProcessCount).toBe(1);
593​
594 const history = yield* telemetry.readHistory({
595 windowMs: 60_000,
596 bucketMs: 10_000,
597 });
598 expect(history.retainedSampleCount).toBeGreaterThan(0);
599 expect(
600 history.topProcesses.find((entry) => entry.identity.pid === 4_242)?.sampleCount,
601 ).toBe(2);
602 expect(history.topProcesses.find((entry) => entry.identity.pid === 4_242)?.cpuTimeMs).toBe(
603 250,
604 );
605 expect(
606 history.topProcesses.find((entry) => entry.identity.pid === 4_242)?.ioWriteBytes,
607 ).toBe(4_000);
608 expect(history.buckets.reduce((total, bucket) => total + bucket.ioWriteBytes, 0)).toBe(
609 4_000,
610 );
611​
612 const retry = yield* telemetry.retry;
613 expect(retry.accepted).toBe(true);
614 expect(yield* Ref.get(retryCount)).toBe(1);
615​
616 yield* Ref.update(nativeHealth, (current) => ({
617 ...current,
618 hello: Option.map(current.hello, (hello) => ({
619 ...hello,
620 sidecarPid: 9_001,
621 })),
622 restartCount: 3,
623 }));
624 yield* TestClock.adjust(Duration.seconds(1));
625 const restarted = yield* telemetry.refresh;
626 expect(DateTime.toEpochMillis(restarted.readAt)).toBe(startedAt + 2_000);
627 expect(Option.getOrNull(restarted.health.sidecarPid)).toBe(9_001);
628​
629 yield* Ref.update(nativeHealth, (current) => ({
630 ...current,
631 status: "degraded" as const,
632 lastError: Option.some("collector exited"),
633 }));
634 yield* PubSub.publish(nativeHealthChanges, yield* Ref.get(nativeHealth));
635 yield* Effect.yieldNow;
636 const healthUpdate = yield* telemetry.latest;
637 expect(healthUpdate.health.native.status).toBe("degraded");
638 expect(Option.getOrNull(healthUpdate.health.native.lastError)).toBe("collector exited");
639 const degradedHistory = yield* telemetry.readHistory({
640 windowMs: 60_000,
641 bucketMs: 10_000,
642 });
643 expect(degradedHistory.health.native.status).toBe("degraded");
644 }).pipe(Effect.provide(layer));
645 }),
646 );
647});