nodeApi.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* eslint no-console: 0 */
  2. /**
  3. * nodeApi.js
  4. *
  5. * Test node api utilities
  6. *
  7. * @author d98762625 [d98762625@gmail.com]
  8. * @copyright Crown Copyright 2018
  9. * @license Apache-2.0
  10. */
  11. import assert from "assert";
  12. import it from "../assertionHandler";
  13. import chef from "../../../src/node/index";
  14. import OperationError from "../../../src/core/errors/OperationError";
  15. import SyncDish from "../../../src/node/SyncDish";
  16. import { toBase32 } from "../../../src/node/index";
  17. import TestRegister from "../../TestRegister";
  18. TestRegister.addApiTests([
  19. it("should have some operations", () => {
  20. assert(chef);
  21. assert(chef.toBase32);
  22. assert(chef.setUnion);
  23. assert(!chef.randomFunction);
  24. }),
  25. it("should export other functions at top level", () => {
  26. assert(toBase32);
  27. }),
  28. it("should be synchronous", () => {
  29. try {
  30. const result = chef.toBase32("input");
  31. assert.notEqual("something", result);
  32. } catch (e) {
  33. // shouldnt reach here
  34. assert(false);
  35. }
  36. try {
  37. const fail = chef.setUnion("1");
  38. // shouldnt get here
  39. assert(!fail || false);
  40. } catch (e) {
  41. assert(true);
  42. }
  43. }),
  44. it("should not catch Errors", () => {
  45. try {
  46. chef.setUnion("1");
  47. assert(false);
  48. } catch (e) {
  49. assert(e instanceof OperationError);
  50. }
  51. }),
  52. it("should accept arguments in object format for operations", () => {
  53. const result = chef.setUnion("1 2 3 4:3 4 5 6", {
  54. itemDelimiter: " ",
  55. sampleDelimiter: ":"
  56. });
  57. assert.equal(result.value, "1 2 3 4 5 6");
  58. }),
  59. it("should accept just some of the optional arguments being overriden", () => {
  60. const result = chef.setIntersection("1 2 3 4 5\\n\\n3 4 5", {
  61. itemDelimiter: " ",
  62. });
  63. assert.equal(result.value, "3 4 5");
  64. }),
  65. it("should accept no override arguments and just use the default values", () => {
  66. const result = chef.powerSet("1,2,3");
  67. assert.equal(result.value, "\n3\n2\n1\n2,3\n1,3\n1,2\n1,2,3\n");
  68. }),
  69. it("should return an object with a .to method", () => {
  70. const result = chef.toBase32("input");
  71. assert(result.to);
  72. assert.equal(result.to("string"), "NFXHA5LU");
  73. }),
  74. it("should return an object with a .get method", () => {
  75. const result = chef.toBase32("input");
  76. assert(result.get);
  77. assert.equal(result.get("string"), "NFXHA5LU");
  78. }),
  79. it("should return a SyncDish", () => {
  80. const result = chef.toBase32("input");
  81. assert(result instanceof SyncDish);
  82. }),
  83. it("should coerce to a string as you expect", () => {
  84. const result = chef.fromBase32(chef.toBase32("something"));
  85. assert.equal(String(result), "something");
  86. // This kind of coercion uses toValue
  87. assert.equal(""+result, "NaN");
  88. }),
  89. it("should coerce to a number as you expect", () => {
  90. const result = chef.fromBase32(chef.toBase32("32"));
  91. assert.equal(3 + result, 35);
  92. }),
  93. ]);