-
Notifications
You must be signed in to change notification settings - Fork 631
chore(go): added tests to cover various default value scenarios in prompts #4057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @apascal07, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the robustness of the Go AI module by introducing a suite of new unit tests focused on the intricate handling of default values for prompt inputs. These tests validate that the system correctly interprets and applies defaults from various sources, including Go struct fields, explicit map inputs, and Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds a comprehensive set of tests for default value handling in prompts, covering scenarios with structs, maps, zero values, and jsonschema default tags. The tests are well-structured and cover important edge cases.
My review includes a few suggestions:
- A high-severity concern about an inconsistency in how
WithInputTypehandlesmap[string]anyversus structs, which could lead to bugs. - Several medium-severity suggestions to refactor repetitive test assertions into table-driven tests to improve code maintainability and readability.
| t.Run("map input is used directly as DefaultInput", func(t *testing.T) { | ||
| input := map[string]any{ | ||
| "name": "default", | ||
| "age": 25, | ||
| } | ||
|
|
||
| opt := WithInputType(input).(*inputOptions) | ||
|
|
||
| if diff := cmp.Diff(input, opt.DefaultInput); diff != "" { | ||
| t.Errorf("DefaultInput should match map input, diff (-want +got):\n%s", diff) | ||
| } | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test map input is used directly as DefaultInput confirms a behavior in WithInputType that creates an inconsistency in how default inputs are handled. When a map[string]any is provided, it bypasses the JSON marshaling/unmarshaling cycle that struct inputs go through.
This leads to different types for numeric values in DefaultInput:
- For structs,
intvalues becomefloat64(e.g., lines 677, 700). - For maps,
intvalues remainint(as tested here).
This inconsistency can lead to subtle bugs if downstream code expects all numbers in DefaultInput to be float64. To ensure consistent behavior, it would be better to process all input types through the same JSON marshaling/unmarshaling logic in WithInputType.
I'd recommend updating WithInputType to remove the special case for map[string]any and then adjusting this test to expect float64 values, similar to the other tests in this file. Since I cannot suggest changes to option.go, here is how this test could be updated after that change:
t.Run("map input is converted consistently", func(t *testing.T) {
input := map[string]any{
"name": "default",
"age": 25,
}
opt := WithInputType(input).(*inputOptions)
expected := map[string]any{
"name": "default",
"age": float64(25),
}
if diff := cmp.Diff(expected, opt.DefaultInput); diff != "" {
t.Errorf("DefaultInput should be consistent with struct processing, diff (-want +got):\n%s", diff)
}
})| t.Run("jsonschema default tag is reflected in schema", func(t *testing.T) { | ||
| type TestInputWithDefaults struct { | ||
| Name string `json:"name" jsonschema:"default=guest"` | ||
| Age int `json:"age" jsonschema:"default=25"` | ||
| Active bool `json:"active" jsonschema:"default=true"` | ||
| } | ||
|
|
||
| opt := WithInputType(TestInputWithDefaults{}).(*inputOptions) | ||
|
|
||
| props, ok := opt.InputSchema["properties"].(map[string]any) | ||
| if !ok { | ||
| t.Fatal("expected properties in schema") | ||
| } | ||
|
|
||
| nameSchema, ok := props["name"].(map[string]any) | ||
| if !ok { | ||
| t.Fatal("expected name property in schema") | ||
| } | ||
| if nameSchema["default"] != "guest" { | ||
| t.Errorf("expected name default to be 'guest', got %v", nameSchema["default"]) | ||
| } | ||
|
|
||
| ageSchema, ok := props["age"].(map[string]any) | ||
| if !ok { | ||
| t.Fatal("expected age property in schema") | ||
| } | ||
| if ageSchema["default"] != float64(25) { | ||
| t.Errorf("expected age default to be 25, got %v", ageSchema["default"]) | ||
| } | ||
|
|
||
| activeSchema, ok := props["active"].(map[string]any) | ||
| if !ok { | ||
| t.Fatal("expected active property in schema") | ||
| } | ||
| if activeSchema["default"] != true { | ||
| t.Errorf("expected active default to be true, got %v", activeSchema["default"]) | ||
| } | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test contains repetitive checks for each field in the schema. To improve maintainability and readability, consider refactoring it into a table-driven test, similar to the pattern used in TestSchemaAsMapWithDefaults in go/internal/base/json_test.go.
t.Run("jsonschema default tag is reflected in schema", func(t *testing.T) {
type TestInputWithDefaults struct {
Name string `json:"name" jsonschema:"default=guest"`
Age int `json:"age" jsonschema:"default=25"`
Active bool `json:"active" jsonschema:"default=true"`
}
opt := WithInputType(TestInputWithDefaults{}).(*inputOptions)
props, ok := opt.InputSchema["properties"].(map[string]any)
if !ok {
t.Fatal("expected properties in schema")
}
tests := []struct {
field string
expected any
}{
{"name", "guest"},
{"age", float64(25)},
{"active", true},
}
for _, tc := range tests {
t.Run(tc.field, func(t *testing.T) {
prop, ok := props[tc.field].(map[string]any)
if !ok {
t.Fatalf("expected %s property in schema", tc.field)
}
if prop["default"] != tc.expected {
t.Errorf("expected default for %s to be %v, got %v", tc.field, tc.expected, prop["default"])
}
})
}
})| t.Run("schema has jsonschema defaults exposed", func(t *testing.T) { | ||
| p := DefinePrompt( | ||
| reg, "schema-defaults-exposed", | ||
| WithPrompt("{{name}}"), | ||
| WithInputType(InputWithSchemaDefaults{Name: "admin", Priority: 10}), | ||
| ) | ||
|
|
||
| desc := p.(api.Action).Desc() | ||
| schema := desc.InputSchema | ||
|
|
||
| props, ok := schema["properties"].(map[string]any) | ||
| if !ok { | ||
| t.Fatal("expected properties in input schema") | ||
| } | ||
|
|
||
| nameSchema, ok := props["name"].(map[string]any) | ||
| if !ok { | ||
| t.Fatal("expected name property in schema") | ||
| } | ||
|
|
||
| if nameSchema["default"] != "guest" { | ||
| t.Errorf("expected schema default to be 'guest', got %v", nameSchema["default"]) | ||
| } | ||
|
|
||
| prioritySchema, ok := props["priority"].(map[string]any) | ||
| if !ok { | ||
| t.Fatal("expected priority property in schema") | ||
| } | ||
|
|
||
| if prioritySchema["default"] != float64(5) { | ||
| t.Errorf("expected schema default to be 5, got %v", prioritySchema["default"]) | ||
| } | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test has repetitive checks for schema properties. For better readability and maintainability, you could refactor this into a table-driven test. This would make it easier to add more properties to check in the future.
t.Run("schema has jsonschema defaults exposed", func(t *testing.T) {
p := DefinePrompt(
reg, "schema-defaults-exposed",
WithPrompt("{{name}}"),
WithInputType(InputWithSchemaDefaults{Name: "admin", Priority: 10}),
)
desc := p.(api.Action).Desc()
schema := desc.InputSchema
props, ok := schema["properties"].(map[string]any)
if !ok {
t.Fatal("expected properties in input schema")
}
testCases := []struct {
field string
expected any
}{
{"name", "guest"},
{"priority", float64(5)},
}
for _, tc := range testCases {
t.Run(tc.field, func(t *testing.T) {
prop, ok := props[tc.field].(map[string]any)
if !ok {
t.Fatalf("expected %s property in schema", tc.field)
}
if prop["default"] != tc.expected {
t.Errorf("expected schema default to be %v, got %v", tc.expected, prop["default"])
}
})
}
})| t.Run("defaultInput in metadata uses struct values not jsonschema defaults", func(t *testing.T) { | ||
| p := DefinePrompt( | ||
| reg, "schema-defaults-metadata", | ||
| WithPrompt("{{name}}"), | ||
| WithInputType(InputWithSchemaDefaults{Name: "admin", Priority: 10}), | ||
| ) | ||
|
|
||
| desc := p.(api.Action).Desc() | ||
| promptMeta, ok := desc.Metadata["prompt"].(map[string]any) | ||
| if !ok { | ||
| t.Fatal("expected prompt metadata") | ||
| } | ||
|
|
||
| defaultInput, ok := promptMeta["defaultInput"].(map[string]any) | ||
| if !ok { | ||
| t.Fatal("expected defaultInput in prompt metadata") | ||
| } | ||
|
|
||
| if defaultInput["name"] != "admin" { | ||
| t.Errorf("expected defaultInput name to be 'admin', got %v", defaultInput["name"]) | ||
| } | ||
|
|
||
| if defaultInput["priority"] != float64(10) { | ||
| t.Errorf("expected defaultInput priority to be 10, got %v", defaultInput["priority"]) | ||
| } | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the previous test, this one can be refactored into a table-driven test to reduce code duplication and improve maintainability.
t.Run("defaultInput in metadata uses struct values not jsonschema defaults", func(t *testing.T) {
p := DefinePrompt(
reg, "schema-defaults-metadata",
WithPrompt("{{name}}"),
WithInputType(InputWithSchemaDefaults{Name: "admin", Priority: 10}),
)
desc := p.(api.Action).Desc()
promptMeta, ok := desc.Metadata["prompt"].(map[string]any)
if !ok {
t.Fatal("expected prompt metadata")
}
defaultInput, ok := promptMeta["defaultInput"].(map[string]any)
if !ok {
t.Fatal("expected defaultInput in prompt metadata")
}
testCases := []struct {
field string
expected any
}{
{"name", "admin"},
{"priority", float64(10)},
}
for _, tc := range testCases {
t.Run(tc.field, func(t *testing.T) {
if defaultInput[tc.field] != tc.expected {
t.Errorf("expected defaultInput %s to be %v, got %v", tc.field, tc.expected, defaultInput[tc.field])
}
})
}
})
Checklist (if applicable):