package mcp import ( "bytes" "errors" "strings" "testing" "time" "github.com/yuanyuanxiang/SimpleRemoter/server/go/connection" "github.com/yuanyuanxiang/SimpleRemoter/server/go/hub" ) // terminalTestServer builds a server backed by a real hub with one online // device (1001) whose shell commands are accepted by a no-op sender. func terminalTestServer(t *testing.T) (*Server, *hub.Hub) { t.Helper() h := hub.New() h.SetSender(func(*connection.Context, []byte) error { return nil }) h.Register(&hub.Device{ID: "1001", PeerIP: "10.0.0.1"}, &connection.Context{RemoteAddr: "10.0.0.1:54321"}) s := New(Config{Token: testToken, Hub: h}) t.Cleanup(s.Close) return s, h } // termPacket wraps shell output the way cmd/main.go publishes it. func termPacket(s string) []byte { p := make([]byte, 4+len(s)) copy(p, "TRM1") copy(p[4:], s) return p } func TestTermRingTrimsOldest(t *testing.T) { sess := newTermSession("1001") payload := make([]byte, 1<<20) for i := range payload { payload[i] = byte(i*7 + 1) } sess.mu.Lock() sess.appendLocked(payload) data, next, lost := sess.readLocked(0, 1<<20) size, total, dropped := sess.size, sess.total, sess.droppedBytes sess.mu.Unlock() if size != termRingCap { t.Fatalf("retained = %d, want cap %d", size, termRingCap) } if total != 1<<20 { t.Fatalf("total = %d, want %d", total, 1<<20) } if dropped != uint64((1<<20)-termRingCap) { t.Fatalf("dropped = %d, want %d", dropped, (1<<20)-termRingCap) } if !lost { t.Fatal("reading from 0 after overflow should report lost data") } if len(data) != termRingCap || next != 1<<20 { t.Fatalf("read len/next = %d/%d, want %d/%d", len(data), next, termRingCap, 1<<20) } if want := payload[(1<<20)-termRingCap:]; !bytes.Equal(data, want) { t.Fatal("retained data is not the newest window of the stream") } } func TestTermRingWraps(t *testing.T) { sess := newTermSession("1001") var want []byte for i := range 10 { chunk := bytes.Repeat([]byte{byte('a' + i)}, 40<<10) want = append(want, chunk...) sess.mu.Lock() sess.appendLocked(chunk) sess.mu.Unlock() } sess.mu.Lock() base, total := sess.baseLocked(), sess.total data, next, lost := sess.readLocked(0, termRingCap) tail, _, _ := sess.readLocked(total-100, 100) sess.mu.Unlock() if !lost { t.Fatal("reading from 0 after wraparound should report lost data") } if base != uint64(len(want)-termRingCap) { t.Fatalf("base = %d, want %d", base, len(want)-termRingCap) } if next != total { t.Fatalf("next = %d, want total %d", next, total) } if wantWin := want[len(want)-termRingCap:]; !bytes.Equal(data, wantWin) { t.Fatal("wrapped ring does not return the newest window in order") } if wantTail := want[len(want)-100:]; !bytes.Equal(tail, wantTail) { t.Fatal("tail read is not the newest bytes") } } func TestScanSentinel(t *testing.T) { const sentinel = "__MCP_DONE_abc123__" cases := []struct { name string in string code int ok bool }{ {"plain", "x\r\n" + sentinel + "0\r\n", 0, true}, {"nonzero", sentinel + "42", 42, true}, {"negative", sentinel + "-1073741510", -1073741510, true}, {"echo then real", sentinel + "%errorlevel%\r\n" + sentinel + "1\r\n", 1, true}, {"posix echo then real", sentinel + "$?\n" + sentinel + "0\n", 0, true}, {"command echo only", sentinel + "%errorlevel%", 0, false}, {"absent", "hello world", 0, false}, {"empty", "", 0, false}, } for _, tc := range cases { code, start, end, ok := scanSentinel([]byte(tc.in), sentinel) if ok != tc.ok { t.Errorf("%s: ok = %v, want %v", tc.name, ok, tc.ok) continue } if ok && code != tc.code { t.Errorf("%s: code = %d, want %d", tc.name, code, tc.code) } if ok && (start < 0 || start >= end || end > len(tc.in)) { t.Errorf("%s: start/end = %d/%d, want 0<=start