|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/josephgoksu/TaskWing/internal/project" |
| 8 | +) |
| 9 | + |
| 10 | +func TestSetProjectContext_NilReturnsError(t *testing.T) { |
| 11 | + // Clear any existing context |
| 12 | + ClearProjectContext() |
| 13 | + |
| 14 | + err := SetProjectContext(nil) |
| 15 | + if err == nil { |
| 16 | + t.Fatal("expected error for nil context, got nil") |
| 17 | + } |
| 18 | + |
| 19 | + // Verify error message is helpful |
| 20 | + if err.Error() != "SetProjectContext called with nil context" { |
| 21 | + t.Errorf("unexpected error message: %s", err.Error()) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func TestSetProjectContext_ValidContext(t *testing.T) { |
| 26 | + // Clear any existing context |
| 27 | + ClearProjectContext() |
| 28 | + defer ClearProjectContext() |
| 29 | + |
| 30 | + ctx := &project.Context{ |
| 31 | + RootPath: "/test/path", |
| 32 | + MarkerType: project.MarkerGit, |
| 33 | + } |
| 34 | + |
| 35 | + err := SetProjectContext(ctx) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("unexpected error: %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + // Verify context was set |
| 41 | + got := GetProjectContext() |
| 42 | + if got == nil { |
| 43 | + t.Fatal("expected context to be set") |
| 44 | + } |
| 45 | + if got.RootPath != ctx.RootPath { |
| 46 | + t.Errorf("expected RootPath %q, got %q", ctx.RootPath, got.RootPath) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestGetProjectContextOrError_NotSet(t *testing.T) { |
| 51 | + ClearProjectContext() |
| 52 | + |
| 53 | + ctx, err := GetProjectContextOrError() |
| 54 | + if err == nil { |
| 55 | + t.Fatal("expected error when context not set") |
| 56 | + } |
| 57 | + if !errors.Is(err, ErrProjectContextNotSet) { |
| 58 | + t.Errorf("expected ErrProjectContextNotSet, got: %v", err) |
| 59 | + } |
| 60 | + if ctx != nil { |
| 61 | + t.Error("expected nil context") |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestGetProjectContextOrError_Set(t *testing.T) { |
| 66 | + ClearProjectContext() |
| 67 | + defer ClearProjectContext() |
| 68 | + |
| 69 | + expected := &project.Context{RootPath: "/test"} |
| 70 | + _ = SetProjectContext(expected) |
| 71 | + |
| 72 | + ctx, err := GetProjectContextOrError() |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("unexpected error: %v", err) |
| 75 | + } |
| 76 | + if ctx != expected { |
| 77 | + t.Error("context does not match expected") |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestGetProjectRoot_NotSet(t *testing.T) { |
| 82 | + ClearProjectContext() |
| 83 | + |
| 84 | + root, err := GetProjectRoot() |
| 85 | + if err == nil { |
| 86 | + t.Fatal("expected error when context not set") |
| 87 | + } |
| 88 | + if !errors.Is(err, ErrProjectContextNotSet) { |
| 89 | + t.Errorf("expected ErrProjectContextNotSet, got: %v", err) |
| 90 | + } |
| 91 | + if root != "" { |
| 92 | + t.Errorf("expected empty root, got: %s", root) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestGetProjectRoot_EmptyRootPath(t *testing.T) { |
| 97 | + ClearProjectContext() |
| 98 | + defer ClearProjectContext() |
| 99 | + |
| 100 | + ctx := &project.Context{RootPath: ""} |
| 101 | + _ = SetProjectContext(ctx) |
| 102 | + |
| 103 | + root, err := GetProjectRoot() |
| 104 | + if err == nil { |
| 105 | + t.Fatal("expected error for empty RootPath") |
| 106 | + } |
| 107 | + if root != "" { |
| 108 | + t.Errorf("expected empty root, got: %s", root) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestGetProjectRoot_Valid(t *testing.T) { |
| 113 | + ClearProjectContext() |
| 114 | + defer ClearProjectContext() |
| 115 | + |
| 116 | + expected := "/my/project" |
| 117 | + ctx := &project.Context{RootPath: expected} |
| 118 | + _ = SetProjectContext(ctx) |
| 119 | + |
| 120 | + root, err := GetProjectRoot() |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("unexpected error: %v", err) |
| 123 | + } |
| 124 | + if root != expected { |
| 125 | + t.Errorf("expected %q, got %q", expected, root) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestGetMemoryBasePath_NotSet(t *testing.T) { |
| 130 | + ClearProjectContext() |
| 131 | + |
| 132 | + path, err := GetMemoryBasePath() |
| 133 | + if err == nil { |
| 134 | + t.Fatal("expected error when context not set") |
| 135 | + } |
| 136 | + if !errors.Is(err, ErrProjectContextNotSet) { |
| 137 | + t.Errorf("expected ErrProjectContextNotSet, got: %v", err) |
| 138 | + } |
| 139 | + if path != "" { |
| 140 | + t.Errorf("expected empty path, got: %s", path) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func TestGetMemoryBasePathOrGlobal_FallsBackToGlobal(t *testing.T) { |
| 145 | + ClearProjectContext() |
| 146 | + |
| 147 | + // Should fall back to global without error |
| 148 | + path, err := GetMemoryBasePathOrGlobal() |
| 149 | + if err != nil { |
| 150 | + t.Fatalf("unexpected error: %v", err) |
| 151 | + } |
| 152 | + if path == "" { |
| 153 | + t.Error("expected non-empty path") |
| 154 | + } |
| 155 | + // Should contain "memory" in the path |
| 156 | + if len(path) < 6 || path[len(path)-6:] != "memory" { |
| 157 | + t.Errorf("expected path to end with 'memory', got: %s", path) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func TestGetMemoryBasePathOrGlobal_GlobalDirError(t *testing.T) { |
| 162 | + ClearProjectContext() |
| 163 | + |
| 164 | + // Save original function |
| 165 | + original := GetGlobalConfigDir |
| 166 | + defer func() { GetGlobalConfigDir = original }() |
| 167 | + |
| 168 | + // Mock to return error |
| 169 | + GetGlobalConfigDir = func() (string, error) { |
| 170 | + return "", errors.New("test error: cannot get home dir") |
| 171 | + } |
| 172 | + |
| 173 | + path, err := GetMemoryBasePathOrGlobal() |
| 174 | + if err == nil { |
| 175 | + t.Fatal("expected error when global config dir fails") |
| 176 | + } |
| 177 | + if path != "" { |
| 178 | + t.Errorf("expected empty path on error, got: %s", path) |
| 179 | + } |
| 180 | +} |
0 commit comments