@@ -127,6 +127,110 @@ func TestWriteJSON_ResultsOnlyDoesNotPreferScalarKnownKeyOverArray(t *testing.T)
127127 }
128128}
129129
130+ func TestWriteJSON_ResultsOnlyPreservesExplicitPrimaryResult (t * testing.T ) {
131+ ctx := WithJSONTransform (context .Background (), JSONTransform {ResultsOnly : true })
132+ input := map [string ]any {
133+ "draftId" : "d1" ,
134+ "message" : map [string ]any {
135+ "id" : "m1" ,
136+ "threadId" : "t1" ,
137+ },
138+ "threadId" : "t1" ,
139+ "attachments" : []map [string ]any {{
140+ "filename" : "fixture.txt" ,
141+ "size" : 7 ,
142+ }},
143+ }
144+
145+ var baseline bytes.Buffer
146+ if err := WriteJSON (context .Background (), & baseline , input ); err != nil {
147+ t .Fatalf ("WriteJSON baseline: %v" , err )
148+ }
149+
150+ var got bytes.Buffer
151+ if err := WriteJSON (ctx , & got , PrimaryResult (input )); err != nil {
152+ t .Fatalf ("WriteJSON primary result: %v" , err )
153+ }
154+
155+ if got .String () != baseline .String () {
156+ t .Fatalf ("complete primary result changed:\n got:\n %s\n want:\n %s" , got .String (), baseline .String ())
157+ }
158+ }
159+
160+ func TestWriteJSON_ResultsOnlyGenericArraySelectionIsDeterministic (t * testing.T ) {
161+ ctx := WithJSONTransform (context .Background (), JSONTransform {ResultsOnly : true })
162+ input := map [string ]any {
163+ "zItems" : []map [string ]any {{"id" : "z" }},
164+ "aItems" : []map [string ]any {{"id" : "a" }},
165+ }
166+
167+ for range 50 {
168+ var buf bytes.Buffer
169+ if err := WriteJSON (ctx , & buf , input ); err != nil {
170+ t .Fatalf ("WriteJSON: %v" , err )
171+ }
172+
173+ var got []map [string ]any
174+ if err := json .Unmarshal (buf .Bytes (), & got ); err != nil {
175+ t .Fatalf ("unmarshal: %v (out=%q)" , err , buf .String ())
176+ }
177+
178+ if len (got ) != 1 || got [0 ]["id" ] != "a" {
179+ t .Fatalf ("generic selection was not deterministic: %s" , strings .TrimSpace (buf .String ()))
180+ }
181+ }
182+ }
183+
184+ func TestWriteJSON_ResultsOnlyAttachmentCommandStillReturnsAttachments (t * testing.T ) {
185+ ctx := WithJSONTransform (context .Background (), JSONTransform {ResultsOnly : true })
186+ input := map [string ]any {
187+ "threadId" : "t1" ,
188+ "attachments" : []map [string ]any {{
189+ "messageId" : "m1" ,
190+ "filename" : "fixture.txt" ,
191+ }},
192+ }
193+
194+ var buf bytes.Buffer
195+ if err := WriteJSON (ctx , & buf , input ); err != nil {
196+ t .Fatalf ("WriteJSON: %v" , err )
197+ }
198+
199+ var got []map [string ]any
200+ if err := json .Unmarshal (buf .Bytes (), & got ); err != nil {
201+ t .Fatalf ("unmarshal: %v (out=%q)" , err , buf .String ())
202+ }
203+
204+ if len (got ) != 1 || got [0 ]["messageId" ] != "m1" || got [0 ]["filename" ] != "fixture.txt" {
205+ t .Fatalf ("attachment-specific result changed: %s" , strings .TrimSpace (buf .String ()))
206+ }
207+ }
208+
209+ func TestWriteJSON_ResultsOnlyListCommandStillReturnsItems (t * testing.T ) {
210+ ctx := WithJSONTransform (context .Background (), JSONTransform {ResultsOnly : true })
211+ input := map [string ]any {
212+ "messages" : []map [string ]any {
213+ {"id" : "m1" , "threadId" : "t1" },
214+ {"id" : "m2" , "threadId" : "t2" },
215+ },
216+ "nextPageToken" : "next-page" ,
217+ }
218+
219+ var buf bytes.Buffer
220+ if err := WriteJSON (ctx , & buf , input ); err != nil {
221+ t .Fatalf ("WriteJSON: %v" , err )
222+ }
223+
224+ var got []map [string ]any
225+ if err := json .Unmarshal (buf .Bytes (), & got ); err != nil {
226+ t .Fatalf ("unmarshal: %v (out=%q)" , err , buf .String ())
227+ }
228+
229+ if len (got ) != 2 || got [0 ]["id" ] != "m1" || got [1 ]["id" ] != "m2" {
230+ t .Fatalf ("list result changed: %s" , strings .TrimSpace (buf .String ()))
231+ }
232+ }
233+
130234func TestWriteJSONTransformPreservesLargeNumbers (t * testing.T ) {
131235 ctx := WithJSONTransform (context .Background (), JSONTransform {Select : []string {"id" }})
132236
0 commit comments