1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
import type { ModelMetadata } from "@dispatch/transport-contract";
import { describe, expect, it } from "vitest";
import {
AUTO_COMPACTION_MODEL,
compactionModelChanged,
compactionModelFromValue,
compactionModelOptions,
DEFAULT_IMAGE_LIMIT,
imageLimitChanged,
imageLimitLabel,
MAX_IMAGE_LIMIT,
normalizeVisionSettings,
parseImageLimit,
selectedCompactionValue,
} from "./view-model";
describe("normalizeVisionSettings", () => {
it("returns the value as-is for a well-formed body", () => {
expect(normalizeVisionSettings({ imageLimit: 5, compactionModel: "kimi/k2" })).toEqual({
imageLimit: 5,
compactionModel: "kimi/k2",
});
});
it("coerces a null compactionModel to null", () => {
expect(normalizeVisionSettings({ imageLimit: 10, compactionModel: null })).toEqual({
imageLimit: 10,
compactionModel: null,
});
});
it("defaults imageLimit when absent or non-numeric", () => {
expect(normalizeVisionSettings({ compactionModel: "kimi/k2" })).toEqual({
imageLimit: DEFAULT_IMAGE_LIMIT,
compactionModel: "kimi/k2",
});
expect(normalizeVisionSettings({ imageLimit: "oops", compactionModel: null })).toEqual({
imageLimit: DEFAULT_IMAGE_LIMIT,
compactionModel: null,
});
});
it("floors a fractional imageLimit", () => {
expect(normalizeVisionSettings({ imageLimit: 7.9, compactionModel: null }).imageLimit).toBe(7);
});
it("defaults for a null/non-object body (never crashes)", () => {
expect(normalizeVisionSettings(null)).toEqual({
imageLimit: DEFAULT_IMAGE_LIMIT,
compactionModel: null,
});
expect(normalizeVisionSettings("nope")).toEqual({
imageLimit: DEFAULT_IMAGE_LIMIT,
compactionModel: null,
});
});
it("treats a negative imageLimit as the default", () => {
expect(normalizeVisionSettings({ imageLimit: -3, compactionModel: null }).imageLimit).toBe(
DEFAULT_IMAGE_LIMIT,
);
});
it("treats an empty compactionModel string as null", () => {
expect(
normalizeVisionSettings({ imageLimit: 10, compactionModel: "" }).compactionModel,
).toBeNull();
});
});
describe("parseImageLimit", () => {
it("parses a non-negative integer", () => {
expect(parseImageLimit("5")).toEqual({ ok: true, value: 5 });
expect(parseImageLimit("0")).toEqual({ ok: true, value: 0 });
});
it("floors a fractional value", () => {
expect(parseImageLimit("7.9")).toEqual({ ok: true, value: 7 });
});
it("trims whitespace", () => {
expect(parseImageLimit(" 12 ")).toEqual({ ok: true, value: 12 });
});
it("errors on empty input", () => {
expect(parseImageLimit("")).toEqual({ ok: false, error: "Enter a number." });
});
it("errors on non-numeric input", () => {
expect(parseImageLimit("lots").ok).toBe(false);
});
it("errors on a negative value", () => {
expect(parseImageLimit("-1").ok).toBe(false);
});
it("errors when above the max", () => {
expect(parseImageLimit(String(MAX_IMAGE_LIMIT + 1)).ok).toBe(false);
});
it("accepts the max", () => {
expect(parseImageLimit(String(MAX_IMAGE_LIMIT))).toEqual({ ok: true, value: MAX_IMAGE_LIMIT });
});
});
describe("imageLimitChanged", () => {
it("is true when the typed value differs after normalization", () => {
expect(imageLimitChanged("5", 10)).toBe(true);
});
it("is false when equal", () => {
expect(imageLimitChanged("10", 10)).toBe(false);
});
it("is false for invalid input", () => {
expect(imageLimitChanged("abc", 10)).toBe(false);
expect(imageLimitChanged("", 10)).toBe(false);
});
it("compares after flooring", () => {
expect(imageLimitChanged("7.9", 7)).toBe(false);
});
});
describe("compactionModelOptions", () => {
const modelInfo: Record<string, ModelMetadata> = {
"kimi/k2": { vision: true },
"kimi/k1.5": { vision: true },
"umans/glm-5.2": { vision: false },
"openai/gpt-4": { contextWindow: 128000 },
};
it("includes the Auto option first", () => {
const opts = compactionModelOptions([], {});
expect(opts).toHaveLength(1);
expect(opts[0]?.auto).toBe(true);
expect(opts[0]?.value).toBe(AUTO_COMPACTION_MODEL);
});
it("includes only vision-capable models, in catalog order", () => {
const models = ["umans/glm-5.2", "kimi/k2", "openai/gpt-4", "kimi/k1.5"];
const opts = compactionModelOptions(models, modelInfo);
expect(opts.map((o) => o.label)).toEqual(["Auto (server-selected)", "kimi/k2", "kimi/k1.5"]);
});
it("excludes non-vision models even with metadata present", () => {
const opts = compactionModelOptions(["umans/glm-5.2"], modelInfo);
expect(opts).toHaveLength(1); // only Auto
});
});
describe("compactionModel value round-trip", () => {
it("selectedCompactionValue maps null to the auto sentinel", () => {
expect(selectedCompactionValue(null)).toBe(AUTO_COMPACTION_MODEL);
});
it("selectedCompactionValue maps a model name to itself", () => {
expect(selectedCompactionValue("kimi/k2")).toBe("kimi/k2");
});
it("compactionModelFromValue maps the auto sentinel back to null", () => {
expect(compactionModelFromValue(AUTO_COMPACTION_MODEL)).toBeNull();
});
it("compactionModelFromValue maps a model name to itself", () => {
expect(compactionModelFromValue("kimi/k2")).toBe("kimi/k2");
});
});
describe("compactionModelChanged", () => {
it("is true when the value maps to a different model", () => {
expect(compactionModelChanged("kimi/k2", null)).toBe(true);
expect(compactionModelChanged(AUTO_COMPACTION_MODEL, "kimi/k2")).toBe(true);
});
it("is false when equal (null vs auto, or same model)", () => {
expect(compactionModelChanged(AUTO_COMPACTION_MODEL, null)).toBe(false);
expect(compactionModelChanged("kimi/k2", "kimi/k2")).toBe(false);
});
});
describe("imageLimitLabel", () => {
it("labels null as loading", () => {
expect(imageLimitLabel(null)).toBe("Loading…");
});
it("labels 0 as disabled", () => {
expect(imageLimitLabel(0)).toBe("0 (compaction disabled)");
});
it("labels the default with (default)", () => {
expect(imageLimitLabel(DEFAULT_IMAGE_LIMIT)).toBe(`${DEFAULT_IMAGE_LIMIT} (default)`);
});
it("labels other values plainly", () => {
expect(imageLimitLabel(7)).toBe("7");
});
});
|