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

status.test.ts

192 lines6.7 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import { MuseSettings } from "../settings.ts";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Schema from "effect/Schema";
7import { vi } from "vite-plus/test";
8​
9import { writeFakeCli } from "@t3tools/provider-testing/fakeCli";
10import type { MuseSdkHost } from "./sdk.ts";
11import { COMPACT_SLASH_COMMAND } from "@t3tools/provider-core/server/snapshotProbe";
12import { checkMuseProviderStatus } from "./status.ts";
13​
14const settings = Schema.decodeSync(MuseSettings);
15const makeHost = (catalog: Record<string, unknown>) => {
16 const host: MuseSdkHost = {
17 initializeResult: { grantedCapabilities: [] },
18 connection: {
19 request: vi.fn(async () => catalog),
20 command: vi.fn(async () => ({})),
21 mintCommandId: () => "test-command",
22 onNotification: () => {},
23 onServerRequest: () => {},
24 onProtocolError: () => {},
25 closed: new Promise(() => {}),
26 },
27 exited: new Promise(() => {}),
28 close: vi.fn(async () => {}),
29 };
30 return host;
31};
32const metaCatalog = {
33 providerId: "meta",
34 models: [
35 {
36 modelId: "muse-wonder-1.0",
37 displayLabel: "muse-wonder-1.0",
38 providerId: "meta",
39 isDefault: true,
40 },
41 {
42 modelId: "foreign-model",
43 displayLabel: "Another provider",
44 providerId: "another",
45 isDefault: false,
46 },
47 ],
48};
49​
50const fakeCli = (source = 'process.stdout.write("muse 1.0.3-R2198.1\\n");') =>
51 Effect.gen(function* () {
52 const fs = yield* FileSystem.FileSystem;
53 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "muse-status-test-" });
54 return writeFakeCli({ directory, name: "muse", source });
55 });
56​
57it.layer(NodeServices.layer)("Muse status", (it) => {
58 it.effect("does not start a host when disabled", () =>
59 Effect.gen(function* () {
60 const createHost = vi.fn(async () => makeHost(metaCatalog));
61 const snapshot = yield* checkMuseProviderStatus(settings({}), {}, undefined, createHost);
62 expect(snapshot.status).toBe("disabled");
63 expect(createHost).not.toHaveBeenCalled();
64 }),
65 );
66​
67 it.effect("gives host-local install and login guidance for a missing binary", () =>
68 Effect.gen(function* () {
69 const snapshot = yield* checkMuseProviderStatus(
70 settings({ enabled: true, binaryPath: "/definitely-missing/muse" }),
71 );
72 expect(snapshot.installed).toBe(false);
73 expect(snapshot.status).toBe("error");
74 expect(snapshot.message).toContain("muse login");
75 expect(snapshot.message).toContain("this T3 server host");
76 }),
77 );
78​
79 it.effect("does not expose CLI stderr or start an SDK host after a failed version check", () =>
80 Effect.scoped(
81 Effect.gen(function* () {
82 const binaryPath = yield* fakeCli(
83 'process.stderr.write("secret-test-value");process.exit(2);',
84 );
85 const createHost = vi.fn(async () => makeHost(metaCatalog));
86 const snapshot = yield* checkMuseProviderStatus(
87 settings({ enabled: true, binaryPath }),
88 undefined,
89 undefined,
90 createHost,
91 );
92 expect(snapshot.installed).toBe(true);
93 expect(snapshot.status).toBe("error");
94 expect(snapshot.message).not.toContain("secret-test-value");
95 expect(createHost).not.toHaveBeenCalled();
96 }),
97 ),
98 );
99​
100 it.effect("discovers the Meta catalog without treating it as authentication proof", () =>
101 Effect.scoped(
102 Effect.gen(function* () {
103 const binaryPath = yield* fakeCli();
104 const host = makeHost(metaCatalog);
105 const snapshot = yield* checkMuseProviderStatus(
106 settings({ enabled: true, binaryPath, customModels: ["muse-custom"] }),
107 undefined,
108 undefined,
109 async () => host,
110 );
111 expect(snapshot.status).toBe("ready");
112 expect(snapshot.message).toBeUndefined();
113 expect(snapshot.slashCommands).toEqual([COMPACT_SLASH_COMMAND]);
114 expect(snapshot.auth).toEqual({ status: "unknown" });
115 expect(snapshot.version).toBe("1.0.3-R2198.1");
116 expect(snapshot.models.map((model) => model.slug)).toEqual([
117 "muse-wonder-1.0",
118 "muse-custom",
119 ]);
120 expect(snapshot.models.map((model) => model.name)).toEqual([
121 "Muse Wonder 1.0",
122 "muse-custom",
123 ]);
124 const descriptor = snapshot.models[0]?.capabilities?.optionDescriptors?.[0];
125 expect(
126 descriptor?.type === "select" && descriptor.options.map((option) => option.id),
127 ).toEqual(["low", "medium", "high", "xhigh", "max"]);
128 expect(host.connection.request).toHaveBeenCalledWith("model/list", {});
129 expect(host.connection.command).not.toHaveBeenCalled();
130 expect(host.close).toHaveBeenCalledOnce();
131 }),
132 ),
133 );
134​
135 it.effect("preserves a configured display name for a discovered Muse model", () =>
136 Effect.scoped(
137 Effect.gen(function* () {
138 const binaryPath = yield* fakeCli();
139 const snapshot = yield* checkMuseProviderStatus(
140 settings({
141 enabled: true,
142 binaryPath,
143 customModels: [{ slug: "muse-wonder-1.0", name: "Team Wonder" }],
144 }),
145 undefined,
146 undefined,
147 async () => makeHost(metaCatalog),
148 );
149 expect(snapshot.models.map(({ slug, name }) => [slug, name])).toEqual([
150 ["muse-wonder-1.0", "Team Wonder"],
151 ]);
152 }),
153 ),
154 );
155​
156 it.effect("closes the host after malformed metadata and reports unavailable", () =>
157 Effect.scoped(
158 Effect.gen(function* () {
159 const binaryPath = yield* fakeCli();
160 const host = makeHost({ providerId: "meta", models: [{ modelId: 1 }] });
161 const snapshot = yield* checkMuseProviderStatus(
162 settings({ enabled: true, binaryPath }),
163 undefined,
164 undefined,
165 async () => host,
166 );
167 expect(snapshot.status).toBe("error");
168 expect(snapshot.auth.status).toBe("unknown");
169 expect(snapshot.models).toEqual([]);
170 expect(host.close).toHaveBeenCalledOnce();
171 }),
172 ),
173 );
174​
175 it.effect("keeps an empty discovered catalog empty instead of advertising a fallback model", () =>
176 Effect.scoped(
177 Effect.gen(function* () {
178 const binaryPath = yield* fakeCli();
179 const host = makeHost({ providerId: "meta", models: [] });
180 const snapshot = yield* checkMuseProviderStatus(
181 settings({ enabled: true, binaryPath }),
182 undefined,
183 undefined,
184 async () => host,
185 );
186 expect(snapshot.status).toBe("warning");
187 expect(snapshot.models).toEqual([]);
188 expect(host.close).toHaveBeenCalledOnce();
189 }),
190 ),
191 );
192});