Open History to see the last commit
1import type { ServerConfig, ServerConfigStreamEvent } from "@t3tools/contracts";
2import * as Option from "effect/Option";
3import { describe, expect, it } from "vite-plus/test";
4
5import { applyServerConfigProjection, reuseUnchanged } from "./serverConfigProjection.ts";
6
7describe("reuseUnchanged", () => {
8 const provider = (checkedAt: string, skills: ReadonlyArray<string>) => ({
9 instanceId: "claude",
10 checkedAt,
11 models: [{ slug: "opus", name: "Opus" }],
12 skills: skills.map((name) => ({ name })),
13 });
14
15 it("returns the previous value when nothing changed", () => {
16 const previous = [provider("a", ["x"])];
17 expect(reuseUnchanged(previous, [provider("a", ["x"])])).toBe(previous);
18 });
19
20 it("keeps unchanged parts when one field changes", () => {
21 const previous = [provider("a", ["x"]), { ...provider("a", ["y"]), instanceId: "codex" }];
22 const next = reuseUnchanged(previous, [
23 provider("b", ["x"]),
24 { ...provider("a", ["y"]), instanceId: "codex" },
25 ]);
26 expect(next).not.toBe(previous);
27 expect(next[0]).not.toBe(previous[0]);
28 expect(next[0]!.checkedAt).toBe("b");
29 expect(next[0]!.models).toBe(previous[0]!.models);
30 expect(next[0]!.skills).toBe(previous[0]!.skills);
31 expect(next[1]).toBe(previous[1]);
32 });
33
34 it("takes the new value when keys or lengths change", () => {
35 const previous = [provider("a", ["x"])];
36 const next = reuseUnchanged(previous, [provider("a", ["x", "z"])]);
37 expect(next[0]!.skills).toHaveLength(2);
38 expect(next[0]!.models).toBe(previous[0]!.models);
39 const dropped = reuseUnchanged({ a: 1, b: 2 }, { a: 1 });
40 expect(dropped).toEqual({ a: 1 });
41 });
42});
43
44describe("applyServerConfigProjection snapshot", () => {
45 const snapshot = (checkedAt: string) =>
46 ({
47 type: "snapshot",
48 config: {
49 environment: { capabilities: {} },
50 providers: [{ instanceId: "claude", checkedAt, skills: [{ name: "review" }] }],
51 },
52 }) as unknown as ServerConfigStreamEvent;
53 const providersOf = (projection: ReturnType<typeof applyServerConfigProjection>) =>
54 (Option.getOrThrow(projection).config as ServerConfig).providers;
55
56 it("keeps unchanged skills when a reconnect re-sends the config", () => {
57 const first = applyServerConfigProjection(Option.none(), snapshot("a"));
58 const second = applyServerConfigProjection(first, snapshot("b"));
59 expect(providersOf(second)[0]!.checkedAt).toBe("b");
60 expect(providersOf(second)[0]!.skills).toBe(providersOf(first)[0]!.skills);
61 });
62});
63
64describe("applyServerConfigProjection settingsUpdated", () => {
65 it("keeps unchanged settings sections when one field changes", () => {
66 const settings = {
67 favorites: [{ provider: "claude", model: "opus" }],
68 providerInstances: {
69 claude: { driver: "claudeAgent", config: { launchArgs: "" } },
70 codex: { driver: "codex" },
71 },
72 };
73 const current = Option.some({
74 config: { settings } as unknown as ServerConfig,
75 latestEvent: {} as ServerConfigStreamEvent,
76 source: "live" as const,
77 });
78 const next = applyServerConfigProjection(current, {
79 type: "settingsUpdated",
80 payload: {
81 settings: {
82 favorites: [{ provider: "claude", model: "opus" }],
83 providerInstances: {
84 claude: { driver: "claudeAgent", config: { launchArgs: "--verbose" } },
85 codex: { driver: "codex" },
86 },
87 },
88 },
89 } as unknown as ServerConfigStreamEvent);
90 const nextSettings = Option.getOrThrow(next).config.settings as unknown as typeof settings;
91 expect(nextSettings.favorites).toBe(settings.favorites);
92 expect(nextSettings.providerInstances.codex).toBe(settings.providerInstances.codex);
93 expect(nextSettings.providerInstances.claude.config.launchArgs).toBe("--verbose");
94 });
95});