-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathcompose_test.go
More file actions
170 lines (159 loc) · 5.59 KB
/
Copy pathcompose_test.go
File metadata and controls
170 lines (159 loc) · 5.59 KB
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
package commands
import (
"fmt"
"math"
"testing"
"github.com/docker/model-runner/pkg/inference"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestUpCommandContextSizeFlagBehavior verifies that the --context-size flag on
// the compose up command is not "changed" by default (i.e. nil ContextSize
// should be sent when the flag is absent) and is marked as changed after an
// explicit value is provided.
func TestUpCommandContextSizeFlagBehavior(t *testing.T) {
t.Run("context-size flag not changed by default", func(t *testing.T) {
cmd := newUpCommand()
// Parse with just the required --model flag — no --context-size.
err := cmd.ParseFlags([]string{"--model", "mymodel"})
require.NoError(t, err)
// The flag must NOT be marked as changed so that ContextSize is omitted
// from the configure request (i.e. remains nil).
assert.False(t, cmd.Flags().Changed("context-size"),
"context-size must not be Changed when the flag is absent")
})
t.Run("context-size flag changed after explicit value", func(t *testing.T) {
cmd := newUpCommand()
err := cmd.ParseFlags([]string{"--model", "mymodel", "--context-size", "4096"})
require.NoError(t, err)
assert.True(t, cmd.Flags().Changed("context-size"),
"context-size must be Changed when explicitly provided")
})
t.Run("context-size flag changed with unlimited value -1", func(t *testing.T) {
cmd := newUpCommand()
err := cmd.ParseFlags([]string{"--model", "mymodel", "--context-size", "-1"})
require.NoError(t, err)
assert.True(t, cmd.Flags().Changed("context-size"),
"context-size must be Changed when explicitly set to -1 (unlimited)")
})
t.Run("ContextSize is nil in BackendConfiguration when flag not set", func(t *testing.T) {
cmd := newUpCommand()
require.NoError(t, cmd.ParseFlags([]string{"--model", "mymodel"}))
// Simulate the logic in compose.go RunE: only add ContextSize when Changed.
backendConfig := inference.BackendConfiguration{}
if cmd.Flags().Changed("context-size") {
size := int32(-1) // default value
backendConfig.ContextSize = &size
}
assert.Nil(t, backendConfig.ContextSize,
"ContextSize must be nil in BackendConfiguration when --context-size is not provided")
})
t.Run("ContextSize is non-nil in BackendConfiguration when flag is set", func(t *testing.T) {
cmd := newUpCommand()
require.NoError(t, cmd.ParseFlags([]string{"--model", "mymodel", "--context-size", "64000"}))
ctxSize, err := cmd.Flags().GetInt64("context-size")
require.NoError(t, err)
backendConfig := inference.BackendConfiguration{}
if cmd.Flags().Changed("context-size") {
size := int32(ctxSize)
backendConfig.ContextSize = &size
}
require.NotNil(t, backendConfig.ContextSize,
"ContextSize must be non-nil when --context-size is provided")
assert.Equal(t, int32(64000), *backendConfig.ContextSize)
})
t.Run("context-size above int32 max is out of range", func(t *testing.T) {
tooBig := int64(math.MaxInt32) + 1
cmd := newUpCommand()
require.NoError(t, cmd.ParseFlags([]string{"--model", "mymodel", "--context-size", fmt.Sprintf("%d", tooBig)}))
ctxSize, err := cmd.Flags().GetInt64("context-size")
require.NoError(t, err)
require.True(t, cmd.Flags().Changed("context-size"))
// Simulate the range check from compose.go RunE.
if ctxSize > math.MaxInt32 || ctxSize < math.MinInt32 {
// Expected: would return an error in RunE.
return
}
t.Fatal("expected out-of-range check to trigger for value above MaxInt32")
})
t.Run("context-size below int32 min is out of range", func(t *testing.T) {
tooSmall := int64(math.MinInt32) - 1
cmd := newUpCommand()
require.NoError(t, cmd.ParseFlags([]string{"--model", "mymodel", "--context-size", fmt.Sprintf("%d", tooSmall)}))
ctxSize, err := cmd.Flags().GetInt64("context-size")
require.NoError(t, err)
require.True(t, cmd.Flags().Changed("context-size"))
if ctxSize > math.MaxInt32 || ctxSize < math.MinInt32 {
return
}
t.Fatal("expected out-of-range check to trigger for value below MinInt32")
})
}
func TestParseBackendMode(t *testing.T) {
tests := []struct {
name string
input string
expected inference.BackendMode
expectError bool
}{
{
name: "completion mode lowercase",
input: "completion",
expected: inference.BackendModeCompletion,
expectError: false,
},
{
name: "completion mode uppercase",
input: "COMPLETION",
expected: inference.BackendModeCompletion,
expectError: false,
},
{
name: "completion mode mixed case",
input: "Completion",
expected: inference.BackendModeCompletion,
expectError: false,
},
{
name: "embedding mode",
input: "embedding",
expected: inference.BackendModeEmbedding,
expectError: false,
},
{
name: "reranking mode",
input: "reranking",
expected: inference.BackendModeReranking,
expectError: false,
},
{
name: "image-generation mode",
input: "image-generation",
expected: inference.BackendModeImageGeneration,
expectError: false,
},
{
name: "invalid mode",
input: "invalid",
expected: inference.BackendModeCompletion, // default on error
expectError: true,
},
{
name: "empty string",
input: "",
expected: inference.BackendModeCompletion, // default on error
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parseBackendMode(tt.input)
if tt.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}