utils-test.js 2.2 KB

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