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

thread-list-environments.test.ts

222 lines7.0 KB

1import {
2 EnvironmentId,
3 ProviderInstanceId,
4 ProviderDriverKind,
5 type ServerConfig,
6 type ServerProvider,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "@effect/vitest";
9import { Atom, AtomRegistry } from "effect/reactivity";
10import { createThreadListEnvironmentsAtom } from "./thread-list-environments";
11​
12const ID = EnvironmentId.make("one");
13const OTHER_ID = EnvironmentId.make("two");
14const provider: ServerProvider = {
15 instanceId: ProviderInstanceId.make("codex"),
16 driver: ProviderDriverKind.make("codex"),
17 enabled: true,
18 installed: true,
19 version: null,
20 status: "ready",
21 auth: { status: "authenticated" },
22 checkedAt: "2026-09-01T00:00:00.000Z",
23 models: [],
24 slashCommands: [],
25 skills: [],
26};
27const config = {
28 providers: [provider],
29 environment: { capabilities: {}, platform: { machine: "laptop" } },
30 settings: {},
31} as unknown as ServerConfig;
32​
33function harness() {
34 const registry = AtomRegistry.make();
35 const configs = Atom.make<ReadonlyMap<EnvironmentId, ServerConfig>>(
36 new Map([
37 [ID, config],
38 [OTHER_ID, config],
39 ]),
40 );
41 const list = createThreadListEnvironmentsAtom(configs);
42 let sourceNotifications = 0;
43 let listNotifications = 0;
44 registry.subscribe(configs, () => sourceNotifications++);
45 registry.subscribe(list, () => listNotifications++);
46 registry.get(list);
47 sourceNotifications = 0;
48 listNotifications = 0;
49 return {
50 registry,
51 configs,
52 list,
53 read: () => registry.get(list),
54 write: (next: ServerConfig) =>
55 registry.set(
56 configs,
57 new Map([
58 [ID, next],
59 [OTHER_ID, config],
60 ]),
61 ),
62 notifications: () => ({ source: sourceNotifications, list: listNotifications }),
63 };
65​
66describe("thread list environment projection", () => {
67 it("keeps navigation stable while freshness, catalog and workspace updates reach full-config consumers", () => {
68 const h = harness();
69 try {
70 const initial = h.read();
71 const changed: ServerConfig = {
72 ...config,
73 providers: [
74 {
75 ...provider,
76 checkedAt: "2026-09-02T00:00:00.000Z",
77 usageLimits: { checkedAt: "2026-09-02T00:00:00.000Z", windows: [] },
78 models: [{ slug: "new-model", name: "New model", isCustom: false, capabilities: null }],
79 workspaceSnapshots: [
80 {
81 cwd: "/repo",
82 checkedAt: "2026-09-02T00:00:00.000Z",
83 slashCommands: [],
84 skills: [],
85 },
86 ],
87 },
88 ],
89 };
90 h.write(changed);
91 expect(h.read()).toBe(initial);
92 expect(h.registry.get(h.configs).get(ID)?.providers[0]?.usageLimits?.checkedAt).toBe(
93 "2026-09-02T00:00:00.000Z",
94 );
95 expect(h.notifications()).toEqual({ source: 1, list: 0 });
96 } finally {
97 h.registry.dispose();
98 }
99 });
100​
101 it.each([
102 ["threadSettlement", "settlementEnvironmentIds"],
103 ["threadSnooze", "snoozeEnvironmentIds"],
104 ["threadPinning", "pinningEnvironmentIds"],
105 ["threadPinReorder", "pinReorderEnvironmentIds"],
106 ["threadActiveReorder", "activeReorderEnvironmentIds"],
107 ["threadTitleRegeneration", "titleRegenerationEnvironmentIds"],
108 ] as const)("tracks enabling and removing %s", (capability, collection) => {
109 const h = harness();
110 try {
111 h.write({
112 ...config,
113 environment: {
114 ...config.environment,
115 capabilities: { ...config.environment.capabilities, [capability]: true },
116 },
117 });
118 expect([...h.read()[collection]]).toEqual([ID]);
119 h.write({
120 ...config,
121 environment: {
122 ...config.environment,
123 capabilities: { ...config.environment.capabilities, [capability]: false },
124 },
125 });
126 expect(h.read()[collection].size).toBe(0);
127 const disabled = h.read();
128 h.write(config);
129 expect(h.read()).toBe(disabled);
130 expect(h.notifications().list).toBe(2);
131 } finally {
132 h.registry.dispose();
133 }
134 });
135​
136 it.each([
137 ["instanceId", ProviderInstanceId.make("other")],
138 ["driver", ProviderDriverKind.make("claudeAgent")],
139 ["displayName", "Work account"],
140 ["accentColor", "#123456"],
141 ["iconUrl", "https://example.test/icon.png"],
142 ] as const)(
143 "updates and restores provider %s without changing another environment's providers",
144 (key, value) => {
145 const h = harness();
146 try {
147 const otherProviders = h.read().providersByEnvironmentId.get(OTHER_ID);
148 h.write({ ...config, providers: [{ ...provider, [key]: value }] });
149 expect(h.read().providersByEnvironmentId.get(ID)?.[0]?.[key]).toBe(value);
150 expect(h.read().providersByEnvironmentId.get(OTHER_ID)).toBe(otherProviders);
151 h.write(config);
152 expect(h.read().providersByEnvironmentId.get(ID)?.[0]?.[key]).toBe(provider[key]);
153 expect(h.notifications().list).toBe(2);
154 } finally {
155 h.registry.dispose();
156 }
157 },
158 );
159​
160 it("tracks provider membership and order in both directions", () => {
161 const h = harness();
162 try {
163 const second = { ...provider, instanceId: ProviderInstanceId.make("work") };
164 h.write({ ...config, providers: [provider, second] });
165 expect(
166 h
167 .read()
168 .providersByEnvironmentId.get(ID)
169 ?.map((p) => p.instanceId),
170 ).toEqual(["codex", "work"]);
171 h.write({ ...config, providers: [second, provider] });
172 expect(
173 h
174 .read()
175 .providersByEnvironmentId.get(ID)
176 ?.map((p) => p.instanceId),
177 ).toEqual(["work", "codex"]);
178 h.write({ ...config, providers: [] });
179 expect(h.read().providersByEnvironmentId.get(ID)).toEqual([]);
180 h.write(config);
181 expect(
182 h
183 .read()
184 .providersByEnvironmentId.get(ID)
185 ?.map((p) => p.instanceId),
186 ).toEqual(["codex"]);
187 expect(h.notifications().list).toBe(4);
188 } finally {
189 h.registry.dispose();
190 }
191 });
192​
193 it("restores detected machine kind after removing an override", () => {
194 const h = harness();
195 try {
196 expect(h.read().machineByEnvironmentId.get(ID)).toBe("laptop");
197 h.write({ ...config, settings: { ...config.settings, environmentIcon: "server" } });
198 expect(h.read().machineByEnvironmentId.get(ID)).toBe("server");
199 h.write(config);
200 expect(h.read().machineByEnvironmentId.get(ID)).toBe("laptop");
201 const detected = h.read();
202 h.write({ ...config, settings: { ...config.settings, environmentIcon: "laptop" } });
203 expect(h.read()).toBe(detected);
204 } finally {
205 h.registry.dispose();
206 }
207 });
208​
209 it("removes forgotten environments and repopulates them after reconnect", () => {
210 const h = harness();
211 try {
212 h.registry.set(h.configs, new Map());
213 expect(h.read().providersByEnvironmentId.size).toBe(0);
214 expect(h.read().machineByEnvironmentId.size).toBe(0);
215 h.write(config);
216 expect([...h.read().providersByEnvironmentId.keys()]).toEqual([ID, OTHER_ID]);
217 expect(h.notifications().list).toBe(2);
218 } finally {
219 h.registry.dispose();
220 }
221 });
222});