package fileutil_test import ( "testing" "github.com/juanatsap/cv-site/internal/fileutil" ) func TestFindDataFile(t *testing.T) { tests := []struct { name string filename string wantErr bool }{ { name: "Existing file - cv-en.json", filename: "data/cv-en.json", wantErr: false, }, { name: "Existing file - cv-es.json", filename: "data/cv-es.json", wantErr: false, }, { name: "Existing file - ui-en.json", filename: "data/ui-en.json", wantErr: false, }, { name: "Non-existent file", filename: "data/non-existent.json", wantErr: true, }, { name: "Empty filename", filename: "", wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := fileutil.FindDataFile(tt.filename) if (err != nil) != tt.wantErr { t.Errorf("FindDataFile() error = %v, wantErr %v", err, tt.wantErr) return } if !tt.wantErr && got == "" { t.Error("FindDataFile() returned empty path for existing file") } }) } } func TestLoadJSON(t *testing.T) { // Test with actual CV data t.Run("Load valid CV JSON", func(t *testing.T) { type TestCV struct { Personal struct { Name string `json:"name"` } `json:"personal"` } var cv TestCV err := fileutil.LoadJSON("data/cv-en.json", &cv) if err != nil { t.Fatalf("LoadJSON() unexpected error: %v", err) } if cv.Personal.Name == "" { t.Error("LoadJSON() loaded CV but name is empty") } }) // Test with non-existent file t.Run("Load non-existent file", func(t *testing.T) { var data map[string]interface{} err := fileutil.LoadJSON("data/does-not-exist.json", &data) if err == nil { t.Error("LoadJSON() expected error for non-existent file") } }) // Test with invalid target t.Run("Load with nil target", func(t *testing.T) { err := fileutil.LoadJSON("data/cv-en.json", nil) if err == nil { t.Error("LoadJSON() expected error for nil target") } }) }