Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 ServerSettings,
4 type ServerConfig,
5 type ServerConfigStreamEvent,
6 CALM_HOST_PRESSURE,
7} from "@t3tools/contracts";
8import * as Option from "effect/Option";
9import * as Schema from "effect/Schema";
10import { describe, expect, it } from "vite-plus/test";
11
12import { applyServerConfigProjection, reuseUnchanged } from "./serverConfigProjection.ts";
13
14const encodeSettings = Schema.encodeSync(ServerSettings);
15const decodeSettings = Schema.decodeSync(ServerSettings);
16
17describe("reuseUnchanged", () => {
18 const provider = (checkedAt: string, skills: ReadonlyArray<string>) => ({
19 instanceId: "claude",
20 checkedAt,
21 models: [{ slug: "opus", name: "Opus" }],
22 skills: skills.map((name) => ({ name })),
23 });
24
25 it("takes the new value when keys or lengths change", () => {
26 const previous = [provider("a", ["x"])];
27 const next = reuseUnchanged(previous, [provider("a", ["x", "z"])]);
28 expect(next[0]!.skills).toHaveLength(2);
29 expect(next[0]!.models).toBe(previous[0]!.models);
30 const dropped = reuseUnchanged({ a: 1, b: 2 }, { a: 1 });
31 expect(dropped).toEqual({ a: 1 });
32 });
33});
34
35describe("applyServerConfigProjection settingsUpdated", () => {
36 it("keeps unchanged settings sections when one field changes", () => {
37 const settings = {
38 favorites: [{ provider: "claude", model: "opus" }],
39 providerInstances: {
40 claude: { driver: "claudeAgent", config: { launchArgs: "" } },
41 codex: { driver: "codex" },
42 },
43 };
44 const current = Option.some({
45 config: { settings } as unknown as ServerConfig,
46 latestEvent: {} as ServerConfigStreamEvent,
47 source: "live" as const,
48 });
49 const next = applyServerConfigProjection(current, {
50 type: "settingsUpdated",
51 payload: {
52 settings: {
53 favorites: [{ provider: "claude", model: "opus" }],
54 providerInstances: {
55 claude: { driver: "claudeAgent", config: { launchArgs: "--verbose" } },
56 codex: { driver: "codex" },
57 },
58 },
59 },
60 } as unknown as ServerConfigStreamEvent);
61 const nextSettings = Option.getOrThrow(next).config.settings as unknown as typeof settings;
62 expect(nextSettings.favorites).toBe(settings.favorites);
63 expect(nextSettings.providerInstances.codex).toBe(settings.providerInstances.codex);
64 expect(nextSettings.providerInstances.claude.config.launchArgs).toBe("--verbose");
65 });
66
67 it("keeps the settings object when an update repeats the current settings", () => {
68 const wire = encodeSettings(DEFAULT_SERVER_SETTINGS);
69 const decode = () => decodeSettings(structuredClone(wire));
70 const settings = decode();
71 const current = Option.some({
72 config: { settings } as unknown as ServerConfig,
73 latestEvent: {} as ServerConfigStreamEvent,
74 source: "live" as const,
75 });
76 const next = applyServerConfigProjection(current, {
77 type: "settingsUpdated",
78 payload: { settings: decode() },
79 } as ServerConfigStreamEvent);
80 expect(Option.getOrThrow(next).config).toBe(Option.getOrThrow(current).config);
81 });
82});
83
84describe("applyServerConfigProjection provider catalogs", () => {
85 const provider = (fields: Record<string, unknown>) =>
86 ({
87 instanceId: "claude",
88 checkedAt: "a",
89 models: [],
90 ...fields,
91 }) as unknown as ServerConfig["providers"][number];
92 const projection = (providers: ServerConfig["providers"]) =>
93 Option.some({
94 config: { providers } as unknown as ServerConfig,
95 latestEvent: {} as ServerConfigStreamEvent,
96 source: "cache" as const,
97 });
98 const apply = (current: ReturnType<typeof projection>, event: unknown) =>
99 Option.getOrThrow(applyServerConfigProjection(current, event as ServerConfigStreamEvent)).config
100 .providers[0]!;
101
102 it("keeps held slash commands and skills when the server sends a digest in their place", () => {
103 const slashCommands = [{ name: "compact" }];
104 const skills = [{ name: "review", path: "/skills/review", enabled: true }];
105 const held = provider({
106 slashCommands,
107 skills,
108 catalogDigest: "d1",
109 loadedCatalogDigest: "d1",
110 });
111 const sent = (fields: Record<string, unknown>) =>
112 provider({ slashCommands: [], skills: [], catalogDigest: "d1", ...fields });
113
114 const reconnected = apply(projection([held]), {
115 type: "snapshot",
116 config: { providers: [sent({ checkedAt: "b" })] },
117 });
118 expect(reconnected).toMatchObject({ checkedAt: "b", loadedCatalogDigest: "d1" });
119 expect(reconnected.slashCommands).toBe(slashCommands);
120 expect(reconnected.skills).toBe(skills);
121
122 // A changed catalog keeps the stale lists; the old marker says they need a reload.
123 const { slashCommands: _c, skills: _s, ...delta } = sent({ catalogDigest: "d2" });
124 const changed = apply(projection([held]), {
125 type: "providerStatusDeltas",
126 payload: { providers: [delta] },
127 });
128 expect(changed).toMatchObject({ catalogDigest: "d2", loadedCatalogDigest: "d1" });
129 expect(changed.skills).toBe(skills);
130
131 // Lists cached before digests existed carry over without a marker.
132 const legacy = apply(projection([provider({ slashCommands, skills })]), {
133 type: "providerStatuses",
134 payload: { providers: [sent({})] },
135 });
136 expect(legacy.slashCommands).toBe(slashCommands);
137 expect(legacy.loadedCatalogDigest).toBeUndefined();
138 });
139});
140
141describe("applyServerConfigProjection hostPressureUpdated", () => {
142 it("keeps pressure across a reconnect snapshot from a server that sends it, and drops it once pressure ends", () => {
143 const pressure = (level: "none" | "severe") =>
144 ({
145 type: "hostPressureUpdated",
146 version: 1,
147 payload: { ...CALM_HOST_PRESSURE, memory: level },
148 }) as ServerConfigStreamEvent;
149 const snapshotFrom = (hostPressure: boolean) =>
150 ({
151 type: "snapshot",
152 version: 1,
153 config: { environment: { capabilities: { hostPressure } } },
154 }) as ServerConfigStreamEvent;
155 const snapshot = snapshotFrom(true);
156 // Kept, not dropped: a session that drops it cannot replay the calm to a client still holding severe.
157 const fresh = applyServerConfigProjection(Option.none(), snapshot);
158 expect(Option.getOrThrow(applyServerConfigProjection(fresh, pressure("none")))).toBe(
159 Option.getOrThrow(fresh),
160 );
161 const severe = applyServerConfigProjection(
162 applyServerConfigProjection(Option.none(), snapshot),
163 pressure("severe"),
164 );
165 const reconnected = applyServerConfigProjection(severe, snapshot);
166 expect(Option.getOrThrow(reconnected).config.hostPressure?.memory).toBe("severe");
167 const olderServer = applyServerConfigProjection(severe, snapshotFrom(false));
168 expect(Option.getOrThrow(olderServer).config.hostPressure).toBeUndefined();
169 const ended = applyServerConfigProjection(reconnected, pressure("none"));
170 expect(Option.getOrThrow(ended).config.hostPressure).toBeUndefined();
171 });
172});