Files
t3code/packages/client-runtime/src/environmentRuntimeState.test.ts
T
2026-06-03 16:16:53 -07:00

76 lines
1.9 KiB
TypeScript

import { AtomRegistry } from "effect/unstable/reactivity";
import { EnvironmentId } from "@t3tools/contracts";
import { afterEach, describe, expect, it } from "vite-plus/test";
import { createEnvironmentRuntimeManager } from "./environmentRuntimeState.ts";
let atomRegistry = AtomRegistry.make();
function resetAtomRegistry() {
atomRegistry.dispose();
atomRegistry = AtomRegistry.make();
}
const TARGET = { environmentId: EnvironmentId.make("env-local") } as const;
describe("createEnvironmentRuntimeManager", () => {
afterEach(() => {
resetAtomRegistry();
});
it("stores state per environment", () => {
const manager = createEnvironmentRuntimeManager({
getRegistry: () => atomRegistry,
});
manager.setState(TARGET, {
connectionState: "ready",
connectionError: null,
serverConfig: null,
});
expect(manager.getSnapshot(TARGET)).toEqual({
connectionState: "ready",
connectionError: null,
serverConfig: null,
});
});
it("patches the current state", () => {
const manager = createEnvironmentRuntimeManager({
getRegistry: () => atomRegistry,
});
manager.patch(TARGET, (current) => ({
...current,
connectionState: "disconnected",
connectionError: "Socket closed.",
}));
expect(manager.getSnapshot(TARGET)).toEqual({
connectionState: "disconnected",
connectionError: "Socket closed.",
serverConfig: null,
});
});
it("invalidates a single environment", () => {
const manager = createEnvironmentRuntimeManager({
getRegistry: () => atomRegistry,
});
manager.setState(TARGET, {
connectionState: "ready",
connectionError: null,
serverConfig: null,
});
manager.invalidate(TARGET);
expect(manager.getSnapshot(TARGET)).toEqual({
connectionState: "idle",
connectionError: null,
serverConfig: null,
});
});
});