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

ChatView.modelOptions.test.ts

116 lines3.8 KB

1import type {
2 ModelCapabilities,
3 ProviderOptionDescriptor,
4 ProviderOptionSelection,
5} from "@t3tools/contracts";
6import { describe, expect, it } from "vite-plus/test";
7​
8import { preserveCompatibleOptions } from "./ChatView.modelOptions";
9​
10function selectDescriptor(
11 id: string,
12 options: Array<{ id: string; label: string; isDefault?: boolean }>,
13): ProviderOptionDescriptor {
14 return {
15 type: "select",
16 id,
17 label: id,
18 options: options.map((option) => ({
19 id: option.id,
20 label: option.label,
21 ...(option.isDefault ? { isDefault: true } : {}),
22 })),
23 };
25​
26function caps(descriptors: ProviderOptionDescriptor[]): ModelCapabilities {
27 return { optionDescriptors: descriptors };
29​
30const effortDescriptor = selectDescriptor("effort", [
31 { id: "low", label: "Low", isDefault: true },
32 { id: "high", label: "High" },
33]);
34​
35const variantDescriptor = selectDescriptor("variant", [
36 { id: "fast", label: "Fast", isDefault: true },
37 { id: "thorough", label: "Thorough" },
38]);
39​
40describe("preserveCompatibleOptions", () => {
41 it("keeps options whose descriptor id exists on the next model", () => {
42 const current: ProviderOptionSelection[] = [
43 { id: "effort", value: "high" },
44 { id: "agent", value: "plan" },
45 ];
46 const nextCaps = caps([effortDescriptor]);
47​
48 const result = preserveCompatibleOptions(current, nextCaps);
49​
50 expect(result).toEqual([{ id: "effort", value: "high" }]);
51 });
52​
53 it("preserves the user's explicit value, not the new model's default", () => {
54 const current: ProviderOptionSelection[] = [{ id: "effort", value: "high" }];
55 const nextCaps = caps([effortDescriptor]);
56​
57 const result = preserveCompatibleOptions(current, nextCaps);
58​
59 expect(result).toEqual([{ id: "effort", value: "high" }]);
60 });
61​
62 it("drops options the new model does not expose", () => {
63 const current: ProviderOptionSelection[] = [
64 { id: "effort", value: "high" },
65 { id: "agent", value: "plan" },
66 ];
67 const nextCaps = caps([effortDescriptor, variantDescriptor]);
68​
69 const result = preserveCompatibleOptions(current, nextCaps);
70​
71 expect(result).toEqual([{ id: "effort", value: "high" }]);
72 });
73​
74 it("returns undefined when no options survive", () => {
75 const current: ProviderOptionSelection[] = [{ id: "agent", value: "plan" }];
76 const nextCaps = caps([effortDescriptor]);
77​
78 expect(preserveCompatibleOptions(current, nextCaps)).toBeUndefined();
79 });
80​
81 it("returns undefined when the next model has no option descriptors", () => {
82 const current: ProviderOptionSelection[] = [{ id: "effort", value: "high" }];
83 const nextCaps = caps([]);
84​
85 expect(preserveCompatibleOptions(current, nextCaps)).toBeUndefined();
86 });
87​
88 it("returns undefined when there are no current options", () => {
89 expect(preserveCompatibleOptions([], caps([effortDescriptor]))).toBeUndefined();
90 expect(preserveCompatibleOptions(null, caps([effortDescriptor]))).toBeUndefined();
91 expect(preserveCompatibleOptions(undefined, caps([effortDescriptor]))).toBeUndefined();
92 });
93​
94 it("returns undefined when capabilities are null", () => {
95 const current: ProviderOptionSelection[] = [{ id: "effort", value: "high" }];
96​
97 expect(preserveCompatibleOptions(current, null)).toBeUndefined();
98 expect(preserveCompatibleOptions(current, undefined)).toBeUndefined();
99 });
100​
101 it("carries over multiple compatible options", () => {
102 const current: ProviderOptionSelection[] = [
103 { id: "effort", value: "high" },
104 { id: "variant", value: "thorough" },
105 { id: "agent", value: "plan" },
106 ];
107 const nextCaps = caps([effortDescriptor, variantDescriptor]);
108​
109 const result = preserveCompatibleOptions(current, nextCaps);
110​
111 expect(result).toEqual([
112 { id: "effort", value: "high" },
113 { id: "variant", value: "thorough" },
114 ]);
115 });
116});