Skip to content

Commit bda00de

Browse files
committed
Merge branch '2025-11-11-ki40-YDd14'
# Conflicts: # internal/features/scripting/infrastructure/cache/filesystem_cache_test.go
2 parents 3fcaff3 + c96d058 commit bda00de

File tree

6 files changed

+1466
-0
lines changed

6 files changed

+1466
-0
lines changed

internal/features/process/infrastructure/detector/detector_darwin_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,103 @@ func TestParseLsofOutput_Invalid(t *testing.T) {
133133
t.Error("Expected error for output without PID")
134134
}
135135
}
136+
137+
// TestDarwinDetector_DetectByPort_InvalidPort tests DetectByPort with invalid port
138+
func TestDarwinDetector_DetectByPort_InvalidPort(t *testing.T) {
139+
detector := &darwinDetector{}
140+
ctx := context.Background()
141+
142+
// Use a very large port number
143+
invalidPort := uint32(99999)
144+
145+
_, err := detector.DetectByPort(ctx, invalidPort)
146+
if err == nil {
147+
t.Error("Expected error for invalid port")
148+
}
149+
150+
if err != nil && !strings.Contains(err.Error(), "lsof failed") && !strings.Contains(err.Error(), "no process found") {
151+
t.Errorf("Unexpected error: %v", err)
152+
}
153+
}
154+
155+
// TestDarwinDetector_DetectByPort_ContextCancellation tests DetectByPort with cancelled context
156+
func TestDarwinDetector_DetectByPort_ContextCancellation(t *testing.T) {
157+
detector := &darwinDetector{}
158+
ctx, cancel := context.WithCancel(context.Background())
159+
cancel()
160+
161+
port := uint32(8080)
162+
163+
_, err := detector.DetectByPort(ctx, port)
164+
if err == nil {
165+
t.Error("Expected error for cancelled context")
166+
}
167+
168+
if err != nil && !strings.Contains(err.Error(), "context canceled") && !strings.Contains(err.Error(), "lsof failed") {
169+
t.Errorf("Unexpected error: %v", err)
170+
}
171+
}
172+
173+
// TestParseLsofOutput_MultipleEntries tests parseLsofOutput with multiple entries
174+
func TestParseLsofOutput_MultipleEntries(t *testing.T) {
175+
output := `p12345
176+
cmyapp
177+
n127.0.0.1:8080
178+
p67890
179+
canotherapp
180+
n127.0.0.1:8081`
181+
182+
info, err := parseLsofOutput(output)
183+
if err != nil {
184+
t.Fatalf("parseLsofOutput failed: %v", err)
185+
}
186+
187+
if info == nil {
188+
t.Fatal("Expected non-nil ProcessInfo")
189+
}
190+
191+
// Функция перезаписывает pid при каждом найденном 'p', поэтому возвращается последний
192+
if info.PID != 67890 {
193+
t.Errorf("Expected PID 67890 (last PID found), got %d", info.PID)
194+
}
195+
196+
// Проверяем что команда и имя тоже соответствуют последней записи
197+
if info.Name != "anotherapp" {
198+
t.Errorf("Expected name 'anotherapp', got '%s'", info.Name)
199+
}
200+
}
201+
202+
// TestParseLsofOutput_InvalidPID tests parseLsofOutput with invalid PID
203+
func TestParseLsofOutput_InvalidPID(t *testing.T) {
204+
output := `pnotanumber
205+
cmyapp
206+
n127.0.0.1:8080`
207+
208+
_, err := parseLsofOutput(output)
209+
if err == nil {
210+
t.Error("Expected error for invalid PID")
211+
}
212+
}
213+
214+
// TestParseLsofOutput_EmptyLines tests parseLsofOutput with empty lines
215+
func TestParseLsofOutput_EmptyLines(t *testing.T) {
216+
output := `p12345
217+
218+
cmyapp
219+
220+
n127.0.0.1:8080
221+
`
222+
223+
info, err := parseLsofOutput(output)
224+
if err != nil {
225+
t.Fatalf("parseLsofOutput failed: %v", err)
226+
}
227+
228+
if info == nil {
229+
t.Fatal("Expected non-nil ProcessInfo")
230+
}
231+
232+
if info.PID != 12345 {
233+
t.Errorf("Expected PID 12345, got %d", info.PID)
234+
}
235+
}

