interactive_test.go 3.38 KB
Newer Older
mashun1's avatar
v1  
mashun1 committed
1
2
3
4
5
package cmd

import (
	"testing"

xuxzh1's avatar
init  
xuxzh1 committed
6
	"github.com/google/go-cmp/cmp"
mashun1's avatar
v1  
mashun1 committed
7
8
9
10
11
12
13
14
	"github.com/stretchr/testify/assert"

	"github.com/ollama/ollama/api"
)

func TestExtractFilenames(t *testing.T) {
	// Unix style paths
	input := ` some preamble 
xuxzh1's avatar
update  
xuxzh1 committed
15
16
 ./relative\ path/one.png inbetween1 ./not a valid two.jpg inbetween2 ./1.svg
/unescaped space /three.jpeg inbetween3 /valid\ path/dir/four.png "./quoted with spaces/five.JPG`
mashun1's avatar
v1  
mashun1 committed
17
18
19
20
21
22
	res := extractFileNames(input)
	assert.Len(t, res, 5)
	assert.Contains(t, res[0], "one.png")
	assert.Contains(t, res[1], "two.jpg")
	assert.Contains(t, res[2], "three.jpeg")
	assert.Contains(t, res[3], "four.png")
xuxzh1's avatar
update  
xuxzh1 committed
23
	assert.Contains(t, res[4], "five.JPG")
mashun1's avatar
v1  
mashun1 committed
24
	assert.NotContains(t, res[4], '"')
xuxzh1's avatar
update  
xuxzh1 committed
25
26
	assert.NotContains(t, res, "inbetween1")
	assert.NotContains(t, res, "./1.svg")
mashun1's avatar
v1  
mashun1 committed
27
28
29
30
31

	// Windows style paths
	input = ` some preamble
 c:/users/jdoe/one.png inbetween1 c:/program files/someplace/two.jpg inbetween2 
 /absolute/nospace/three.jpeg inbetween3 /absolute/with space/four.png inbetween4
xuxzh1's avatar
update  
xuxzh1 committed
32
33
34
./relative\ path/five.JPG inbetween5 "./relative with/spaces/six.png inbetween6
d:\path with\spaces\seven.JPEG inbetween7 c:\users\jdoe\eight.png inbetween8 
 d:\program files\someplace\nine.png inbetween9 "E:\program files\someplace\ten.PNG some ending
mashun1's avatar
v1  
mashun1 committed
35
36
37
`
	res = extractFileNames(input)
	assert.Len(t, res, 10)
xuxzh1's avatar
update  
xuxzh1 committed
38
	assert.NotContains(t, res, "inbetween2")
mashun1's avatar
v1  
mashun1 committed
39
40
41
42
43
44
	assert.Contains(t, res[0], "one.png")
	assert.Contains(t, res[0], "c:")
	assert.Contains(t, res[1], "two.jpg")
	assert.Contains(t, res[1], "c:")
	assert.Contains(t, res[2], "three.jpeg")
	assert.Contains(t, res[3], "four.png")
xuxzh1's avatar
update  
xuxzh1 committed
45
	assert.Contains(t, res[4], "five.JPG")
mashun1's avatar
v1  
mashun1 committed
46
	assert.Contains(t, res[5], "six.png")
xuxzh1's avatar
update  
xuxzh1 committed
47
	assert.Contains(t, res[6], "seven.JPEG")
mashun1's avatar
v1  
mashun1 committed
48
49
50
51
52
	assert.Contains(t, res[6], "d:")
	assert.Contains(t, res[7], "eight.png")
	assert.Contains(t, res[7], "c:")
	assert.Contains(t, res[8], "nine.png")
	assert.Contains(t, res[8], "d:")
xuxzh1's avatar
update  
xuxzh1 committed
53
	assert.Contains(t, res[9], "ten.PNG")
mashun1's avatar
v1  
mashun1 committed
54
55
56
57
58
	assert.Contains(t, res[9], "E:")
}

func TestModelfileBuilder(t *testing.T) {
	opts := runOptions{
xuxzh1's avatar
init  
xuxzh1 committed
59
60
		Model:  "hork",
		System: "You are part horse and part shark, but all hork. Do horklike things",
mashun1's avatar
v1  
mashun1 committed
61
62
63
64
		Messages: []api.Message{
			{Role: "user", Content: "Hey there hork!"},
			{Role: "assistant", Content: "Yes it is true, I am half horse, half shark."},
		},
xuxzh1's avatar
init  
xuxzh1 committed
65
66
67
68
69
70
		Options: map[string]any{
			"temperature":      0.9,
			"seed":             42,
			"penalize_newline": false,
			"stop":             []string{"hi", "there"},
		},
mashun1's avatar
v1  
mashun1 committed
71
72
	}

xuxzh1's avatar
init  
xuxzh1 committed
73
74
75
	t.Run("model", func(t *testing.T) {
		expect := `FROM hork
SYSTEM You are part horse and part shark, but all hork. Do horklike things
mashun1's avatar
v1  
mashun1 committed
76
77
PARAMETER penalize_newline false
PARAMETER seed 42
xuxzh1's avatar
init  
xuxzh1 committed
78
79
PARAMETER stop hi
PARAMETER stop there
mashun1's avatar
v1  
mashun1 committed
80
PARAMETER temperature 0.9
xuxzh1's avatar
init  
xuxzh1 committed
81
82
MESSAGE user Hey there hork!
MESSAGE assistant Yes it is true, I am half horse, half shark.
mashun1's avatar
v1  
mashun1 committed
83
84
`

xuxzh1's avatar
init  
xuxzh1 committed
85
86
87
88
89
		actual := buildModelfile(opts)
		if diff := cmp.Diff(expect, actual); diff != "" {
			t.Errorf("mismatch (-want +got):\n%s", diff)
		}
	})
mashun1's avatar
v1  
mashun1 committed
90

xuxzh1's avatar
init  
xuxzh1 committed
91
92
93
94
	t.Run("parent model", func(t *testing.T) {
		opts.ParentModel = "horseshark"
		expect := `FROM horseshark
SYSTEM You are part horse and part shark, but all hork. Do horklike things
mashun1's avatar
v1  
mashun1 committed
95
96
PARAMETER penalize_newline false
PARAMETER seed 42
xuxzh1's avatar
init  
xuxzh1 committed
97
98
PARAMETER stop hi
PARAMETER stop there
mashun1's avatar
v1  
mashun1 committed
99
PARAMETER temperature 0.9
xuxzh1's avatar
init  
xuxzh1 committed
100
101
MESSAGE user Hey there hork!
MESSAGE assistant Yes it is true, I am half horse, half shark.
mashun1's avatar
v1  
mashun1 committed
102
`
xuxzh1's avatar
init  
xuxzh1 committed
103
104
105
106
107
		actual := buildModelfile(opts)
		if diff := cmp.Diff(expect, actual); diff != "" {
			t.Errorf("mismatch (-want +got):\n%s", diff)
		}
	})
mashun1's avatar
v1  
mashun1 committed
108
}