Pārlūkot izejas kodu

fix: readable fractional size-limit message, drop a vacuous form test

formatMb() replaces a bare static_cast<int> truncation in the per-field
upload size error, so a 0.5 MB maxSizeMb reads "0.5 MB" instead of "0
MB" - and a whole-number limit still reads as one, not "16.000000".

tests/nodes/form-trigger-shape.json asserted only JS types after a
direct run with no submission, so it passed under both the broken
context.triggerData read and the fixed one - it could not have caught
the bug it was named after. form-submit.json now covers the same
ground through the real webhook controller with assertions that do
fail under a broken node, so this one is removed rather than patched.
fszontagh 1 mēnesi atpakaļ
vecāks
revīzija
703d7bdd7e

+ 22 - 1
src/webserver/api/webhook_controller.cpp

@@ -8,11 +8,32 @@
 #include <grpcpp/grpcpp.h>
 #include <cctype>
 #include <optional>
+#include <sstream>
+#include <iomanip>
 
 namespace smartbotic::webserver::api {
 
 using namespace common;
 
+namespace {
+
+// std::to_string(static_cast<int>(0.5)) prints "0" - a fractional field
+// limit like 0.5 MB would read back as "larger than the 0 MB limit", which
+// is worse than useless. Print with enough precision to keep a fraction,
+// then drop a trailing ".0"/".00" etc so a whole number still reads as one.
+std::string formatMb(double mb) {
+    std::ostringstream ss;
+    ss << std::fixed << std::setprecision(2) << mb;
+    std::string s = ss.str();
+    if (s.find('.') != std::string::npos) {
+        while (!s.empty() && s.back() == '0') s.pop_back();
+        if (!s.empty() && s.back() == '.') s.pop_back();
+    }
+    return s;
+}
+
+}  // namespace
+
 WebhookController::WebhookController(storage::StorageClient& storage,
                                      runners::RunnerRegistry& registry,
                                      runners::LoadBalancer& load_balancer,
@@ -418,7 +439,7 @@ bool WebhookController::buildFormTriggerData(const httplib::Request& req,
             // this only tightens, never loosens.
             const double max_mb = f.value("maxSizeMb", 0.0);
             if (max_mb > 0 && static_cast<double>(file.content.size()) > max_mb * 1024 * 1024) {
-                error = label + " is larger than the " + std::to_string(static_cast<int>(max_mb)) +
+                error = label + " is larger than the " + formatMb(max_mb) +
                         " MB limit for this field.";
                 return false;
             }

+ 0 - 16
tests/nodes/form-trigger-shape.json

@@ -1,16 +0,0 @@
-{
-  "name": "verify-form-trigger-shape",
-  "nodes": [
-    {"id": "n1", "name": "Form", "type": "form-trigger", "position": {"x": 0, "y": 0},
-     "config": {"title": "Send an image",
-                "fields": [{"name": "image", "label": "Image", "type": "file", "required": true}]}},
-    {"id": "n2", "name": "Check", "type": "code", "position": {"x": 0, "y": 100},
-     "config": {"code": "return { hasForm: typeof input.data.form === 'object' && input.data.form !== null, submitted: typeof input.data.submittedAt === 'number' };"}}
-  ],
-  "connections": [
-    {"sourceNodeId": "n1", "sourceOutput": "main", "targetNodeId": "n2", "targetInput": "data"}
-  ],
-  "expect": {
-    "n2": {"status": "completed", "output": {"result": {"hasForm": true, "submitted": true}}}
-  }
-}