-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_test.go
More file actions
262 lines (214 loc) · 4.9 KB
/
context_test.go
File metadata and controls
262 lines (214 loc) · 4.9 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package cortex_test
import (
"sync"
"testing"
"github.com/kolosys/cortex"
)
func TestEvalContextBasic(t *testing.T) {
ctx := cortex.NewEvalContext()
if ctx.ID == "" {
t.Error("expected non-empty ID")
}
ctx.Set("key", "value")
v, ok := ctx.Get("key")
if !ok || v != "value" {
t.Errorf("expected value='value', got %v", v)
}
if !ctx.Has("key") {
t.Error("expected Has('key') to be true")
}
ctx.Delete("key")
if ctx.Has("key") {
t.Error("expected Has('key') to be false after delete")
}
}
func TestEvalContextTyped(t *testing.T) {
ctx := cortex.NewEvalContext()
cortex.SetTyped(ctx, "str", "hello")
cortex.SetTyped(ctx, "num", 42.5)
cortex.SetTyped(ctx, "bool", true)
s, ok := cortex.GetTyped[string](ctx, "str")
if !ok || s != "hello" {
t.Errorf("expected 'hello', got %v", s)
}
n, ok := cortex.GetTyped[float64](ctx, "num")
if !ok || n != 42.5 {
t.Errorf("expected 42.5, got %v", n)
}
b, ok := cortex.GetTyped[bool](ctx, "bool")
if !ok || !b {
t.Errorf("expected true, got %v", b)
}
// Wrong type
_, ok = cortex.GetTyped[int](ctx, "str")
if ok {
t.Error("expected false for wrong type")
}
}
func TestEvalContextGetFloat64(t *testing.T) {
ctx := cortex.NewEvalContext()
tests := []struct {
name string
value any
expected float64
}{
{"float64", 42.5, 42.5},
{"float32", float32(42.5), 42.5},
{"int", 42, 42.0},
{"int64", int64(42), 42.0},
{"int32", int32(42), 42.0},
{"uint", uint(42), 42.0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx.Set("value", tt.value)
f, err := ctx.GetFloat64("value")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if f != tt.expected {
t.Errorf("expected %f, got %f", tt.expected, f)
}
})
}
}
func TestEvalContextGetString(t *testing.T) {
ctx := cortex.NewEvalContext()
ctx.Set("str", "hello")
s, err := ctx.GetString("str")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if s != "hello" {
t.Errorf("expected 'hello', got %q", s)
}
// Not found
_, err = ctx.GetString("missing")
if err == nil {
t.Error("expected error for missing key")
}
// Wrong type
ctx.Set("num", 42)
_, err = ctx.GetString("num")
if err == nil {
t.Error("expected error for wrong type")
}
}
func TestEvalContextGetBool(t *testing.T) {
ctx := cortex.NewEvalContext()
ctx.Set("flag", true)
b, err := ctx.GetBool("flag")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !b {
t.Error("expected true")
}
ctx.Set("flag2", false)
b, err = ctx.GetBool("flag2")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if b {
t.Error("expected false")
}
}
func TestEvalContextKeys(t *testing.T) {
ctx := cortex.NewEvalContext()
ctx.Set("a", 1)
ctx.Set("b", 2)
ctx.Set("c", 3)
keys := ctx.Keys()
if len(keys) != 3 {
t.Errorf("expected 3 keys, got %d", len(keys))
}
keySet := make(map[string]bool)
for _, k := range keys {
keySet[k] = true
}
for _, expected := range []string{"a", "b", "c"} {
if !keySet[expected] {
t.Errorf("expected key %q", expected)
}
}
}
func TestEvalContextValues(t *testing.T) {
ctx := cortex.NewEvalContext()
ctx.Set("x", 1)
ctx.Set("y", 2)
values := ctx.Values()
if len(values) != 2 {
t.Errorf("expected 2 values, got %d", len(values))
}
// Modify the returned map shouldn't affect original
values["z"] = 3
if ctx.Has("z") {
t.Error("modifying returned map should not affect context")
}
}
func TestEvalContextHalt(t *testing.T) {
ctx := cortex.NewEvalContext()
if ctx.IsHalted() {
t.Error("expected not halted initially")
}
ctx.Halt("test-rule")
if !ctx.IsHalted() {
t.Error("expected halted after Halt()")
}
if ctx.HaltedBy() != "test-rule" {
t.Errorf("expected haltedBy='test-rule', got %q", ctx.HaltedBy())
}
}
func TestEvalContextMetadata(t *testing.T) {
ctx := cortex.NewEvalContext()
ctx.SetMetadata("key", "value")
v, ok := ctx.GetMetadata("key")
if !ok || v != "value" {
t.Errorf("expected 'value', got %q", v)
}
_, ok = ctx.GetMetadata("missing")
if ok {
t.Error("expected false for missing metadata")
}
}
func TestEvalContextClone(t *testing.T) {
ctx := cortex.NewEvalContext()
ctx.Set("x", 42)
ctx.SetMetadata("env", "test")
clone := ctx.Clone()
// Clone should have same values
x, _ := clone.GetFloat64("x")
if x != 42 {
t.Errorf("expected x=42, got %f", x)
}
// Clone should have different ID
if clone.ID == ctx.ID {
t.Error("clone should have different ID")
}
// Modifying clone shouldn't affect original
clone.Set("y", 100)
if ctx.Has("y") {
t.Error("modifying clone should not affect original")
}
}
func TestEvalContextConcurrency(t *testing.T) {
ctx := cortex.NewEvalContext()
var wg sync.WaitGroup
// Concurrent writes
for i := range 100 {
wg.Add(1)
go func(i int) {
defer wg.Done()
ctx.Set("key", i)
}(i)
}
// Concurrent reads
for range 100 {
wg.Add(1)
go func() {
defer wg.Done()
ctx.Get("key")
}()
}
wg.Wait()
}