test(muxterm): unit tests for protocol, token validation, and pool

This commit is contained in:
Ken
2026-05-28 06:17:50 +00:00
parent f2d999973a
commit 11794c4e24
3 changed files with 80 additions and 417 deletions
+41 -134
View File
@@ -5,102 +5,8 @@ import (
"testing"
)
// --- Client-to-server message type tests ---
func TestAttachMsgJSON(t *testing.T) {
msg := AttachMsg{Attach: "my-session"}
data, err := json.Marshal(msg)
if err != nil {
t.Fatalf("marshal AttachMsg: %v", err)
}
var m map[string]interface{}
json.Unmarshal(data, &m)
if m["attach"] != "my-session" {
t.Errorf("expected attach=my-session, got %v", m["attach"])
}
}
func TestResizeMsgJSON(t *testing.T) {
msg := ResizeMsg{Resize: struct {
Cols int `json:"cols"`
Rows int `json:"rows"`
}{Cols: 80, Rows: 24}}
data, err := json.Marshal(msg)
if err != nil {
t.Fatalf("marshal ResizeMsg: %v", err)
}
var m map[string]interface{}
json.Unmarshal(data, &m)
resize, ok := m["resize"].(map[string]interface{})
if !ok {
t.Fatalf("expected resize to be a map, got %T", m["resize"])
}
if resize["cols"].(float64) != 80 {
t.Errorf("expected cols=80, got %v", resize["cols"])
}
if resize["rows"].(float64) != 24 {
t.Errorf("expected rows=24, got %v", resize["rows"])
}
}
func TestDetachMsgJSON(t *testing.T) {
msg := DetachMsg{Detach: true}
data, err := json.Marshal(msg)
if err != nil {
t.Fatalf("marshal DetachMsg: %v", err)
}
var m map[string]interface{}
json.Unmarshal(data, &m)
if m["detach"] != true {
t.Errorf("expected detach=true, got %v", m["detach"])
}
}
// --- Server-to-client message type tests ---
func TestAttachedMsgJSON(t *testing.T) {
msg := AttachedMsg{Attached: "sess1"}
data, err := json.Marshal(msg)
if err != nil {
t.Fatalf("marshal AttachedMsg: %v", err)
}
var m map[string]interface{}
json.Unmarshal(data, &m)
if m["attached"] != "sess1" {
t.Errorf("expected attached=sess1, got %v", m["attached"])
}
}
func TestErrorMsgJSON(t *testing.T) {
msg := ErrorMsg{Error: "something broke"}
data, err := json.Marshal(msg)
if err != nil {
t.Fatalf("marshal ErrorMsg: %v", err)
}
var m map[string]interface{}
json.Unmarshal(data, &m)
if m["error"] != "something broke" {
t.Errorf("expected error='something broke', got %v", m["error"])
}
}
func TestExitedMsgJSON(t *testing.T) {
msg := ExitedMsg{Exited: "sess1"}
data, err := json.Marshal(msg)
if err != nil {
t.Fatalf("marshal ExitedMsg: %v", err)
}
var m map[string]interface{}
json.Unmarshal(data, &m)
if m["exited"] != "sess1" {
t.Errorf("expected exited=sess1, got %v", m["exited"])
}
}
// --- ParseControlMessage tests ---
func TestParseControlMessage_Attach(t *testing.T) {
data := []byte(`{"attach":"dev"}`)
func TestParseAttachMessage(t *testing.T) {
data := []byte(`{"attach": "dev-server"}`)
kind, val := ParseControlMessage(data)
if kind != "attach" {
t.Fatalf("expected kind=attach, got %q", kind)
@@ -109,13 +15,13 @@ func TestParseControlMessage_Attach(t *testing.T) {
if !ok {
t.Fatalf("expected *AttachMsg, got %T", val)
}
if msg.Attach != "dev" {
t.Errorf("expected Attach=dev, got %q", msg.Attach)
if msg.Attach != "dev-server" {
t.Errorf("expected Attach=%q, got %q", "dev-server", msg.Attach)
}
}
func TestParseControlMessage_Resize(t *testing.T) {
data := []byte(`{"resize":{"cols":120,"rows":40}}`)
func TestParseResizeMessage(t *testing.T) {
data := []byte(`{"resize": {"cols": 120, "rows": 40}}`)
kind, val := ParseControlMessage(data)
if kind != "resize" {
t.Fatalf("expected kind=resize, got %q", kind)
@@ -124,13 +30,16 @@ func TestParseControlMessage_Resize(t *testing.T) {
if !ok {
t.Fatalf("expected *ResizeMsg, got %T", val)
}
if msg.Resize.Cols != 120 || msg.Resize.Rows != 40 {
t.Errorf("expected 120x40, got %dx%d", msg.Resize.Cols, msg.Resize.Rows)
if msg.Resize.Cols != 120 {
t.Errorf("expected Cols=120, got %d", msg.Resize.Cols)
}
if msg.Resize.Rows != 40 {
t.Errorf("expected Rows=40, got %d", msg.Resize.Rows)
}
}
func TestParseControlMessage_Detach(t *testing.T) {
data := []byte(`{"detach":true}`)
func TestParseDetachMessage(t *testing.T) {
data := []byte(`{"detach": true}`)
kind, val := ParseControlMessage(data)
if kind != "detach" {
t.Fatalf("expected kind=detach, got %q", kind)
@@ -144,19 +53,8 @@ func TestParseControlMessage_Detach(t *testing.T) {
}
}
func TestParseControlMessage_Unknown(t *testing.T) {
data := []byte(`{"foo":"bar"}`)
kind, val := ParseControlMessage(data)
if kind != "unknown" {
t.Errorf("expected kind=unknown, got %q", kind)
}
if val != nil {
t.Errorf("expected nil val for unknown, got %v", val)
}
}
func TestParseControlMessage_Invalid(t *testing.T) {
data := []byte(`not json at all`)
func TestParseInvalidJSON(t *testing.T) {
data := []byte(`not json`)
kind, val := ParseControlMessage(data)
if kind != "invalid" {
t.Errorf("expected kind=invalid, got %q", kind)
@@ -166,37 +64,46 @@ func TestParseControlMessage_Invalid(t *testing.T) {
}
}
// --- Marshal function tests ---
func TestParseUnknownMessage(t *testing.T) {
data := []byte(`{"foo": "bar"}`)
kind, val := ParseControlMessage(data)
if kind != "unknown" {
t.Errorf("expected kind=unknown, got %q", kind)
}
if val != nil {
t.Errorf("expected nil val for unknown, got %v", val)
}
}
func TestMarshalAttached(t *testing.T) {
data := MarshalAttached("sess1")
var m map[string]interface{}
if err := json.Unmarshal(data, &m); err != nil {
data := MarshalAttached("dev-server")
var msg AttachedMsg
if err := json.Unmarshal(data, &msg); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if m["attached"] != "sess1" {
t.Errorf("expected attached=sess1, got %v", m["attached"])
if msg.Attached != "dev-server" {
t.Errorf("expected Attached=%q, got %q", "dev-server", msg.Attached)
}
}
func TestMarshalError(t *testing.T) {
data := MarshalError("bad thing")
var m map[string]interface{}
if err := json.Unmarshal(data, &m); err != nil {
data := MarshalError("something broke")
var msg ErrorMsg
if err := json.Unmarshal(data, &msg); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if m["error"] != "bad thing" {
t.Errorf("expected error='bad thing', got %v", m["error"])
if msg.Error != "something broke" {
t.Errorf("expected Error=%q, got %q", "something broke", msg.Error)
}
}
func TestMarshalExited(t *testing.T) {
data := MarshalExited("sess1")
var m map[string]interface{}
if err := json.Unmarshal(data, &m); err != nil {
data := MarshalExited("dev-server")
var msg ExitedMsg
if err := json.Unmarshal(data, &msg); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if m["exited"] != "sess1" {
t.Errorf("expected exited=sess1, got %v", m["exited"])
if msg.Exited != "dev-server" {
t.Errorf("expected Exited=%q, got %q", "dev-server", msg.Exited)
}
}
}