-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock_handler_test.go
More file actions
78 lines (66 loc) · 2.01 KB
/
lock_handler_test.go
File metadata and controls
78 lines (66 loc) · 2.01 KB
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
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestHandleTabLock(t *testing.T) {
b := &Bridge{locks: newLockManager()}
// Lock a tab
body, _ := json.Marshal(map[string]any{"tabId": "t1", "owner": "agent-a", "timeoutSec": 10})
w := httptest.NewRecorder()
r, _ := http.NewRequest("POST", "/tab/lock", bytes.NewReader(body))
b.handleTabLock(w, r)
if w.Code != 200 {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
var resp map[string]any
_ = json.Unmarshal(w.Body.Bytes(), &resp)
if resp["locked"] != true {
t.Fatalf("expected locked=true: %v", resp)
}
if resp["owner"] != "agent-a" {
t.Fatalf("expected owner=agent-a: %v", resp)
}
// Conflict from different owner
body, _ = json.Marshal(map[string]any{"tabId": "t1", "owner": "agent-b"})
w = httptest.NewRecorder()
r, _ = http.NewRequest("POST", "/tab/lock", bytes.NewReader(body))
b.handleTabLock(w, r)
if w.Code != 409 {
t.Fatalf("expected 409, got %d", w.Code)
}
}
func TestHandleTabUnlock(t *testing.T) {
b := &Bridge{locks: newLockManager()}
_ = b.locks.Lock("t1", "agent-a", 0)
// Wrong owner
body, _ := json.Marshal(map[string]any{"tabId": "t1", "owner": "agent-b"})
w := httptest.NewRecorder()
r, _ := http.NewRequest("POST", "/tab/unlock", bytes.NewReader(body))
b.handleTabUnlock(w, r)
if w.Code != 409 {
t.Fatalf("expected 409, got %d", w.Code)
}
// Correct owner
body, _ = json.Marshal(map[string]any{"tabId": "t1", "owner": "agent-a"})
w = httptest.NewRecorder()
r, _ = http.NewRequest("POST", "/tab/unlock", bytes.NewReader(body))
b.handleTabUnlock(w, r)
if w.Code != 200 {
t.Fatalf("expected 200, got %d", w.Code)
}
}
func TestHandleTabLockValidation(t *testing.T) {
b := &Bridge{locks: newLockManager()}
// Missing fields
body, _ := json.Marshal(map[string]any{"tabId": "t1"})
w := httptest.NewRecorder()
r, _ := http.NewRequest("POST", "/tab/lock", bytes.NewReader(body))
b.handleTabLock(w, r)
if w.Code != 400 {
t.Fatalf("expected 400, got %d", w.Code)
}
}