File size: 6,794 Bytes
8d3471e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package shared

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"ds2api/internal/config"
)

// ─── reverseAccounts ─────────────────────────────────────────────────

func TestReverseAccountsEmpty(t *testing.T) {
	a := []config.Account{}
	reverseAccounts(a)
	if len(a) != 0 {
		t.Fatal("expected empty")
	}
}

func TestReverseAccountsTwoElements(t *testing.T) {
	a := []config.Account{
		{Email: "a@test.com"},
		{Email: "b@test.com"},
	}
	reverseAccounts(a)
	if a[0].Email != "b@test.com" || a[1].Email != "a@test.com" {
		t.Fatalf("unexpected order after reverse: %v", a)
	}
}

func TestReverseAccountsThreeElements(t *testing.T) {
	a := []config.Account{
		{Email: "1@test.com"},
		{Email: "2@test.com"},
		{Email: "3@test.com"},
	}
	reverseAccounts(a)
	if a[0].Email != "3@test.com" || a[1].Email != "2@test.com" || a[2].Email != "1@test.com" {
		t.Fatalf("unexpected order: %v", a)
	}
}

// ─── intFromQuery edge cases ─────────────────────────────────────────

func TestIntFromQueryPresent(t *testing.T) {
	req := httptest.NewRequest("GET", "/?limit=5", nil)
	if got := intFromQuery(req, "limit", 10); got != 5 {
		t.Fatalf("expected 5, got %d", got)
	}
}

func TestIntFromQueryMissing(t *testing.T) {
	req := httptest.NewRequest("GET", "/", nil)
	if got := intFromQuery(req, "limit", 10); got != 10 {
		t.Fatalf("expected default 10, got %d", got)
	}
}

func TestIntFromQueryInvalid(t *testing.T) {
	req := httptest.NewRequest("GET", "/?limit=abc", nil)
	if got := intFromQuery(req, "limit", 10); got != 10 {
		t.Fatalf("expected default 10 for invalid, got %d", got)
	}
}

func TestIntFromQueryNegative(t *testing.T) {
	req := httptest.NewRequest("GET", "/?limit=-3", nil)
	if got := intFromQuery(req, "limit", 10); got != -3 {
		t.Fatalf("expected -3, got %d", got)
	}
}

func TestIntFromQueryZero(t *testing.T) {
	req := httptest.NewRequest("GET", "/?limit=0", nil)
	if got := intFromQuery(req, "limit", 10); got != 0 {
		t.Fatalf("expected 0, got %d", got)
	}
}

// ─── nilIfEmpty ──────────────────────────────────────────────────────

func TestNilIfEmptyEmpty(t *testing.T) {
	if nilIfEmpty("") != nil {
		t.Fatal("expected nil for empty string")
	}
}

func TestNilIfEmptyNonEmpty(t *testing.T) {
	if nilIfEmpty("hello") != "hello" {
		t.Fatal("expected 'hello'")
	}
}

// ─── nilIfZero ───────────────────────────────────────────────────────

func TestNilIfZeroZero(t *testing.T) {
	if nilIfZero(0) != nil {
		t.Fatal("expected nil for zero")
	}
}

func TestNilIfZeroNonZero(t *testing.T) {
	if nilIfZero(42) != int64(42) {
		t.Fatal("expected 42")
	}
}

func TestNilIfZeroNegative(t *testing.T) {
	if nilIfZero(-1) != int64(-1) {
		t.Fatal("expected -1")
	}
}

// ─── toStringSlice ───────────────────────────────────────────────────

func TestToStringSliceFromAnySlice(t *testing.T) {
	input := []any{"a", "b", "c"}
	got, ok := toStringSlice(input)
	if !ok || len(got) != 3 {
		t.Fatalf("expected 3 strings, got %#v ok=%v", got, ok)
	}
	if got[0] != "a" || got[1] != "b" || got[2] != "c" {
		t.Fatalf("unexpected values: %#v", got)
	}
}

func TestToStringSliceFromMixed(t *testing.T) {
	input := []any{"hello", 42, true}
	got, ok := toStringSlice(input)
	if !ok {
		t.Fatal("expected ok for mixed types")
	}
	if got[0] != "hello" || got[1] != "42" || got[2] != "true" {
		t.Fatalf("unexpected values: %#v", got)
	}
}

func TestToStringSliceFromNonSlice(t *testing.T) {
	_, ok := toStringSlice("not a slice")
	if ok {
		t.Fatal("expected not ok for string input")
	}
}

func TestToStringSliceFromNil(t *testing.T) {
	_, ok := toStringSlice(nil)
	if ok {
		t.Fatal("expected not ok for nil input")
	}
}

func TestToStringSliceEmpty(t *testing.T) {
	got, ok := toStringSlice([]any{})
	if !ok {
		t.Fatal("expected ok for empty slice")
	}
	if len(got) != 0 {
		t.Fatalf("expected empty result, got %#v", got)
	}
}

func TestToStringSliceTrimsWhitespace(t *testing.T) {
	got, ok := toStringSlice([]any{" hello ", " world "})
	if !ok {
		t.Fatal("expected ok")
	}
	if got[0] != "hello" || got[1] != "world" {
		t.Fatalf("expected trimmed values, got %#v", got)
	}
}

// ─── toAccount edge cases ────────────────────────────────────────────

func TestToAccountAllFields(t *testing.T) {
	acc := toAccount(map[string]any{
		"email":    "user@test.com",
		"mobile":   "13800138000",
		"password": "secret",
		"token":    "tok123",
	})
	if acc.Email != "user@test.com" {
		t.Fatalf("unexpected email: %q", acc.Email)
	}
	if acc.Mobile != "+8613800138000" {
		t.Fatalf("unexpected mobile: %q", acc.Mobile)
	}
	if acc.Password != "secret" {
		t.Fatalf("unexpected password: %q", acc.Password)
	}
	if acc.Token != "" {
		t.Fatalf("expected token to be ignored, got %q", acc.Token)
	}
}

func TestToAccountNumericValues(t *testing.T) {
	acc := toAccount(map[string]any{
		"email": 12345,
	})
	if acc.Email != "12345" {
		t.Fatalf("expected numeric converted to string, got %q", acc.Email)
	}
}

// ─── fieldString edge cases ──────────────────────────────────────────

func TestFieldStringNonString(t *testing.T) {
	got := fieldString(map[string]any{"key": 42}, "key")
	if got != "42" {
		t.Fatalf("expected '42' for int, got %q", got)
	}
}

func TestFieldStringBool(t *testing.T) {
	got := fieldString(map[string]any{"key": true}, "key")
	if got != "true" {
		t.Fatalf("expected 'true', got %q", got)
	}
}

func TestFieldStringWhitespace(t *testing.T) {
	got := fieldString(map[string]any{"key": "  hello  "}, "key")
	if got != "hello" {
		t.Fatalf("expected trimmed 'hello', got %q", got)
	}
}

// ─── statusOr ────────────────────────────────────────────────────────

func TestStatusOrZeroReturnsDefault(t *testing.T) {
	if got := statusOr(0, http.StatusOK); got != http.StatusOK {
		t.Fatalf("expected %d, got %d", http.StatusOK, got)
	}
}

func TestStatusOrNonZeroReturnsValue(t *testing.T) {
	if got := statusOr(http.StatusBadRequest, http.StatusOK); got != http.StatusBadRequest {
		t.Fatalf("expected %d, got %d", http.StatusBadRequest, got)
	}
}