1#include "cli/handlers/commands.h"
6#include "absl/flags/declare.h"
7#include "absl/flags/flag.h"
13#include "absl/status/status.h"
14#include "absl/strings/str_cat.h"
17#include "nlohmann/json.hpp"
30 return ::absl::OkStatus();
34 bool use_mock = ::absl::GetFlag(FLAGS_mock_rom);
41 std::cout <<
"โ
Mock ROM initialized with embedded Zelda3 labels\n";
42 return ::absl::OkStatus();
46 std::string rom_path = ::absl::GetFlag(FLAGS_rom);
47 if (rom_path.empty()) {
48 return ::absl::InvalidArgumentError(
49 "No ROM loaded. Pass --rom=<path> or use --mock-rom for testing.");
54 return ::absl::FailedPreconditionError(
55 ::absl::StrCat(
"Failed to load ROM from '", rom_path,
56 "': ", status.message()));
59 return ::absl::OkStatus();
67 bool expect_tool_calls =
false;
68 bool expect_commands =
false;
74 .name =
"embedded_labels_room_query",
75 .description =
"Ask about room names using embedded labels",
76 .user_prompts = {
"What is the name of room 5?"},
77 .expected_keywords = {
"room",
"Tower of Hera",
"Moldorm"},
78 .expect_tool_calls =
false,
79 .expect_commands =
false,
82 .name =
"embedded_labels_sprite_query",
83 .description =
"Ask about sprite names using embedded labels",
84 .user_prompts = {
"What is sprite 9?"},
85 .expected_keywords = {
"sprite",
"Moldorm",
"Boss"},
86 .expect_tool_calls =
false,
87 .expect_commands =
false,
90 .name =
"embedded_labels_entrance_query",
91 .description =
"Ask about entrance names using embedded labels",
92 .user_prompts = {
"What is entrance 0?"},
93 .expected_keywords = {
"entrance",
"Link",
"House"},
94 .expect_tool_calls =
false,
95 .expect_commands =
false,
98 .name =
"simple_question",
99 .description =
"Ask about dungeons in the ROM",
100 .user_prompts = {
"What dungeons are in this ROM?"},
101 .expected_keywords = {
"dungeon",
"palace",
"castle"},
102 .expect_tool_calls =
true,
103 .expect_commands =
false,
106 .name =
"list_all_rooms",
107 .description =
"List all room names with embedded labels",
108 .user_prompts = {
"List the first 10 dungeon rooms"},
109 .expected_keywords = {
"room",
"Ganon",
"Hyrule",
"Palace"},
110 .expect_tool_calls =
true,
111 .expect_commands =
false,
114 .name =
"overworld_tile_search",
115 .description =
"Find specific tiles in overworld",
116 .user_prompts = {
"Find all trees on the overworld"},
117 .expected_keywords = {
"tree",
"tile",
"map"},
118 .expect_tool_calls =
true,
119 .expect_commands =
false,
122 .name =
"multi_step_query",
123 .description =
"Ask multiple questions in sequence",
125 "What is the name of room 0?",
126 "What sprites are defined in the game?",
128 .expected_keywords = {
"Ganon",
"sprite",
"room"},
129 .expect_tool_calls =
true,
130 .expect_commands =
false,
133 .name =
"map_description",
134 .description =
"Get information about a specific map",
135 .user_prompts = {
"Describe overworld map 0"},
136 .expected_keywords = {
"map",
"light world",
"tile"},
137 .expect_tool_calls =
true,
138 .expect_commands =
false,
144 std::cout <<
"\n===========================================\n";
145 std::cout <<
"Test: " << test_case.
name <<
"\n";
146 std::cout <<
"Description: " << test_case.
description <<
"\n";
147 std::cout <<
"===========================================\n\n";
151 std::cout <<
"๐ค User: " << prompt <<
"\n\n";
155 std::cout <<
"๐ค Agent: " << response.
message <<
"\n\n";
158 std::cout <<
"๐งพ JSON Output:\n" << *response.
json_pretty <<
"\n\n";
162 std::cout <<
"๐ Table Output:\n";
163 const auto& table = response.
table_data.value();
167 for (
size_t i = 0; i < table.headers.size(); ++i) {
168 std::cout << table.headers[i];
169 if (i < table.headers.size() - 1) {
174 for (
size_t i = 0; i < table.headers.size(); ++i) {
175 std::cout << std::string(table.headers[i].length(),
'-');
176 if (i < table.headers.size() - 1) {
183 const size_t max_rows = std::min<size_t>(10, table.rows.size());
184 for (
size_t i = 0; i < max_rows; ++i) {
186 for (
size_t j = 0; j < table.rows[i].size(); ++j) {
187 std::cout << table.rows[i][j];
188 if (j < table.rows[i].size() - 1) {
195 if (!verbose && table.rows.size() > max_rows) {
196 std::cout <<
" ... (" << (table.rows.size() - max_rows)
200 if (verbose && table.rows.size() > max_rows) {
201 for (
size_t i = max_rows; i < table.rows.size(); ++i) {
203 for (
size_t j = 0; j < table.rows[i].size(); ++j) {
204 std::cout << table.rows[i][j];
205 if (j < table.rows[i].size() - 1) {
222 if (response.
message.find(keyword) == std::string::npos) {
223 std::cout <<
"โ ๏ธ Warning: Expected keyword '" << keyword
224 <<
"' not found in response\n";
231 std::cout <<
"โ ๏ธ Warning: Expected tool calls but no table data found\n";
236 bool has_commands = response.
message.find(
"overworld") != std::string::npos ||
237 response.
message.find(
"dungeon") != std::string::npos ||
238 response.
message.find(
"set-tile") != std::string::npos;
240 std::cout <<
"โ ๏ธ Warning: Expected commands but none found\n";
252 bool all_passed =
true;
260 if (!response_or.ok()) {
261 std::cout <<
"โ FAILED: " << response_or.status().message() <<
"\n\n";
266 const auto& response = response_or.value();
276 std::cout <<
"๐ Conversation Summary (" << history.size()
277 <<
" message" << (history.size() == 1 ?
"" :
"s") <<
")\n";
278 for (
const auto& message : history) {
281 std::cout <<
" [" << sender <<
"] " << message.message <<
"\n";
287 std::cout <<
"โ
Test PASSED: " << test_case.
name <<
"\n";
288 return absl::OkStatus();
291 std::cout <<
"โ ๏ธ Test completed with warnings: " << test_case.
name <<
"\n";
292 return absl::InternalError(
293 absl::StrCat(
"Conversation test failed validation: ", test_case.
name));
297 std::vector<ConversationTestCase>* test_cases) {
298 std::ifstream file(file_path);
299 if (!file.is_open()) {
300 return absl::NotFoundError(
301 absl::StrCat(
"Could not open test file: ", file_path));
304 nlohmann::json test_json;
307 }
catch (
const nlohmann::json::parse_error& e) {
308 return absl::InvalidArgumentError(
309 absl::StrCat(
"Failed to parse test file: ", e.what()));
312 if (!test_json.is_array()) {
313 return absl::InvalidArgumentError(
314 "Test file must contain a JSON array of test cases");
317 for (
const auto& test_obj : test_json) {
319 test_case.
name = test_obj.value(
"name",
"unnamed_test");
320 test_case.
description = test_obj.value(
"description",
"");
322 if (test_obj.contains(
"prompts") && test_obj[
"prompts"].is_array()) {
323 for (
const auto& prompt : test_obj[
"prompts"]) {
324 if (prompt.is_string()) {
325 test_case.
user_prompts.push_back(prompt.get<std::string>());
330 if (test_obj.contains(
"expected_keywords") &&
331 test_obj[
"expected_keywords"].is_array()) {
332 for (
const auto& keyword : test_obj[
"expected_keywords"]) {
333 if (keyword.is_string()) {
342 test_cases->push_back(test_case);
345 return absl::OkStatus();
351 const std::vector<std::string>& arg_vec) {
352 std::string test_file;
353 bool use_defaults =
true;
354 bool verbose =
false;
356 for (
size_t i = 0; i < arg_vec.size(); ++i) {
357 const std::string& arg = arg_vec[i];
358 if (arg ==
"--file" && i + 1 < arg_vec.size()) {
359 test_file = arg_vec[i + 1];
360 use_defaults =
false;
362 }
else if (arg ==
"--verbose") {
367 std::cout <<
"๐ Debug: Starting test-conversation handler...\n";
371 std::cout <<
"๐ Debug: Loading ROM...\n";
372 auto load_status = LoadRomForAgent(rom);
373 if (!load_status.ok()) {
374 std::cerr <<
"โ Error loading ROM: " << load_status.message() <<
"\n";
378 std::cout <<
"โ
ROM loaded: " << rom.
title() <<
"\n";
381 std::cout <<
"๐ Debug: Initializing embedded labels...\n";
384 if (!labels_status.ok()) {
385 std::cerr <<
"โ ๏ธ Warning: Could not initialize embedded labels: "
386 << labels_status.message() <<
"\n";
388 std::cout <<
"โ
Embedded labels initialized successfully\n";
392 std::cout <<
"๐ Debug: Checking resource label manager...\n";
394 std::cout <<
"๐ Debug: Associating labels with ROM...\n";
397 std::cout <<
"โ
Embedded labels loaded and associated with ROM\n";
399 std::cout <<
"โ ๏ธ ROM has no resource label manager\n";
403 std::cout <<
"๐ Debug: Creating conversational agent service...\n";
404 std::cout <<
"๐ Debug: About to construct service object...\n";
407 std::cout <<
"โ
Service object created\n";
409 std::cout <<
"๐ Debug: Setting ROM context...\n";
411 std::cout <<
"โ
Service initialized\n";
414 std::vector<ConversationTestCase> test_cases;
416 test_cases = GetDefaultTestCases();
417 std::cout <<
"Using default test cases (" << test_cases.size() <<
" tests)\n";
419 auto status = LoadTestCasesFromFile(test_file, &test_cases);
423 std::cout <<
"Loaded " << test_cases.size() <<
" test cases from "
424 << test_file <<
"\n";
427 if (test_cases.empty()) {
428 return absl::InvalidArgumentError(
"No test cases to run");
435 for (
const auto& test_case : test_cases) {
436 auto status = RunTestCase(test_case, service, verbose);
441 std::cerr <<
"Test case '" << test_case.name <<
"' failed: "
442 << status.message() <<
"\n";
447 std::cout <<
"\n===========================================\n";
448 std::cout <<
"Test Summary\n";
449 std::cout <<
"===========================================\n";
450 std::cout <<
"Total tests: " << test_cases.size() <<
"\n";
451 std::cout <<
"Passed: " << passed <<
"\n";
452 std::cout <<
"Failed: " << failed <<
"\n";
455 std::cout <<
"\nโ
All tests passed!\n";
457 std::cout <<
"\nโ ๏ธ Some tests failed\n";
461 return absl::OkStatus();
464 return absl::InternalError(
465 absl::StrCat(failed,
" conversation test(s) reported failures"));
The Rom class is used to load, save, and modify Rom data.
absl::Status LoadFromFile(const std::string &filename, bool z3_load=true)
core::ResourceLabelManager * resource_label()
absl::StatusOr< ChatMessage > SendMessage(const std::string &message)
void SetRomContext(Rom *rom)
const std::vector< ChatMessage > & GetHistory() const
ABSL_DECLARE_FLAG(std::string, rom)
bool ValidateResponse(const ChatMessage &response, const ConversationTestCase &test_case)
void PrintTestHeader(const ConversationTestCase &test_case)
void PrintUserPrompt(const std::string &prompt)
absl::Status LoadTestCasesFromFile(const std::string &file_path, std::vector< ConversationTestCase > *test_cases)
void PrintAgentResponse(const ChatMessage &response, bool verbose)
std::vector< ConversationTestCase > GetDefaultTestCases()
absl::Status RunTestCase(const ConversationTestCase &test_case, ConversationalAgentService &service, bool verbose)
absl::Status LoadRomForAgent(Rom &rom)
absl::Status HandleTestConversationCommand(const std::vector< std::string > &args)
absl::Status InitializeMockRom(Rom &rom)
Initialize a mock ROM for testing without requiring an actual ROM file.
Main namespace for the application.
std::optional< TableData > table_data
std::optional< std::string > json_pretty
std::vector< std::string > expected_keywords
std::vector< std::string > user_prompts
std::unordered_map< std::string, std::unordered_map< std::string, std::string > > labels_
Modern project structure with comprehensive settings consolidation.
std::unordered_map< std::string, std::unordered_map< std::string, std::string > > resource_labels
absl::Status InitializeEmbeddedLabels()