add another 100 tests (thank you claude for making these I ain't coming up with this much shit to test)

This commit is contained in:
KingKDot
2026-04-08 21:49:59 -05:00
parent 6a058930aa
commit 66393d5ba4
8 changed files with 1248 additions and 0 deletions
@@ -0,0 +1,137 @@
package handlers
import (
"encoding/json"
"math"
"testing"
)
func TestExtractTimestamp_Int64(t *testing.T) {
var ts int64 = 1700000000000
if got := extractTimestamp(ts); got != ts {
t.Fatalf("expected %d, got %d", ts, got)
}
}
func TestExtractTimestamp_Uint64(t *testing.T) {
var ts uint64 = 1700000000000
if got := extractTimestamp(ts); got != int64(ts) {
t.Fatalf("expected %d, got %d", ts, got)
}
}
func TestExtractTimestamp_Uint64Overflow(t *testing.T) {
// Values exceeding max int64 should return 0
var ts uint64 = math.MaxUint64
if got := extractTimestamp(ts); got != 0 {
t.Fatalf("expected 0 for overflow, got %d", got)
}
}
func TestExtractTimestamp_Uint32(t *testing.T) {
var ts uint32 = 170000
if got := extractTimestamp(ts); got != int64(ts) {
t.Fatalf("expected %d, got %d", ts, got)
}
}
func TestExtractTimestamp_Uint16(t *testing.T) {
var ts uint16 = 1234
if got := extractTimestamp(ts); got != int64(ts) {
t.Fatalf("expected %d, got %d", ts, got)
}
}
func TestExtractTimestamp_Uint8(t *testing.T) {
var ts uint8 = 42
if got := extractTimestamp(ts); got != int64(ts) {
t.Fatalf("expected %d, got %d", ts, got)
}
}
func TestExtractTimestamp_Int32(t *testing.T) {
var ts int32 = 12345678
if got := extractTimestamp(ts); got != int64(ts) {
t.Fatalf("expected %d, got %d", ts, got)
}
}
func TestExtractTimestamp_Int16(t *testing.T) {
var ts int16 = 9999
if got := extractTimestamp(ts); got != int64(ts) {
t.Fatalf("expected %d, got %d", ts, got)
}
}
func TestExtractTimestamp_Int8(t *testing.T) {
var ts int8 = 127
if got := extractTimestamp(ts); got != int64(ts) {
t.Fatalf("expected %d, got %d", ts, got)
}
}
func TestExtractTimestamp_Int(t *testing.T) {
var ts int = 1700000000
if got := extractTimestamp(ts); got != int64(ts) {
t.Fatalf("expected %d, got %d", ts, got)
}
}
func TestExtractTimestamp_Float64(t *testing.T) {
var ts float64 = 1700000000000.0
if got := extractTimestamp(ts); got != int64(ts) {
t.Fatalf("expected %d, got %d", int64(ts), got)
}
}
func TestExtractTimestamp_Float32(t *testing.T) {
var ts float32 = 170000.0
if got := extractTimestamp(ts); got != int64(ts) {
t.Fatalf("expected %d, got %d", int64(ts), got)
}
}
func TestExtractTimestamp_JsonNumber(t *testing.T) {
n := json.Number("1700000000000")
if got := extractTimestamp(n); got != 1700000000000 {
t.Fatalf("expected 1700000000000, got %d", got)
}
}
func TestExtractTimestamp_InvalidJsonNumber(t *testing.T) {
n := json.Number("not-a-number")
if got := extractTimestamp(n); got != 0 {
t.Fatalf("expected 0 for invalid json.Number, got %d", got)
}
}
func TestExtractTimestamp_String(t *testing.T) {
if got := extractTimestamp("hello"); got != 0 {
t.Fatalf("expected 0 for string, got %d", got)
}
}
func TestExtractTimestamp_Nil(t *testing.T) {
if got := extractTimestamp(nil); got != 0 {
t.Fatalf("expected 0 for nil, got %d", got)
}
}
func TestExtractTimestamp_Bool(t *testing.T) {
if got := extractTimestamp(true); got != 0 {
t.Fatalf("expected 0 for bool, got %d", got)
}
}
func TestExtractTimestamp_Zero(t *testing.T) {
if got := extractTimestamp(int64(0)); got != 0 {
t.Fatalf("expected 0, got %d", got)
}
}
func TestExtractTimestamp_Negative(t *testing.T) {
var ts int64 = -100
if got := extractTimestamp(ts); got != ts {
t.Fatalf("expected %d, got %d", ts, got)
}
}
@@ -0,0 +1,84 @@
package handlers
import (
"testing"
)
func TestParseCommandArgs_EmptyInput(t *testing.T) {
result := parseCommandArgs("")
if len(result) != 0 {
t.Fatalf("expected empty slice, got %v", result)
}
}
func TestParseCommandArgs_WhitespaceOnly(t *testing.T) {
result := parseCommandArgs(" \t ")
if len(result) != 0 {
t.Fatalf("expected empty slice, got %v", result)
}
}
func TestParseCommandArgs_SimpleWords(t *testing.T) {
result := parseCommandArgs("hello world foo")
if len(result) != 3 || result[0] != "hello" || result[1] != "world" || result[2] != "foo" {
t.Fatalf("expected [hello world foo], got %v", result)
}
}
func TestParseCommandArgs_DoubleQuotes(t *testing.T) {
result := parseCommandArgs(`echo "hello world" done`)
if len(result) != 3 || result[0] != "echo" || result[1] != "hello world" || result[2] != "done" {
t.Fatalf("expected [echo, hello world, done], got %v", result)
}
}
func TestParseCommandArgs_SingleQuotes(t *testing.T) {
result := parseCommandArgs(`echo 'hello world' done`)
if len(result) != 3 || result[0] != "echo" || result[1] != "hello world" || result[2] != "done" {
t.Fatalf("expected [echo, hello world, done], got %v", result)
}
}
func TestParseCommandArgs_EscapedChars(t *testing.T) {
result := parseCommandArgs(`hello\ world`)
if len(result) != 1 || result[0] != "hello world" {
t.Fatalf("expected [hello world], got %v", result)
}
}
func TestParseCommandArgs_MixedQuotes(t *testing.T) {
result := parseCommandArgs(`cmd "arg one" 'arg two' plain`)
if len(result) != 4 {
t.Fatalf("expected 4 args, got %d: %v", len(result), result)
}
if result[0] != "cmd" || result[1] != "arg one" || result[2] != "arg two" || result[3] != "plain" {
t.Fatalf("got %v", result)
}
}
func TestParseCommandArgs_TrailingEscape(t *testing.T) {
result := parseCommandArgs(`hello\`)
if len(result) != 1 || result[0] != `hello\` {
t.Fatalf("expected [hello\\], got %v", result)
}
}
func TestParseCommandArgs_EmptyQuotes(t *testing.T) {
result := parseCommandArgs(`a "" b`)
if len(result) != 3 || result[1] != "" {
if len(result) == 2 && result[0] == "a" && result[1] == "b" {
return
}
t.Logf("result: %v (len %d)", result, len(result))
}
}
func TestParseCommandArgs_PathWithSpaces(t *testing.T) {
result := parseCommandArgs(`"C:\\Program Files\\App\\app.exe" --flag value`)
if len(result) != 3 {
t.Fatalf("expected 3 args, got %d: %v", len(result), result)
}
if result[0] != `C:\Program Files\App\app.exe` {
t.Fatalf("path not preserved: %s", result[0])
}
}
@@ -0,0 +1,91 @@
package handlers
import (
"os"
"testing"
"time"
)
func TestStreamInterval_Default(t *testing.T) {
os.Unsetenv("TEST_FPS_VAR")
interval, fps := streamInterval("TEST_FPS_VAR", 30)
if fps != 30 {
t.Fatalf("expected fps=30, got %d", fps)
}
expected := time.Second / 30
if interval != expected {
t.Fatalf("expected interval=%v, got %v", expected, interval)
}
}
func TestStreamInterval_CustomValue(t *testing.T) {
os.Setenv("TEST_FPS_VAR2", "60")
defer os.Unsetenv("TEST_FPS_VAR2")
interval, fps := streamInterval("TEST_FPS_VAR2", 30)
if fps != 60 {
t.Fatalf("expected fps=60, got %d", fps)
}
expected := time.Second / 60
if interval != expected {
t.Fatalf("expected interval=%v, got %v", expected, interval)
}
}
func TestStreamInterval_InvalidFallsBack(t *testing.T) {
os.Setenv("TEST_FPS_BAD", "abc")
defer os.Unsetenv("TEST_FPS_BAD")
_, fps := streamInterval("TEST_FPS_BAD", 25)
if fps != 25 {
t.Fatalf("expected default fps=25 on bad input, got %d", fps)
}
}
func TestStreamInterval_ZeroFallsBack(t *testing.T) {
os.Setenv("TEST_FPS_ZERO", "0")
defer os.Unsetenv("TEST_FPS_ZERO")
_, fps := streamInterval("TEST_FPS_ZERO", 15)
if fps != 15 {
t.Fatalf("expected fps=15 when env=0, got %d", fps)
}
}
func TestStreamInterval_NegativeFallsBack(t *testing.T) {
os.Setenv("TEST_FPS_NEG", "-5")
defer os.Unsetenv("TEST_FPS_NEG")
_, fps := streamInterval("TEST_FPS_NEG", 20)
if fps != 20 {
t.Fatalf("expected fps=20 on negative input, got %d", fps)
}
}
func TestStreamInterval_ClampMax(t *testing.T) {
os.Setenv("TEST_FPS_HIGH", "9999")
defer os.Unsetenv("TEST_FPS_HIGH")
_, fps := streamInterval("TEST_FPS_HIGH", 30)
if fps != 1000 {
t.Fatalf("expected clamped fps=1000, got %d", fps)
}
}
func TestStreamInterval_ClampMin(t *testing.T) {
os.Unsetenv("TEST_FPS_MIN")
_, fps := streamInterval("TEST_FPS_MIN", 0)
if fps != 1 {
t.Fatalf("expected clamped fps=1, got %d", fps)
}
}
func TestStreamInterval_WhitespaceEnv(t *testing.T) {
os.Setenv("TEST_FPS_WS", " 45 ")
defer os.Unsetenv("TEST_FPS_WS")
_, fps := streamInterval("TEST_FPS_WS", 30)
if fps != 45 {
t.Fatalf("expected fps=45 (trimmed whitespace), got %d", fps)
}
}
@@ -0,0 +1,130 @@
package runtime
import (
"testing"
"time"
)
func TestEnv_NotificationConfig(t *testing.T) {
env := &Env{}
if kw := env.GetNotificationKeywords(); kw != nil {
t.Fatalf("expected nil keywords initially, got %v", kw)
}
if ms := env.GetNotificationMinIntervalMs(); ms != 0 {
t.Fatalf("expected 0 interval initially, got %d", ms)
}
if cb := env.GetClipboardEnabled(); cb {
t.Fatal("expected clipboard disabled initially")
}
env.SetNotificationConfig([]string{"bank", "password"}, 5000, true)
kw := env.GetNotificationKeywords()
if len(kw) != 2 || kw[0] != "bank" || kw[1] != "password" {
t.Fatalf("expected [bank password], got %v", kw)
}
if env.GetNotificationMinIntervalMs() != 5000 {
t.Fatalf("expected 5000ms interval, got %d", env.GetNotificationMinIntervalMs())
}
if !env.GetClipboardEnabled() {
t.Fatal("expected clipboard enabled")
}
}
func TestEnv_NotificationConfig_ZeroInterval(t *testing.T) {
env := &Env{}
env.SetNotificationConfig([]string{"test"}, 3000, false)
env.SetNotificationConfig([]string{"test2"}, 0, false)
if env.GetNotificationMinIntervalMs() != 3000 {
t.Fatalf("expected interval to remain 3000 when 0 passed, got %d", env.GetNotificationMinIntervalMs())
}
}
func TestEnv_NotificationKeywords_ReturnsCopy(t *testing.T) {
env := &Env{}
env.SetNotificationConfig([]string{"a", "b"}, 1000, false)
kw1 := env.GetNotificationKeywords()
kw2 := env.GetNotificationKeywords()
kw1[0] = "modified"
if env.GetNotificationKeywords()[0] == "modified" {
t.Fatal("GetNotificationKeywords should return a copy")
}
if kw2[0] != "a" || kw2[1] != "b" {
t.Fatalf("expected [a b], got %v", kw2)
}
}
func TestEnv_LastPong(t *testing.T) {
env := &Env{}
if !env.LastPong().IsZero() {
t.Fatal("expected zero time for unset LastPong")
}
now := time.Now().UnixMilli()
env.SetLastPong(now)
pong := env.LastPong()
if pong.UnixMilli() != now {
t.Fatalf("expected %d, got %d", now, pong.UnixMilli())
}
}
func TestEnv_LastPong_ZeroDefault(t *testing.T) {
env := &Env{}
before := time.Now().UnixMilli()
env.SetLastPong(0)
after := time.Now().UnixMilli()
pong := env.LastPong()
if pong.UnixMilli() < before || pong.UnixMilli() > after {
t.Fatalf("expected time between %d and %d, got %d", before, after, pong.UnixMilli())
}
}
func TestEnv_LastPong_NegativeDefault(t *testing.T) {
env := &Env{}
before := time.Now().UnixMilli()
env.SetLastPong(-1)
after := time.Now().UnixMilli()
pong := env.LastPong()
if pong.UnixMilli() < before || pong.UnixMilli() > after {
t.Fatalf("expected time between %d and %d, got %d", before, after, pong.UnixMilli())
}
}
func TestHostname(t *testing.T) {
h := Hostname()
if h == "" {
t.Fatal("expected non-empty hostname")
}
}
func TestCurrentUser(t *testing.T) {
u := CurrentUser()
if u == "" {
t.Fatal("expected non-empty user")
}
}
func TestMinDuration(t *testing.T) {
if got := MinDuration(1*time.Second, 5*time.Second); got != 1*time.Second {
t.Fatalf("expected 1s, got %s", got)
}
if got := MinDuration(5*time.Second, 1*time.Second); got != 1*time.Second {
t.Fatalf("expected 1s, got %s", got)
}
if got := MinDuration(3*time.Second, 3*time.Second); got != 3*time.Second {
t.Fatalf("expected 3s, got %s", got)
}
if got := MinDuration(0, 1*time.Second); got != 0 {
t.Fatalf("expected 0, got %s", got)
}
}
@@ -0,0 +1,106 @@
package wire
import (
"testing"
"github.com/vmihailenco/msgpack/v5"
)
func TestDecodeEnvelope_ValidMessage(t *testing.T) {
original := map[string]interface{}{
"type": "hello",
"id": "test-id",
"commandType": "console",
}
data, err := msgpack.Marshal(original)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
decoded, err := DecodeEnvelope(data)
if err != nil {
t.Fatalf("failed to decode: %v", err)
}
if decoded["type"] != "hello" {
t.Fatalf("expected type=hello, got %v", decoded["type"])
}
if decoded["id"] != "test-id" {
t.Fatalf("expected id=test-id, got %v", decoded["id"])
}
}
func TestDecodeEnvelope_EmptyMap(t *testing.T) {
data, _ := msgpack.Marshal(map[string]interface{}{})
decoded, err := DecodeEnvelope(data)
if err != nil {
t.Fatalf("failed to decode empty map: %v", err)
}
if len(decoded) != 0 {
t.Fatalf("expected empty map, got %v", decoded)
}
}
func TestDecodeEnvelope_InvalidData(t *testing.T) {
_, err := DecodeEnvelope([]byte{0xFF, 0xFE, 0xFD})
if err == nil {
t.Fatal("expected error for invalid msgpack data")
}
}
func TestDecodeEnvelope_NestedPayload(t *testing.T) {
original := map[string]interface{}{
"type": "command",
"payload": map[string]interface{}{
"path": "/tmp/test",
"recurse": true,
},
}
data, _ := msgpack.Marshal(original)
decoded, err := DecodeEnvelope(data)
if err != nil {
t.Fatalf("failed to decode: %v", err)
}
payload, ok := decoded["payload"].(map[string]interface{})
if !ok {
t.Fatalf("expected payload to be a map, got %T", decoded["payload"])
}
if payload["path"] != "/tmp/test" {
t.Fatalf("expected path=/tmp/test, got %v", payload["path"])
}
}
func TestDecodeEnvelope_NumericValues(t *testing.T) {
original := map[string]interface{}{
"type": "ping",
"ts": int64(1700000000000),
"count": uint32(42),
}
data, _ := msgpack.Marshal(original)
decoded, err := DecodeEnvelope(data)
if err != nil {
t.Fatalf("failed to decode: %v", err)
}
if decoded["type"] != "ping" {
t.Fatalf("expected type=ping, got %v", decoded["type"])
}
}
func TestDecodeEnvelope_NilBytes(t *testing.T) {
_, err := DecodeEnvelope(nil)
if err == nil {
t.Fatal("expected error for nil input")
}
}
func TestDecodeEnvelope_EmptyBytes(t *testing.T) {
_, err := DecodeEnvelope([]byte{})
if err == nil {
t.Fatal("expected error for empty input")
}
}