// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package base
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestExtractJSONFromMarkdown(t *testing.T) {
tests := []struct {
desc string
in string
want string
}{
{
desc: "no markdown",
in: "abcdefg",
want: "abcdefg",
},
{
desc: "no markdown (with line breaks)",
in: "ab\ncd\nfg",
want: "ab\ncd\nfg",
},
{
desc: "simple markdown",
in: "```foo bar```",
want: "```foo bar```",
},
{
desc: "empty markdown",
in: "``` ```",
want: "``` ```",
},
{
desc: "json markdown",
in: "```json{\"a\":1}```",
want: "{\"a\":1}",
},
{
desc: "json multiple line markdown",
in: "```json\n{\"a\": 1}\n```",
want: "{\"a\": 1}",
},
{
desc: "returns first of multiple blocks",
in: "```json{\"a\":\n1}```\n```json\n{\"b\":\n1}```",
want: "{\"a\":\n1}",
},
{
desc: "yaml markdown",
in: "```yaml\nkey: 1\nanother-key: 2```",
want: "```yaml\nkey: 1\nanother-key: 2```",
},
{
desc: "yaml + json markdown",
in: "```yaml\nkey: 1\nanother-key: 2``` ```json\n{\"a\": 1}\n```",
want: "{\"a\": 1}",
},
{
desc: "json + yaml markdown",
in: "```json\n{\"a\": 1}\n``` ```yaml\nkey: 1\nanother-key: 2```",
want: "{\"a\": 1}",
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
if diff := cmp.Diff(ExtractJSONFromMarkdown(tc.in), tc.want); diff != "" {
t.Errorf("ExtractJSONFromMarkdown diff (+got -want):\n%s", diff)
}
})
}
}
func TestSchemaAsMap(t *testing.T) {
type Bar struct {
Bar string
}
type Foo struct {
BarField Bar
Str string
}
want := map[string]any{
"additionalProperties": bool(false),
"properties": map[string]any{
"BarField": map[string]any{
"additionalProperties": bool(false),
"properties": map[string]any{
"Bar": map[string]any{"type": string("string")},
},
"required": []any{string("Bar")},
"type": string("object"),
},
"Str": map[string]any{"type": string("string")},
},
"required": []any{string("BarField"), string("Str")},
"type": string("object"),
}
got := SchemaAsMap(InferJSONSchema(Foo{}))
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("SchemaAsMap diff (+got -want):\n%s", diff)
}
}
func TestSchemaAsMapRecursive(t *testing.T) {
type Node struct {
Value string `json:"value,omitempty"`
Children []*Node `json:"children,omitempty"`
}
schema := SchemaAsMap(InferJSONSchema(Node{}))
// With DoNotReference and recursion limiting, the schema should be flat
// and recursive references should become "any" schema.
if _, ok := schema["$defs"]; ok {
t.Error("expected no $defs with DoNotReference: true")
}
if _, ok := schema["$ref"]; ok {
t.Error("expected no $ref with DoNotReference: true")
}
// Check top-level structure
if schema["type"] != "object" {
t.Errorf("expected type to be object, got %v", schema["type"])
}
props, ok := schema["properties"].(map[string]any)
if !ok {
t.Fatal("expected properties in schema")
}
// Check value field
valueField, ok := props["value"].(map[string]any)
if !ok {
t.Fatal("expected value field in properties")
}
if valueField["type"] != "string" {
t.Errorf("expected value.type to be string, got %v", valueField["type"])
}
// Check children field - recursive reference should be "any" schema
childrenField, ok := props["children"].(map[string]any)
if !ok {
t.Fatal("expected children field in properties")
}
if childrenField["type"] != "array" {
t.Errorf("expected children.type to be array, got %v", childrenField["type"])
}
items, ok := childrenField["items"].(map[string]any)
if !ok {
t.Fatal("expected children to have items")
}
// The recursive Node reference should have become an "any" schema
if items["additionalProperties"] != true {
t.Errorf("expected children.items to be 'any' schema (additionalProperties: true), got %v", items)
}
}