Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2import {
3 createOllamaClient,
4 generateId,
5 getOllamaHostFromEnv,
6} from '../src/utils'
7import { estimateTokens } from '../src/utils/client'
8
9vi.mock('ollama', () => {
10 return {
11 Ollama: class {
12 constructor(public readonly config: unknown) {}
13 },
14 }
15})
16
17afterEach(() => {
18 vi.unstubAllEnvs()
19})
20
21describe('createOllamaClient', () => {
22 it('defaults to http://localhost:11434 when no host is given', () => {
23 const client = createOllamaClient() as unknown as {
24 config: { host: string }
25 }
26 expect(client.config.host).toBe('http://localhost:11434')
27 })
28
29 it('respects an explicit host override', () => {
30 const client = createOllamaClient({
31 host: 'https://ollama.example.com',
32 }) as unknown as { config: { host: string } }
33 expect(client.config.host).toBe('https://ollama.example.com')
34 })
35
36 it('forwards custom headers', () => {
37 const client = createOllamaClient({
38 headers: { Authorization: 'Bearer xyz' },
39 }) as unknown as { config: { headers: Record<string, string> } }
40 expect(client.config.headers).toEqual({ Authorization: 'Bearer xyz' })
41 })
42
43 it('maps baseURL and defaultHeaders onto host and headers', () => {
44 const client = createOllamaClient({
45 baseURL: 'https://gw.example/ollama',
46 defaultHeaders: { 'X-Gateway': 'yes' },
47 }) as unknown as {
48 config: { host: string; headers: Record<string, string> }
49 }
50 expect(client.config.host).toBe('https://gw.example/ollama')
51 expect(client.config.headers).toEqual({ 'X-Gateway': 'yes' })
52 })
53
54 it('lets baseURL and defaultHeaders win over host and headers', () => {
55 const client = createOllamaClient({
56 host: 'https://old.example',
57 headers: { a: '1', b: 'old' },
58 baseURL: 'https://new.example',
59 defaultHeaders: { b: 'new' },
60 }) as unknown as {
61 config: { host: string; headers: Record<string, string> }
62 }
63 expect(client.config.host).toBe('https://new.example')
64 expect(client.config.headers).toEqual({ b: 'new' })
65 })
66})
67
68describe('getOllamaHostFromEnv', () => {
69 it('reads OLLAMA_HOST from process.env when set', () => {
70 vi.stubEnv('OLLAMA_HOST', 'http://custom-host:9999')
71 expect(getOllamaHostFromEnv()).toBe('http://custom-host:9999')
72 })
73
74 it('falls back to localhost:11434 when OLLAMA_HOST is empty', () => {
75 vi.stubEnv('OLLAMA_HOST', '')
76 expect(getOllamaHostFromEnv()).toBe('http://localhost:11434')
77 })
78})
79
80describe('generateId', () => {
81 it('uses the provided prefix', () => {
82 expect(generateId('tool_call')).toMatch(/^tool_call-\d+-[a-z0-9]+$/)
83 })
84
85 it('defaults to msg prefix', () => {
86 expect(generateId()).toMatch(/^msg-\d+-/)
87 })
88
89 it('generates distinct ids on repeated calls', () => {
90 const ids = new Set<string>()
91 for (let i = 0; i < 25; i++) ids.add(generateId('x'))
92 expect(ids.size).toBe(25)
93 })
94})
95
96describe('estimateTokens', () => {
97 it('returns 0 for empty string', () => {
98 expect(estimateTokens('')).toBe(0)
99 })
100
101 it('rounds up to whole tokens (~4 chars each)', () => {
102 expect(estimateTokens('1234')).toBe(1)
103 expect(estimateTokens('12345')).toBe(2)
104 })
105})