package server import "testing" func TestTerminalViewersActiveClient(t *testing.T) { var v terminalViewers // The first client to resize is active and sizes the terminal. if got := v.resize("a", 156, 37, false); got != (terminalSize{156, 37, true}) { t.Fatalf("first resize = %+v", got) } v.attach("b") // Another client's size is kept but not applied; its answers are dropped. if got := v.resize("b", 121, 11, false); got.ok { t.Fatalf("background resize applied: %+v", got) } if !v.answers("a") || v.answers("b") || v.answers("stranger") { t.Fatal("only the active client answers") } // Typing hands the terminal to b, at b's size; typing again changes nothing. if got := v.typed("b"); got != (terminalSize{121, 11, true}) { t.Fatalf("typed = %+v", got) } if got := v.typed("b"); got.ok || v.active() != "b" || v.answers("a") { t.Fatalf("typed again = %+v, active %q", got, v.active()) } // Focusing (activate) hands it back at a's new size. if got := v.resize("a", 150, 40, true); got != (terminalSize{150, 40, true}) || v.active() != "a" { t.Fatalf("activate = %+v", got) } // A background client leaving changes nothing; the active one leaving // hands the terminal to the next most recent, at its last size. v.attach("c") if got := v.leave("c"); got.ok { t.Fatalf("background leave = %+v", got) } if got := v.leave("a"); got != (terminalSize{121, 11, true}) || v.active() != "b" || !v.answers("b") { t.Fatalf("leave = %+v, active %q", got, v.active()) } if got := v.leave("b"); got.ok || v.active() != "" || !v.answers("anyone") { t.Fatalf("last leave = %+v", got) } } func TestTerminalViewersWithoutSizes(t *testing.T) { var v terminalViewers v.attach("reader") // The active client has drawn nothing yet: another's size is applied. if got := v.resize("other", 90, 20, false); !got.ok || v.active() != "reader" { t.Fatalf("resize = %+v, active %q", got, v.active()) } // Typing from a client of unknown size makes it active without a resize. if got := v.typed("typist"); got.ok || v.active() != "typist" { t.Fatalf("typed = %+v", got) } // A connection that is not told apart (tests, internal callers) always // applies and answers. if got := v.resize("", 10, 5, false); !got.ok || !v.answers("") { t.Fatalf("anonymous = %+v", got) } }