Implement COMMAND_QUERY_ACTIVITY -> TOKEN_REPORT_ACTIVITY with two backends picked at startup. With a desktop, a sampler thread reads the foreground window every second and books dwell time like the Windows client: under five seconds is dropped, five idle minutes close the window, the last 500 records stay newest first, each line being "[YYYY-MM-DD HH:MM:SS] [title] duration". It uses its own ActivityChecker because an Xlib connection cannot be shared with the heartbeat thread, and it is stopped before main's locals go away. A reachable X server is not enough: the window backend is only chosen when the root window carries _NET_SUPPORTING_WM_CHECK, so Xvfb or a bare X without an EWMH window manager falls back to the headless backend instead of sampling a window id that never appears. Without a desktop the answer is computed on demand: logins from "last -n 100 -F" plus bash/zsh history of root and /home/*, tagged [登录] or [命令] after a [来源] header. Both halves are merged and sorted by timestamp; three fifths of the budget goes to logins and reboot records are capped at ten, since a WSL or container writes one reboot per start. activity_commands=0 drops the command half. A single detached thread builds and sends it, only while the main link is up. The MCP gate now admits Linux for get_activity_history and documents both shapes. test/unit/linux covers the parsers, ordering and UTF-8 truncation; 15 cases pass. Co-Authored-By: deepseek-flash
199 lines
7.8 KiB
C++
199 lines
7.8 KiB
C++
// ActivityHistoryTest.cpp - Linux 历史活动(无头后端)解析单元测试
|
||
//
|
||
// 只测纯文本解析:窗口后端要 X11 与真实桌面,只能在真机上验。
|
||
// 这些解析直接决定服务端对话框和 MCP get_activity_history 看到的内容,
|
||
// 格式一变(比如 last 的字段顺序)应当在这里先红。
|
||
|
||
#include <gtest/gtest.h>
|
||
#include "LinuxActivityHistory.h" // 名字带前缀:test 里也把 ../client 放进了头文件搜索路径
|
||
|
||
using namespace LinuxActivity;
|
||
|
||
// 与 Windows 端 client/ActivityHistory.cpp::FormatDuration 完全一致
|
||
TEST(LinuxActivity, FormatDuration)
|
||
{
|
||
EXPECT_EQ("5s", FormatDuration(5));
|
||
EXPECT_EQ("1m", FormatDuration(60));
|
||
EXPECT_EQ("1m30s", FormatDuration(90));
|
||
EXPECT_EQ("1h0m", FormatDuration(3600));
|
||
EXPECT_EQ("2h5m", FormatDuration(7500));
|
||
EXPECT_EQ("0s", FormatDuration(-3));
|
||
}
|
||
|
||
// bash 设了 HISTTIMEFORMAT 时按 "#<epoch>" + 命令成对写入
|
||
TEST(LinuxActivity, ParseBashHistoryWithTimestamps)
|
||
{
|
||
const std::string text =
|
||
"#1699560000\n"
|
||
"ls -la\n"
|
||
"#1699560060\n"
|
||
"cd /tmp\n";
|
||
|
||
auto lines = ParseShellHistoryText(text, "alice", 100);
|
||
ASSERT_EQ(2u, lines.size());
|
||
EXPECT_EQ("[2023-", lines[0].substr(0, 6));
|
||
EXPECT_NE(std::string::npos, lines[0].find("] [命令] alice: ls -la"));
|
||
EXPECT_NE(std::string::npos, lines[1].find("] [命令] alice: cd /tmp"));
|
||
// 时间戳行本身不能当成命令
|
||
for (const auto& l : lines)
|
||
EXPECT_EQ(std::string::npos, l.find("1699560000"));
|
||
}
|
||
|
||
// 没有时间戳(默认情况):整行当命令,时间标"未知"
|
||
TEST(LinuxActivity, ParseBashHistoryWithoutTimestamps)
|
||
{
|
||
auto lines = ParseShellHistoryText("echo hi\n\nvim /etc/hosts\n", "bob", 100);
|
||
ASSERT_EQ(2u, lines.size());
|
||
EXPECT_EQ("[时间未知] [命令] bob: echo hi", lines[0]);
|
||
EXPECT_EQ("[时间未知] [命令] bob: vim /etc/hosts", lines[1]);
|
||
}
|
||
|
||
// zsh 开 EXTENDED_HISTORY:": <epoch>:<elapsed>;命令"
|
||
TEST(LinuxActivity, ParseZshExtendedHistory)
|
||
{
|
||
auto lines = ParseShellHistoryText(": 1699560000:0;git status\n", "carol", 100);
|
||
ASSERT_EQ(1u, lines.size());
|
||
EXPECT_NE(std::string::npos, lines[0].find("] [命令] carol: git status"));
|
||
EXPECT_EQ("[2023-", lines[0].substr(0, 6));
|
||
}
|
||
|
||
// bash 里 ": ${PATH:=/usr/bin}; ls" 这种命令行也以 ": " 开头、也带 ';',
|
||
// 但冒号前不是纯数字,必须原样保留,不能被当成 zsh 元数据截掉
|
||
TEST(LinuxActivity, ColonCommandIsNotZshMetadata)
|
||
{
|
||
auto lines = ParseShellHistoryText(": ${PATH:=/usr/bin}; ls\n", "carol", 100);
|
||
ASSERT_EQ(1u, lines.size());
|
||
EXPECT_EQ("[时间未知] [命令] carol: : ${PATH:=/usr/bin}; ls", lines[0]);
|
||
}
|
||
|
||
// "#注释"(后面不是纯数字)不能被当成时间戳
|
||
TEST(LinuxActivity, HashCommentsAreNotTimestamps)
|
||
{
|
||
auto lines = ParseShellHistoryText("#note\nls\n", "dave", 100);
|
||
ASSERT_EQ(2u, lines.size());
|
||
EXPECT_EQ("[时间未知] [命令] dave: #note", lines[0]);
|
||
}
|
||
|
||
// 超限时丢旧的、保留最近的
|
||
TEST(LinuxActivity, KeepsOnlyTail)
|
||
{
|
||
std::string text;
|
||
for (int i = 0; i < 10; ++i)
|
||
text += "cmd" + std::to_string(i) + "\n";
|
||
|
||
auto lines = ParseShellHistoryText(text, "eve", 3);
|
||
ASSERT_EQ(3u, lines.size());
|
||
EXPECT_NE(std::string::npos, lines[0].find("cmd7"));
|
||
EXPECT_NE(std::string::npos, lines[2].find("cmd9"));
|
||
}
|
||
|
||
// last -F 的三种行:远程登录带 host、本地登录不带 host、reboot 带内核版本
|
||
TEST(LinuxActivity, ParseLastOutput)
|
||
{
|
||
const std::string text =
|
||
"root pts/0 203.0.113.7 Fri Nov 7 22:10:33 2026 - Fri Nov 7 23:00:00 2026 (00:49)\n"
|
||
"root tty1 Sat Nov 8 09:00:00 2026 still logged in\n"
|
||
"reboot system boot 6.8.0-45-generic Fri Nov 7 22:10:20 2026 - Fri Nov 7 23:11:00 2026 (01:00)\n"
|
||
"\n"
|
||
"wtmp begins Sat Nov 1 00:00:00 2026\n";
|
||
|
||
auto lines = ParseLastOutput(text, 50);
|
||
ASSERT_EQ(3u, lines.size());
|
||
EXPECT_EQ("[2026-11-07 22:10:33] [登录] root pts/0 from 203.0.113.7", lines[0]);
|
||
EXPECT_EQ("[2026-11-08 09:00:00] [登录] root tty1", lines[1]);
|
||
EXPECT_EQ("[2026-11-07 22:10:20] [登录] reboot (system boot) from 6.8.0-45-generic", lines[2]);
|
||
}
|
||
|
||
// 认不出的行走原文兜底,不能整条丢掉
|
||
TEST(LinuxActivity, ParseLastOutputFallsBack)
|
||
{
|
||
auto lines = ParseLastOutput("weird line with no date\n", 50);
|
||
ASSERT_EQ(1u, lines.size());
|
||
EXPECT_EQ("[时间未知] [登录] weird line with no date", lines[0]);
|
||
}
|
||
|
||
TEST(LinuxActivity, LineCap)
|
||
{
|
||
auto lines = ParseLastOutput(
|
||
"root tty1 Sat Nov 8 09:00:00 2026 still logged in\n"
|
||
"root tty1 Sat Nov 8 10:00:00 2026 still logged in\n",
|
||
1);
|
||
ASSERT_EQ(1u, lines.size());
|
||
EXPECT_NE(std::string::npos, lines[0].find("09:00:00"));
|
||
}
|
||
|
||
// 无头机的 wtmp 里 reboot 行会堆满几十条(WSL 每次启动一条),只留最近几条,
|
||
// 否则真正的登录和命令历史会被挤没
|
||
TEST(LinuxActivity, LimitRebootSpam)
|
||
{
|
||
std::vector<std::string> lines;
|
||
for (int i = 0; i < 20; ++i) {
|
||
char buf[32];
|
||
snprintf(buf, sizeof(buf), "%02d", i);
|
||
lines.push_back(std::string("[2026-10-27 19:") + buf +
|
||
":00] [登录] reboot (system boot) from 6.6.87.2-microso");
|
||
}
|
||
lines.push_back("[2026-11-08 09:00:00] [登录] root pts/0 from 203.0.113.7");
|
||
|
||
auto kept = LimitRebootSpam(lines, 3, 100);
|
||
ASSERT_EQ(4u, kept.size());
|
||
EXPECT_NE(std::string::npos, kept[0].find("19:00:00"));
|
||
EXPECT_NE(std::string::npos, kept[2].find("19:02:00"));
|
||
// 真正的登录不能被 reboot 挤掉
|
||
EXPECT_EQ("[2026-11-08 09:00:00] [登录] root pts/0 from 203.0.113.7", kept.back());
|
||
}
|
||
|
||
// 额度仍是硬上限
|
||
TEST(LinuxActivity, LimitRebootSpamRespectsBudget)
|
||
{
|
||
std::vector<std::string> lines;
|
||
for (int i = 0; i < 5; ++i)
|
||
lines.push_back("[t] [登录] user" + std::to_string(i));
|
||
auto kept = LimitRebootSpam(lines, 10, 2);
|
||
ASSERT_EQ(2u, kept.size());
|
||
EXPECT_EQ("[t] [登录] user0", kept[0]);
|
||
EXPECT_EQ("[t] [登录] user1", kept[1]);
|
||
}
|
||
|
||
|
||
// 登录记录和命令历史合并后要按时间戳真正排一遍:最近的命令不能排在更早的登录后面
|
||
TEST(LinuxActivity, SortNewestFirst)
|
||
{
|
||
std::vector<std::string> lines = {
|
||
"[2026-11-08 09:00:00] [登录] root pts/0 from 203.0.113.7", // 5 分钟前
|
||
"[时间未知] [命令] yuan: make",
|
||
"[2026-11-08 09:04:30] [命令] yuan: ls", // 30 秒前
|
||
"[2026-11-07 22:00:00] [登录] reboot (system boot)",
|
||
};
|
||
SortNewestFirst(lines);
|
||
EXPECT_EQ("[2026-11-08 09:04:30] [命令] yuan: ls", lines[0]);
|
||
EXPECT_EQ("[2026-11-08 09:00:00] [登录] root pts/0 from 203.0.113.7", lines[1]);
|
||
EXPECT_EQ("[2026-11-07 22:00:00] [登录] reboot (system boot)", lines[2]);
|
||
// 时间未知的排最后
|
||
EXPECT_EQ("[时间未知] [命令] yuan: make", lines[3]);
|
||
}
|
||
|
||
// 按字节截断不能切在多字节字符中间:服务端要按 UTF-8 转 GBK / 塞 JSON
|
||
TEST(LinuxActivity, CleanLineKeepsUtf8Whole)
|
||
{
|
||
// 511 个 ASCII + 4 字节 emoji = 515 字节,截到 512 会剩半个 emoji
|
||
std::string s(511, 'a');
|
||
s += "\xF0\x9F\x98\x80";
|
||
std::string out = CleanLine(s);
|
||
EXPECT_EQ(511u, out.size());
|
||
EXPECT_EQ(std::string::npos, out.find('\xF0'));
|
||
|
||
// 恰好落在边界上时不该多丢一个字符
|
||
std::string exact = std::string(508, 'a') + "\xF0\x9F\x98\x80";
|
||
EXPECT_EQ(exact.size(), CleanLine(exact).size());
|
||
}
|
||
|
||
|
||
TEST(LinuxActivity, WindowRecordFormat)
|
||
{
|
||
EXPECT_EQ("[2026-11-08 09:00:00] [vim main.cpp] 1m30s",
|
||
FormatWindowRecord("2026-11-08 09:00:00", "vim main.cpp", 90));
|
||
// 标题里的换行必须被压平,否则服务端按行切分会多出一条
|
||
EXPECT_EQ("[t] [a b] 5s", FormatWindowRecord("t", "a\nb", 5));
|
||
}
|