Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import type * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6import * as Schema from "effect/Schema";
7import * as Sink from "effect/Sink";
8import * as Stream from "effect/Stream";
9import * as ChildProcess from "effect/process/ChildProcess";
10import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11import * as TestClock from "effect/testing/TestClock";
12
13import { FxSettings } from "../settings.ts";
14import { checkFxProviderStatus } from "./status.ts";
15
16const encoder = new TextEncoder();
17const settings = Schema.decodeSync(FxSettings)({ enabled: true });
18
19/** Holds stdout and the exit back until `answered` completes, like a CLI slow to answer. */
20function processHandle(stdout: string, code: number, answered: Effect.Effect<void> = Effect.void) {
21 return ChildProcessSpawner.makeHandle({
22 pid: ChildProcessSpawner.ProcessId(900_000_101),
23 exitCode: Effect.as(answered, ChildProcessSpawner.ExitCode(code)),
24 isRunning: Effect.succeed(false),
25 kill: () => Effect.void,
26 unref: Effect.succeed(Effect.void),
27 stdin: Sink.drain,
28 stdout: Stream.unwrap(
29 Effect.as(
30 answered,
31 stdout.length === 0 ? Stream.empty : Stream.succeed(encoder.encode(stdout)),
32 ),
33 ),
34 stderr: Stream.empty,
35 all: Stream.empty,
36 getInputFd: () => Sink.drain,
37 getOutputFd: () => Stream.empty,
38 });
39}
40
41/** Answers `fx` invocations by their arguments and records which ran. */
42const check = (statusJson: string, versionDelay: Duration.Input = 0) =>
43 Effect.gen(function* () {
44 const outputs: Record<string, string> = {
45 "--version": "fx 0.0.13\n",
46 "status --json": statusJson,
47 // `fx models --json` from fx 0.0.13, ids trimmed.
48 "models --json": JSON.stringify({
49 kind: "models",
50 count: 2,
51 shown_count: 2,
52 more_count: 0,
53 private_models_hidden: false,
54 ids: ["anthropic/claude-sonnet-5.5", "anthropic/claude-opus-5.5"],
55 }),
56 };
57 const invoked: Array<string> = [];
58 const spawner = ChildProcessSpawner.make((command) => {
59 const args = ChildProcess.isStandardCommand(command) ? command.args.join(" ") : "";
60 invoked.push(args);
61 const stdout = outputs[args];
62 return Effect.succeed(
63 processHandle(
64 stdout ?? "",
65 stdout === undefined ? 2 : 0,
66 args === "--version" ? Effect.sleep(versionDelay) : Effect.void,
67 ),
68 );
69 });
70 const draft = yield* checkFxProviderStatus(settings, {}).pipe(
71 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
72 );
73 return { draft, invoked };
74 }).pipe(Effect.provide(NodeServices.layer));
75
76describe("checkFxProviderStatus", () => {
77 it.effect.each([
78 {
79 label: "answers after 6 s",
80 versionDelay: "6 seconds",
81 status: "ready",
82 version: "0.0.13",
83 message: undefined,
84 invoked: ["--version", "status --json", "models --json"],
85 },
86 {
87 label: "never answers",
88 versionDelay: "Infinity",
89 status: "error",
90 version: null,
91 message: "Failed to run `fx --version`.",
92 invoked: ["--version"],
93 },
94 ] as const)("gives `fx --version` 10 s when it $label", (row) =>
95 Effect.gen(function* () {
96 const fiber = yield* check(
97 JSON.stringify({ kind: "status", model: "anthropic/claude-opus-5.5", auth: "fx login" }),
98 row.versionDelay,
99 ).pipe(Effect.forkChild);
100 yield* TestClock.adjust("10 seconds");
101 const { draft, invoked } = yield* Fiber.join(fiber);
102 assert.equal(draft.status, row.status);
103 assert.equal(draft.version, row.version);
104 assert.equal(draft.message, row.message);
105 assert.deepEqual(invoked, [...row.invoked]);
106 }),
107 );
108
109 // `fx status --json` from fx 0.0.13, trimmed to the fields T3 reads plus a few it ignores.
110 it.effect("reports sign-in from `fx status` without opening an ACP session", () =>
111 Effect.gen(function* () {
112 const signedIn = yield* check(
113 JSON.stringify({
114 kind: "status",
115 model: "anthropic/claude-opus-5.5",
116 auth: "fx login",
117 auth_refreshable: true,
118 permission_mode: "ask",
119 }),
120 );
121 assert.deepEqual(signedIn.invoked, ["--version", "status --json", "models --json"]);
122 assert.equal(signedIn.draft.status, "ready");
123 assert.equal(signedIn.draft.version, "0.0.13");
124 assert.deepEqual(signedIn.draft.auth, { status: "authenticated", label: "fx login" });
125 assert.deepEqual(
126 signedIn.draft.models.map((model) => model.slug),
127 ["default", "anthropic/claude-sonnet-5.5", "anthropic/claude-opus-5.5"],
128 );
129 // "Default" carries the model `fx status` reports, which the adapter selects for it.
130 assert.deepEqual(signedIn.draft.models[0]?.aliases, ["anthropic/claude-opus-5.5"]);
131 assert.isDefined(signedIn.draft.versionAdvisory);
132
133 const signedOut = yield* check(
134 JSON.stringify({
135 kind: "status",
136 model: "spacexai/grok-4.7",
137 auth: "missing",
138 auth_refreshable: false,
139 auth_help:
140 "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.",
141 }),
142 );
143 assert.equal(signedOut.draft.status, "error");
144 assert.equal(signedOut.draft.auth.status, "unauthenticated");
145 assert.equal(signedOut.draft.message, "Run `fx login` to sign in.");
146
147 // An fx whose status output T3 cannot read is not reported as signed out.
148 const unknown = yield* check("not json");
149 assert.equal(unknown.draft.status, "warning");
150 assert.equal(unknown.draft.auth.status, "unknown");
151 }),
152 );
153});