package main
import (
"net/http"
"os"
"strings"
"testing"
"time"
)
// homeTestWorld: one Claude Code session waiting for the person, with
// permission prompts off and agent-written text that tries to be markup; its
// background service; and an app. Every name and title here is invented.
func homeTestWorld(t *testing.T, authority bool) {
t.Helper()
uiTestConfigDir(t)
stubProcIdentity(t, "5551")
home := fakeClaudeHome(t, 4242, "5551", `{"type":"ai-title","aiTitle":" Rename invoices","sessionId":"x"}
{"type":"permission-mode","permissionMode":"bypassPermissions","sessionId":"x"}
{"type":"last-prompt","lastPrompt":"PRIVATE PROMPT TEXT","sessionId":"x"}
`)
prevObs, prevTree, prevHome, prevAuth, prevWait := observedAgents, processTree, homeForUserFn, machineAuthorityHeld, homeWaitingFn
prevUID := uidForUserFn
t.Cleanup(func() {
observedAgents, processTree, homeForUserFn, machineAuthorityHeld, homeWaitingFn = prevObs, prevTree, prevHome, prevAuth, prevWait
uidForUserFn = prevUID
})
uidForUserFn = func(string) (int, bool) { return os.Getuid(), true }
observedAgents = func() ([]agentObs, error) {
return []agentObs{
{PID: 4242, Kind: kindAgent, Signature: "claude-code", User: "u", Cmdline: "claude", Origin: originRemote},
{PID: 4300, Kind: kindAgent, Signature: "claude-code", User: "u", Cmdline: "claude.exe daemon run --spawned"},
{PID: 5000, Kind: kindApp, Signature: "claude-code", User: "u", Cmdline: "Claude"},
}, nil
}
processTree = func() map[int]int { return nil }
homeForUserFn = func(string) string { return home }
machineAuthorityHeld = func() bool { return authority }
homeWaitingFn = func() int { return 2 }
}
func TestTheHomePageCountsSessionsNotTheServiceOrApps(t *testing.T) {
homeTestWorld(t, true)
v := collectHomeView(time.Now())
if v.Sessions != 1 || v.WaitingForYou != 1 || v.NotYours != 1 {
t.Errorf("sessions %d, waiting %d, not yours %d; want 1, 1, 1", v.Sessions, v.WaitingForYou, v.NotYours)
}
if len(v.Families) != 1 || !strings.HasPrefix(v.Families[0].Heading, "Claude Code: 1 session (1 waiting for you)") {
t.Fatalf("families %+v", v.Families)
}
rows := v.Families[0].Rows
if !rows[0].WaitingForYou || !rows[0].PermissionsOff || rows[0].PID != 4242 {
t.Errorf("the waiting session is not first, flagged: %+v", rows[0])
}
var service bool
for _, r := range rows {
service = service || (r.Service && r.PID == 4300)
}
if !service {
t.Error("the background service is not shown as one")
}
if v.PerAccount || v.Waiting != 2 {
t.Errorf("per-account %v, waiting %d", v.PerAccount, v.Waiting)
}
}
// What an agent writes about itself is text on this page, never markup, and
// its last prompt is not on the home page at all.
func TestTheHomePageShowsAgentTextAsTextAndNoPrompts(t *testing.T) {
homeTestWorld(t, true)
u := uiTestServer(t)
body := uiGet(t, u.handler(), "/", uiSignedIn(t, u)).Body.String()
if strings.Contains(body, "