@@ -19,8 +19,11 @@ package com.google.a2ui.core.schema
1919import kotlin.test.Test
2020import kotlin.test.assertEquals
2121import kotlin.test.assertFailsWith
22+ import kotlin.test.assertFalse
2223import kotlin.test.assertNull
2324import kotlin.test.assertTrue
25+ import kotlinx.serialization.json.JsonObject
26+ import kotlinx.serialization.json.JsonPrimitive
2427
2528class CatalogTest {
2629
@@ -80,4 +83,154 @@ class CatalogTest {
8083 )
8184 assertEquals(" /absolute/examples" , config.examplesPath)
8285 }
86+
87+ @Test
88+ fun loadsExamplesWithGlob () {
89+ val tempDir = java.nio.file.Files .createTempDirectory(" examples" ).toFile()
90+ try {
91+ val nestedDir = java.io.File (tempDir, " nested" )
92+ nestedDir.mkdir()
93+
94+ java.io
95+ .File (tempDir, " top.json" )
96+ .writeText(" [{\" beginRendering\" : {\" surfaceId\" : \" top\" }}]" )
97+ java.io
98+ .File (nestedDir, " deep.json" )
99+ .writeText(" [{\" beginRendering\" : {\" surfaceId\" : \" deep\" }}]" )
100+ java.io.File (tempDir, " ignored.txt" ).writeText(" not json" )
101+
102+ val catalog =
103+ A2uiCatalog (
104+ version = A2uiVersion .VERSION_0_8 ,
105+ name = " basic" ,
106+ serverToClientSchema = JsonObject (emptyMap()),
107+ commonTypesSchema = JsonObject (emptyMap()),
108+ catalogSchema = JsonObject (mapOf (A2uiConstants .CATALOG_ID_KEY to JsonPrimitive (" basic" ))),
109+ )
110+
111+ // Match only top-level using a specific glob
112+ val examplesTop = catalog.loadExamples(" ${tempDir.path} /*.json" )
113+ assertTrue(examplesTop.contains(" ---BEGIN top---" ))
114+ assertFalse(examplesTop.contains(" ---BEGIN deep---" ))
115+
116+ // Match recursively using globstar
117+ val examplesAll = catalog.loadExamples(" ${tempDir.path} /**/*.json" )
118+ assertTrue(examplesAll.contains(" ---BEGIN top---" ))
119+ assertTrue(examplesAll.contains(" ---BEGIN deep---" ))
120+ } finally {
121+ tempDir.deleteRecursively()
122+ }
123+ }
124+
125+ @Test
126+ fun loadsExamplesWithGlobPrefixSuffix () {
127+ val tempDir = java.nio.file.Files .createTempDirectory(" examples" ).toFile()
128+ try {
129+ java.io
130+ .File (tempDir, " user_profile.json" )
131+ .writeText(" [{\" beginRendering\" : {\" surfaceId\" : \" user\" }}]" )
132+ java.io
133+ .File (tempDir, " user_settings.json" )
134+ .writeText(" [{\" beginRendering\" : {\" surfaceId\" : \" settings\" }}]" )
135+ java.io
136+ .File (tempDir, " admin_profile.json" )
137+ .writeText(" [{\" beginRendering\" : {\" surfaceId\" : \" admin\" }}]" )
138+
139+ val catalog =
140+ A2uiCatalog (
141+ version = A2uiVersion .VERSION_0_8 ,
142+ name = " basic" ,
143+ serverToClientSchema = JsonObject (emptyMap()),
144+ commonTypesSchema = JsonObject (emptyMap()),
145+ catalogSchema = JsonObject (mapOf (A2uiConstants .CATALOG_ID_KEY to JsonPrimitive (" basic" ))),
146+ )
147+
148+ // Filter by prefix: user_*.json
149+ val userExamples = catalog.loadExamples(" ${tempDir.path} /user_*.json" )
150+ assertTrue(userExamples.contains(" ---BEGIN user_profile---" ))
151+ assertTrue(userExamples.contains(" ---BEGIN user_settings---" ))
152+ assertFalse(userExamples.contains(" ---BEGIN admin_profile---" ))
153+
154+ // Filter by suffix: *_profile.json
155+ val profileExamples = catalog.loadExamples(" ${tempDir.path} /*_profile.json" )
156+ assertTrue(profileExamples.contains(" ---BEGIN user_profile---" ))
157+ assertTrue(profileExamples.contains(" ---BEGIN admin_profile---" ))
158+ assertFalse(profileExamples.contains(" ---BEGIN user_settings---" ))
159+ } finally {
160+ tempDir.deleteRecursively()
161+ }
162+ }
163+
164+ @Test
165+ fun loadsExamplesWithGlobAdvancedCases () {
166+ val tempDir = java.nio.file.Files .createTempDirectory(" examples" ).toFile()
167+ try {
168+ java.io
169+ .File (tempDir, " step1.json" )
170+ .writeText(" [{\" beginRendering\" : {\" surfaceId\" : \" 1\" }}]" )
171+ java.io
172+ .File (tempDir, " step2.json" )
173+ .writeText(" [{\" beginRendering\" : {\" surfaceId\" : \" 2\" }}]" )
174+ java.io
175+ .File (tempDir, " step3.json" )
176+ .writeText(" [{\" beginRendering\" : {\" surfaceId\" : \" 3\" }}]" )
177+
178+ val fakeJsonDir = java.io.File (tempDir, " directory.json" )
179+ fakeJsonDir.mkdir()
180+
181+ val catalog =
182+ A2uiCatalog (
183+ version = A2uiVersion .VERSION_0_8 ,
184+ name = " basic" ,
185+ serverToClientSchema = JsonObject (emptyMap()),
186+ commonTypesSchema = JsonObject (emptyMap()),
187+ catalogSchema = JsonObject (mapOf (A2uiConstants .CATALOG_ID_KEY to JsonPrimitive (" basic" ))),
188+ )
189+
190+ // Test character range matching
191+ val rangeExamples = catalog.loadExamples(" ${tempDir.path} /step[1-2].json" )
192+ assertTrue(rangeExamples.contains(" ---BEGIN step1---" ))
193+ assertTrue(rangeExamples.contains(" ---BEGIN step2---" ))
194+ assertFalse(rangeExamples.contains(" ---BEGIN step3---" ))
195+
196+ // Test that directory matching *.json is skipped correctly
197+ val allExamples = catalog.loadExamples(" ${tempDir.path} /*.json" )
198+ assertTrue(allExamples.contains(" ---BEGIN step1---" ))
199+ assertFalse(allExamples.contains(" directory" ))
200+
201+ // Test zero matches returns empty string
202+ assertEquals(expected = " " , actual = catalog.loadExamples(" ${tempDir.path} /*.yaml" ))
203+ } finally {
204+ tempDir.deleteRecursively()
205+ }
206+ }
207+
208+ @Test
209+ fun loadsExamplesWithGlobNegation () {
210+ val tempDir = java.nio.file.Files .createTempDirectory(" examples" ).toFile()
211+ try {
212+ java.io
213+ .File (tempDir, " visible.json" )
214+ .writeText(" [{\" beginRendering\" : {\" surfaceId\" : \" visible\" }}]" )
215+ java.io
216+ .File (tempDir, " index.json" )
217+ .writeText(" [{\" beginRendering\" : {\" surfaceId\" : \" index\" }}]" )
218+
219+ val catalog =
220+ A2uiCatalog (
221+ version = A2uiVersion .VERSION_0_8 ,
222+ name = " basic" ,
223+ serverToClientSchema = JsonObject (emptyMap()),
224+ commonTypesSchema = JsonObject (emptyMap()),
225+ catalogSchema = JsonObject (mapOf (A2uiConstants .CATALOG_ID_KEY to JsonPrimitive (" basic" ))),
226+ )
227+
228+ // Test negation to exclude files starting with 'i' (like index.json)
229+ val negationExamples = catalog.loadExamples(" ${tempDir.path} /[!i]*.json" )
230+ assertTrue(negationExamples.contains(" ---BEGIN visible---" ))
231+ assertFalse(negationExamples.contains(" ---BEGIN index---" ))
232+ } finally {
233+ tempDir.deleteRecursively()
234+ }
235+ }
83236}
0 commit comments