strawberry/app/tests/test_tray_state.cpp
trvon 76ec40e4f5
Replace web UI with a Qt tray app
Remove the browser UI, its Python server and the web API contract test.
Add the Qt6 tray app (status, findings, process search, policy
controls, notifications) and EvidenceStore, the read-only view of the
daemon's evidence that the app and MCP server share. The GUI demo
config becomes strawberry.demo.conf.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:17:03 -06:00

145 lines
4.8 KiB
C++

#include "../tray_state.hpp"
#include <cassert>
#include <iostream>
using namespace strawberry::app;
namespace {
DaemonSnapshot healthy() {
DaemonSnapshot s;
s.reachable = true;
s.monitor_enabled = true;
s.backend = "bpf";
s.execveat = true;
s.rules_loaded = true;
return s;
}
void test_levels() {
std::cout << "Test tray levels:" << std::endl;
const std::vector<FindingSummary> none;
DaemonSnapshot down;
down.unreachable_reason = "scanner is not running";
auto state = evaluate_tray(down, none, {}, 0);
assert(state.level == TrayLevel::unknown);
assert(state.tooltip.find("not running") != std::string::npos);
state = evaluate_tray(healthy(), none, {}, 0);
assert(state.level == TrayLevel::ok);
assert(state.problems.empty());
auto degraded = healthy();
degraded.backend = "procfs";
state = evaluate_tray(degraded, none, {}, 0);
assert(state.level == TrayLevel::warning);
assert(state.problems.size() == 1);
degraded = healthy();
degraded.execveat = false;
assert(evaluate_tray(degraded, none, {}, 0).level == TrayLevel::warning);
degraded = healthy();
degraded.rules_error = "syntax error";
assert(evaluate_tray(degraded, none, {}, 0).level == TrayLevel::warning);
// Scan errors only warn while they are increasing.
degraded = healthy();
degraded.scan_errors = 5;
assert(evaluate_tray(degraded, none, {}, 4).level == TrayLevel::warning);
assert(evaluate_tray(degraded, none, {}, 5).level == TrayLevel::ok);
std::cout << " PASSED" << std::endl;
}
void test_feed_health() {
std::cout << "Test rule feed health:" << std::endl;
auto s = healthy();
s.enabled_feeds = {"core"};
s.now_ns = 30LL * 24 * 3600 * 1'000'000'000LL;
s.newest_feed_update_ns = s.now_ns - 1'000'000'000LL; // updated a second ago
assert(evaluate_tray(s, {}, {}, 0).level == TrayLevel::ok);
auto stale = s;
stale.newest_feed_update_ns = s.now_ns - 20LL * 24 * 3600 * 1'000'000'000LL;
auto state = evaluate_tray(stale, {}, {}, 0);
assert(state.level == TrayLevel::warning);
assert(state.problems.front().find("20 days") != std::string::npos);
auto failing = s;
failing.feed_errors = {"core: GitHub API rate limit reached"};
assert(evaluate_tray(failing, {}, {}, 0).level == TrayLevel::warning);
auto missing = s;
missing.feeds_not_installed = {"core"};
assert(evaluate_tray(missing, {}, {}, 0).problems.front().find("not been downloaded") != std::string::npos);
auto dropped = s;
dropped.events_dropped = 3;
assert(evaluate_tray(dropped, {}, {}, 0).level == TrayLevel::warning);
std::cout << " PASSED" << std::endl;
}
void test_findings_drive_level_and_seen_state() {
std::cout << "Test findings and seen state:" << std::endl;
std::vector<FindingSummary> findings{
{"f-high", "memfd exec", "high", 100},
{"f-low", "deleted exe", "low", 50},
};
auto state = evaluate_tray(healthy(), findings, {}, 0);
assert(state.level == TrayLevel::alert);
assert(state.unseen_alerts == 1 && state.unseen_other == 1);
AckMap seen{{"f-high", 100}};
state = evaluate_tray(healthy(), findings, seen, 0);
assert(state.level == TrayLevel::warning); // low finding still unseen
seen["f-low"] = 50;
assert(evaluate_tray(healthy(), findings, seen, 0).level == TrayLevel::ok);
// New evidence on an acknowledged finding makes it unseen again.
findings[0].last_seen = 200;
assert(evaluate_tray(healthy(), findings, seen, 0).level == TrayLevel::alert);
// Stored alerts still show red when the daemon is down.
DaemonSnapshot down;
assert(evaluate_tray(down, findings, {}, 0).level == TrayLevel::alert);
std::cout << " PASSED" << std::endl;
}
void test_notifications() {
std::cout << "Test notification selection:" << std::endl;
std::vector<FindingSummary> findings{
{"crit", "a", "critical", 10},
{"high", "b", "high", 10},
{"med", "c", "medium", 10},
};
auto pending = findings_to_notify(findings, {}, {});
assert(pending.size() == 2); // medium never notifies
AckMap notified{{"crit", 10}, {"high", 10}};
assert(findings_to_notify(findings, {}, notified).empty());
findings[1].last_seen = 20; // escalation / new evidence
pending = findings_to_notify(findings, {}, notified);
assert(pending.size() == 1 && pending[0].id == "high");
AckMap seen{{"high", 20}};
assert(findings_to_notify(findings, seen, notified).empty());
std::cout << " PASSED" << std::endl;
}
} // namespace
int main() {
std::cout << "Running tray state tests..." << std::endl << std::endl;
test_levels();
test_findings_drive_level_and_seen_state();
test_feed_health();
test_notifications();
std::cout << std::endl << "All tests passed!" << std::endl;
return 0;
}