Files
bilicraft-bot/tests/ConsoleCommandTests.cpp
T
2026-11-24 12:43:25 +08:00

126 lines
4.5 KiB
C++

#include "ConsoleCommand.h"
#include "ActionScheduler.h"
#include "TerminalInput.h"
#include <gtest/gtest.h>
#include <thread>
#ifndef _WIN32
#include <cstdlib>
#include <fcntl.h>
#include <unistd.h>
#endif
namespace
{
using namespace Bilicraft;
TEST(ConsoleCommand, ParsesControlCommandsAndPathsWithSpaces)
{
EXPECT_EQ(ParseConsoleCommand(" ").kind, ConsoleKind::Empty);
EXPECT_EQ(ParseConsoleCommand("# comment").kind, ConsoleKind::Empty);
EXPECT_EQ(ParseConsoleCommand("help # help").kind, ConsoleKind::Help);
EXPECT_EQ(ParseConsoleCommand("tasks").kind, ConsoleKind::Tasks);
EXPECT_EQ(ParseConsoleCommand("exit").kind, ConsoleKind::Quit);
EXPECT_EQ(ParseConsoleCommand("quit").kind, ConsoleKind::Quit);
EXPECT_EQ(ParseConsoleCommand("run ../../actions/demo.act").path, "../../actions/demo.act");
EXPECT_EQ(ParseConsoleCommand("run \"path with spaces.act\"").path, "path with spaces.act");
EXPECT_EQ(ParseConsoleCommand("run 'path with spaces.act'").path, "path with spaces.act");
}
TEST(ConsoleCommand, RejectsInvalidControlsAndInteractiveWaitOrImport)
{
for (auto text : {"run", "run \"\"", "help now", "tasks 1", "quit now", "wait 3", "import demo",
"sw 1\nsw 2", "sw 1\rsw 2"})
EXPECT_THROW(ParseConsoleCommand(text), std::exception) << text;
EXPECT_THROW(ParseConsoleCommand(std::string("sw 1\0x", 6)), std::exception);
}
TEST(ConsoleCommand, UsesTheSameGameCommandParser)
{
const auto message = ParseConsoleCommand("sendmsg @Steve hello world");
ASSERT_EQ(message.kind, ConsoleKind::Action);
ASSERT_TRUE(message.action);
EXPECT_EQ(message.action->recipient, "Steve");
EXPECT_EQ(message.action->text, "hello world");
EXPECT_EQ(ParseConsoleCommand("drop all").action->kind, ActionKind::Drop);
EXPECT_TRUE(ParseConsoleCommand("drop all").action->all);
}
#ifndef _WIN32
TEST(ConsoleCommand, NativePseudoTerminalWaitsForEnterAndParsesTheCommand)
{
const int master = posix_openpt(O_RDWR | O_NOCTTY);
ASSERT_GE(master, 0);
struct Fd
{
int value;
~Fd() { if (value >= 0) close(value); }
} masterGuard{master};
ASSERT_EQ(grantpt(master), 0);
ASSERT_EQ(unlockpt(master), 0);
const char *name = ptsname(master);
ASSERT_NE(name, nullptr);
const int slave = open(name, O_RDWR | O_NOCTTY);
ASSERT_GE(slave, 0);
Fd slaveGuard{slave};
TerminalInput terminal(slave);
ASSERT_TRUE(terminal.IsInteractive());
ASSERT_EQ(write(master, "sw 3", 4), 4);
EXPECT_FALSE(terminal.PollLine());
ASSERT_EQ(write(master, "\n", 1), 1);
// Delivery through a PTY can be asynchronous; a bounded poll, not a blocking read.
std::optional<std::string> line;
const auto deadline = ActionScheduler::Clock::now() + std::chrono::seconds(1);
while (!line && ActionScheduler::Clock::now() < deadline)
{
line = terminal.PollLine();
if (!line) std::this_thread::yield();
}
ASSERT_TRUE(line);
const auto command = ParseConsoleCommand(*line);
ASSERT_TRUE(command.action);
EXPECT_EQ(command.action->kind, ActionKind::SelectHotbar);
EXPECT_EQ(command.action->slot, 3);
}
TEST(ConsoleCommand, PipeInputDoesNotConsumeNextCommandUntilPreviousExecutionFinishes)
{
int descriptors[2];
ASSERT_EQ(pipe(descriptors), 0);
struct Close
{
int a, b;
~Close() { close(a); close(b); }
} closePipe{descriptors[0], descriptors[1]};
const std::string input = "sendmsg first\nsendmsg second\n";
ASSERT_EQ(write(descriptors[1], input.data(), input.size()), static_cast<ssize_t>(input.size()));
TerminalInput terminal(descriptors[0]);
ActionScheduler scheduler;
const auto now = ActionScheduler::Clock::time_point{};
scheduler.Enqueue({"active", {*ParseInteractiveCommand("sendmsg active")}});
auto work = scheduler.Next(now);
ASSERT_EQ(work->command.text, "active");
int linesRead = 0;
const auto pump = [&] {
if (scheduler.InteractivePending()) return;
if (auto line = terminal.PollLine())
{
++linesRead;
scheduler.SubmitInteractive(*ParseConsoleCommand(*line).action);
}
};
pump();
pump();
EXPECT_EQ(linesRead, 1);
scheduler.Finish(true);
work = scheduler.Next(now);
ASSERT_TRUE(work->interactive);
EXPECT_EQ(work->command.text, "first");
pump();
EXPECT_EQ(linesRead, 1);
scheduler.Finish(true);
pump();
EXPECT_EQ(linesRead, 2);
EXPECT_EQ(scheduler.Next(now)->command.text, "second");
}
#endif
} // namespace