11 const std::vector<ai::AIAction>& actions) {
13 auto json_result = GenerateTestJSON(actions);
14 if (!json_result.ok()) {
15 return json_result.status();
17 return json_result->dump(2);
19 return absl::UnimplementedError(
20 "JSON support required for test script generation");
25absl::StatusOr<nlohmann::json> GuiActionGenerator::GenerateTestJSON(
26 const std::vector<ai::AIAction>& actions) {
27 nlohmann::json test_script;
28 test_script[
"test_name"] =
"ai_generated_test";
29 test_script[
"description"] =
"Automatically generated from AI actions";
30 test_script[
"steps"] = nlohmann::json::array();
32 for (
size_t i = 0; i < actions.size(); ++i) {
33 nlohmann::json step = ActionToJSON(actions[i]);
34 step[
"step_number"] = i + 1;
35 test_script[
"steps"].push_back(step);
41nlohmann::json GuiActionGenerator::ActionToJSON(
const ai::AIAction& action) {
44 switch (action.type) {
46 step[
"action"] =
"click";
47 auto it = action.parameters.find(
"editor");
48 if (it != action.parameters.end()) {
49 step[
"target"] = absl::StrCat(
"button:", it->second,
" Editor");
50 step[
"wait_after"] = 500;
56 step[
"action"] =
"click";
57 auto it = action.parameters.find(
"tile_id");
58 if (it != action.parameters.end()) {
59 int tile_id = std::stoi(it->second);
62 int tile_x = (tile_id % 8) * 16 + 8;
63 int tile_y = (tile_id / 8) * 16 + 8;
65 step[
"target"] =
"canvas:tile16_selector";
66 step[
"position"] = {{
"x", tile_x}, {
"y", tile_y}};
67 step[
"wait_after"] = 200;
73 step[
"action"] =
"click";
74 auto x_it = action.parameters.find(
"x");
75 auto y_it = action.parameters.find(
"y");
77 if (x_it != action.parameters.end() && y_it != action.parameters.end()) {
80 int screen_x = std::stoi(x_it->second) * 16 + 8;
81 int screen_y = std::stoi(y_it->second) * 16 + 8;
83 step[
"target"] =
"canvas:overworld_map";
84 step[
"position"] = {{
"x", screen_x}, {
"y", screen_y}};
85 step[
"wait_after"] = 200;
91 step[
"action"] =
"click";
92 step[
"target"] =
"button:Save to ROM";
93 step[
"wait_after"] = 300;
98 step[
"action"] =
"click";
99 auto it = action.parameters.find(
"button");
100 if (it != action.parameters.end()) {
101 step[
"target"] = absl::StrCat(
"button:", it->second);
102 step[
"wait_after"] = 200;
108 step[
"action"] =
"wait";
109 auto it = action.parameters.find(
"duration_ms");
111 it != action.parameters.end() ? std::stoi(it->second) : 500;
112 step[
"duration_ms"] = duration;
117 step[
"action"] =
"screenshot";
118 auto it = action.parameters.find(
"filename");
119 if (it != action.parameters.end()) {
120 step[
"filename"] = it->second;
122 step[
"filename"] =
"verification.png";
128 step[
"action"] =
"verify";
129 step[
"target"] =
"tile_placement";
131 for (
const auto& [key, value] : action.parameters) {
138 step[
"action"] =
"error";
139 step[
"message"] =
"Invalid action type";
149 switch (action.
type) {
165 return absl::StrFormat(
"# Step %d: Unknown action", step_number);
173 return absl::StrFormat(
"Click button:'%s Editor'\nWait 500ms", it->second);
175 return "Click button:'Editor'\nWait 500ms";
182 int tile_id = std::stoi(it->second);
183 int tile_x = (tile_id % 8) * 16 + 8;
184 int tile_y = (tile_id / 8) * 16 + 8;
185 return absl::StrFormat(
186 "Click canvas:'tile16_selector' at (%d, %d)\nWait 200ms", tile_x,
189 return "Click canvas:'tile16_selector'\nWait 200ms";
198 int screen_x = std::stoi(x_it->second) * 16 + 8;
199 int screen_y = std::stoi(y_it->second) * 16 + 8;
200 return absl::StrFormat(
201 "Click canvas:'overworld_map' at (%d, %d)\nWait 200ms", screen_x,
204 return "Click canvas:'overworld_map'\nWait 200ms";
209 return "Click button:'Save to ROM'\nWait 300ms";
216 return absl::StrFormat(
"Click button:'%s'\nWait 200ms", it->second);
218 return "Click button\nWait 200ms";
222 auto it = action.
parameters.find(
"duration_ms");
223 int duration = it != action.
parameters.end() ? std::stoi(it->second) : 500;
224 return absl::StrFormat(
"Wait %dms", duration);
231 return absl::StrFormat(
"Screenshot '%s'", it->second);
233 return "Screenshot 'verification.png'";