|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/avivsinai/agent-message-queue/internal/config" |
| 11 | + "github.com/avivsinai/agent-message-queue/internal/fsq" |
| 12 | + "github.com/avivsinai/agent-message-queue/internal/presence" |
| 13 | +) |
| 14 | + |
| 15 | +func TestRunWhoDistinguishesNotifierLiveFromRecentActivity(t *testing.T) { |
| 16 | + root := setupPresenceSourceFixture(t) |
| 17 | + |
| 18 | + output, err := captureEnvStdout(t, func() error { |
| 19 | + return runWho([]string{"--root", root, "--json"}) |
| 20 | + }) |
| 21 | + if err != nil { |
| 22 | + t.Fatalf("runWho json: %v", err) |
| 23 | + } |
| 24 | + var sessions []struct { |
| 25 | + Agents []struct { |
| 26 | + Handle string `json:"handle"` |
| 27 | + Active bool `json:"active"` |
| 28 | + PresenceSource string `json:"presence_source"` |
| 29 | + } `json:"agents"` |
| 30 | + } |
| 31 | + if err := json.Unmarshal([]byte(output), &sessions); err != nil { |
| 32 | + t.Fatalf("unmarshal who json: %v\n%s", err, output) |
| 33 | + } |
| 34 | + if len(sessions) != 1 { |
| 35 | + t.Fatalf("sessions=%#v, want one", sessions) |
| 36 | + } |
| 37 | + agents := make(map[string]struct { |
| 38 | + Active bool |
| 39 | + Source string |
| 40 | + }) |
| 41 | + for _, agent := range sessions[0].Agents { |
| 42 | + agents[agent.Handle] = struct { |
| 43 | + Active bool |
| 44 | + Source string |
| 45 | + }{Active: agent.Active, Source: agent.PresenceSource} |
| 46 | + } |
| 47 | + if got := agents["notifier"]; !got.Active || got.Source != "notifier_live" { |
| 48 | + t.Fatalf("notifier=%#v, want active notifier_live", got) |
| 49 | + } |
| 50 | + if got := agents["recent"]; !got.Active || got.Source != "recent_activity" { |
| 51 | + t.Fatalf("recent=%#v, want active recent_activity", got) |
| 52 | + } |
| 53 | + if got := agents["unverified"]; !got.Active || got.Source != "recent_activity" { |
| 54 | + t.Fatalf("unverified=%#v, want recent_activity without notifier claim", got) |
| 55 | + } |
| 56 | + if got := agents["stale"]; got.Active || got.Source != "" { |
| 57 | + t.Fatalf("stale=%#v, want inactive without source", got) |
| 58 | + } |
| 59 | + |
| 60 | + text, err := captureEnvStdout(t, func() error { |
| 61 | + return runWho([]string{"--root", root}) |
| 62 | + }) |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("runWho text: %v", err) |
| 65 | + } |
| 66 | + for _, want := range []string{ |
| 67 | + "notifier active (notifier_live)", |
| 68 | + "recent active (recent_activity)", |
| 69 | + "unverified active (recent_activity)", |
| 70 | + } { |
| 71 | + if !strings.Contains(text, want) { |
| 72 | + t.Fatalf("who text missing %q:\n%s", want, text) |
| 73 | + } |
| 74 | + } |
| 75 | + for _, forbidden := range []string{"consumer_live", "consumption"} { |
| 76 | + if strings.Contains(text, forbidden) { |
| 77 | + t.Fatalf("who text must not claim %q:\n%s", forbidden, text) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestDoctorOpsDistinguishesNotifierLiveFromRecentActivity(t *testing.T) { |
| 83 | + root := setupPresenceSourceFixture(t) |
| 84 | + result := runOpsChecks(root, "test", false) |
| 85 | + |
| 86 | + agents := make(map[string]opsAgent) |
| 87 | + for _, agent := range result.Agents { |
| 88 | + agents[agent.Handle] = agent |
| 89 | + } |
| 90 | + if got := agents["notifier"].PresenceSource; got != "notifier_live" { |
| 91 | + t.Fatalf("notifier presence_source=%q, want notifier_live", got) |
| 92 | + } |
| 93 | + if got := agents["recent"].PresenceSource; got != "recent_activity" { |
| 94 | + t.Fatalf("recent presence_source=%q, want recent_activity", got) |
| 95 | + } |
| 96 | + if got := agents["unverified"].PresenceSource; got != "recent_activity" { |
| 97 | + t.Fatalf("unverified presence_source=%q, want recent_activity", got) |
| 98 | + } |
| 99 | + if got := agents["stale"].PresenceSource; got != "" { |
| 100 | + t.Fatalf("stale presence_source=%q, want empty", got) |
| 101 | + } |
| 102 | + |
| 103 | + data, err := json.Marshal(result) |
| 104 | + if err != nil { |
| 105 | + t.Fatalf("marshal ops result: %v", err) |
| 106 | + } |
| 107 | + if !strings.Contains(string(data), `"presence_source":"notifier_live"`) || |
| 108 | + !strings.Contains(string(data), `"presence_source":"recent_activity"`) { |
| 109 | + t.Fatalf("doctor JSON missing presence sources: %s", data) |
| 110 | + } |
| 111 | + |
| 112 | + t.Setenv("AM_ROOT", root) |
| 113 | + t.Setenv("AM_BASE_ROOT", "") |
| 114 | + t.Setenv("AM_SESSION", "") |
| 115 | + text, err := captureEnvStdout(t, func() error { |
| 116 | + return runDoctor([]string{"--ops"}) |
| 117 | + }) |
| 118 | + if err != nil { |
| 119 | + t.Fatalf("runDoctor text: %v", err) |
| 120 | + } |
| 121 | + for agent, source := range map[string]string{ |
| 122 | + "notifier": "notifier_live", |
| 123 | + "recent": "recent_activity", |
| 124 | + } { |
| 125 | + line := lineWithPrefix(text, " "+agent+":") |
| 126 | + if !strings.Contains(line, ", source "+source) { |
| 127 | + t.Fatalf("doctor line for %s missing source %q: %q\n%s", agent, source, line, text) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func lineWithPrefix(text, prefix string) string { |
| 133 | + for _, line := range strings.Split(text, "\n") { |
| 134 | + if strings.HasPrefix(line, prefix) { |
| 135 | + return line |
| 136 | + } |
| 137 | + } |
| 138 | + return "" |
| 139 | +} |
| 140 | + |
| 141 | +func setupPresenceSourceFixture(t *testing.T) string { |
| 142 | + t.Helper() |
| 143 | + root := filepath.Join(secureTempDirForTest(t), "collab") |
| 144 | + agents := []string{"notifier", "recent", "stale", "unverified"} |
| 145 | + for _, agent := range agents { |
| 146 | + if err := fsq.EnsureAgentDirs(root, agent); err != nil { |
| 147 | + t.Fatalf("EnsureAgentDirs(%s): %v", agent, err) |
| 148 | + } |
| 149 | + } |
| 150 | + if err := config.WriteConfig(filepath.Join(root, "meta", "config.json"), config.Config{ |
| 151 | + Version: 1, |
| 152 | + Agents: agents, |
| 153 | + }, true); err != nil { |
| 154 | + t.Fatalf("write config: %v", err) |
| 155 | + } |
| 156 | + |
| 157 | + now := time.Now() |
| 158 | + for _, item := range []struct { |
| 159 | + agent string |
| 160 | + seenAt time.Time |
| 161 | + }{ |
| 162 | + {agent: "notifier", seenAt: now.Add(-time.Hour)}, |
| 163 | + {agent: "recent", seenAt: now}, |
| 164 | + {agent: "stale", seenAt: now.Add(-time.Hour)}, |
| 165 | + {agent: "unverified", seenAt: now}, |
| 166 | + } { |
| 167 | + if err := presence.Write(root, presence.New(item.agent, "active", "", item.seenAt)); err != nil { |
| 168 | + t.Fatalf("write %s presence: %v", item.agent, err) |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + const pid = 4242 |
| 173 | + const processStart = "verified-start" |
| 174 | + const bootID = "verified-boot" |
| 175 | + args := []string{"amq", "wake", "--root", root, "--me", "notifier"} |
| 176 | + writeWakeLockForTest(t, root, "notifier", wakeLock{ |
| 177 | + PID: pid, |
| 178 | + ProcessStart: processStart, |
| 179 | + BootID: bootID, |
| 180 | + Executable: "amq", |
| 181 | + Args: args, |
| 182 | + }) |
| 183 | + const unverifiedPID = 4343 |
| 184 | + writeWakeLockForTest(t, root, "unverified", wakeLock{ |
| 185 | + PID: unverifiedPID, |
| 186 | + Executable: "amq", |
| 187 | + }) |
| 188 | + stubInspectWakeProcess(t, func(gotPID int) wakeProcessInfo { |
| 189 | + if gotPID == unverifiedPID { |
| 190 | + return wakeProcessInfo{ |
| 191 | + PID: gotPID, |
| 192 | + Running: true, |
| 193 | + Executable: "/usr/local/bin/amq", |
| 194 | + } |
| 195 | + } |
| 196 | + if gotPID != pid { |
| 197 | + return wakeProcessInfo{PID: gotPID} |
| 198 | + } |
| 199 | + return wakeProcessInfo{ |
| 200 | + PID: gotPID, |
| 201 | + Running: true, |
| 202 | + StartToken: processStart, |
| 203 | + BootID: bootID, |
| 204 | + Executable: "/usr/local/bin/amq", |
| 205 | + Args: args, |
| 206 | + } |
| 207 | + }) |
| 208 | + return root |
| 209 | +} |
0 commit comments