@@ -14,6 +14,7 @@ import {
1414 WS_METHODS ,
1515} from "@t3tools/contracts" ;
1616import { describe , expect , it } from "@effect/vitest" ;
17+ import { vi } from "vite-plus/test" ;
1718import * as Cause from "effect/Cause" ;
1819import * as Deferred from "effect/Deferred" ;
1920import * as Duration from "effect/Duration" ;
@@ -36,6 +37,7 @@ import {
3637 AVAILABLE_CONNECTION_STATE ,
3738 PrimaryConnectionTarget ,
3839 type PreparedConnection ,
40+ type SupervisorConnectionState ,
3941} from "../connection/model.ts" ;
4042import * as EnvironmentRegistry from "../connection/registry.ts" ;
4143import * as EnvironmentSupervisor from "../connection/supervisor.ts" ;
@@ -1394,3 +1396,84 @@ describe("provider setup commands", () => {
13941396 } ) . pipe ( Effect . scoped ) ,
13951397 ) ;
13961398} ) ;
1399+
1400+ describe ( "resource telemetry history" , ( ) => {
1401+ it . effect ( "refreshes an initially empty history while mounted and stops after unmount" , ( ) =>
1402+ Effect . gen ( function * ( ) {
1403+ vi . useFakeTimers ( ) ;
1404+ try {
1405+ yield * Effect . scoped (
1406+ Effect . gen ( function * ( ) {
1407+ let reads = 0 ;
1408+ const client = {
1409+ [ WS_METHODS . serverGetResourceTelemetryHistory ] : ( ) =>
1410+ Effect . sync ( ( ) => ( {
1411+ retainedSampleCount : reads ++ ,
1412+ } ) ) ,
1413+ } as unknown as WsRpcProtocolClient ;
1414+ const supervisor = EnvironmentSupervisor . EnvironmentSupervisor . of ( {
1415+ target : TARGET ,
1416+ state : yield * SubscriptionRef . make < SupervisorConnectionState > ( {
1417+ ...AVAILABLE_CONNECTION_STATE ,
1418+ phase : "connected" ,
1419+ } ) ,
1420+ session : yield * SubscriptionRef . make ( Option . some ( session ( client ) ) ) ,
1421+ prepared : yield * SubscriptionRef . make ( Option . none < PreparedConnection > ( ) ) ,
1422+ connect : Effect . void ,
1423+ disconnect : Effect . void ,
1424+ retryNow : Effect . void ,
1425+ } ) ;
1426+ const environments = EnvironmentRegistry . EnvironmentRegistry . of ( {
1427+ run : ( _id , effect ) =>
1428+ Effect . provideService (
1429+ effect ,
1430+ EnvironmentSupervisor . EnvironmentSupervisor ,
1431+ supervisor ,
1432+ ) ,
1433+ followStream : ( _id , stream ) =>
1434+ Stream . provideService (
1435+ stream ,
1436+ EnvironmentSupervisor . EnvironmentSupervisor ,
1437+ supervisor ,
1438+ ) ,
1439+ } as EnvironmentRegistry . EnvironmentRegistry [ "Service" ] ) ;
1440+ const runtime = Atom . runtime (
1441+ Layer . merge (
1442+ Layer . succeed ( EnvironmentRegistry . EnvironmentRegistry , environments ) ,
1443+ Layer . succeed ( Persistence . EnvironmentCacheStore , {
1444+ loadServerConfig : ( ) => Effect . succeedNone ,
1445+ } as unknown as Persistence . EnvironmentCacheStore [ "Service" ] ) ,
1446+ ) ,
1447+ ) ;
1448+ const atoms = createServerEnvironmentAtoms ( runtime , {
1449+ initialConfigValueAtom : ( ) => Atom . make ( CONFIG ) ,
1450+ } ) ;
1451+ const registry = yield * Effect . acquireRelease ( Effect . sync ( AtomRegistry . make ) , ( value ) =>
1452+ Effect . sync ( ( ) => value . dispose ( ) ) ,
1453+ ) ;
1454+ const history = atoms . resourceTelemetryHistory ( {
1455+ environmentId : TARGET . environmentId ,
1456+ input : { windowMs : 900_000 , bucketMs : 30_000 } ,
1457+ } ) ;
1458+ const unmount = registry . mount ( history ) ;
1459+ const initial = yield * AtomRegistry . getResult ( registry , history , {
1460+ suspendOnWaiting : true ,
1461+ } ) ;
1462+ expect ( initial . retainedSampleCount ) . toBe ( 0 ) ;
1463+ yield * Effect . promise ( ( ) => vi . advanceTimersByTimeAsync ( 5_000 ) ) ;
1464+ expect ( reads ) . toBe ( 2 ) ;
1465+ const updated = yield * AtomRegistry . getResult ( registry , history , {
1466+ suspendOnWaiting : true ,
1467+ } ) ;
1468+ expect ( updated . retainedSampleCount ) . toBe ( 1 ) ;
1469+ unmount ( ) ;
1470+ yield * Effect . promise ( ( ) => vi . advanceTimersByTimeAsync ( 10_000 ) ) ;
1471+ expect ( reads ) . toBe ( 2 ) ;
1472+ } ) ,
1473+ ) ;
1474+ } finally {
1475+ vi . useRealTimers ( ) ;
1476+ }
1477+ } ) ,
1478+ ) ;
1479+ } ) ;
0 commit comments