@@ -2,10 +2,208 @@ package expression
22
33import (
44 "context"
5+ "regexp"
56 "strings"
67 "testing"
78)
89
10+ // =============================================================================
11+ // UUID Built-in Tests
12+ // =============================================================================
13+
14+ var (
15+ uuidV4Regex = regexp .MustCompile (`^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$` )
16+ uuidV7Regex = regexp .MustCompile (`^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$` )
17+ )
18+
19+ func TestBuiltinUUID_DefaultIsV4 (t * testing.T ) {
20+ env := NewUnifiedEnv (nil )
21+ ctx := context .Background ()
22+
23+ result , err := env .Eval (ctx , "uuid()" )
24+ if err != nil {
25+ t .Fatalf ("unexpected error: %v" , err )
26+ }
27+
28+ str , ok := result .(string )
29+ if ! ok {
30+ t .Fatalf ("expected string, got %T" , result )
31+ }
32+
33+ if ! uuidV4Regex .MatchString (str ) {
34+ t .Errorf ("expected valid UUID v4, got: %s" , str )
35+ }
36+ }
37+
38+ func TestBuiltinUUID_ExplicitV4 (t * testing.T ) {
39+ env := NewUnifiedEnv (nil )
40+ ctx := context .Background ()
41+
42+ result , err := env .Eval (ctx , `uuid("v4")` )
43+ if err != nil {
44+ t .Fatalf ("unexpected error: %v" , err )
45+ }
46+
47+ str , ok := result .(string )
48+ if ! ok {
49+ t .Fatalf ("expected string, got %T" , result )
50+ }
51+
52+ if ! uuidV4Regex .MatchString (str ) {
53+ t .Errorf ("expected valid UUID v4, got: %s" , str )
54+ }
55+ }
56+
57+ func TestBuiltinUUID_V7 (t * testing.T ) {
58+ env := NewUnifiedEnv (nil )
59+ ctx := context .Background ()
60+
61+ result , err := env .Eval (ctx , `uuid("v7")` )
62+ if err != nil {
63+ t .Fatalf ("unexpected error: %v" , err )
64+ }
65+
66+ str , ok := result .(string )
67+ if ! ok {
68+ t .Fatalf ("expected string, got %T" , result )
69+ }
70+
71+ if ! uuidV7Regex .MatchString (str ) {
72+ t .Errorf ("expected valid UUID v7, got: %s" , str )
73+ }
74+ }
75+
76+ func TestBuiltinUUID_InvalidVersion (t * testing.T ) {
77+ env := NewUnifiedEnv (nil )
78+ ctx := context .Background ()
79+
80+ _ , err := env .Eval (ctx , `uuid("v5")` )
81+ if err == nil {
82+ t .Fatal ("expected error for unsupported version, got nil" )
83+ }
84+
85+ if ! strings .Contains (err .Error (), "unsupported version" ) {
86+ t .Errorf ("expected 'unsupported version' in error, got: %v" , err )
87+ }
88+ }
89+
90+ func TestBuiltinUUID_UniquePerCall (t * testing.T ) {
91+ env := NewUnifiedEnv (nil )
92+ ctx := context .Background ()
93+
94+ result1 , err := env .Eval (ctx , "uuid()" )
95+ if err != nil {
96+ t .Fatalf ("unexpected error: %v" , err )
97+ }
98+
99+ result2 , err := env .Eval (ctx , "uuid()" )
100+ if err != nil {
101+ t .Fatalf ("unexpected error: %v" , err )
102+ }
103+
104+ if result1 == result2 {
105+ t .Errorf ("expected unique UUIDs, got same value twice: %v" , result1 )
106+ }
107+ }
108+
109+ func TestBuiltinUUID_Interpolation (t * testing.T ) {
110+ env := NewUnifiedEnv (nil )
111+
112+ result , err := env .Interpolate ("id={{ uuid() }}" )
113+ if err != nil {
114+ t .Fatalf ("unexpected error: %v" , err )
115+ }
116+
117+ if ! strings .HasPrefix (result , "id=" ) {
118+ t .Errorf ("expected 'id=' prefix, got: %s" , result )
119+ }
120+
121+ uuidPart := strings .TrimPrefix (result , "id=" )
122+ if ! uuidV4Regex .MatchString (uuidPart ) {
123+ t .Errorf ("expected valid UUID v4 after prefix, got: %s" , uuidPart )
124+ }
125+ }
126+
127+ func TestBuiltinUUID_V7Interpolation (t * testing.T ) {
128+ env := NewUnifiedEnv (nil )
129+
130+ result , err := env .Interpolate (`id={{ uuid("v7") }}` )
131+ if err != nil {
132+ t .Fatalf ("unexpected error: %v" , err )
133+ }
134+
135+ uuidPart := strings .TrimPrefix (result , "id=" )
136+ if ! uuidV7Regex .MatchString (uuidPart ) {
137+ t .Errorf ("expected valid UUID v7 after prefix, got: %s" , uuidPart )
138+ }
139+ }
140+
141+ // =============================================================================
142+ // ULID Built-in Tests
143+ // =============================================================================
144+
145+ var ulidRegex = regexp .MustCompile (`^[0-9A-HJKMNP-TV-Z]{26}$` )
146+
147+ func TestBuiltinULID_Eval (t * testing.T ) {
148+ env := NewUnifiedEnv (nil )
149+ ctx := context .Background ()
150+
151+ result , err := env .Eval (ctx , "ulid()" )
152+ if err != nil {
153+ t .Fatalf ("unexpected error: %v" , err )
154+ }
155+
156+ str , ok := result .(string )
157+ if ! ok {
158+ t .Fatalf ("expected string, got %T" , result )
159+ }
160+
161+ if ! ulidRegex .MatchString (str ) {
162+ t .Errorf ("expected valid ULID, got: %s" , str )
163+ }
164+ }
165+
166+ func TestBuiltinULID_UniquePerCall (t * testing.T ) {
167+ env := NewUnifiedEnv (nil )
168+ ctx := context .Background ()
169+
170+ result1 , err := env .Eval (ctx , "ulid()" )
171+ if err != nil {
172+ t .Fatalf ("unexpected error: %v" , err )
173+ }
174+
175+ result2 , err := env .Eval (ctx , "ulid()" )
176+ if err != nil {
177+ t .Fatalf ("unexpected error: %v" , err )
178+ }
179+
180+ if result1 == result2 {
181+ t .Errorf ("expected unique ULIDs, got same value twice: %v" , result1 )
182+ }
183+ }
184+
185+ func TestBuiltinULID_Interpolation (t * testing.T ) {
186+ env := NewUnifiedEnv (nil )
187+
188+ result , err := env .Interpolate ("id={{ ulid() }}" )
189+ if err != nil {
190+ t .Fatalf ("unexpected error: %v" , err )
191+ }
192+
193+ if ! strings .HasPrefix (result , "id=" ) {
194+ t .Errorf ("expected 'id=' prefix, got: %s" , result )
195+ }
196+
197+ ulidPart := strings .TrimPrefix (result , "id=" )
198+ if ! ulidRegex .MatchString (ulidPart ) {
199+ t .Errorf ("expected valid ULID after prefix, got: %s" , ulidPart )
200+ }
201+ }
202+
203+ // =============================================================================
204+ // AI Built-in Tests
205+ // =============================================================================
206+
9207func TestBuiltinAI_ErrorWhenNotFound (t * testing.T ) {
10208 env := NewUnifiedEnv (nil )
11209
0 commit comments