@@ -76,66 +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" )
101- }
102-
103- runConfig := & config.RuntimeConfig {}
104-
105- type versioned struct {
106- Version string `yaml:"version"`
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" )
10783 }
10884
109- // Load all the examples.
110- // Note: don't use t.Parallel() to avoid SQLite lock contention when
111- // multiple RAG examples share the same relative database paths (e.g., ./bm25.db).
11285 for _ , agentFilename := range examples {
11386 t .Run (agentFilename , func (t * testing.T ) {
114- agentSource , err := config .Resolve (agentFilename , nil )
115- require .NoError (t , err )
87+ t .Parallel ()
11688
117- // First make sure it doesn't define a version
118- data , err := agentSource .Read (t .Context ())
89+ data , err := os .ReadFile (agentFilename )
11990 require .NoError (t , err )
12091
121- var v versioned
122- err = yaml .Unmarshal (data , & v )
123- 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 ))
12498 require .Empty (t , v .Version , "example %s should not define a version" , agentFilename )
12599
126- // Then make sure the config loads successfully
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.
103+ agentSource := config .NewBytesSource (agentFilename , data )
104+ runConfig := & config.RuntimeConfig {}
105+ runConfig .WorkingDir = t .TempDir ()
106+
127107 teams , err := Load (t .Context (), agentSource , runConfig )
128- if err != nil {
129- if errors .Is (err , dmr .ErrNotInstalled ) && filepath .Base (agentFilename ) == "dmr.yaml" {
130- t .Skip ("Skipping DMR example: Docker Model Runner not installed" )
131- }
108+ if errors .Is (err , dmr .ErrNotInstalled ) && filepath .Base (agentFilename ) == "dmr.yaml" {
109+ t .Skip ("Skipping DMR example: Docker Model Runner not installed" )
132110 }
133111 require .NoError (t , err )
134112 assert .NotEmpty (t , teams )
135113 })
136114 }
137115}
138116
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+
139142func TestLoadDefaultAgent (t * testing.T ) {
140143 t .Setenv ("HOME" , t .TempDir ())
141144
0 commit comments