config_test.go

 1package config
 2
 3import (
 4	"reflect"
 5	"testing"
 6)
 7
 8// TestSaveAndLoadConfig verifies that the config can be saved to and loaded from a file correctly.
 9func TestSaveAndLoadConfig(t *testing.T) {
10	// Create a temporary directory for the test to avoid interfering with actual user config.
11	tempDir := t.TempDir()
12
13	// Temporarily override the user home directory to our temp directory.
14	// This ensures that our config file is written to a predictable, temporary location.
15	t.Setenv("HOME", tempDir)
16
17	// Define a sample configuration to save.
18	expectedConfig := &Config{
19		ServiceProvider: "gmail",
20		Email:           "test@example.com",
21		Password:        "supersecret",
22		Name:            "Test User",
23	}
24
25	// Attempt to save the configuration.
26	err := SaveConfig(expectedConfig)
27	if err != nil {
28		t.Fatalf("SaveConfig() failed: %v", err)
29	}
30
31	// Attempt to load the configuration back.
32	loadedConfig, err := LoadConfig()
33	if err != nil {
34		t.Fatalf("LoadConfig() failed: %v", err)
35	}
36
37	// Compare the loaded configuration with the original one.
38	// reflect.DeepEqual is used for a deep comparison of the structs.
39	if !reflect.DeepEqual(loadedConfig, expectedConfig) {
40		t.Errorf("Loaded config does not match expected config.\nGot:  %+v\nWant: %+v", loadedConfig, expectedConfig)
41	}
42}
43
44// TestIMAPServer tests the logic that determines the IMAP server address.
45func TestIMAPServer(t *testing.T) {
46	testCases := []struct {
47		name     string
48		provider string
49		want     string
50	}{
51		{"Gmail", "gmail", "imap.gmail.com"},
52		{"iCloud", "icloud", "imap.mail.me.com"},
53		{"Unsupported", "yahoo", ""},
54		{"Empty", "", ""},
55	}
56
57	for _, tc := range testCases {
58		t.Run(tc.name, func(t *testing.T) {
59			cfg := &Config{ServiceProvider: tc.provider}
60			got := cfg.IMAPServer()
61			if got != tc.want {
62				t.Errorf("IMAPServer() = %q, want %q", got, tc.want)
63			}
64		})
65	}
66}