|
| 1 | +package engine |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// TestL2PipelineStatePathsAreHomeRelative is a regression guard for L2 — |
| 11 | +// the three learning-pipeline stores (ExperienceStore, KnowledgeBase, |
| 12 | +// FeedbackCollector) created by NewIntegrationPipeline must write to |
| 13 | +// ~/.hawk/{experience,knowledge,feedback}/, not to <cwd>/.hawk/... |
| 14 | +// |
| 15 | +// Pre-fix, NewIntegrationPipeline passed the literal strings |
| 16 | +// ".hawk/experience", ".hawk/knowledge", ".hawk/feedback" to those |
| 17 | +// constructors, which leaked into <cwd>/cmd/.hawk/ when hawk was run |
| 18 | +// from its own source tree. |
| 19 | +func TestL2PipelineStatePathsAreHomeRelative(t *testing.T) { |
| 20 | + home, err := os.UserHomeDir() |
| 21 | + if err != nil { |
| 22 | + t.Fatalf("os.UserHomeDir: %v", err) |
| 23 | + } |
| 24 | + if home == "" { |
| 25 | + t.Fatal("os.UserHomeDir returned empty string") |
| 26 | + } |
| 27 | + wantPrefix := filepath.Clean(home) + string(filepath.Separator) |
| 28 | + |
| 29 | + check := func(name, got string) { |
| 30 | + t.Helper() |
| 31 | + if !filepath.IsAbs(got) { |
| 32 | + t.Errorf("%s: path %q is not absolute", name, got) |
| 33 | + return |
| 34 | + } |
| 35 | + if !strings.HasPrefix(got, wantPrefix) && !strings.HasPrefix(got, filepath.Clean(home)) { |
| 36 | + t.Errorf("%s: path %q does not start with home dir %q", name, got, home) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + p := NewIntegrationPipeline() |
| 41 | + if p == nil { |
| 42 | + t.Fatal("NewIntegrationPipeline returned nil") |
| 43 | + } |
| 44 | + if p.ExperienceStore == nil || p.KnowledgeBase == nil || p.FeedbackCollector == nil { |
| 45 | + t.Fatal("NewIntegrationPipeline left a learning-pipeline store nil") |
| 46 | + } |
| 47 | + |
| 48 | + check("ExperienceStore.Dir", p.ExperienceStore.Dir) |
| 49 | + check("KnowledgeBase.Dir", p.KnowledgeBase.Dir) |
| 50 | + check("FeedbackCollector.Dir", p.FeedbackCollector.Dir) |
| 51 | +} |
0 commit comments