Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

12.9 KB
Raw Blame
/**
 * Constants Validation Tests
 *
 * Tests data integrity of exported constants from constants.ts:
 * - DEFAULT_SETTINGS — required fields + valid types
 * - BUILT_IN_PERSONAS — structure, uniqueness
 * - ONBOARDING_MODELS — structure, URLs, uniqueness
 * - CAVEMAN_PROMPTS — all levels present
 * - CAVEMAN_REMINDERS — all levels present
 * - FEATURE_FLAGS — valid booleans
 *
 * Run: npx vitest run src/lib/__tests__/constants-validation.test.ts
 */
import { describe, it, expect } from 'vitest'
import { DEFAULT_EFFORT } from '../effort'
import {
  DEFAULT_SETTINGS,
  BUILT_IN_PERSONAS,
  ONBOARDING_MODELS,
  CAVEMAN_PROMPTS,
  CAVEMAN_REMINDERS,
  FEATURE_FLAGS,
} from '../constants'

describe('constants-validation', () => {
  // ── DEFAULT_SETTINGS ─────────────────────────────────────────

  describe('DEFAULT_SETTINGS', () => {
    it('has apiEndpoint as a string', () => {
      expect(typeof DEFAULT_SETTINGS.apiEndpoint).toBe('string')
      expect(DEFAULT_SETTINGS.apiEndpoint.length).toBeGreaterThan(0)
    })

    it('has temperature as a number between 0 and 2', () => {
      expect(typeof DEFAULT_SETTINGS.temperature).toBe('number')
      expect(DEFAULT_SETTINGS.temperature).toBeGreaterThanOrEqual(0)
      expect(DEFAULT_SETTINGS.temperature).toBeLessThanOrEqual(2)
    })

    it('has topP as a number between 0 and 1', () => {
      expect(typeof DEFAULT_SETTINGS.topP).toBe('number')
      expect(DEFAULT_SETTINGS.topP).toBeGreaterThanOrEqual(0)
      expect(DEFAULT_SETTINGS.topP).toBeLessThanOrEqual(1)
    })

    it('has topK as a positive number', () => {
      expect(typeof DEFAULT_SETTINGS.topK).toBe('number')
      expect(DEFAULT_SETTINGS.topK).toBeGreaterThan(0)
    })

    it('has maxTokens as a number', () => {
      expect(typeof DEFAULT_SETTINGS.maxTokens).toBe('number')
    })

    it('has theme as light or dark', () => {
      expect(['light', 'dark']).toContain(DEFAULT_SETTINGS.theme)
    })

    it('has onboardingDone as boolean', () => {
      expect(typeof DEFAULT_SETTINGS.onboardingDone).toBe('boolean')
    })

    it('has thinkingEnabled as boolean', () => {
      expect(typeof DEFAULT_SETTINGS.thinkingEnabled).toBe('boolean')
    })

    it('has reasoningEffort on the rung the client has always sent', () => {
      // Not a taste. The default IS the old behaviour: 'high' is what the
      // provider put on the wire for thinking ON before the control existed,
      // so a profile that rides the v22 migration sends byte-for-byte the same
      // request it sent before. Any other default moves an existing customer's
      // token bill without them touching anything.
      expect(DEFAULT_SETTINGS.reasoningEffort).toBe('high')
      expect(DEFAULT_SETTINGS.reasoningEffort).toBe(DEFAULT_EFFORT)
    })

    it('has cavemanMode as a valid level', () => {
      expect(['off', 'lite', 'full', 'ultra']).toContain(DEFAULT_SETTINGS.cavemanMode)
    })

    it('has searchProvider as a valid value', () => {
      expect(['auto', 'duckduckgo', 'brave', 'tavily']).toContain(DEFAULT_SETTINGS.searchProvider)
    })

    it('has braveApiKey as string', () => {
      expect(typeof DEFAULT_SETTINGS.braveApiKey).toBe('string')
    })

    it('has tavilyApiKey as string', () => {
      expect(typeof DEFAULT_SETTINGS.tavilyApiKey).toBe('string')
    })
  })

  // ── BUILT_IN_PERSONAS ────────────────────────────────────────

  describe('BUILT_IN_PERSONAS', () => {
    it('has at least 5 personas', () => {
      expect(BUILT_IN_PERSONAS.length).toBeGreaterThanOrEqual(5)
    })

    it('all personas have required fields', () => {
      for (const persona of BUILT_IN_PERSONAS) {
        expect(typeof persona.id).toBe('string')
        expect(persona.id.length).toBeGreaterThan(0)
        expect(typeof persona.name).toBe('string')
        expect(persona.name.length).toBeGreaterThan(0)
        expect(typeof persona.icon).toBe('string')
        expect(persona.icon.length).toBeGreaterThan(0)
        expect(typeof persona.systemPrompt).toBe('string')
        expect(persona.isBuiltIn).toBe(true)
      }
    })

    it('all persona IDs are unique', () => {
      const ids = BUILT_IN_PERSONAS.map(p => p.id)
      const uniqueIds = new Set(ids)
      expect(uniqueIds.size).toBe(ids.length)
    })

    it('contains the "assistant" default persona', () => {
      const assistant = BUILT_IN_PERSONAS.find(p => p.id === 'assistant')
      expect(assistant).toBeDefined()
      expect(assistant!.systemPrompt.length).toBeGreaterThan(0)
    })

    it('contains the "coder" persona', () => {
      const coder = BUILT_IN_PERSONAS.find(p => p.id === 'coder')
      expect(coder).toBeDefined()
    })

    // Bis 10.09.2026 stand hier `toBe('')`. Ein leerer Systemtext ist aber
    // nicht neutral: das Modell faellt dann auf seine eigene Grundhaltung
    // zurueck, und gemessen an 46 Katalogmodellen hat genau das sechs Modelle
    // vom Antworten aufs Ablehnen gebracht. Die Rolle steht jetzt drin, der
    // Test haelt fest, dass sie da ist und keine Inhaltsregel enthaelt.
    // R5-3: leer, damit der Grundtext greift und der Hausteil genau einmal
    // rausgeht. Die Rolle steht in CHAT_BASE_SYSTEM_PROMPT, nicht hier.
    it('contains the "unrestricted" persona, and it carries no text of its own', () => {
      const unrestricted = BUILT_IN_PERSONAS.find(p => p.id === 'unrestricted')
      expect(unrestricted).toBeDefined()
      expect(unrestricted!.systemPrompt).toBe('')
    })

    it('all persona names are unique', () => {
      const names = BUILT_IN_PERSONAS.map(p => p.name)
      const uniqueNames = new Set(names)
      expect(uniqueNames.size).toBe(names.length)
    })
  })

  // ── ONBOARDING_MODELS ────────────────────────────────────────

  describe('ONBOARDING_MODELS', () => {
    // Previously asserted ≥10 entries; P4 (Sweep #2) intentionally trimmed
    // the curated list down to a single starter pick. The list is allowed
    // to grow over time — keep a permissive lower bound rather than a
    // hard count.
    it('is non-empty', () => {
      expect(ONBOARDING_MODELS.length).toBeGreaterThanOrEqual(1)
    })

    it('all models have required fields', () => {
      for (const model of ONBOARDING_MODELS) {
        expect(typeof model.name).toBe('string')
        expect(model.name.length).toBeGreaterThan(0)
        expect(typeof model.label).toBe('string')
        expect(model.label.length).toBeGreaterThan(0)
        expect(typeof model.description).toBe('string')
        expect(model.description.length).toBeGreaterThan(0)
        expect(typeof model.size).toBe('string')
        expect(typeof model.vram).toBe('string')
        expect(typeof model.vramGB).toBe('number')
        expect(model.vramGB).toBeGreaterThan(0)
      }
    })

    it('all models have valid download URLs', () => {
      for (const model of ONBOARDING_MODELS) {
        expect(typeof model.downloadUrl).toBe('string')
        expect(model.downloadUrl).toContain('huggingface.co')
        expect(model.downloadUrl).toContain('/resolve/main/')
      }
    })

    it('all models have valid filenames ending in .gguf', () => {
      for (const model of ONBOARDING_MODELS) {
        expect(typeof model.filename).toBe('string')
        expect(model.filename.toLowerCase()).toMatch(/\.gguf$/)
      }
    })

    it('all models have sizeGB > 0', () => {
      for (const model of ONBOARDING_MODELS) {
        expect(typeof model.sizeGB).toBe('number')
        expect(model.sizeGB).toBeGreaterThan(0)
      }
    })

    it('all model names are unique', () => {
      const names = ONBOARDING_MODELS.map(m => m.name)
      const uniqueNames = new Set(names)
      expect(uniqueNames.size).toBe(names.length)
    })

    // P4 (Sweep #2 + Sweep #4 b1) intentionally trims ONBOARDING_MODELS
    // to a single entry — the tiny 0.5B starter pick. Previous assertions
    // demanded both categories / agent-capable entries, which were valid
    // before the trim and break by design now. Replaced with assertions
    // that fit the current "one-and-only starter" contract: the list is
    // never empty, contains exactly one recommended model, and the
    // Onboarding tab-default logic in `Onboarding.tsx::initialSubTab`
    // can resolve to a non-empty filter for whatever category the
    // starter belongs to.
    it('has at least one model', () => {
      expect(ONBOARDING_MODELS.length).toBeGreaterThan(0)
    })

    it('has exactly one recommended starter model', () => {
      const recommended = ONBOARDING_MODELS.filter(m => m.recommended === true)
      expect(recommended.length).toBe(1)
    })

    it('default sub-tab can resolve to a non-empty filter', () => {
      // Mirrors Onboarding.tsx::initialSubTab — ensures the tab default
      // points at whichever category actually has entries, so the user
      // never lands on an empty starter step.
      const hasUncensored = ONBOARDING_MODELS.some(m => m.uncensored)
      const initialTab: 'uncensored' | 'mainstream' = hasUncensored ? 'uncensored' : 'mainstream'
      const visible = ONBOARDING_MODELS.filter(m =>
        initialTab === 'uncensored' ? m.uncensored : !m.uncensored,
      )
      expect(visible.length).toBeGreaterThan(0)
    })

    it('downloadUrl contains the filename', () => {
      for (const model of ONBOARDING_MODELS) {
        expect(model.downloadUrl).toContain(model.filename)
      }
    })
  })

  // ── CAVEMAN_PROMPTS ──────────────────────────────────────────

  describe('CAVEMAN_PROMPTS', () => {
    it('has all 3 levels: lite, full, ultra', () => {
      expect(CAVEMAN_PROMPTS).toHaveProperty('lite')
      expect(CAVEMAN_PROMPTS).toHaveProperty('full')
      expect(CAVEMAN_PROMPTS).toHaveProperty('ultra')
    })

    it('all prompts are non-empty strings', () => {
      expect(typeof CAVEMAN_PROMPTS.lite).toBe('string')
      expect(CAVEMAN_PROMPTS.lite.length).toBeGreaterThan(0)
      expect(typeof CAVEMAN_PROMPTS.full).toBe('string')
      expect(CAVEMAN_PROMPTS.full.length).toBeGreaterThan(0)
      expect(typeof CAVEMAN_PROMPTS.ultra).toBe('string')
      expect(CAVEMAN_PROMPTS.ultra.length).toBeGreaterThan(0)
    })

    it('ultra is the most restrictive (contains brevity keywords)', () => {
      expect(CAVEMAN_PROMPTS.ultra.toLowerCase()).toContain('brevity')
    })

    it('lite preserves grammar (mentions grammar or articles)', () => {
      const lite = CAVEMAN_PROMPTS.lite.toLowerCase()
      expect(lite).toContain('grammar')
    })

    it('all levels contain "ACTIVE EVERY RESPONSE" or style enforcement', () => {
      // All prompts should have some enforcement mechanism
      for (const level of ['lite', 'full', 'ultra'] as const) {
        const prompt = CAVEMAN_PROMPTS[level].toLowerCase()
        const hasEnforcement = prompt.includes('every response') || prompt.includes('active')
        expect(hasEnforcement).toBe(true)
      }
    })
  })

  // ── CAVEMAN_REMINDERS ────────────────────────────────────────

  describe('CAVEMAN_REMINDERS', () => {
    it('has all 3 levels: lite, full, ultra', () => {
      expect(CAVEMAN_REMINDERS).toHaveProperty('lite')
      expect(CAVEMAN_REMINDERS).toHaveProperty('full')
      expect(CAVEMAN_REMINDERS).toHaveProperty('ultra')
    })

    it('all reminders are non-empty strings', () => {
      expect(typeof CAVEMAN_REMINDERS.lite).toBe('string')
      expect(CAVEMAN_REMINDERS.lite.length).toBeGreaterThan(0)
      expect(typeof CAVEMAN_REMINDERS.full).toBe('string')
      expect(CAVEMAN_REMINDERS.full.length).toBeGreaterThan(0)
      expect(typeof CAVEMAN_REMINDERS.ultra).toBe('string')
      expect(CAVEMAN_REMINDERS.ultra.length).toBeGreaterThan(0)
    })

    it('all reminders are wrapped in square brackets', () => {
      for (const level of ['lite', 'full', 'ultra'] as const) {
        expect(CAVEMAN_REMINDERS[level]).toMatch(/^\[.*\]$/)
      }
    })

    it('reminders are shorter than prompts', () => {
      for (const level of ['lite', 'full', 'ultra'] as const) {
        expect(CAVEMAN_REMINDERS[level].length).toBeLessThan(CAVEMAN_PROMPTS[level].length)
      }
    })
  })

  // ── FEATURE_FLAGS ────────────────────────────────────────────

  describe('FEATURE_FLAGS', () => {
    it('has AGENT_MODE as boolean', () => {
      expect(typeof FEATURE_FLAGS.AGENT_MODE).toBe('boolean')
    })

    it('has AGENT_WORKFLOWS as boolean', () => {
      expect(typeof FEATURE_FLAGS.AGENT_WORKFLOWS).toBe('boolean')
    })

    it('all flags are booleans', () => {
      for (const [key, value] of Object.entries(FEATURE_FLAGS)) {
        expect(typeof value, `FEATURE_FLAGS.${key}`).toBe('boolean')
      }
    })
  })
})