Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

thread-settings-sheet-state.test.ts

162 lines4.6 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { ProviderInstanceId, type ProviderOptionSelection } from "@t3tools/contracts";
4​
5import type { ModelOption } from "../../lib/modelOptions";
6import {
7 canCommitPendingModel,
8 favoritesFirst,
9 modelFavoriteKey,
10 modelMatchesCatalogQuery,
11 pendingModelAfterPress,
12 toggleModelFavorite,
13} from "./thread-settings-sheet-state";
14​
15function modelOption(
16 model: string,
17 options: ReadonlyArray<ProviderOptionSelection> = [],
18): ModelOption {
19 return {
20 key: `codex:${model}`,
21 label: model,
22 subtitle: "",
23 providerKey: "codex",
24 providerLabel: "Codex",
25 providerDriver: "codex",
26 isDefault: false,
27 isLegacy: false,
28 capabilities: null,
29 selection: {
30 instanceId: ProviderInstanceId.make("codex"),
31 model,
32 options,
33 },
34 };
36​
37describe("thread settings sheet state", () => {
38 it("keeps favorites in catalog order ahead of other models", () => {
39 const models = [
40 modelOption("first"),
41 modelOption("second"),
42 modelOption("third"),
43 modelOption("fourth"),
44 ];
45 const favorites = new Set([models[2]!.key, models[0]!.key]);
46​
47 expect(favoritesFirst(models, favorites).map((model) => model.selection.model)).toEqual([
48 "first",
49 "third",
50 "second",
51 "fourth",
52 ]);
53 expect(models.map((model) => model.selection.model)).toEqual([
54 "first",
55 "second",
56 "third",
57 "fourth",
58 ]);
59 });
60​
61 it("adds and removes favorites for one provider instance", () => {
62 const codexModel = modelOption("shared");
63 const otherProvider = ProviderInstanceId.make("codex_personal");
64 const personalModel = {
65 ...codexModel,
66 key: modelFavoriteKey(otherProvider, "shared"),
67 selection: { ...codexModel.selection, instanceId: otherProvider },
68 };
69 const favorites = toggleModelFavorite([], codexModel);
70​
71 expect(toggleModelFavorite(favorites, personalModel)).toEqual([
72 { provider: ProviderInstanceId.make("codex"), model: "shared" },
73 { provider: otherProvider, model: "shared" },
74 ]);
75 expect(toggleModelFavorite(favorites, codexModel)).toEqual([]);
76 });
77​
78 it("matches visible model and provider terms", () => {
79 const model = modelOption("gpt-next");
80​
81 expect(modelMatchesCatalogQuery({ model, providerLabel: "Codex", query: "NEXT" })).toBe(true);
82 expect(modelMatchesCatalogQuery({ model, providerLabel: "Codex", query: "codex" })).toBe(true);
83 expect(modelMatchesCatalogQuery({ model, providerLabel: "Codex", query: "claude" })).toBe(
84 false,
85 );
86 });
87​
88 it("treats whitespace-only catalog searches as empty", () => {
89 expect(
90 modelMatchesCatalogQuery({
91 model: modelOption("gpt-next"),
92 providerLabel: "Codex",
93 query: " ",
94 }),
95 ).toBe(true);
96 });
97​
98 it("matches the upstream provider's display name", () => {
99 const model = {
100 ...modelOption("opencode/claude-fable-5"),
101 label: "Claude Fable 5",
102 subtitle: "OpenCode Zen",
103 };
104​
105 expect(modelMatchesCatalogQuery({ model, providerLabel: "OpenCode", query: " ZEN " })).toBe(
106 true,
107 );
108 expect(modelMatchesCatalogQuery({ model, providerLabel: "OpenCode", query: "copilot" })).toBe(
109 false,
110 );
111 });
112​
113 it("clears staging when the applied model is pressed", () => {
114 expect(
115 pendingModelAfterPress({
116 current: modelOption("gpt-next"),
117 pressed: modelOption("gpt-current"),
118 pressedIsApplied: true,
119 }),
120 ).toBeNull();
121 });
122​
123 it("preserves staged options when the highlighted model is pressed again", () => {
124 const pending = modelOption("gpt-next", [{ id: "effort", value: "high" }]);
125​
126 expect(
127 pendingModelAfterPress({
128 current: pending,
129 pressed: modelOption("gpt-next"),
130 pressedIsApplied: false,
131 }),
132 ).toBe(pending);
133 });
134​
135 it("stages a different model", () => {
136 const pressed = modelOption("gpt-other");
137​
138 expect(
139 pendingModelAfterPress({
140 current: modelOption("gpt-next"),
141 pressed,
142 pressedIsApplied: false,
143 }),
144 ).toBe(pressed);
145 });
146​
147 it("cannot save a staged model after sign-out removes it from the catalog", () => {
148 const pending = modelOption("gemini-native");
149 const group = { providerKey: "codex", providerLabel: "Codex", models: [pending] };
150​
151 expect(canCommitPendingModel(pending, [group])).toBe(true);
152 expect(canCommitPendingModel(pending, [])).toBe(false);
153 expect(
154 canCommitPendingModel(pending, [
155 {
156 ...group,
157 models: [{ ...pending, isUnavailable: true }],
158 },
159 ]),
160 ).toBe(false);
161 });
162});