internal/features/proxy/infrastructure/persistence/repo_test.go

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,261 @@ func TestRepo_SaveLoad(t *testing.T) {
4747
t.Fatalf("unexpected %+v", got)
4848
}
4949
}
50+
51+
// TestRepo_Load_DefaultConfig tests Load with default configuration
52+
func TestRepo_Load_DefaultConfig(t *testing.T) {
53+
db := newTestDB(t)
54+
r := NewRepo(db)
55+
ctx := context.Background()
56+
57+
// Удаляем возможную существующую запись, чтобы проверить создание по умолчанию
58+
db.WithContext(ctx).Exec("DELETE FROM proxy_config WHERE id = 1")
59+
60+
pc, err := r.Load(ctx)
61+
if err != nil {
62+
t.Fatalf("Load() error = %v, want nil", err)
63+
}
64+
65+
if pc.ID != 1 {
66+
t.Errorf("Load() pc.ID = %d, want 1", pc.ID)
67+
}
68+
69+
if !pc.ForwardEnabled {
70+
t.Error("Load() pc.ForwardEnabled = false, want true")
71+
}
72+
73+
if pc.ForwardAddr != ":9091" {
74+
t.Errorf("Load() pc.ForwardAddr = %q, want \":9091\"", pc.ForwardAddr)
75+
}
76+
77+
if pc.SocksEnabled {
78+
t.Error("Load() pc.SocksEnabled = true, want false")
79+
}
80+
81+
if pc.UpdatedAt.IsZero() {
82+
t.Error("Load() pc.UpdatedAt should not be zero")
83+
}
84+
}
85+
86+
// TestRepo_Load_MultipleTimes tests Load called multiple times
87+
func TestRepo_Load_MultipleTimes(t *testing.T) {
88+
db := newTestDB(t)
89+
r := NewRepo(db)
90+
ctx := context.Background()
91+
92+
pc1, err := r.Load(ctx)
93+
if err != nil {
94+
t.Fatalf("First Load() error = %v, want nil", err)
95+
}
96+
97+
pc2, err := r.Load(ctx)
98+
if err != nil {
99+
t.Fatalf("Second Load() error = %v, want nil", err)
100+
}
101+
102+
if pc1.ID != pc2.ID {
103+
t.Errorf("Load() pc1.ID = %d, pc2.ID = %d, want same", pc1.ID, pc2.ID)
104+
}
105+
106+
if pc1.ForwardAddr != pc2.ForwardAddr {
107+
t.Errorf("Load() pc1.ForwardAddr = %q, pc2.ForwardAddr = %q, want same", pc1.ForwardAddr, pc2.ForwardAddr)
108+
}
109+
}
110+
111+
// TestRepo_Load_WithContextCancellation tests Load with cancelled context
112+
func TestRepo_Load_WithContextCancellation(t *testing.T) {
113+
db := newTestDB(t)
114+
r := NewRepo(db)
115+
ctx, cancel := context.WithCancel(context.Background())
116+
cancel()
117+
118+
_, err := r.Load(ctx)
119+
if err == nil {
120+
t.Error("Load() with cancelled context should return error")
121+
}
122+
}
123+
124+
// TestRepo_Load_AfterSave tests Load after Save
125+
func TestRepo_Load_AfterSave(t *testing.T) {
126+
db := newTestDB(t)
127+
r := NewRepo(db)
128+
ctx := context.Background()
129+
130+
// Загружаем конфигурацию
131+
pc, err := r.Load(ctx)
132+
if err != nil {
133+
t.Fatalf("Load() error = %v, want nil", err)
134+
}
135+
136+
// Изменяем конфигурацию
137+
pc.ForwardAddr = ":8888"
138+
pc.SocksEnabled = true
139+
pc.SocksAddr = ":7777"
140+
141+
// Сохраняем
142+
err = r.Save(ctx, pc)
143+
if err != nil {
144+
t.Fatalf("Save() error = %v, want nil", err)
145+
}
146+
147+
// Загружаем снова
148+
pc2, err := r.Load(ctx)
149+
if err != nil {
150+
t.Fatalf("Second Load() error = %v, want nil", err)
151+
}
152+
153+
if pc2.ForwardAddr != ":8888" {
154+
t.Errorf("Load() after Save() pc2.ForwardAddr = %q, want \":8888\"", pc2.ForwardAddr)
155+
}
156+
157+
if !pc2.SocksEnabled {
158+
t.Error("Load() after Save() pc2.SocksEnabled = false, want true")
159+
}
160+
161+
if pc2.SocksAddr != ":7777" {
162+
t.Errorf("Load() after Save() pc2.SocksAddr = %q, want \":7777\"", pc2.SocksAddr)
163+
}
164+
}
165+
166+
// TestRepo_Save tests Save method
167+
func TestRepo_Save(t *testing.T) {
168+
db := newTestDB(t)
169+
r := NewRepo(db)
170+
ctx := context.Background()
171+
172+
pc, err := r.Load(ctx)
173+
if err != nil {
174+
t.Fatalf("Load() error = %v, want nil", err)
175+
}
176+
177+
pc.ForwardAddr = ":7777"
178+
pc.SocksEnabled = true
179+
180+
err = r.Save(ctx, pc)
181+
if err != nil {
182+
t.Fatalf("Save() error = %v, want nil", err)
183+
}
184+
185+
pc2, err := r.Load(ctx)
186+
if err != nil {
187+
t.Fatalf("Second Load() error = %v, want nil", err)
188+
}
189+
190+
if pc2.ForwardAddr != ":7777" {
191+
t.Errorf("Save() pc2.ForwardAddr = %q, want \":7777\"", pc2.ForwardAddr)
192+
}
193+
194+
if !pc2.SocksEnabled {
195+
t.Error("Save() pc2.SocksEnabled = false, want true")
196+
}
197+
}
198+
199+
// TestRepo_Save_UpdatesTimestamp tests Save updates UpdatedAt timestamp
200+
func TestRepo_Save_UpdatesTimestamp(t *testing.T) {
201+
db := newTestDB(t)
202+
r := NewRepo(db)
203+
ctx := context.Background()
204+
205+
pc, err := r.Load(ctx)
206+
if err != nil {
207+
t.Fatalf("Load() error = %v, want nil", err)
208+
}
209+
210+
originalTime := pc.UpdatedAt
211+
212+
time.Sleep(10 * time.Millisecond)
213+
214+
pc.ForwardAddr = ":8888"
215+
err = r.Save(ctx, pc)
216+
if err != nil {
217+
t.Fatalf("Save() error = %v, want nil", err)
218+
}
219+
220+
pc2, err := r.Load(ctx)
221+
if err != nil {
222+
t.Fatalf("Second Load() error = %v, want nil", err)
223+
}
224+
225+
if !pc2.UpdatedAt.After(originalTime) {
226+
t.Error("Save() should update UpdatedAt timestamp")
227+
}
228+
}
229+
230+
// TestRepo_Save_WithZeroTimestamp tests Save with zero timestamp
231+
func TestRepo_Save_WithZeroTimestamp(t *testing.T) {
232+
db := newTestDB(t)
233+
r := NewRepo(db)
234+
ctx := context.Background()
235+
236+
pc, err := r.Load(ctx)
237+
if err != nil {
238+
t.Fatalf("Load() error = %v, want nil", err)
239+
}
240+
241+
pc.UpdatedAt = time.Time{}
242+
243+
err = r.Save(ctx, pc)
244+
if err != nil {
245+
t.Fatalf("Save() error = %v, want nil", err)
246+
}
247+
248+
pc2, err := r.Load(ctx)
249+
if err != nil {
250+
t.Fatalf("Second Load() error = %v, want nil", err)
251+
}
252+
253+
if pc2.UpdatedAt.IsZero() {
254+
t.Error("Save() should set UpdatedAt if zero")
255+
}
256+
}
257+
258+
// TestRepo_Save_WithContextCancellation tests Save with cancelled context
259+
func TestRepo_Save_WithContextCancellation(t *testing.T) {
260+
db := newTestDB(t)
261+
r := NewRepo(db)
262+
ctx, cancel := context.WithCancel(context.Background())
263+
cancel()
264+
265+
pc, err := r.Load(context.Background())
266+
if err != nil {
267+
t.Fatalf("Load() error = %v, want nil", err)
268+
}
269+
270+
err = r.Save(ctx, pc)
271+
if err == nil {
272+
t.Error("Save() with cancelled context should return error")
273+
}
274+
}
275+
276+
// TestRepo_Save_MultipleTimes tests Save called multiple times
277+
func TestRepo_Save_MultipleTimes(t *testing.T) {
278+
db := newTestDB(t)
279+
r := NewRepo(db)
280+
ctx := context.Background()
281+
282+
pc, err := r.Load(ctx)
283+
if err != nil {
284+
t.Fatalf("Load() error = %v, want nil", err)
285+
}
286+
287+
pc.ForwardAddr = ":1111"
288+
err = r.Save(ctx, pc)
289+
if err != nil {
290+
t.Fatalf("First Save() error = %v, want nil", err)
291+
}
292+
293+
pc.ForwardAddr = ":2222"
294+
err = r.Save(ctx, pc)
295+
if err != nil {
296+
t.Fatalf("Second Save() error = %v, want nil", err)
297+
}
298+
299+
pc2, err := r.Load(ctx)
300+
if err != nil {
301+
t.Fatalf("Load() error = %v, want nil", err)
302+
}
303+
304+
if pc2.ForwardAddr != ":2222" {
305+
t.Errorf("Save() pc2.ForwardAddr = %q, want \":2222\"", pc2.ForwardAddr)
306+
}
307+
}

0 commit comments

Comments
 (0)