5#include "absl/flags/declare.h"
6#include "absl/flags/flag.h"
12#include "absl/status/status.h"
13#include "absl/strings/str_cat.h"
16#include "nlohmann/json.hpp"
29 return ::absl::OkStatus();
33 bool use_mock = ::absl::GetFlag(FLAGS_mock_rom);
40 std::cout <<
"โ
Mock ROM initialized with embedded Zelda3 labels\n";
41 return ::absl::OkStatus();
45 std::string rom_path = ::absl::GetFlag(FLAGS_rom);
46 if (rom_path.empty()) {
47 return ::absl::InvalidArgumentError(
48 "No ROM loaded. Pass --rom=<path> or use --mock-rom for testing.");
53 return ::absl::FailedPreconditionError(
54 ::absl::StrCat(
"Failed to load ROM from '", rom_path,
55 "': ", status.message()));
58 return ::absl::OkStatus();
66 bool expect_tool_calls =
false;
67 bool expect_commands =
false;
73 .name =
"embedded_labels_room_query",
74 .description =
"Ask about room names using embedded labels",
75 .user_prompts = {
"What is the name of room 5?"},
76 .expected_keywords = {
"room",
"Tower of Hera",
"Moldorm"},
77 .expect_tool_calls =
false,
78 .expect_commands =
false,
81 .name =
"embedded_labels_sprite_query",
82 .description =
"Ask about sprite names using embedded labels",
83 .user_prompts = {
"What is sprite 9?"},
84 .expected_keywords = {
"sprite",
"Moldorm",
"Boss"},
85 .expect_tool_calls =
false,
86 .expect_commands =
false,
89 .name =
"embedded_labels_entrance_query",
90 .description =
"Ask about entrance names using embedded labels",
91 .user_prompts = {
"What is entrance 0?"},
92 .expected_keywords = {
"entrance",
"Link",
"House"},
93 .expect_tool_calls =
false,
94 .expect_commands =
false,
97 .name =
"simple_question",
98 .description =
"Ask about dungeons in the ROM",
99 .user_prompts = {
"What dungeons are in this ROM?"},
100 .expected_keywords = {
"dungeon",
"palace",
"castle"},
101 .expect_tool_calls =
true,
102 .expect_commands =
false,
105 .name =
"list_all_rooms",
106 .description =
"List all room names with embedded labels",
107 .user_prompts = {
"List the first 10 dungeon rooms"},
108 .expected_keywords = {
"room",
"Ganon",
"Hyrule",
"Palace"},
109 .expect_tool_calls =
true,
110 .expect_commands =
false,
113 .name =
"overworld_tile_search",
114 .description =
"Find specific tiles in overworld",
115 .user_prompts = {
"Find all trees on the overworld"},
116 .expected_keywords = {
"tree",
"tile",
"map"},
117 .expect_tool_calls =
true,
118 .expect_commands =
false,
121 .name =
"multi_step_query",
122 .description =
"Ask multiple questions in sequence",
124 "What is the name of room 0?",
125 "What sprites are defined in the game?",
127 .expected_keywords = {
"Ganon",
"sprite",
"room"},
128 .expect_tool_calls =
true,
129 .expect_commands =
false,
132 .name =
"map_description",
133 .description =
"Get information about a specific map",
134 .user_prompts = {
"Describe overworld map 0"},
135 .expected_keywords = {
"map",
"light world",
"tile"},
136 .expect_tool_calls =
true,
137 .expect_commands =
false,
143 std::cout <<
"\n===========================================\n";
144 std::cout <<
"Test: " << test_case.
name <<
"\n";
145 std::cout <<
"Description: " << test_case.
description <<
"\n";
146 std::cout <<
"===========================================\n\n";
150 std::cout <<
"๐ค User: " << prompt <<
"\n\n";
154 std::cout <<
"๐ค Agent: " << response.
message <<
"\n\n";
157 std::cout <<
"๐งพ JSON Output:\n" << *response.
json_pretty <<
"\n\n";
161 std::cout <<
"๐ Table Output:\n";
162 const auto& table = response.
table_data.value();
166 for (
size_t i = 0; i < table.headers.size(); ++i) {
167 std::cout << table.headers[i];
168 if (i < table.headers.size() - 1) {
173 for (
size_t i = 0; i < table.headers.size(); ++i) {
174 std::cout << std::string(table.headers[i].length(),
'-');
175 if (i < table.headers.size() - 1) {
182 const size_t max_rows = std::min<size_t>(10, table.rows.size());
183 for (
size_t i = 0; i < max_rows; ++i) {
185 for (
size_t j = 0; j < table.rows[i].size(); ++j) {
186 std::cout << table.rows[i][j];
187 if (j < table.rows[i].size() - 1) {
194 if (!verbose && table.rows.size() > max_rows) {
195 std::cout <<
" ... (" << (table.rows.size() - max_rows)
199 if (verbose && table.rows.size() > max_rows) {
200 for (
size_t i = max_rows; i < table.rows.size(); ++i) {
202 for (
size_t j = 0; j < table.rows[i].size(); ++j) {
203 std::cout << table.rows[i][j];
204 if (j < table.rows[i].size() - 1) {
221 if (response.
message.find(keyword) == std::string::npos) {
222 std::cout <<
"โ ๏ธ Warning: Expected keyword '" << keyword
223 <<
"' not found in response\n";
230 std::cout <<
"โ ๏ธ Warning: Expected tool calls but no table data found\n";
235 bool has_commands = response.
message.find(
"overworld") != std::string::npos ||
236 response.
message.find(
"dungeon") != std::string::npos ||
237 response.
message.find(
"set-tile") != std::string::npos;
239 std::cout <<
"โ ๏ธ Warning: Expected commands but none found\n";
251 bool all_passed =
true;
259 if (!response_or.ok()) {
260 std::cout <<
"โ FAILED: " << response_or.status().message() <<
"\n\n";
265 const auto& response = response_or.value();
275 std::cout <<
"๐ Conversation Summary (" << history.size()
276 <<
" message" << (history.size() == 1 ?
"" :
"s") <<
")\n";
277 for (
const auto& message : history) {
280 std::cout <<
" [" << sender <<
"] " << message.message <<
"\n";
286 std::cout <<
"โ
Test PASSED: " << test_case.
name <<
"\n";
287 return absl::OkStatus();
290 std::cout <<
"โ ๏ธ Test completed with warnings: " << test_case.
name <<
"\n";
291 return absl::InternalError(
292 absl::StrCat(
"Conversation test failed validation: ", test_case.
name));
296 std::vector<ConversationTestCase>* test_cases) {
297 std::ifstream file(file_path);
298 if (!file.is_open()) {
299 return absl::NotFoundError(
300 absl::StrCat(
"Could not open test file: ", file_path));
303 nlohmann::json test_json;
306 }
catch (
const nlohmann::json::parse_error& e) {
307 return absl::InvalidArgumentError(
308 absl::StrCat(
"Failed to parse test file: ", e.what()));
311 if (!test_json.is_array()) {
312 return absl::InvalidArgumentError(
313 "Test file must contain a JSON array of test cases");
316 for (
const auto& test_obj : test_json) {
318 test_case.
name = test_obj.value(
"name",
"unnamed_test");
319 test_case.
description = test_obj.value(
"description",
"");
321 if (test_obj.contains(
"prompts") && test_obj[
"prompts"].is_array()) {
322 for (
const auto& prompt : test_obj[
"prompts"]) {
323 if (prompt.is_string()) {
324 test_case.
user_prompts.push_back(prompt.get<std::string>());
329 if (test_obj.contains(
"expected_keywords") &&
330 test_obj[
"expected_keywords"].is_array()) {
331 for (
const auto& keyword : test_obj[
"expected_keywords"]) {
332 if (keyword.is_string()) {
341 test_cases->push_back(test_case);
344 return absl::OkStatus();
350 const std::vector<std::string>& arg_vec) {
351 std::string test_file;
352 bool use_defaults =
true;
353 bool verbose =
false;
355 for (
size_t i = 0; i < arg_vec.size(); ++i) {
356 const std::string& arg = arg_vec[i];
357 if (arg ==
"--file" && i + 1 < arg_vec.size()) {
358 test_file = arg_vec[i + 1];
359 use_defaults =
false;
361 }
else if (arg ==
"--verbose") {
366 std::cout <<
"๐ Debug: Starting test-conversation handler...\n";
370 std::cout <<
"๐ Debug: Loading ROM...\n";
371 auto load_status = LoadRomForAgent(rom);
372 if (!load_status.ok()) {
373 std::cerr <<
"โ Error loading ROM: " << load_status.message() <<
"\n";
377 std::cout <<
"โ
ROM loaded: " << rom.
title() <<
"\n";
380 std::cout <<
"๐ Debug: Initializing embedded labels...\n";
383 if (!labels_status.ok()) {
384 std::cerr <<
"โ ๏ธ Warning: Could not initialize embedded labels: "
385 << labels_status.message() <<
"\n";
387 std::cout <<
"โ
Embedded labels initialized successfully\n";
391 std::cout <<
"๐ Debug: Checking resource label manager...\n";
393 std::cout <<
"๐ Debug: Associating labels with ROM...\n";
396 std::cout <<
"โ
Embedded labels loaded and associated with ROM\n";
398 std::cout <<
"โ ๏ธ ROM has no resource label manager\n";
402 std::cout <<
"๐ Debug: Creating conversational agent service...\n";
403 std::cout <<
"๐ Debug: About to construct service object...\n";
406 std::cout <<
"โ
Service object created\n";
408 std::cout <<
"๐ Debug: Setting ROM context...\n";
410 std::cout <<
"โ
Service initialized\n";
413 std::vector<ConversationTestCase> test_cases;
415 test_cases = GetDefaultTestCases();
416 std::cout <<
"Using default test cases (" << test_cases.size() <<
" tests)\n";
418 auto status = LoadTestCasesFromFile(test_file, &test_cases);
422 std::cout <<
"Loaded " << test_cases.size() <<
" test cases from "
423 << test_file <<
"\n";
426 if (test_cases.empty()) {
427 return absl::InvalidArgumentError(
"No test cases to run");
434 for (
const auto& test_case : test_cases) {
435 auto status = RunTestCase(test_case, service, verbose);
440 std::cerr <<
"Test case '" << test_case.name <<
"' failed: "
441 << status.message() <<
"\n";
446 std::cout <<
"\n===========================================\n";
447 std::cout <<
"Test Summary\n";
448 std::cout <<
"===========================================\n";
449 std::cout <<
"Total tests: " << test_cases.size() <<
"\n";
450 std::cout <<
"Passed: " << passed <<
"\n";
451 std::cout <<
"Failed: " << failed <<
"\n";
454 std::cout <<
"\nโ
All tests passed!\n";
456 std::cout <<
"\nโ ๏ธ Some tests failed\n";
460 return absl::OkStatus();
463 return absl::InternalError(
464 absl::StrCat(failed,
" conversation test(s) reported failures"));
The Rom class is used to load, save, and modify Rom data.
project::ResourceLabelManager * resource_label()
absl::Status LoadFromFile(const std::string &filename, bool z3_load=true)
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.
absl::Status InitializeEmbeddedLabels()
std::unordered_map< std::string, std::unordered_map< std::string, std::string > > resource_labels