|
1 | 1 | package evaluation |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "os" |
5 | 4 | "path/filepath" |
6 | 5 | "testing" |
7 | 6 |
|
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
8 | 9 | "github.com/docker/cagent/pkg/session" |
9 | 10 | ) |
10 | 11 |
|
11 | 12 | func TestSaveWithCustomFilename(t *testing.T) { |
12 | | - // Create a temporary directory for tests |
13 | | - tmpDir := t.TempDir() |
14 | | - |
15 | | - // Change to temp directory |
16 | | - oldWd, err := os.Getwd() |
17 | | - if err != nil { |
18 | | - t.Fatalf("Failed to get working directory: %v", err) |
19 | | - } |
20 | | - defer func() { |
21 | | - if err := os.Chdir(oldWd); err != nil { |
22 | | - t.Errorf("Failed to restore working directory: %v", err) |
23 | | - } |
24 | | - }() |
25 | | - |
26 | | - if err := os.Chdir(tmpDir); err != nil { |
27 | | - t.Fatalf("Failed to change to temp dir: %v", err) |
28 | | - } |
| 13 | + // Create a temporary directory and change to it |
| 14 | + t.Chdir(t.TempDir()) |
29 | 15 |
|
30 | 16 | // Create a test session |
31 | 17 | sess := session.New() |
32 | 18 | sess.ID = "test-session-id" |
33 | 19 |
|
34 | 20 | // Test 1: Save with custom filename |
35 | 21 | evalFile, err := Save(sess, "my-custom-eval") |
36 | | - if err != nil { |
37 | | - t.Fatalf("Failed to save eval: %v", err) |
38 | | - } |
39 | | - |
40 | | - expectedPath := filepath.Join("evals", "my-custom-eval.json") |
41 | | - if evalFile != expectedPath { |
42 | | - t.Errorf("Expected file path %q, got %q", expectedPath, evalFile) |
43 | | - } |
44 | | - |
45 | | - if _, err := os.Stat(evalFile); os.IsNotExist(err) { |
46 | | - t.Errorf("Expected file %q to exist", evalFile) |
47 | | - } |
| 22 | + require.NoError(t, err) |
| 23 | + require.Equal(t, filepath.Join("evals", "my-custom-eval.json"), evalFile) |
| 24 | + require.FileExists(t, evalFile) |
48 | 25 |
|
49 | 26 | // Test 2: Save without filename (should use session ID) |
50 | 27 | evalFile2, err := Save(sess, "") |
51 | | - if err != nil { |
52 | | - t.Fatalf("Failed to save eval: %v", err) |
53 | | - } |
54 | | - |
55 | | - expectedPath2 := filepath.Join("evals", sess.ID+".json") |
56 | | - if evalFile2 != expectedPath2 { |
57 | | - t.Errorf("Expected file path %q, got %q", expectedPath2, evalFile2) |
58 | | - } |
59 | | - |
60 | | - if _, err := os.Stat(evalFile2); os.IsNotExist(err) { |
61 | | - t.Errorf("Expected file %q to exist", evalFile2) |
62 | | - } |
| 28 | + require.NoError(t, err) |
| 29 | + require.Equal(t, filepath.Join("evals", sess.ID+".json"), evalFile2) |
| 30 | + require.FileExists(t, evalFile2) |
63 | 31 |
|
64 | 32 | // Test 3: Save with same filename (should add _1 suffix) |
65 | 33 | evalFile3, err := Save(sess, "my-custom-eval") |
66 | | - if err != nil { |
67 | | - t.Fatalf("Failed to save eval: %v", err) |
68 | | - } |
69 | | - |
70 | | - expectedPath3 := filepath.Join("evals", "my-custom-eval_1.json") |
71 | | - if evalFile3 != expectedPath3 { |
72 | | - t.Errorf("Expected file path %q, got %q", expectedPath3, evalFile3) |
73 | | - } |
74 | | - |
75 | | - if _, err := os.Stat(evalFile3); os.IsNotExist(err) { |
76 | | - t.Errorf("Expected file %q to exist", evalFile3) |
77 | | - } |
| 34 | + require.NoError(t, err) |
| 35 | + require.Equal(t, filepath.Join("evals", "my-custom-eval_1.json"), evalFile3) |
| 36 | + require.FileExists(t, evalFile3) |
78 | 37 | } |
0 commit comments