|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { migrateLegacyVariedPack, parseThreadSettings, resolveThreadActiveModelPackId } from '../settings.js'; |
| 4 | +import type { GlobalSettings, StorageSettings } from '../settings.js'; |
| 5 | + |
| 6 | +function createSettings(overrides?: Partial<GlobalSettings>): GlobalSettings { |
| 7 | + const storage: StorageSettings = { backend: 'libsql', libsql: {}, pg: {} }; |
| 8 | + return { |
| 9 | + onboarding: { |
| 10 | + completedAt: null, |
| 11 | + skippedAt: null, |
| 12 | + version: 0, |
| 13 | + modePackId: null, |
| 14 | + omPackId: null, |
| 15 | + claudeMaxOAuthWarningAcknowledgedAt: null, |
| 16 | + }, |
| 17 | + models: { |
| 18 | + activeModelPackId: 'anthropic', |
| 19 | + modeDefaults: {}, |
| 20 | + activeOmPackId: null, |
| 21 | + omModelOverride: null, |
| 22 | + subagentModels: {}, |
| 23 | + }, |
| 24 | + preferences: { yolo: null, theme: 'auto' }, |
| 25 | + storage, |
| 26 | + customModelPacks: [ |
| 27 | + { |
| 28 | + name: 'My Pack', |
| 29 | + models: { |
| 30 | + plan: 'openai/gpt-5.3-codex', |
| 31 | + build: 'anthropic/claude-sonnet-4-5', |
| 32 | + fast: 'openai/gpt-5.1-codex-mini', |
| 33 | + }, |
| 34 | + createdAt: '2026-01-01T00:00:00.000Z', |
| 35 | + }, |
| 36 | + ], |
| 37 | + modelUseCounts: {}, |
| 38 | + ...overrides, |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +const builtinPacks = [ |
| 43 | + { |
| 44 | + id: 'anthropic', |
| 45 | + models: { |
| 46 | + plan: 'anthropic/claude-sonnet-4-5', |
| 47 | + build: 'anthropic/claude-sonnet-4-5', |
| 48 | + fast: 'anthropic/claude-haiku-4-5', |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + id: 'openai', |
| 53 | + models: { |
| 54 | + plan: 'openai/gpt-5.3-codex', |
| 55 | + build: 'openai/gpt-5.3-codex', |
| 56 | + fast: 'openai/gpt-5.1-codex-mini', |
| 57 | + }, |
| 58 | + }, |
| 59 | +]; |
| 60 | + |
| 61 | +describe('parseThreadSettings', () => { |
| 62 | + it('extracts active pack and mode model ids from metadata', () => { |
| 63 | + const parsed = parseThreadSettings({ |
| 64 | + activeModelPackId: 'custom:My Pack', |
| 65 | + modeModelId_plan: 'openai/gpt-5.3-codex', |
| 66 | + modeModelId_build: 'anthropic/claude-sonnet-4-5', |
| 67 | + ignored: 123, |
| 68 | + }); |
| 69 | + |
| 70 | + expect(parsed.activeModelPackId).toBe('custom:My Pack'); |
| 71 | + expect(parsed.modeModelIds).toEqual({ |
| 72 | + plan: 'openai/gpt-5.3-codex', |
| 73 | + build: 'anthropic/claude-sonnet-4-5', |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('returns empty values when metadata is undefined', () => { |
| 78 | + const parsed = parseThreadSettings(undefined); |
| 79 | + |
| 80 | + expect(parsed.activeModelPackId).toBeNull(); |
| 81 | + expect(parsed.modeModelIds).toEqual({}); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +describe('resolveThreadActiveModelPackId', () => { |
| 86 | + it('prefers explicit thread metadata pack id when valid', () => { |
| 87 | + const settings = createSettings(); |
| 88 | + |
| 89 | + const resolved = resolveThreadActiveModelPackId(settings, builtinPacks, { |
| 90 | + activeModelPackId: 'custom:My Pack', |
| 91 | + }); |
| 92 | + |
| 93 | + expect(resolved).toBe('custom:My Pack'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('infers pack from thread modeModelId values when explicit pack id is missing', () => { |
| 97 | + const settings = createSettings({ models: { ...createSettings().models, activeModelPackId: 'anthropic' } }); |
| 98 | + |
| 99 | + const resolved = resolveThreadActiveModelPackId(settings, builtinPacks, { |
| 100 | + modeModelId_plan: 'openai/gpt-5.3-codex', |
| 101 | + modeModelId_build: 'openai/gpt-5.3-codex', |
| 102 | + modeModelId_fast: 'openai/gpt-5.1-codex-mini', |
| 103 | + }); |
| 104 | + |
| 105 | + expect(resolved).toBe('openai'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('falls back to global activeModelPackId when no thread metadata matches', () => { |
| 109 | + const settings = createSettings({ models: { ...createSettings().models, activeModelPackId: 'anthropic' } }); |
| 110 | + |
| 111 | + const resolved = resolveThreadActiveModelPackId(settings, builtinPacks, { |
| 112 | + modeModelId_plan: 'unknown/model', |
| 113 | + }); |
| 114 | + |
| 115 | + expect(resolved).toBe('anthropic'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('returns null when global activeModelPackId points to a deleted custom pack', () => { |
| 119 | + const settings = createSettings({ |
| 120 | + customModelPacks: [], |
| 121 | + models: { ...createSettings().models, activeModelPackId: 'custom:Deleted Pack' }, |
| 122 | + }); |
| 123 | + |
| 124 | + const resolved = resolveThreadActiveModelPackId(settings, builtinPacks, { |
| 125 | + modeModelId_plan: 'unknown/model', |
| 126 | + }); |
| 127 | + |
| 128 | + expect(resolved).toBeNull(); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +describe('migrateLegacyVariedPack', () => { |
| 133 | + it('migrates legacy varied active selection to a custom varied pack', () => { |
| 134 | + const settings = createSettings({ |
| 135 | + models: { ...createSettings().models, activeModelPackId: 'varied', modeDefaults: {} }, |
| 136 | + onboarding: { ...createSettings().onboarding, modePackId: 'varied' }, |
| 137 | + customModelPacks: [], |
| 138 | + }); |
| 139 | + |
| 140 | + const migrated = migrateLegacyVariedPack(settings); |
| 141 | + |
| 142 | + expect(migrated).toBe(true); |
| 143 | + expect(settings.models.activeModelPackId).toBe('custom:varied'); |
| 144 | + expect(settings.onboarding.modePackId).toBe('custom:varied'); |
| 145 | + expect(settings.customModelPacks.find(p => p.name === 'varied')).toBeDefined(); |
| 146 | + expect(settings.models.modeDefaults).toEqual({ |
| 147 | + plan: 'openai/gpt-5.3-codex', |
| 148 | + build: 'anthropic/claude-sonnet-4-5', |
| 149 | + fast: 'anthropic/claude-haiku-4-5', |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments