Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 ProviderInstanceId,
5 ServerProvider,
6 type ProviderOptionSelection,
7} from "@t3tools/contracts";
8import * as Schema from "effect/Schema";
9
10import type { ModelOption } from "../../lib/modelOptions";
11import {
12 canCommitPendingModel,
13 modelMatchesCatalogQuery,
14 pendingModelAfterPress,
15 providerSetupCandidates,
16} from "./thread-settings-sheet-state";
17
18function modelOption(
19 model: string,
20 options: ReadonlyArray<ProviderOptionSelection> = [],
21): ModelOption {
22 return {
23 key: `codex:${model}`,
24 label: model,
25 subtitle: "",
26 providerKey: "codex",
27 providerLabel: "Codex",
28 providerDriver: "codex",
29 isDefault: false,
30 isLegacy: false,
31 capabilities: null,
32 selection: {
33 instanceId: ProviderInstanceId.make("codex"),
34 model,
35 options,
36 },
37 };
38}
39
40describe("thread settings sheet state", () => {
41 it("matches visible model and provider terms", () => {
42 const model = modelOption("gpt-next");
43
44 expect(modelMatchesCatalogQuery({ model, providerLabel: "Codex", query: "NEXT" })).toBe(true);
45 expect(modelMatchesCatalogQuery({ model, providerLabel: "Codex", query: "codex" })).toBe(true);
46 expect(modelMatchesCatalogQuery({ model, providerLabel: "Codex", query: "claude" })).toBe(
47 false,
48 );
49 });
50
51 it("treats whitespace-only catalog searches as empty", () => {
52 expect(
53 modelMatchesCatalogQuery({
54 model: modelOption("gpt-next"),
55 providerLabel: "Codex",
56 query: " ",
57 }),
58 ).toBe(true);
59 });
60
61 it("matches the upstream provider's display name", () => {
62 const model = {
63 ...modelOption("opencode/claude-fable-5"),
64 label: "Claude Fable 5",
65 subtitle: "OpenCode Zen",
66 };
67
68 expect(modelMatchesCatalogQuery({ model, providerLabel: "OpenCode", query: " ZEN " })).toBe(
69 true,
70 );
71 expect(modelMatchesCatalogQuery({ model, providerLabel: "OpenCode", query: "copilot" })).toBe(
72 false,
73 );
74 });
75
76 it("clears staging when the applied model is pressed", () => {
77 expect(
78 pendingModelAfterPress({
79 current: modelOption("gpt-next"),
80 pressed: modelOption("gpt-current"),
81 pressedIsApplied: true,
82 }),
83 ).toBeNull();
84 });
85
86 it("preserves staged options when the highlighted model is pressed again", () => {
87 const pending = modelOption("gpt-next", [{ id: "effort", value: "high" }]);
88
89 expect(
90 pendingModelAfterPress({
91 current: pending,
92 pressed: modelOption("gpt-next"),
93 pressedIsApplied: false,
94 }),
95 ).toBe(pending);
96 });
97
98 it("stages a different model", () => {
99 const pressed = modelOption("gpt-other");
100
101 expect(
102 pendingModelAfterPress({
103 current: modelOption("gpt-next"),
104 pressed,
105 pressedIsApplied: false,
106 }),
107 ).toBe(pressed);
108 });
109
110 it("cannot save a staged model after sign-out removes it from the catalog", () => {
111 const pending = modelOption("gemini-native");
112 const group = { providerKey: "codex", providerLabel: "Codex", models: [pending] };
113
114 expect(canCommitPendingModel(pending, [group])).toBe(true);
115 expect(canCommitPendingModel(pending, [])).toBe(false);
116 expect(
117 canCommitPendingModel(pending, [
118 {
119 ...group,
120 models: [{ ...pending, isUnavailable: true }],
121 },
122 ]),
123 ).toBe(false);
124 });
125});
126
127const decodeServerProvider = Schema.decodeSync(ServerProvider);
128
129function setupProvider(overrides: Partial<ServerProvider> = {}): ServerProvider {
130 return decodeServerProvider({
131 instanceId: "antigravity",
132 driver: "antigravity",
133 displayName: "Antigravity",
134 enabled: false,
135 installed: false,
136 version: null,
137 status: "disabled",
138 auth: { status: "unauthenticated" },
139 checkedAt: "2026-09-02T00:00:00.000Z",
140 setup: { canAuthenticate: true, canInstall: true },
141 models: [],
142 ...overrides,
143 });
144}
145
146describe("providerSetupCandidates", () => {
147 const unfiltered = { providerFilter: null, query: "" };
148
149 it("offers setup without a selectable model and after sign-out", () => {
150 const disabled = setupProvider();
151 const signedOut = setupProvider({ enabled: true, installed: true });
152
153 expect(providerSetupCandidates({ providers: [disabled], ...unfiltered })).toEqual([disabled]);
154 expect(providerSetupCandidates({ providers: [signedOut], ...unfiltered })).toEqual([signedOut]);
155 });
156
157 it("uses the selected environment's status for identical instance IDs", () => {
158 const offlineAccount = setupProvider();
159 const readyAccount = setupProvider({
160 enabled: true,
161 installed: true,
162 auth: { status: "authenticated" },
163 models: [{ slug: "gemini-native", name: "Gemini", isCustom: false, capabilities: null }],
164 });
165
166 expect(providerSetupCandidates({ providers: [offlineAccount], ...unfiltered })).toHaveLength(1);
167 expect(providerSetupCandidates({ providers: [readyAccount], ...unfiltered })).toEqual([]);
168 });
169
170 it("limits existing threads to their provider and respects search", () => {
171 const personal = setupProvider();
172 const work = setupProvider({
173 instanceId: ProviderInstanceId.make("google_work"),
174 displayName: "Work Google",
175 });
176
177 expect(
178 providerSetupCandidates({
179 providers: [personal, work],
180 ...unfiltered,
181 instanceId: work.instanceId,
182 }),
183 ).toEqual([work]);
184 expect(
185 providerSetupCandidates({
186 providers: [personal, work],
187 providerFilter: work.instanceId,
188 query: "work",
189 }),
190 ).toEqual([work]);
191 expect(
192 providerSetupCandidates({
193 providers: [personal, work],
194 providerFilter: null,
195 query: "no-match",
196 }),
197 ).toEqual([]);
198 });
199});