package main import ( "testing" "time" ) // The idle reapers must treat remote output as activity, not just keystrokes: // a tail -f or long build that only scrolls used to be killed mid-stream by // the 20-minute idle timeout. The detach grace rule must keep working exactly // as before — fresh activity never rescues a session nobody is watching. func TestCleanupIdleTerminalSessionsCountsOutputAsActivity(t *testing.T) { stale := time.Now().Add(-terminalIdleTimeout - time.Minute) outputActive := &terminalSession{ owner: "idle-output@example.com", id: "idle-output-active", done: make(chan struct{}), lastActive: stale, // stale on purpose: only addOutput can rescue it } outputActive.addOutput("build output\r\n") trulyIdle := &terminalSession{ owner: "idle-quiet@example.com", id: "idle-quiet-stale", done: make(chan struct{}), lastActive: stale, // no input, no output, never detached (zero lastDetach) } detached := &terminalSession{ owner: "idle-detached@example.com", id: "idle-detached-grace", done: make(chan struct{}), lastActive: time.Now(), // fresh activity, but no viewer and grace elapsed lastDetach: time.Now().Add(-terminalResumeGrace - time.Minute), } terminalSessions.Lock() terminalSessions.items[terminalSessionKey(outputActive.owner, outputActive.id)] = outputActive terminalSessions.items[terminalSessionKey(trulyIdle.owner, trulyIdle.id)] = trulyIdle terminalSessions.items[terminalSessionKey(detached.owner, detached.id)] = detached terminalSessions.Unlock() t.Cleanup(func() { terminalSessions.Lock() for _, ts := range []*terminalSession{outputActive, trulyIdle, detached} { delete(terminalSessions.items, terminalSessionKey(ts.owner, ts.id)) } terminalSessions.Unlock() }) outputActive.mu.Lock() refreshed := time.Since(outputActive.lastActive) < terminalIdleTimeout outputActive.mu.Unlock() if !refreshed { t.Fatal("addOutput must refresh the session activity timestamp") } cleanupIdleTerminalSessions() select { case <-outputActive.done: t.Fatal("session with fresh output activity must not be reaped as idle") default: } select { case <-trulyIdle.done: default: t.Fatal("session with no input and no output after the timeout must be reaped") } select { case <-detached.done: default: t.Fatal("unwatched session past the detach grace must still be reaped despite fresh activity") } }