cli: harden tool output and tests

This commit is contained in:
scawful
2025-12-22 14:52:33 -05:00
parent 76512daba2
commit face88a940
9 changed files with 85 additions and 23 deletions

View File

@@ -23,6 +23,8 @@ namespace tools {
*/
class FileSystemToolBase : public resources::CommandHandler {
protected:
bool RequiresRom() const override { return false; }
/**
* @brief Validate and normalize a path for safe access
*
@@ -187,4 +189,4 @@ class FileSystemInfoTool : public FileSystemToolBase {
} // namespace cli
} // namespace yaze
#endif // YAZE_SRC_CLI_SERVICE_AGENT_TOOLS_FILESYSTEM_TOOL_H_
#endif // YAZE_SRC_CLI_SERVICE_AGENT_TOOLS_FILESYSTEM_TOOL_H_

View File

@@ -212,6 +212,13 @@ int VisualAnalysisBase::GetTileCountForSheet(int sheet_index) const {
std::string VisualAnalysisBase::FormatMatchesAsJson(
const std::vector<TileSimilarityMatch>& matches) const {
std::ostringstream json;
if (matches.empty()) {
json << "{\n \"matches\": [],\n";
json << " \"total_matches\": 0\n";
json << "}\n";
return json.str();
}
json << "{\n \"matches\": [\n";
for (size_t i = 0; i < matches.size(); ++i) {
@@ -238,6 +245,14 @@ std::string VisualAnalysisBase::FormatMatchesAsJson(
std::string VisualAnalysisBase::FormatRegionsAsJson(
const std::vector<UnusedRegion>& regions) const {
std::ostringstream json;
if (regions.empty()) {
json << "{\n \"unused_regions\": [],\n";
json << " \"total_regions\": 0,\n";
json << " \"total_free_tiles\": 0\n";
json << "}\n";
return json.str();
}
json << "{\n \"unused_regions\": [\n";
for (size_t i = 0; i < regions.size(); ++i) {
@@ -939,4 +954,3 @@ double ComputeSSIM(const std::vector<uint8_t>& tile_a,
} // namespace agent
} // namespace cli
} // namespace yaze

View File

@@ -270,8 +270,15 @@ void OutputFormatter::BeginObject(const std::string& title) {
void OutputFormatter::EndObject() {
if (IsJson()) {
buffer_ += "\n";
indent_level_--;
if (first_field_) {
if (!buffer_.empty() && buffer_.back() == '\n') {
buffer_.pop_back();
}
buffer_ += "}";
return;
}
buffer_ += "\n";
AddIndent();
buffer_ += "}";
}
@@ -292,6 +299,10 @@ void OutputFormatter::AddField(const std::string& key,
}
}
void OutputFormatter::AddField(const std::string& key, const char* value) {
AddField(key, value != nullptr ? std::string(value) : std::string());
}
void OutputFormatter::AddField(const std::string& key, int value) {
if (IsJson()) {
if (!first_field_) {
@@ -368,8 +379,15 @@ void OutputFormatter::EndArray() {
in_array_ = false;
if (IsJson()) {
buffer_ += "\n";
indent_level_--;
if (array_item_count_ == 0) {
if (!buffer_.empty() && buffer_.back() == '\n') {
buffer_.pop_back();
}
buffer_ += "]";
return;
}
buffer_ += "\n";
AddIndent();
buffer_ += "]";
}

View File

@@ -144,6 +144,7 @@ class OutputFormatter {
* @brief Add a key-value pair
*/
void AddField(const std::string& key, const std::string& value);
void AddField(const std::string& key, const char* value);
void AddField(const std::string& key, int value);
void AddField(const std::string& key, uint64_t value);
void AddField(const std::string& key, bool value);