Open History to see the last commit
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 expose CLI stderr or start an SDK host after a failed version check", () =>
59 Effect.scoped(
60 Effect.gen(function* () {
61 const binaryPath = yield* fakeCli(
62 'process.stderr.write("secret-test-value");process.exit(2);',
63 );
64 const createHost = vi.fn(async () => makeHost(metaCatalog));
65 const snapshot = yield* checkMuseProviderStatus(
66 settings({ enabled: true, binaryPath }),
67 undefined,
68 undefined,
69 createHost,
70 );
71 expect(snapshot.installed).toBe(true);
72 expect(snapshot.status).toBe("error");
73 expect(snapshot.message).not.toContain("secret-test-value");
74 expect(createHost).not.toHaveBeenCalled();
75 }),
76 ),
77 );
78
79 it.effect("discovers the Meta catalog without treating it as authentication proof", () =>
80 Effect.scoped(
81 Effect.gen(function* () {
82 const binaryPath = yield* fakeCli();
83 const host = makeHost(metaCatalog);
84 const snapshot = yield* checkMuseProviderStatus(
85 settings({ enabled: true, binaryPath, customModels: ["muse-custom"] }),
86 undefined,
87 undefined,
88 async () => host,
89 );
90 expect(snapshot.status).toBe("ready");
91 expect(snapshot.message).toBeUndefined();
92 expect(snapshot.slashCommands).toEqual([COMPACT_SLASH_COMMAND]);
93 expect(snapshot.auth).toEqual({ status: "unknown" });
94 expect(snapshot.version).toBe("1.0.3-R2198.1");
95 expect(snapshot.models.map((model) => model.slug)).toEqual([
96 "muse-wonder-1.0",
97 "muse-custom",
98 ]);
99 expect(snapshot.models.map((model) => model.name)).toEqual([
100 "Muse Wonder 1.0",
101 "muse-custom",
102 ]);
103 const descriptor = snapshot.models[0]?.capabilities?.optionDescriptors?.[0];
104 expect(
105 descriptor?.type === "select" && descriptor.options.map((option) => option.id),
106 ).toEqual(["low", "medium", "high", "xhigh", "max"]);
107 expect(host.connection.request).toHaveBeenCalledWith("model/list", {});
108 expect(host.connection.command).not.toHaveBeenCalled();
109 expect(host.close).toHaveBeenCalledOnce();
110 }),
111 ),
112 );
113
114 it.effect("keeps an empty discovered catalog empty instead of advertising a fallback model", () =>
115 Effect.scoped(
116 Effect.gen(function* () {
117 const binaryPath = yield* fakeCli();
118 const host = makeHost({ providerId: "meta", models: [] });
119 const snapshot = yield* checkMuseProviderStatus(
120 settings({ enabled: true, binaryPath }),
121 undefined,
122 undefined,
123 async () => host,
124 );
125 expect(snapshot.status).toBe("warning");
126 expect(snapshot.models).toEqual([]);
127 expect(host.close).toHaveBeenCalledOnce();
128 }),
129 ),
130 );
131});