import {
DEFAULT_SERVER_SETTINGS,
ProviderDriverKind,
ProviderInstanceId,
type ServerProvider,
} from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";
import {
getOnboardingProviderState,
resolveOnboardingProviderLoginCommand,
selectOnboardingProvidersByDriver,
} from "./providerReadiness.logic";
const readyCodex: ServerProvider = {
instanceId: ProviderInstanceId.make("codex"),
driver: ProviderDriverKind.make("codex"),
enabled: true,
installed: true,
version: "1.0.0",
status: "ready",
auth: { status: "unknown" },
checkedAt: "2026-08-23T00:00:00.000Z",
models: [],
slashCommands: [],
skills: [],
};
describe("getOnboardingProviderState", () => {
it("treats authenticated providers as ready only when their provider status is ready", () => {
expect(getOnboardingProviderState({ ...readyCodex, auth: { status: "authenticated" } })).toBe(
"ready",
);
expect(
getOnboardingProviderState({
...readyCodex,
auth: { status: "authenticated" },
status: "error",
}),
).toBe("attention");
expect(
getOnboardingProviderState({
...readyCodex,
auth: { status: "authenticated" },
status: "warning",
}),
).toBe("attention");
});
it("offers sign-in only when the server reports an authentication failure", () => {
expect(
getOnboardingProviderState({
...readyCodex,
status: "error",
auth: { status: "unauthenticated" },
}),
).toBe("signIn");
expect(getOnboardingProviderState({ ...readyCodex, status: "error" })).toBe("attention");
expect(getOnboardingProviderState({ ...readyCodex, status: "warning" })).toBe("attention");
});
it("does not offer installation or sign-in for disabled providers", () => {
expect(getOnboardingProviderState({ ...readyCodex, enabled: false, installed: false })).toBe(
"disabled",
);
expect(getOnboardingProviderState({ ...readyCodex, status: "disabled" })).toBe("disabled");
});
it("offers installation only when an enabled provider is missing", () => {
expect(getOnboardingProviderState({ ...readyCodex, installed: false, status: "error" })).toBe(
"install",
);
});
});
describe("selectOnboardingProvidersByDriver", () => {
it("prefers a ready instance with unknown authentication to an unauthenticated instance", () => {
const signedOutCodex: ServerProvider = {
...readyCodex,
instanceId: ProviderInstanceId.make("codex_work"),
status: "error",
auth: { status: "unauthenticated" },
};
expect(selectOnboardingProvidersByDriver([signedOutCodex, readyCodex]).get("codex")).toBe(
readyCodex,
);
});
it("prefers a provider with an actionable sign-in over a failed provider", () => {
const failedCodex: ServerProvider = { ...readyCodex, status: "error" };
const signedOutCodex: ServerProvider = {
...readyCodex,
instanceId: ProviderInstanceId.make("codex_work"),
status: "error",
auth: { status: "unauthenticated" },
};
expect(selectOnboardingProvidersByDriver([failedCodex, signedOutCodex]).get("codex")).toBe(
signedOutCodex,
);
});
it("prefers installed providers over missing or disabled instances", () => {
const disabledCodex: ServerProvider = { ...readyCodex, enabled: false };
const missingCodex: ServerProvider = {
...readyCodex,
instanceId: ProviderInstanceId.make("codex_work"),
installed: false,
status: "error",
};
expect(
selectOnboardingProvidersByDriver([disabledCodex, missingCodex, readyCodex]).get("codex"),
).toBe(readyCodex);
});
});
describe("resolveOnboardingProviderLoginCommand", () => {
it("uses the selected Codex account binary", () => {
const provider = { ...readyCodex, instanceId: ProviderInstanceId.make("codex_work") };
expect(
resolveOnboardingProviderLoginCommand(
provider,
{
...DEFAULT_SERVER_SETTINGS,
providerInstances: {
[provider.instanceId]: {
driver: provider.driver,
config: { binaryPath: "/opt/codex-work/bin/codex" },
},
},
},
"linux",
),
).toBe("/opt/codex-work/bin/codex login");
});
it("quotes a Codex path with spaces for PowerShell", () => {
expect(
resolveOnboardingProviderLoginCommand(
readyCodex,
{
...DEFAULT_SERVER_SETTINGS,
providerInstances: {
[ProviderInstanceId.make("codex")]: {
driver: ProviderDriverKind.make("codex"),
config: {
binaryPath: "C:\\Program Files\\Codex & Tools\\codex.exe",
},
},
},
},
"windows",
),
).toBe("& 'C:\\Program Files\\Codex & Tools\\codex.exe' login");
});
it("quotes a Claude path with shell metacharacters on POSIX", () => {
const provider: ServerProvider = {
...readyCodex,
driver: ProviderDriverKind.make("claudeAgent"),
instanceId: ProviderInstanceId.make("claude"),
};
expect(
resolveOnboardingProviderLoginCommand(
provider,
{
...DEFAULT_SERVER_SETTINGS,
providerInstances: {
[ProviderInstanceId.make("claude")]: {
driver: ProviderDriverKind.make("claudeAgent"),
config: {
binaryPath: "/opt/Claude Tools/$current/claude",
},
},
},
},
"linux",
),
).toBe("'/opt/Claude Tools/$current/claude' auth login");
});
it.each([
["~/my tools/codex", "~/'my tools/codex' login"],
["~\\my tools/codex", "~/'my tools/codex' login"],
["~/tools/codex's build", `~/'tools/codex'"'"'s build' login`],
["~\\tools\\codex's build", `~/'tools\\codex'"'"'s build' login`],
["~/tools/codex; echo unsafe", "~/'tools/codex; echo unsafe' login"],
])("keeps the home prefix expandable while quoting %s", (binaryPath, expectedCommand) => {
expect(
resolveOnboardingProviderLoginCommand(
readyCodex,
{
...DEFAULT_SERVER_SETTINGS,
providerInstances: {
[ProviderInstanceId.make("codex")]: {
driver: ProviderDriverKind.make("codex"),
config: {
binaryPath,
},
},
},
},
"linux",
),
).toBe(expectedCommand);
});
it("uses the default command when an old server reports an unknown shell", () => {
expect(
resolveOnboardingProviderLoginCommand(
readyCodex,
{
...DEFAULT_SERVER_SETTINGS,
providerInstances: {
[ProviderInstanceId.make("codex")]: {
driver: ProviderDriverKind.make("codex"),
config: {
binaryPath: "/opt/Codex Tools/codex",
},
},
},
},
"unknown",
),
).toBe("codex login");
});
});