utils-test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * @node utils-test
  3. * @name Utils Test
  4. * @description Test node for smartbotic.utils API
  5. * @category development
  6. * @version 1.0.0
  7. * @icon flask-conical
  8. * @trigger false
  9. */
  10. const configSchema = {};
  11. const inputSchema = {};
  12. const outputSchema = {};
  13. async function execute(config, input, context) {
  14. const results = {};
  15. // Test uuid()
  16. results.uuid = smartbotic.utils.uuid();
  17. smartbotic.log.info("Generated UUID: " + results.uuid);
  18. // Test interpolate()
  19. const template = "Hello {{name}}, your order #{{order.id}} for {{order.items.0}} is ready!";
  20. const data = {
  21. name: "John",
  22. order: {
  23. id: 12345,
  24. items: ["Widget", "Gadget"]
  25. }
  26. };
  27. results.interpolated = smartbotic.utils.interpolate(template, data);
  28. smartbotic.log.info("Interpolated: " + results.interpolated);
  29. // Test deepClone()
  30. const original = { a: 1, b: { c: 2 } };
  31. const cloned = smartbotic.utils.deepClone(original);
  32. cloned.b.c = 999; // Modify clone
  33. results.deepClone = {
  34. original: original,
  35. cloned: cloned,
  36. independentCheck: original.b.c === 2 // Should be true if deep cloned properly
  37. };
  38. smartbotic.log.info("Deep clone check: original.b.c = " + original.b.c + ", cloned.b.c = " + cloned.b.c);
  39. // Test isEmpty()
  40. results.isEmpty = {
  41. emptyString: smartbotic.utils.isEmpty(""),
  42. nonEmptyString: smartbotic.utils.isEmpty("hello"),
  43. emptyArray: smartbotic.utils.isEmpty([]),
  44. nonEmptyArray: smartbotic.utils.isEmpty([1, 2]),
  45. emptyObject: smartbotic.utils.isEmpty({}),
  46. nonEmptyObject: smartbotic.utils.isEmpty({ a: 1 }),
  47. nullValue: smartbotic.utils.isEmpty(null),
  48. undefinedValue: smartbotic.utils.isEmpty(undefined)
  49. };
  50. smartbotic.log.info("isEmpty tests: " + JSON.stringify(results.isEmpty));
  51. // Test pick()
  52. const obj = { a: 1, b: 2, c: 3, d: 4 };
  53. results.pick = smartbotic.utils.pick(obj, ["a", "c"]);
  54. smartbotic.log.info("pick result: " + JSON.stringify(results.pick));
  55. // Test omit()
  56. results.omit = smartbotic.utils.omit(obj, ["b", "d"]);
  57. smartbotic.log.info("omit result: " + JSON.stringify(results.omit));
  58. return results;
  59. }
  60. module.exports = { configSchema, inputSchema, outputSchema, execute };