Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ServerProvider,
6} from "@t3tools/contracts";
7import { describe, expect, it } from "vite-plus/test";
8
9import {
10 getOnboardingProviderState,
11 resolveOnboardingProviderLoginCommand,
12 selectOnboardingProvidersByDriver,
13} from "./providerReadiness.logic";
14
15const readyCodex: ServerProvider = {
16 instanceId: ProviderInstanceId.make("codex"),
17 driver: ProviderDriverKind.make("codex"),
18 enabled: true,
19 installed: true,
20 version: "1.0.0",
21 status: "ready",
22 auth: { status: "unknown" },
23 checkedAt: "2026-08-23T00:00:00.000Z",
24 models: [],
25 slashCommands: [],
26 skills: [],
27};
28
29describe("getOnboardingProviderState", () => {
30 it("treats authenticated providers as ready only when their provider status is ready", () => {
31 expect(getOnboardingProviderState({ ...readyCodex, auth: { status: "authenticated" } })).toBe(
32 "ready",
33 );
34 expect(
35 getOnboardingProviderState({
36 ...readyCodex,
37 auth: { status: "authenticated" },
38 status: "error",
39 }),
40 ).toBe("attention");
41 expect(
42 getOnboardingProviderState({
43 ...readyCodex,
44 auth: { status: "authenticated" },
45 status: "warning",
46 }),
47 ).toBe("attention");
48 });
49
50 it("offers sign-in only when the server reports an authentication failure", () => {
51 expect(
52 getOnboardingProviderState({
53 ...readyCodex,
54 status: "error",
55 auth: { status: "unauthenticated" },
56 }),
57 ).toBe("signIn");
58 expect(getOnboardingProviderState({ ...readyCodex, status: "error" })).toBe("attention");
59 expect(getOnboardingProviderState({ ...readyCodex, status: "warning" })).toBe("attention");
60 });
61
62 it("does not offer installation or sign-in for disabled providers", () => {
63 expect(getOnboardingProviderState({ ...readyCodex, enabled: false, installed: false })).toBe(
64 "disabled",
65 );
66 expect(getOnboardingProviderState({ ...readyCodex, status: "disabled" })).toBe("disabled");
67 });
68
69 it("offers installation only when an enabled provider is missing", () => {
70 expect(getOnboardingProviderState({ ...readyCodex, installed: false, status: "error" })).toBe(
71 "install",
72 );
73 });
74});
75
76describe("selectOnboardingProvidersByDriver", () => {
77 it("prefers a ready instance with unknown authentication to an unauthenticated instance", () => {
78 const signedOutCodex: ServerProvider = {
79 ...readyCodex,
80 instanceId: ProviderInstanceId.make("codex_work"),
81 status: "error",
82 auth: { status: "unauthenticated" },
83 };
84
85 expect(selectOnboardingProvidersByDriver([signedOutCodex, readyCodex]).get("codex")).toBe(
86 readyCodex,
87 );
88 });
89
90 it("prefers a provider with an actionable sign-in over a failed provider", () => {
91 const failedCodex: ServerProvider = { ...readyCodex, status: "error" };
92 const signedOutCodex: ServerProvider = {
93 ...readyCodex,
94 instanceId: ProviderInstanceId.make("codex_work"),
95 status: "error",
96 auth: { status: "unauthenticated" },
97 };
98
99 expect(selectOnboardingProvidersByDriver([failedCodex, signedOutCodex]).get("codex")).toBe(
100 signedOutCodex,
101 );
102 });
103
104 it("prefers installed providers over missing or disabled instances", () => {
105 const disabledCodex: ServerProvider = { ...readyCodex, enabled: false };
106 const missingCodex: ServerProvider = {
107 ...readyCodex,
108 instanceId: ProviderInstanceId.make("codex_work"),
109 installed: false,
110 status: "error",
111 };
112
113 expect(
114 selectOnboardingProvidersByDriver([disabledCodex, missingCodex, readyCodex]).get("codex"),
115 ).toBe(readyCodex);
116 });
117});
118
119describe("resolveOnboardingProviderLoginCommand", () => {
120 it("uses the selected Codex account binary", () => {
121 const provider = { ...readyCodex, instanceId: ProviderInstanceId.make("codex_work") };
122
123 expect(
124 resolveOnboardingProviderLoginCommand(
125 provider,
126 {
127 ...DEFAULT_SERVER_SETTINGS,
128 providerInstances: {
129 [provider.instanceId]: {
130 driver: provider.driver,
131 config: { binaryPath: "/opt/codex-work/bin/codex" },
132 },
133 },
134 },
135 "linux",
136 ),
137 ).toBe("/opt/codex-work/bin/codex login");
138 });
139
140 it("quotes a Codex path with spaces for PowerShell", () => {
141 expect(
142 resolveOnboardingProviderLoginCommand(
143 readyCodex,
144 {
145 ...DEFAULT_SERVER_SETTINGS,
146 providerInstances: {
147 [ProviderInstanceId.make("codex")]: {
148 driver: ProviderDriverKind.make("codex"),
149 config: {
150 binaryPath: "C:\\Program Files\\Codex & Tools\\codex.exe",
151 },
152 },
153 },
154 },
155 "windows",
156 ),
157 ).toBe("& 'C:\\Program Files\\Codex & Tools\\codex.exe' login");
158 });
159
160 it("quotes a Claude path with shell metacharacters on POSIX", () => {
161 const provider: ServerProvider = {
162 ...readyCodex,
163 driver: ProviderDriverKind.make("claudeAgent"),
164 instanceId: ProviderInstanceId.make("claude"),
165 };
166
167 expect(
168 resolveOnboardingProviderLoginCommand(
169 provider,
170 {
171 ...DEFAULT_SERVER_SETTINGS,
172 providerInstances: {
173 [ProviderInstanceId.make("claude")]: {
174 driver: ProviderDriverKind.make("claudeAgent"),
175 config: {
176 binaryPath: "/opt/Claude Tools/$current/claude",
177 },
178 },
179 },
180 },
181 "linux",
182 ),
183 ).toBe("'/opt/Claude Tools/$current/claude' auth login");
184 });
185
186 it.each([
187 ["~/my tools/codex", "~/'my tools/codex' login"],
188 ["~\\my tools/codex", "~/'my tools/codex' login"],
189 ["~/tools/codex's build", `~/'tools/codex'"'"'s build' login`],
190 ["~\\tools\\codex's build", `~/'tools\\codex'"'"'s build' login`],
191 ["~/tools/codex; echo unsafe", "~/'tools/codex; echo unsafe' login"],
192 ])("keeps the home prefix expandable while quoting %s", (binaryPath, expectedCommand) => {
193 expect(
194 resolveOnboardingProviderLoginCommand(
195 readyCodex,
196 {
197 ...DEFAULT_SERVER_SETTINGS,
198 providerInstances: {
199 [ProviderInstanceId.make("codex")]: {
200 driver: ProviderDriverKind.make("codex"),
201 config: {
202 binaryPath,
203 },
204 },
205 },
206 },
207 "linux",
208 ),
209 ).toBe(expectedCommand);
210 });
211
212 it("uses the default command when an old server reports an unknown shell", () => {
213 expect(
214 resolveOnboardingProviderLoginCommand(
215 readyCodex,
216 {
217 ...DEFAULT_SERVER_SETTINGS,
218 providerInstances: {
219 [ProviderInstanceId.make("codex")]: {
220 driver: ProviderDriverKind.make("codex"),
221 config: {
222 binaryPath: "/opt/Codex Tools/codex",
223 },
224 },
225 },
226 },
227 "unknown",
228 ),
229 ).toBe("codex login");
230 });
231});