|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestConfigSet_Success(t *testing.T) { |
| 11 | + t.Setenv("HEYGEN_CONFIG_DIR", t.TempDir()) |
| 12 | + |
| 13 | + res := runCommand(t, "http://example.invalid", "", "config", "set", "output", "human") |
| 14 | + if res.ExitCode != 0 { |
| 15 | + t.Fatalf("ExitCode = %d, want 0\nstderr: %s", res.ExitCode, res.Stderr) |
| 16 | + } |
| 17 | + |
| 18 | + data, err := os.ReadFile(filepath.Join(os.Getenv("HEYGEN_CONFIG_DIR"), "config.toml")) |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("ReadFile: %v", err) |
| 21 | + } |
| 22 | + if string(data) == "" { |
| 23 | + t.Fatal("expected config file contents") |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func TestConfigSet_InvalidKey(t *testing.T) { |
| 28 | + t.Setenv("HEYGEN_CONFIG_DIR", t.TempDir()) |
| 29 | + res := runCommand(t, "http://example.invalid", "", "config", "set", "bogus", "value") |
| 30 | + if res.ExitCode != 2 { |
| 31 | + t.Fatalf("ExitCode = %d, want 2\nstderr: %s", res.ExitCode, res.Stderr) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestConfigSet_APIBaseNotExposed(t *testing.T) { |
| 36 | + t.Setenv("HEYGEN_CONFIG_DIR", t.TempDir()) |
| 37 | + res := runCommand(t, "http://example.invalid", "", "config", "set", "api_base", "https://api-dev.heygen.com") |
| 38 | + if res.ExitCode != 2 { |
| 39 | + t.Fatalf("ExitCode = %d, want 2 (api_base is internal)\nstderr: %s", res.ExitCode, res.Stderr) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestConfigSet_InvalidOutputValue(t *testing.T) { |
| 44 | + t.Setenv("HEYGEN_CONFIG_DIR", t.TempDir()) |
| 45 | + res := runCommand(t, "http://example.invalid", "", "config", "set", "output", "xml") |
| 46 | + if res.ExitCode != 2 { |
| 47 | + t.Fatalf("ExitCode = %d, want 2\nstderr: %s", res.ExitCode, res.Stderr) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestConfigSet_SkipsAuth(t *testing.T) { |
| 52 | + t.Setenv("HEYGEN_CONFIG_DIR", t.TempDir()) |
| 53 | + res := runCommand(t, "http://example.invalid", "", "config", "set", "output", "human") |
| 54 | + if res.ExitCode != 0 { |
| 55 | + t.Fatalf("ExitCode = %d, want 0\nstderr: %s", res.ExitCode, res.Stderr) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestConfigGet_Default(t *testing.T) { |
| 60 | + t.Setenv("HEYGEN_CONFIG_DIR", t.TempDir()) |
| 61 | + res := runCommand(t, "http://example.invalid", "", "config", "get", "output") |
| 62 | + if res.ExitCode != 0 { |
| 63 | + t.Fatalf("ExitCode = %d, want 0\nstderr: %s", res.ExitCode, res.Stderr) |
| 64 | + } |
| 65 | + |
| 66 | + var parsed configResponse |
| 67 | + if err := json.Unmarshal([]byte(res.Stdout), &parsed); err != nil { |
| 68 | + t.Fatalf("Unmarshal: %v", err) |
| 69 | + } |
| 70 | + if parsed.Value != "json" || parsed.Source != "default" { |
| 71 | + t.Fatalf("parsed = %#v", parsed) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestConfigGet_FromEnv(t *testing.T) { |
| 76 | + t.Setenv("HEYGEN_CONFIG_DIR", t.TempDir()) |
| 77 | + t.Setenv("HEYGEN_OUTPUT", "human") |
| 78 | + |
| 79 | + res := runCommand(t, "http://example.invalid", "", "config", "get", "output") |
| 80 | + if res.ExitCode != 0 { |
| 81 | + t.Fatalf("ExitCode = %d, want 0\nstderr: %s", res.ExitCode, res.Stderr) |
| 82 | + } |
| 83 | + |
| 84 | + var parsed configResponse |
| 85 | + if err := json.Unmarshal([]byte(res.Stdout), &parsed); err != nil { |
| 86 | + t.Fatalf("Unmarshal: %v", err) |
| 87 | + } |
| 88 | + if parsed.Value != "human" || parsed.Source != "env" { |
| 89 | + t.Fatalf("parsed = %#v", parsed) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestConfigGet_FromFile(t *testing.T) { |
| 94 | + t.Setenv("HEYGEN_CONFIG_DIR", t.TempDir()) |
| 95 | + if err := os.WriteFile(filepath.Join(os.Getenv("HEYGEN_CONFIG_DIR"), "config.toml"), []byte("output = \"human\"\n"), 0o600); err != nil { |
| 96 | + t.Fatalf("WriteFile: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + res := runCommand(t, "http://example.invalid", "", "config", "get", "output") |
| 100 | + if res.ExitCode != 0 { |
| 101 | + t.Fatalf("ExitCode = %d, want 0\nstderr: %s", res.ExitCode, res.Stderr) |
| 102 | + } |
| 103 | + |
| 104 | + var parsed configResponse |
| 105 | + if err := json.Unmarshal([]byte(res.Stdout), &parsed); err != nil { |
| 106 | + t.Fatalf("Unmarshal: %v", err) |
| 107 | + } |
| 108 | + if parsed.Value != "human" || parsed.Source != "file" { |
| 109 | + t.Fatalf("parsed = %#v", parsed) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestConfigGet_InvalidKey(t *testing.T) { |
| 114 | + t.Setenv("HEYGEN_CONFIG_DIR", t.TempDir()) |
| 115 | + res := runCommand(t, "http://example.invalid", "", "config", "get", "bogus") |
| 116 | + if res.ExitCode != 2 { |
| 117 | + t.Fatalf("ExitCode = %d, want 2\nstderr: %s", res.ExitCode, res.Stderr) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestConfigList_AllDefaults(t *testing.T) { |
| 122 | + t.Setenv("HEYGEN_CONFIG_DIR", t.TempDir()) |
| 123 | + res := runCommand(t, "http://example.invalid", "", "config", "list") |
| 124 | + if res.ExitCode != 0 { |
| 125 | + t.Fatalf("ExitCode = %d, want 0\nstderr: %s", res.ExitCode, res.Stderr) |
| 126 | + } |
| 127 | + |
| 128 | + var parsed []configResponse |
| 129 | + if err := json.Unmarshal([]byte(res.Stdout), &parsed); err != nil { |
| 130 | + t.Fatalf("Unmarshal: %v", err) |
| 131 | + } |
| 132 | + if len(parsed) != 2 { |
| 133 | + t.Fatalf("len(parsed) = %d, want 4", len(parsed)) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestConfigList_MixedSources(t *testing.T) { |
| 138 | + t.Setenv("HEYGEN_CONFIG_DIR", t.TempDir()) |
| 139 | + t.Setenv("HEYGEN_OUTPUT", "json") |
| 140 | + if err := os.WriteFile(filepath.Join(os.Getenv("HEYGEN_CONFIG_DIR"), "config.toml"), []byte("analytics = false\n"), 0o600); err != nil { |
| 141 | + t.Fatalf("WriteFile: %v", err) |
| 142 | + } |
| 143 | + |
| 144 | + res := runCommand(t, "http://example.invalid", "", "config", "list") |
| 145 | + if res.ExitCode != 0 { |
| 146 | + t.Fatalf("ExitCode = %d, want 0\nstderr: %s", res.ExitCode, res.Stderr) |
| 147 | + } |
| 148 | + |
| 149 | + var parsed []configResponse |
| 150 | + if err := json.Unmarshal([]byte(res.Stdout), &parsed); err != nil { |
| 151 | + t.Fatalf("Unmarshal: %v", err) |
| 152 | + } |
| 153 | + if len(parsed) != 2 { |
| 154 | + t.Fatalf("len(parsed) = %d, want 4", len(parsed)) |
| 155 | + } |
| 156 | +} |
0 commit comments