@@ -76,70 +76,69 @@ func TestGetToolsForAgent_ContinuesOnCreateToolError(t *testing.T) {
7676func TestLoadExamples (t * testing.T ) {
7777 examples := collectExamples (t )
7878
79- // Collect required env vars from all examples by checking configs directly.
80- // This avoids calling Load() twice for each example.
81- missingEnvs := make (map [string ]bool )
82- for _ , agentFilename := range examples {
83- agentSource , err := config .Resolve (agentFilename , nil )
84- require .NoError (t , err )
85-
86- cfg , err := config .Load (t .Context (), agentSource )
87- require .NoError (t , err )
88-
89- for _ , env := range config .GatherEnvVarsForModels (cfg ) {
90- missingEnvs [env ] = true
91- }
92-
93- toolEnvs , _ := config .GatherEnvVarsForTools (t .Context (), cfg )
94- for _ , env := range toolEnvs {
95- missingEnvs [env ] = true
96- }
97- }
98-
99- for name := range missingEnvs {
100- t .Setenv (name , "dummy" )
79+ // Set every env var referenced by the examples to a dummy value so model
80+ // and tool initialisation succeeds without real credentials.
81+ for env := range gatherExampleEnvVars (t , examples ) {
82+ t .Setenv (env , "dummy" )
10183 }
10284
103- type versioned struct {
104- Version string `yaml:"version"`
105- }
106-
107- // Load all the examples.
10885 for _ , agentFilename := range examples {
10986 t .Run (agentFilename , func (t * testing.T ) {
11087 t .Parallel ()
11188
11289 data , err := os .ReadFile (agentFilename )
11390 require .NoError (t , err )
11491
115- // First make sure it doesn't define a version
116- var v versioned
117- err = yaml .Unmarshal (data , & v )
118- require .NoError (t , err )
92+ // Examples must not pin a version: they should always parse with
93+ // the latest config schema.
94+ var v struct {
95+ Version string `yaml:"version"`
96+ }
97+ require .NoError (t , yaml .Unmarshal (data , & v ))
11998 require .Empty (t , v .Version , "example %s should not define a version" , agentFilename )
12099
121- // Use a bytes source (ParentDir == "") combined with a temp WorkingDir
122- // so toolsets that create files on disk write into the temp dir.
100+ // Use a bytes source (ParentDir == "") plus a temp WorkingDir so
101+ // toolsets that write to disk (memory, RAG, cache, ...) land in
102+ // the temp dir instead of the examples/ tree.
123103 agentSource := config .NewBytesSource (agentFilename , data )
124- runConfig := & config.RuntimeConfig {
125- Config : config.Config {
126- WorkingDir : t .TempDir (),
127- },
128- }
104+ runConfig := & config.RuntimeConfig {}
105+ runConfig .WorkingDir = t .TempDir ()
129106
130- // Then make sure the config loads successfully
131107 teams , err := Load (t .Context (), agentSource , runConfig )
132- if err != nil {
133- if errors .Is (err , dmr .ErrNotInstalled ) && filepath .Base (agentFilename ) == "dmr.yaml" {
134- t .Skip ("Skipping DMR example: Docker Model Runner not installed" )
135- }
108+ if errors .Is (err , dmr .ErrNotInstalled ) && filepath .Base (agentFilename ) == "dmr.yaml" {
109+ t .Skip ("Skipping DMR example: Docker Model Runner not installed" )
136110 }
137111 require .NoError (t , err )
138112 assert .NotEmpty (t , teams )
139113 })
140114 }
141115}
142116
117+ // gatherExampleEnvVars returns the union of env vars referenced by the given
118+ // example files (both for models and toolsets). The set is collected up-front
119+ // so t.Setenv can be called before any subtest starts.
120+ func gatherExampleEnvVars (t * testing.T , examples []string ) map [string ]bool {
121+ t .Helper ()
122+
123+ envs := make (map [string ]bool )
124+ for _ , agentFilename := range examples {
125+ agentSource , err := config .Resolve (agentFilename , nil )
126+ require .NoError (t , err )
127+
128+ cfg , err := config .Load (t .Context (), agentSource )
129+ require .NoError (t , err )
130+
131+ for _ , env := range config .GatherEnvVarsForModels (cfg ) {
132+ envs [env ] = true
133+ }
134+ toolEnvs , _ := config .GatherEnvVarsForTools (t .Context (), cfg )
135+ for _ , env := range toolEnvs {
136+ envs [env ] = true
137+ }
138+ }
139+ return envs
140+ }
141+
143142func TestLoadDefaultAgent (t * testing.T ) {
144143 t .Setenv ("HOME" , t .TempDir ())
145144
0 commit comments