Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Schema from "effect/Schema";
5import * as Sink from "effect/Sink";
6import * as Stream from "effect/Stream";
7import * as ChildProcess from "effect/process/ChildProcess";
8import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
9
10import { FxSettings } from "../settings.ts";
11import { checkFxProviderStatus } from "./status.ts";
12
13const encoder = new TextEncoder();
14const settings = Schema.decodeSync(FxSettings)({ enabled: true });
15
16function processHandle(stdout: string, code: number) {
17 return ChildProcessSpawner.makeHandle({
18 pid: ChildProcessSpawner.ProcessId(900_000_101),
19 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(code)),
20 isRunning: Effect.succeed(false),
21 kill: () => Effect.void,
22 unref: Effect.succeed(Effect.void),
23 stdin: Sink.drain,
24 stdout: stdout.length === 0 ? Stream.empty : Stream.succeed(encoder.encode(stdout)),
25 stderr: Stream.empty,
26 all: Stream.empty,
27 getInputFd: () => Sink.drain,
28 getOutputFd: () => Stream.empty,
29 });
30}
31
32/** Answers `fx` invocations by their arguments and records which ran. */
33const check = (statusJson: string) =>
34 Effect.gen(function* () {
35 const outputs: Record<string, string> = {
36 "--version": "fx 0.0.13\n",
37 "status --json": statusJson,
38 // `fx models --json` from fx 0.0.13, ids trimmed.
39 "models --json": JSON.stringify({
40 kind: "models",
41 count: 2,
42 shown_count: 2,
43 more_count: 0,
44 private_models_hidden: false,
45 ids: ["anthropic/claude-sonnet-5.5", "anthropic/claude-opus-5.5"],
46 }),
47 };
48 const invoked: Array<string> = [];
49 const spawner = ChildProcessSpawner.make((command) => {
50 const args = ChildProcess.isStandardCommand(command) ? command.args.join(" ") : "";
51 invoked.push(args);
52 const stdout = outputs[args];
53 return Effect.succeed(processHandle(stdout ?? "", stdout === undefined ? 2 : 0));
54 });
55 const draft = yield* checkFxProviderStatus(settings, {}).pipe(
56 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
57 );
58 return { draft, invoked };
59 }).pipe(Effect.provide(NodeServices.layer));
60
61describe("checkFxProviderStatus", () => {
62 // `fx status --json` from fx 0.0.13, trimmed to the fields T3 reads plus a few it ignores.
63 it.effect("reports sign-in from `fx status` without opening an ACP session", () =>
64 Effect.gen(function* () {
65 const signedIn = yield* check(
66 JSON.stringify({
67 kind: "status",
68 model: "anthropic/claude-opus-5.5",
69 auth: "fx login",
70 auth_refreshable: true,
71 permission_mode: "ask",
72 }),
73 );
74 assert.deepEqual(signedIn.invoked, ["--version", "status --json", "models --json"]);
75 assert.equal(signedIn.draft.status, "ready");
76 assert.equal(signedIn.draft.version, "0.0.13");
77 assert.deepEqual(signedIn.draft.auth, { status: "authenticated", label: "fx login" });
78 assert.deepEqual(
79 signedIn.draft.models.map((model) => model.slug),
80 ["default", "anthropic/claude-sonnet-5.5", "anthropic/claude-opus-5.5"],
81 );
82 // "Default" carries the model `fx status` reports, which the adapter selects for it.
83 assert.deepEqual(signedIn.draft.models[0]?.aliases, ["anthropic/claude-opus-5.5"]);
84 assert.isDefined(signedIn.draft.versionAdvisory);
85
86 const signedOut = yield* check(
87 JSON.stringify({
88 kind: "status",
89 model: "spacexai/grok-4.7",
90 auth: "missing",
91 auth_refreshable: false,
92 auth_help:
93 "fx needs access to Vercel AI Gateway. Run fx login to sign in, fx setup to use an API key, or set AI_GATEWAY_API_KEY.",
94 }),
95 );
96 assert.equal(signedOut.draft.status, "error");
97 assert.equal(signedOut.draft.auth.status, "unauthenticated");
98 assert.equal(signedOut.draft.message, "Run `fx login` to sign in.");
99
100 // An fx whose status output T3 cannot read is not reported as signed out.
101 const unknown = yield* check("not json");
102 assert.equal(unknown.draft.status, "warning");
103 assert.equal(unknown.draft.auth.status, "unknown");
104 }),
105 );
106});