Skip to content

Commit 63c9d9a

Browse files
committed
test(huggingface): add tests for token file paths and authentication methods
1 parent 66170c0 commit 63c9d9a

1 file changed

Lines changed: 262 additions & 0 deletions

File tree

pkg/modelprovider/huggingface/downloader_test.go

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package huggingface
1818

1919
import (
20+
"os"
21+
"path/filepath"
2022
"strings"
2123
"testing"
2224
)
@@ -166,3 +168,263 @@ func TestProvider_Name(t *testing.T) {
166168
t.Errorf("Provider.Name() = %v, want %v", got, "huggingface")
167169
}
168170
}
171+
172+
func TestTokenFilePaths(t *testing.T) {
173+
t.Run("without HF_HOME", func(t *testing.T) {
174+
// Ensure HF_HOME is unset
175+
t.Setenv("HF_HOME", "")
176+
177+
paths := tokenFilePaths()
178+
179+
// Should have exactly 2 paths: modern and legacy (no HF_HOME path)
180+
if len(paths) != 2 {
181+
t.Fatalf("tokenFilePaths() returned %d paths, want 2", len(paths))
182+
}
183+
184+
// First should be the modern cache path
185+
if !strings.Contains(paths[0], filepath.Join(".cache", "huggingface", "token")) {
186+
t.Errorf("tokenFilePaths()[0] = %q, want path containing .cache/huggingface/token", paths[0])
187+
}
188+
189+
// Second should be the legacy path
190+
if !strings.Contains(paths[1], filepath.Join(".huggingface", "token")) {
191+
t.Errorf("tokenFilePaths()[1] = %q, want path containing .huggingface/token", paths[1])
192+
}
193+
})
194+
195+
t.Run("with HF_HOME", func(t *testing.T) {
196+
customDir := t.TempDir()
197+
t.Setenv("HF_HOME", customDir)
198+
199+
paths := tokenFilePaths()
200+
201+
// Should have 3 paths: HF_HOME, modern, legacy
202+
if len(paths) != 3 {
203+
t.Fatalf("tokenFilePaths() returned %d paths, want 3", len(paths))
204+
}
205+
206+
// First should be the HF_HOME path
207+
expected := filepath.Join(customDir, "token")
208+
if paths[0] != expected {
209+
t.Errorf("tokenFilePaths()[0] = %q, want %q", paths[0], expected)
210+
}
211+
})
212+
}
213+
214+
func TestCheckHuggingFaceAuth(t *testing.T) {
215+
t.Run("authenticated via HF_TOKEN env var", func(t *testing.T) {
216+
t.Setenv("HF_TOKEN", "hf_test_token_123")
217+
218+
err := checkHuggingFaceAuth()
219+
if err != nil {
220+
t.Errorf("checkHuggingFaceAuth() returned error %v, want nil", err)
221+
}
222+
})
223+
224+
t.Run("authenticated via token file at HF_HOME", func(t *testing.T) {
225+
t.Setenv("HF_TOKEN", "")
226+
227+
tmpDir := t.TempDir()
228+
t.Setenv("HF_HOME", tmpDir)
229+
// Override HOME so the modern/legacy paths don't accidentally find a real token
230+
t.Setenv("HOME", t.TempDir())
231+
232+
tokenPath := filepath.Join(tmpDir, "token")
233+
if err := os.WriteFile(tokenPath, []byte("hf_test_token"), 0644); err != nil {
234+
t.Fatal(err)
235+
}
236+
237+
err := checkHuggingFaceAuth()
238+
if err != nil {
239+
t.Errorf("checkHuggingFaceAuth() returned error %v, want nil", err)
240+
}
241+
})
242+
243+
t.Run("authenticated via modern token path", func(t *testing.T) {
244+
t.Setenv("HF_TOKEN", "")
245+
t.Setenv("HF_HOME", "")
246+
247+
fakeHome := t.TempDir()
248+
t.Setenv("HOME", fakeHome)
249+
250+
tokenPath := filepath.Join(fakeHome, ".cache", "huggingface", "token")
251+
if err := os.MkdirAll(filepath.Dir(tokenPath), 0755); err != nil {
252+
t.Fatal(err)
253+
}
254+
if err := os.WriteFile(tokenPath, []byte("hf_test_token"), 0644); err != nil {
255+
t.Fatal(err)
256+
}
257+
258+
err := checkHuggingFaceAuth()
259+
if err != nil {
260+
t.Errorf("checkHuggingFaceAuth() returned error %v, want nil", err)
261+
}
262+
})
263+
264+
t.Run("authenticated via legacy token path", func(t *testing.T) {
265+
t.Setenv("HF_TOKEN", "")
266+
t.Setenv("HF_HOME", "")
267+
268+
fakeHome := t.TempDir()
269+
t.Setenv("HOME", fakeHome)
270+
271+
tokenPath := filepath.Join(fakeHome, ".huggingface", "token")
272+
if err := os.MkdirAll(filepath.Dir(tokenPath), 0755); err != nil {
273+
t.Fatal(err)
274+
}
275+
if err := os.WriteFile(tokenPath, []byte("hf_test_token"), 0644); err != nil {
276+
t.Fatal(err)
277+
}
278+
279+
err := checkHuggingFaceAuth()
280+
if err != nil {
281+
t.Errorf("checkHuggingFaceAuth() returned error %v, want nil", err)
282+
}
283+
})
284+
285+
t.Run("not authenticated", func(t *testing.T) {
286+
t.Setenv("HF_TOKEN", "")
287+
t.Setenv("HF_HOME", "")
288+
t.Setenv("HOME", t.TempDir())
289+
// Ensure no CLI tools are found by overriding PATH
290+
t.Setenv("PATH", t.TempDir())
291+
292+
err := checkHuggingFaceAuth()
293+
if err == nil {
294+
t.Error("checkHuggingFaceAuth() returned nil, want error")
295+
}
296+
if err != nil && !strings.Contains(err.Error(), "not authenticated") {
297+
t.Errorf("checkHuggingFaceAuth() error = %q, want error containing 'not authenticated'", err.Error())
298+
}
299+
})
300+
}
301+
302+
func TestGetToken(t *testing.T) {
303+
t.Run("token from HF_TOKEN env var", func(t *testing.T) {
304+
t.Setenv("HF_TOKEN", "hf_env_token_abc")
305+
306+
token, err := getToken()
307+
if err != nil {
308+
t.Fatalf("getToken() returned error: %v", err)
309+
}
310+
if token != "hf_env_token_abc" {
311+
t.Errorf("getToken() = %q, want %q", token, "hf_env_token_abc")
312+
}
313+
})
314+
315+
t.Run("token from HF_HOME file", func(t *testing.T) {
316+
t.Setenv("HF_TOKEN", "")
317+
318+
tmpDir := t.TempDir()
319+
t.Setenv("HF_HOME", tmpDir)
320+
t.Setenv("HOME", t.TempDir())
321+
322+
tokenPath := filepath.Join(tmpDir, "token")
323+
if err := os.WriteFile(tokenPath, []byte(" hf_file_token_xyz \n"), 0644); err != nil {
324+
t.Fatal(err)
325+
}
326+
327+
token, err := getToken()
328+
if err != nil {
329+
t.Fatalf("getToken() returned error: %v", err)
330+
}
331+
if token != "hf_file_token_xyz" {
332+
t.Errorf("getToken() = %q, want %q (should be trimmed)", token, "hf_file_token_xyz")
333+
}
334+
})
335+
336+
t.Run("token from modern cache path", func(t *testing.T) {
337+
t.Setenv("HF_TOKEN", "")
338+
t.Setenv("HF_HOME", "")
339+
340+
fakeHome := t.TempDir()
341+
t.Setenv("HOME", fakeHome)
342+
343+
tokenPath := filepath.Join(fakeHome, ".cache", "huggingface", "token")
344+
if err := os.MkdirAll(filepath.Dir(tokenPath), 0755); err != nil {
345+
t.Fatal(err)
346+
}
347+
if err := os.WriteFile(tokenPath, []byte("hf_modern_token"), 0644); err != nil {
348+
t.Fatal(err)
349+
}
350+
351+
token, err := getToken()
352+
if err != nil {
353+
t.Fatalf("getToken() returned error: %v", err)
354+
}
355+
if token != "hf_modern_token" {
356+
t.Errorf("getToken() = %q, want %q", token, "hf_modern_token")
357+
}
358+
})
359+
360+
t.Run("token from legacy path", func(t *testing.T) {
361+
t.Setenv("HF_TOKEN", "")
362+
t.Setenv("HF_HOME", "")
363+
364+
fakeHome := t.TempDir()
365+
t.Setenv("HOME", fakeHome)
366+
367+
tokenPath := filepath.Join(fakeHome, ".huggingface", "token")
368+
if err := os.MkdirAll(filepath.Dir(tokenPath), 0755); err != nil {
369+
t.Fatal(err)
370+
}
371+
if err := os.WriteFile(tokenPath, []byte("hf_legacy_token"), 0644); err != nil {
372+
t.Fatal(err)
373+
}
374+
375+
token, err := getToken()
376+
if err != nil {
377+
t.Fatalf("getToken() returned error: %v", err)
378+
}
379+
if token != "hf_legacy_token" {
380+
t.Errorf("getToken() = %q, want %q", token, "hf_legacy_token")
381+
}
382+
})
383+
384+
t.Run("modern path takes priority over legacy", func(t *testing.T) {
385+
t.Setenv("HF_TOKEN", "")
386+
t.Setenv("HF_HOME", "")
387+
388+
fakeHome := t.TempDir()
389+
t.Setenv("HOME", fakeHome)
390+
391+
// Create both modern and legacy token files
392+
modernPath := filepath.Join(fakeHome, ".cache", "huggingface", "token")
393+
if err := os.MkdirAll(filepath.Dir(modernPath), 0755); err != nil {
394+
t.Fatal(err)
395+
}
396+
if err := os.WriteFile(modernPath, []byte("modern_token"), 0644); err != nil {
397+
t.Fatal(err)
398+
}
399+
400+
legacyPath := filepath.Join(fakeHome, ".huggingface", "token")
401+
if err := os.MkdirAll(filepath.Dir(legacyPath), 0755); err != nil {
402+
t.Fatal(err)
403+
}
404+
if err := os.WriteFile(legacyPath, []byte("legacy_token"), 0644); err != nil {
405+
t.Fatal(err)
406+
}
407+
408+
token, err := getToken()
409+
if err != nil {
410+
t.Fatalf("getToken() returned error: %v", err)
411+
}
412+
if token != "modern_token" {
413+
t.Errorf("getToken() = %q, want %q (modern should take priority)", token, "modern_token")
414+
}
415+
})
416+
417+
t.Run("no token found", func(t *testing.T) {
418+
t.Setenv("HF_TOKEN", "")
419+
t.Setenv("HF_HOME", "")
420+
t.Setenv("HOME", t.TempDir())
421+
422+
_, err := getToken()
423+
if err == nil {
424+
t.Error("getToken() returned nil error, want error")
425+
}
426+
if err != nil && !strings.Contains(err.Error(), "token not found") {
427+
t.Errorf("getToken() error = %q, want error containing 'token not found'", err.Error())
428+
}
429+
})
430+
}

0 commit comments

Comments
 (0)