|
8 | 8 | "testing" |
9 | 9 |
|
10 | 10 | "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
11 | 12 | ) |
12 | 13 |
|
13 | 14 | // TestComputeImportRelPath verifies that computeImportRelPath produces the correct |
@@ -200,3 +201,85 @@ imports: |
200 | 201 | assert.Contains(t, importsResult.MergedJobs, "apm", "MergedJobs should contain the 'apm' job") |
201 | 202 | assert.Contains(t, importsResult.MergedJobs, "ubuntu-slim", "MergedJobs should contain the job runner") |
202 | 203 | } |
| 204 | + |
| 205 | +// TestExtractAllImportFields_BuiltinCacheHit verifies that extractAllImportFields uses the |
| 206 | +// process-level builtin frontmatter cache for builtin files without inputs. |
| 207 | +func TestExtractAllImportFields_BuiltinCacheHit(t *testing.T) { |
| 208 | + builtinPath := BuiltinPathPrefix + "test/cache-hit.md" |
| 209 | + content := []byte(`--- |
| 210 | +tools: |
| 211 | + bash: ["echo"] |
| 212 | +engine: claude |
| 213 | +--- |
| 214 | +
|
| 215 | +# Cache Hit Test |
| 216 | +`) |
| 217 | + |
| 218 | + // Register the builtin virtual file |
| 219 | + RegisterBuiltinVirtualFile(builtinPath, content) |
| 220 | + |
| 221 | + // Warm the cache by parsing once |
| 222 | + cachedResult, err := ExtractFrontmatterFromBuiltinFile(builtinPath, content) |
| 223 | + require.NoError(t, err, "should parse builtin file without error") |
| 224 | + assert.NotNil(t, cachedResult, "cached result should not be nil") |
| 225 | + |
| 226 | + // Verify the cache is populated |
| 227 | + cached, ok := GetBuiltinFrontmatterCache(builtinPath) |
| 228 | + assert.True(t, ok, "builtin cache should have an entry for the path") |
| 229 | + assert.Equal(t, cachedResult, cached, "cached result should match") |
| 230 | + |
| 231 | + // Call extractAllImportFields with no inputs — should hit the cache |
| 232 | + acc := newImportAccumulator() |
| 233 | + item := importQueueItem{ |
| 234 | + fullPath: builtinPath, |
| 235 | + importPath: "test/cache-hit.md", |
| 236 | + sectionName: "", |
| 237 | + inputs: nil, |
| 238 | + } |
| 239 | + visited := map[string]bool{builtinPath: true} |
| 240 | + |
| 241 | + err = acc.extractAllImportFields(content, item, visited) |
| 242 | + require.NoError(t, err, "extractAllImportFields should succeed for builtin file without inputs") |
| 243 | + |
| 244 | + // Verify engine was extracted from the cached frontmatter |
| 245 | + assert.NotEmpty(t, acc.engines, "engines should be populated from cached builtin file") |
| 246 | + assert.Contains(t, acc.engines[0], "claude", "engine should be 'claude' from the builtin file") |
| 247 | +} |
| 248 | + |
| 249 | +// TestExtractAllImportFields_BuiltinWithInputsBypassesCache verifies that builtin files |
| 250 | +// with inputs bypass the cache and use the substituted content. |
| 251 | +func TestExtractAllImportFields_BuiltinWithInputsBypassesCache(t *testing.T) { |
| 252 | + builtinPath := BuiltinPathPrefix + "test/cache-bypass.md" |
| 253 | + content := []byte(`--- |
| 254 | +tools: |
| 255 | + bash: ["echo"] |
| 256 | +engine: copilot |
| 257 | +--- |
| 258 | +
|
| 259 | +# Cache Bypass Test |
| 260 | +`) |
| 261 | + |
| 262 | + // Register the builtin virtual file |
| 263 | + RegisterBuiltinVirtualFile(builtinPath, content) |
| 264 | + |
| 265 | + // Warm the cache |
| 266 | + _, err := ExtractFrontmatterFromBuiltinFile(builtinPath, content) |
| 267 | + require.NoError(t, err, "should parse builtin file without error") |
| 268 | + |
| 269 | + // Call extractAllImportFields WITH inputs — should bypass the cache |
| 270 | + acc := newImportAccumulator() |
| 271 | + item := importQueueItem{ |
| 272 | + fullPath: builtinPath, |
| 273 | + importPath: "test/cache-bypass.md", |
| 274 | + sectionName: "", |
| 275 | + inputs: map[string]any{"key": "value"}, |
| 276 | + } |
| 277 | + visited := map[string]bool{builtinPath: true} |
| 278 | + |
| 279 | + err = acc.extractAllImportFields(content, item, visited) |
| 280 | + require.NoError(t, err, "extractAllImportFields should succeed for builtin file with inputs") |
| 281 | + |
| 282 | + // Verify engine was still extracted (from direct parse, not cache) |
| 283 | + assert.NotEmpty(t, acc.engines, "engines should be populated even when bypassing cache") |
| 284 | + assert.Contains(t, acc.engines[0], "copilot", "engine should be 'copilot' from the builtin file") |
| 285 | +} |
0 commit comments