Open History to see the last commit
1import { AtomRegistry } from "effect/unstable/reactivity";
2import { EnvironmentId } from "@t3tools/contracts";
3import { afterEach, describe, expect, it } from "vitest";
4
5import { createEnvironmentRuntimeManager } from "./environmentRuntimeState.ts";
6
7let atomRegistry = AtomRegistry.make();
8
9function resetAtomRegistry() {
10 atomRegistry.dispose();
11 atomRegistry = AtomRegistry.make();
12}
13
14const TARGET = { environmentId: EnvironmentId.make("env-local") } as const;
15
16describe("createEnvironmentRuntimeManager", () => {
17 afterEach(() => {
18 resetAtomRegistry();
19 });
20
21 it("stores state per environment", () => {
22 const manager = createEnvironmentRuntimeManager({
23 getRegistry: () => atomRegistry,
24 });
25
26 manager.setState(TARGET, {
27 connectionState: "ready",
28 connectionError: null,
29 serverConfig: null,
30 });
31
32 expect(manager.getSnapshot(TARGET)).toEqual({
33 connectionState: "ready",
34 connectionError: null,
35 serverConfig: null,
36 });
37 });
38
39 it("patches the current state", () => {
40 const manager = createEnvironmentRuntimeManager({
41 getRegistry: () => atomRegistry,
42 });
43
44 manager.patch(TARGET, (current) => ({
45 ...current,
46 connectionState: "disconnected",
47 connectionError: "Socket closed.",
48 }));
49
50 expect(manager.getSnapshot(TARGET)).toEqual({
51 connectionState: "disconnected",
52 connectionError: "Socket closed.",
53 serverConfig: null,
54 });
55 });
56
57 it("invalidates a single environment", () => {
58 const manager = createEnvironmentRuntimeManager({
59 getRegistry: () => atomRegistry,
60 });
61
62 manager.setState(TARGET, {
63 connectionState: "ready",
64 connectionError: null,
65 serverConfig: null,
66 });
67 manager.invalidate(TARGET);
68
69 expect(manager.getSnapshot(TARGET)).toEqual({
70 connectionState: "idle",
71 connectionError: null,
72 serverConfig: null,
73 });
74 });
75});