1package config
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10func TestWriteConfigOrdered(t *testing.T) {
11 // Create a temp directory for the test
12 tmpDir := t.TempDir()
13 configPath := filepath.Join(tmpDir, "config.toml")
14
15 // Get default config
16 cfg := DefaultConfig()
17
18 // Write it
19 err := WriteConfigOrdered(cfg, configPath)
20 if err != nil {
21 t.Fatalf("WriteConfigOrdered failed: %v", err)
22 }
23
24 // Read back
25 content, err := os.ReadFile(configPath)
26 if err != nil {
27 t.Fatalf("Failed to read written config: %v", err)
28 }
29
30 // Verify sections are in alphabetical order
31 lines := strings.Split(string(content), "\n")
32 var sections []string
33 for _, line := range lines {
34 if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
35 sections = append(sections, line)
36 }
37 }
38
39 // Check that sections are sorted
40 for i := 1; i < len(sections); i++ {
41 if sections[i-1] > sections[i] {
42 t.Errorf("Sections not sorted: %s > %s", sections[i-1], sections[i])
43 }
44 }
45
46 t.Logf("Found %d sections, all sorted", len(sections))
47
48 // Print first 50 lines for debugging
49 t.Logf("First 50 lines of output:\n%s", strings.Join(lines[:min(50, len(lines))], "\n"))
50}
51
52func TestSortTOMLSections(t *testing.T) {
53 input := `default_search_engine = 'https://example.com'
54
55[workspace]
56new_pane_url = 'about:blank'
57
58[appearance]
59color_scheme = 'default'
60
61[workspace.pane_mode]
62timeout_ms = 3000
63
64[appearance.dark_palette]
65background = '#000'
66`
67
68 result := sortTOMLSections(input)
69
70 // Extract section order
71 lines := strings.Split(result, "\n")
72 var sections []string
73 for _, line := range lines {
74 if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
75 sections = append(sections, line)
76 }
77 }
78
79 // Expected order: appearance, appearance.dark_palette, workspace, workspace.pane_mode
80 expected := []string{
81 "[appearance]",
82 "[appearance.dark_palette]",
83 "[workspace]",
84 "[workspace.pane_mode]",
85 }
86
87 if len(sections) != len(expected) {
88 t.Fatalf("Expected %d sections, got %d: %v", len(expected), len(sections), sections)
89 }
90
91 for i, exp := range expected {
92 if sections[i] != exp {
93 t.Errorf("Section %d: expected %s, got %s", i, exp, sections[i])
94 }
95 }
96
97 t.Logf("Sorted output:\n%s", result)
98}