| Josh Bleecher Snyder | 2abd467 | 2025-06-04 14:56:33 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| banksean | 4a055c5 | 2025-06-28 22:15:32 +0000 | [diff] [blame] | 4 | "context" |
| Josh Bleecher Snyder | 2abd467 | 2025-06-04 14:56:33 -0700 | [diff] [blame] | 5 | "os" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestExpandTilde(t *testing.T) { |
| 10 | homeDir, err := os.UserHomeDir() |
| 11 | if err != nil { |
| 12 | t.Fatalf("Failed to get home directory: %v", err) |
| 13 | } |
| 14 | |
| 15 | tests := []struct { |
| 16 | name string |
| 17 | input string |
| 18 | expected string |
| 19 | }{ |
| 20 | {"tilde only", "~", homeDir}, |
| 21 | {"tilde with path", "~/Documents", homeDir + "/Documents"}, |
| 22 | {"no tilde", "/absolute/path", "/absolute/path"}, |
| 23 | {"tilde in middle", "/path/~/middle", "/path/~/middle"}, |
| 24 | {"relative path", "relative/path", "relative/path"}, |
| 25 | } |
| 26 | |
| 27 | for _, tt := range tests { |
| 28 | t.Run(tt.name, func(t *testing.T) { |
| 29 | result, err := expandTilde(tt.input) |
| 30 | if err != nil { |
| 31 | t.Errorf("expandTilde(%q) returned error: %v", tt.input, err) |
| 32 | } |
| 33 | if result != tt.expected { |
| 34 | t.Errorf("expandTilde(%q) = %q, want %q", tt.input, result, tt.expected) |
| 35 | } |
| 36 | }) |
| 37 | } |
| 38 | } |
| banksean | 4a055c5 | 2025-06-28 22:15:32 +0000 | [diff] [blame] | 39 | |
| 40 | func TestSetupAndRunAgent_SetsPubKeyEnvVar(t *testing.T) { |
| 41 | // Save original environment |
| 42 | originalPubKey := os.Getenv("SKETCH_PUB_KEY") |
| 43 | defer func() { |
| 44 | if originalPubKey == "" { |
| 45 | os.Unsetenv("SKETCH_PUB_KEY") |
| 46 | } else { |
| 47 | os.Setenv("SKETCH_PUB_KEY", originalPubKey) |
| 48 | } |
| 49 | }() |
| 50 | |
| 51 | // Clear the environment variable first |
| 52 | os.Unsetenv("SKETCH_PUB_KEY") |
| 53 | |
| 54 | // Verify it's not set |
| 55 | if os.Getenv("SKETCH_PUB_KEY") != "" { |
| 56 | t.Fatal("SKETCH_PUB_KEY should not be set initially") |
| 57 | } |
| 58 | |
| 59 | // Test data |
| 60 | testPubKey := "test-public-key-123" |
| 61 | |
| 62 | // Create a minimal flags struct |
| 63 | flags := CLIFlags{ |
| 64 | modelName: "claude", |
| 65 | } |
| 66 | |
| 67 | // This should fail due to missing API key, but should still set the environment variable |
| Josh Bleecher Snyder | d1c1ace | 2025-07-29 00:16:27 +0000 | [diff] [blame] | 68 | err := setupAndRunAgent(context.TODO(), flags, modelSpec{}, testPubKey, false, nil) |
| banksean | 4a055c5 | 2025-06-28 22:15:32 +0000 | [diff] [blame] | 69 | |
| 70 | // Check that the environment variable was set correctly |
| 71 | if os.Getenv("SKETCH_PUB_KEY") != testPubKey { |
| 72 | t.Errorf("Expected SKETCH_PUB_KEY to be %q, got %q", testPubKey, os.Getenv("SKETCH_PUB_KEY")) |
| 73 | } |
| 74 | |
| 75 | // We expect this to fail due to missing API key, but that's fine for this test |
| 76 | if err == nil { |
| 77 | t.Error("Expected setupAndRunAgent to fail due to missing API key") |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestSetupAndRunAgent_DoesNotSetEmptyPubKey(t *testing.T) { |
| 82 | // Save original environment |
| 83 | originalPubKey := os.Getenv("SKETCH_PUB_KEY") |
| 84 | defer func() { |
| 85 | if originalPubKey == "" { |
| 86 | os.Unsetenv("SKETCH_PUB_KEY") |
| 87 | } else { |
| 88 | os.Setenv("SKETCH_PUB_KEY", originalPubKey) |
| 89 | } |
| 90 | }() |
| 91 | |
| 92 | // Set a value first |
| 93 | os.Setenv("SKETCH_PUB_KEY", "existing-value") |
| 94 | |
| 95 | // Create a minimal flags struct |
| 96 | flags := CLIFlags{ |
| 97 | modelName: "claude", |
| 98 | } |
| 99 | |
| 100 | // This should fail due to missing API key, but should not change the environment variable |
| Josh Bleecher Snyder | d1c1ace | 2025-07-29 00:16:27 +0000 | [diff] [blame] | 101 | err := setupAndRunAgent(context.TODO(), flags, modelSpec{}, "", false, nil) |
| banksean | 4a055c5 | 2025-06-28 22:15:32 +0000 | [diff] [blame] | 102 | |
| 103 | // Check that the environment variable was not changed |
| 104 | if os.Getenv("SKETCH_PUB_KEY") != "existing-value" { |
| 105 | t.Errorf("Expected SKETCH_PUB_KEY to remain %q, got %q", "existing-value", os.Getenv("SKETCH_PUB_KEY")) |
| 106 | } |
| 107 | |
| 108 | // We expect this to fail due to missing API key, but that's fine for this test |
| 109 | if err == nil { |
| 110 | t.Error("Expected setupAndRunAgent to fail due to missing API key") |
| 111 | } |
| 112 | } |