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