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("tells the user to finish setup when the launcher has not installed its runtime", () =>
80 Effect.scoped(
81 Effect.gen(function* () {
82 // What Muse's launcher prints when a fresh install runs under MUSE_NO_AUTO_UPDATE=1.
83 const binaryPath = yield* fakeCli(
84 'process.stderr.write("muse: installed binary is missing; rerun the installer\\n");process.exit(1);',
85 );
86 const snapshot = yield* checkMuseProviderStatus(settings({ enabled: true, binaryPath }));
87 expect(snapshot.status).toBe("error");
88 expect(snapshot.message).toContain("muse --version");
89 }),
90 ),
91 );
92
93 it.effect("discovers the Meta catalog without treating it as authentication proof", () =>
94 Effect.scoped(
95 Effect.gen(function* () {
96 const binaryPath = yield* fakeCli();
97 const host = makeHost(metaCatalog);
98 const snapshot = yield* checkMuseProviderStatus(
99 settings({ enabled: true, binaryPath, customModels: ["muse-custom"] }),
100 undefined,
101 undefined,
102 async () => host,
103 );
104 expect(snapshot.status).toBe("ready");
105 expect(snapshot.message).toBeUndefined();
106 expect(snapshot.slashCommands).toEqual([COMPACT_SLASH_COMMAND]);
107 expect(snapshot.auth).toEqual({ status: "unknown" });
108 expect(snapshot.version).toBe("1.0.3-R2198.1");
109 expect(snapshot.models.map((model) => model.slug)).toEqual([
110 "muse-wonder-1.0",
111 "muse-custom",
112 ]);
113 expect(snapshot.models.map((model) => model.name)).toEqual([
114 "Muse Wonder 1.0",
115 "muse-custom",
116 ]);
117 const descriptor = snapshot.models[0]?.capabilities?.optionDescriptors?.[0];
118 expect(
119 descriptor?.type === "select" && descriptor.options.map((option) => option.id),
120 ).toEqual(["low", "medium", "high", "xhigh", "max"]);
121 expect(host.connection.request).toHaveBeenCalledWith("model/list", {});
122 expect(host.connection.command).not.toHaveBeenCalled();
123 expect(host.close).toHaveBeenCalledOnce();
124 }),
125 ),
126 );
127
128 it.effect("keeps an empty discovered catalog empty instead of advertising a fallback model", () =>
129 Effect.scoped(
130 Effect.gen(function* () {
131 const binaryPath = yield* fakeCli();
132 const host = makeHost({ providerId: "meta", models: [] });
133 const snapshot = yield* checkMuseProviderStatus(
134 settings({ enabled: true, binaryPath }),
135 undefined,
136 undefined,
137 async () => host,
138 );
139 expect(snapshot.status).toBe("warning");
140 expect(snapshot.models).toEqual([]);
141 expect(host.close).toHaveBeenCalledOnce();
142 }),
143 ),
144 );
145});