Spaces:
Paused
Paused
File size: 8,600 Bytes
a5784e9 | 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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | /**
* SettingsPanel Component Tests
*
* Tests the sub-components (Slider, Toggle, CollapsibleSection) in isolation
*/
import { describe, it, expect, vi } from 'vitest';
// Since we can't easily import the internal components, we test the logic
// =============================================
// Slider Logic Tests
// =============================================
describe('Slider Logic', () => {
describe('value parsing', () => {
it('parses integer values correctly', () => {
const value = parseFloat('8192');
expect(value).toBe(8192);
});
it('parses float values correctly', () => {
const value = parseFloat('0.95');
expect(value).toBe(0.95);
});
it('handles NaN gracefully', () => {
const value = parseFloat('invalid');
expect(isNaN(value)).toBe(true);
});
it('handles empty string', () => {
const value = parseFloat('');
expect(isNaN(value)).toBe(true);
});
});
describe('step formatting', () => {
it('formats decimal values with toFixed when step < 1', () => {
const value = 0.754321;
const step = 0.01;
const formatted = step < 1 ? value.toFixed(2) : value.toString();
expect(formatted).toBe('0.75');
});
it('keeps integers as-is when step >= 1', () => {
const value = 8192;
const step = 1;
const formatted = step < 1 ? value.toFixed(2) : value.toString();
expect(formatted).toBe('8192');
});
});
describe('range constraints', () => {
it('allows values at minimum', () => {
const min = 0;
const max = 100;
const value = 0;
expect(value >= min && value <= max).toBe(true);
});
it('allows values at maximum', () => {
const min = 0;
const max = 100;
const value = 100;
expect(value >= min && value <= max).toBe(true);
});
it('allows values within range', () => {
const min = 0;
const max = 100;
const value = 50;
expect(value >= min && value <= max).toBe(true);
});
});
});
// =============================================
// Toggle Logic Tests
// =============================================
describe('Toggle Logic', () => {
describe('state changes', () => {
it('toggles true to false', () => {
const current = true;
expect(!current).toBe(false);
});
it('toggles false to true', () => {
const current = false;
expect(!current).toBe(true);
});
});
describe('disabled state', () => {
it('does not toggle when disabled', () => {
const disabled = true;
const current = false;
const onChange = vi.fn();
if (!disabled) {
onChange(!current);
}
expect(onChange).not.toHaveBeenCalled();
});
it('toggles when not disabled', () => {
const disabled = false;
const current = false;
const onChange = vi.fn();
if (!disabled) {
onChange(!current);
}
expect(onChange).toHaveBeenCalledWith(true);
});
});
});
// =============================================
// CollapsibleSection Logic Tests
// =============================================
describe('CollapsibleSection Logic', () => {
describe('initial state', () => {
it('manages multiple section states', () => {
const expandedSections: Record<string, boolean> = {
model: true,
thinking: true,
params: true,
tools: true,
system: false,
};
expect(expandedSections.model).toBe(true);
expect(expandedSections.system).toBe(false);
});
});
describe('toggle behavior', () => {
it('toggles section from expanded to collapsed', () => {
const prev = { model: true };
const section = 'model';
const next = { ...prev, [section]: !prev[section] };
expect(next.model).toBe(false);
});
it('toggles section from collapsed to expanded', () => {
const prev = { model: false };
const section = 'model';
const next = { ...prev, [section]: !prev[section] };
expect(next.model).toBe(true);
});
it('preserves other sections when toggling', () => {
const prev = { model: true, thinking: true, params: false };
const section = 'model';
const next = { ...prev, [section]: !prev[section] };
expect(next.model).toBe(false);
expect(next.thinking).toBe(true);
expect(next.params).toBe(false);
});
});
});
// =============================================
// ThinkingLevel Options Builder Tests
// =============================================
describe('Thinking Level Options Builder', () => {
type ThinkingLevel = 'minimal' | 'low' | 'medium' | 'high';
interface CategoryCapabilities {
thinkingType: 'level' | 'budget' | 'none';
levels?: string[];
}
function buildLevelOptions(capabilities: CategoryCapabilities | undefined): { value: ThinkingLevel | ''; label: string }[] {
if (capabilities?.thinkingType !== 'level' || !capabilities.levels) {
return [];
}
const options: { value: ThinkingLevel | ''; label: string }[] = [
{ value: '', label: '未指定' }
];
for (const level of capabilities.levels) {
options.push({
value: level as ThinkingLevel,
label: level.charAt(0).toUpperCase() + level.slice(1)
});
}
return options;
}
it('returns empty array when capabilities is undefined', () => {
expect(buildLevelOptions(undefined)).toEqual([]);
});
it('returns empty array for budget type', () => {
expect(buildLevelOptions({ thinkingType: 'budget' })).toEqual([]);
});
it('returns empty array for none type', () => {
expect(buildLevelOptions({ thinkingType: 'none' })).toEqual([]);
});
it('builds options for Gemini 3 Flash (4 levels)', () => {
const capabilities: CategoryCapabilities = {
thinkingType: 'level',
levels: ['minimal', 'low', 'medium', 'high'],
};
const options = buildLevelOptions(capabilities);
expect(options).toHaveLength(5); // 4 levels + unspecified
expect(options[0]).toEqual({ value: '', label: '未指定' });
expect(options[1]).toEqual({ value: 'minimal', label: 'Minimal' });
expect(options[4]).toEqual({ value: 'high', label: 'High' });
});
it('builds options for Gemini 3 Pro (2 levels)', () => {
const capabilities: CategoryCapabilities = {
thinkingType: 'level',
levels: ['low', 'high'],
};
const options = buildLevelOptions(capabilities);
expect(options).toHaveLength(3); // 2 levels + unspecified
expect(options[1]).toEqual({ value: 'low', label: 'Low' });
expect(options[2]).toEqual({ value: 'high', label: 'High' });
});
it('capitalizes first letter of level names', () => {
const capabilities: CategoryCapabilities = {
thinkingType: 'level',
levels: ['medium'],
};
const options = buildLevelOptions(capabilities);
expect(options[1].label).toBe('Medium');
});
});
// =============================================
// Budget Range Logic Tests
// =============================================
describe('Budget Range Logic', () => {
interface CategoryCapabilities {
budgetRange?: [number, number];
}
function getBudgetRange(capabilities: CategoryCapabilities | undefined): { min: number; max: number } {
if (capabilities?.budgetRange) {
return { min: capabilities.budgetRange[0], max: capabilities.budgetRange[1] };
}
return { min: 512, max: 24576 };
}
it('returns model-specific range when available', () => {
const capabilities: CategoryCapabilities = {
budgetRange: [128, 32768],
};
expect(getBudgetRange(capabilities)).toEqual({ min: 128, max: 32768 });
});
it('returns default range when capabilities undefined', () => {
expect(getBudgetRange(undefined)).toEqual({ min: 512, max: 24576 });
});
it('returns default range when budgetRange undefined', () => {
expect(getBudgetRange({})).toEqual({ min: 512, max: 24576 });
});
it('handles Gemini 2.5 Pro range', () => {
const capabilities: CategoryCapabilities = {
budgetRange: [128, 32768],
};
const range = getBudgetRange(capabilities);
expect(range.max).toBe(32768);
});
it('handles Gemini 2.5 Flash range', () => {
const capabilities: CategoryCapabilities = {
budgetRange: [0, 24576],
};
const range = getBudgetRange(capabilities);
expect(range.min).toBe(0);
expect(range.max).toBe(24576);
});
});
|