Edit fixture repair budgets structurally
This commit is contained in:
35
internal/testsupport/promptfixture/budget.go
Normal file
35
internal/testsupport/promptfixture/budget.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// Package promptfixture adapts complete prompt documents for controlled tests.
|
||||
package promptfixture
|
||||
|
||||
import (
|
||||
"go.yaml.in/yaml/v4"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// WithRepairAttempts changes prompt-owned budgets without depending on YAML layout.
|
||||
func WithRepairAttempts(t testing.TB, data []byte, attempts int) []byte {
|
||||
t.Helper()
|
||||
var document map[string]any
|
||||
if err := yaml.Unmarshal(data, &document); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var update func(map[string]any)
|
||||
update = func(scope map[string]any) {
|
||||
records, _ := scope["prompts"].([]any)
|
||||
for _, record := range records {
|
||||
prompt := record.(map[string]any)
|
||||
output := prompt["output"].(map[string]any)
|
||||
output["repair_attempts"] = attempts
|
||||
}
|
||||
collections, _ := scope["collections"].(map[string]any)
|
||||
for _, collection := range collections {
|
||||
update(collection.(map[string]any))
|
||||
}
|
||||
}
|
||||
update(document)
|
||||
result, err := yaml.Marshal(document)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
42
internal/testsupport/promptfixture/budget_test.go
Normal file
42
internal/testsupport/promptfixture/budget_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package promptfixture
|
||||
|
||||
import (
|
||||
"go.yaml.in/yaml/v4"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBudgetIgnoresLayoutAndPreservesContract(t *testing.T) {
|
||||
for _, input := range []string{
|
||||
`prompts: [{output: {format: json, repair_attempts: 9}, id: test, messages: [{role: user, content: hello}]}]`,
|
||||
"collections:\n test:\n prompts:\n - output:\n format: json\n messages: [{role: user, content: hello}]\n id: test\n",
|
||||
} {
|
||||
result := WithRepairAttempts(t, []byte(input), 2)
|
||||
var doc struct {
|
||||
Prompts []struct {
|
||||
Output struct {
|
||||
Format string
|
||||
RepairAttempts int `yaml:"repair_attempts"`
|
||||
}
|
||||
Messages []struct{ Content string }
|
||||
}
|
||||
Collections map[string]struct {
|
||||
Prompts []struct {
|
||||
Output struct {
|
||||
RepairAttempts int `yaml:"repair_attempts"`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := yaml.Unmarshal(result, &doc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(doc.Prompts) > 0 {
|
||||
p := doc.Prompts[0]
|
||||
if p.Output.RepairAttempts != 2 || p.Output.Format != "json" || p.Messages[0].Content != "hello" {
|
||||
t.Fatalf("lost contract: %s", result)
|
||||
}
|
||||
} else if doc.Collections["test"].Prompts[0].Output.RepairAttempts != 2 {
|
||||
t.Fatalf("budget: %s", result)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user