@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 137 lines 5 KB
Raw
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { expect, it } from "@effect/vitest";
3 import { MuseSettings } from "../settings.ts";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Schema from "effect/Schema";
7 import { vi } from "vite-plus/test";
8
9 import { writeFakeCli } from "@t3tools/provider-testing/fakeCli";
10 import type { MuseSdkHost } from "./sdk.ts";
11 import { COMPACT_SLASH_COMMAND } from "@t3tools/provider-core/server/snapshotProbe";
12 import { checkMuseProviderStatus } from "./status.ts";
13
14 const settings = Schema.decodeSync(MuseSettings);
15 const 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 };
32 const 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
50 const 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
57 it.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.gen(function* () {
60 const binaryPath = yield* fakeCli(
61 'process.stderr.write("secret-test-value");process.exit(2);',
62 );
63 const createHost = vi.fn(async () => makeHost(metaCatalog));
64 const snapshot = yield* checkMuseProviderStatus(
65 settings({ enabled: true, binaryPath }),
66 undefined,
67 undefined,
68 createHost,
69 );
70 expect(snapshot.installed).toBe(true);
71 expect(snapshot.status).toBe("error");
72 expect(snapshot.message).not.toContain("secret-test-value");
73 expect(createHost).not.toHaveBeenCalled();
74 }),
75 );
76
77 it.effect("tells the user to finish setup when the launcher has not installed its runtime", () =>
78 Effect.gen(function* () {
79 // What Muse's launcher prints when a fresh install runs under MUSE_NO_AUTO_UPDATE=1.
80 const binaryPath = yield* fakeCli(
81 'process.stderr.write("muse: installed binary is missing; rerun the installer\\n");process.exit(1);',
82 );
83 const snapshot = yield* checkMuseProviderStatus(settings({ enabled: true, binaryPath }));
84 expect(snapshot.status).toBe("error");
85 expect(snapshot.message).toContain("muse --version");
86 }),
87 );
88
89 it.effect("discovers the Meta catalog without treating it as authentication proof", () =>
90 Effect.gen(function* () {
91 const binaryPath = yield* fakeCli();
92 const host = makeHost(metaCatalog);
93 const snapshot = yield* checkMuseProviderStatus(
94 settings({ enabled: true, binaryPath, customModels: ["muse-custom"] }),
95 undefined,
96 undefined,
97 async () => host,
98 );
99 expect(snapshot.status).toBe("ready");
100 expect(snapshot.message).toBeUndefined();
101 expect(snapshot.slashCommands).toEqual([COMPACT_SLASH_COMMAND]);
102 expect(snapshot.auth).toEqual({ status: "unknown" });
103 expect(snapshot.version).toBe("1.0.3-R2198.1");
104 expect(snapshot.models.map((model) => model.slug)).toEqual([
105 "muse-wonder-1.0",
106 "muse-custom",
107 ]);
108 expect(snapshot.models.map((model) => model.name)).toEqual([
109 "Muse Wonder 1.0",
110 "muse-custom",
111 ]);
112 const descriptor = snapshot.models[0]?.capabilities?.optionDescriptors?.[0];
113 expect(
114 descriptor?.type === "select" && descriptor.options.map((option) => option.id),
115 ).toEqual(["low", "medium", "high", "xhigh", "max"]);
116 expect(host.connection.request).toHaveBeenCalledWith("model/list", {});
117 expect(host.connection.command).not.toHaveBeenCalled();
118 expect(host.close).toHaveBeenCalledOnce();
119 }),
120 );
121
122 it.effect("keeps an empty discovered catalog empty instead of advertising a fallback model", () =>
123 Effect.gen(function* () {
124 const binaryPath = yield* fakeCli();
125 const host = makeHost({ providerId: "meta", models: [] });
126 const snapshot = yield* checkMuseProviderStatus(
127 settings({ enabled: true, binaryPath }),
128 undefined,
129 undefined,
130 async () => host,
131 );
132 expect(snapshot.status).toBe("warning");
133 expect(snapshot.models).toEqual([]);
134 expect(host.close).toHaveBeenCalledOnce();
135 }),
136 );
137 });