mattrguerra/meridian-orders-mcpmainPrivate on GitHub, published here in full

collections.test.mjs

Language JavaScript2.6 KB68 lines

mcp-server/test/collections.test.mjs

  1. // Unit tests for lib/collections.ts. Run after `npm run build` (npm test does both).
  2. import { test } from "node:test";
  3. import assert from "node:assert/strict";
  4. import { normalizeCollection, toNumber } from "../dist/lib/collections.js";
  5. test("normalizeCollection: an empty collection arrives as [] and stays []", () => {
  6. assert.deepEqual(normalizeCollection([]), []);
  7. });
  8. test("normalizeCollection: a populated collection arrives keyed \"1\"..\"N\" and becomes an array", () => {
  9. const lines = { "1": { sku: "a" }, "2": { sku: "b" }, "3": { sku: "c" } };
  10. assert.deepEqual(normalizeCollection(lines), [{ sku: "a" }, { sku: "b" }, { sku: "c" }]);
  11. });
  12. test("normalizeCollection: ordinal keys come out in numeric order, so \"10\" follows \"9\"", () => {
  13. const keyed = {};
  14. for (let i = 10; i >= 1; i--) keyed[String(i)] = i;
  15. assert.deepEqual(normalizeCollection(keyed), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  16. });
  17. test("normalizeCollection: split_lines in either shape within one response normalize alike", () => {
  18. const asArray = [{ qty: 2 }, { qty: 5 }];
  19. const asObject = { "1": { qty: 2 }, "2": { qty: 5 } };
  20. assert.deepEqual(normalizeCollection(asObject), normalizeCollection(asArray));
  21. });
  22. test("normalizeCollection: an array passes through unchanged", () => {
  23. const arr = [3, 1, 2];
  24. assert.equal(normalizeCollection(arr), arr);
  25. });
  26. test("normalizeCollection: null, undefined and primitives give an empty array", () => {
  27. for (const value of [null, undefined, "", "text", 0, 7, true]) {
  28. assert.deepEqual(normalizeCollection(value), [], `input ${JSON.stringify(value)}`);
  29. }
  30. });
  31. test("toNumber: numbers pass through", () => {
  32. assert.equal(toNumber(42), 42);
  33. assert.equal(toNumber(0), 0);
  34. assert.equal(toNumber(-3.5), -3.5);
  35. });
  36. test("toNumber: numeric strings are coerced, surrounding whitespace allowed", () => {
  37. assert.equal(toNumber("42"), 42);
  38. assert.equal(toNumber("0"), 0);
  39. assert.equal(toNumber("19.95"), 19.95);
  40. assert.equal(toNumber(" 7 "), 7);
  41. });
  42. test("toNumber: empty, blank and unparseable strings give undefined, not NaN", () => {
  43. assert.equal(toNumber(""), undefined);
  44. assert.equal(toNumber(" "), undefined);
  45. assert.equal(toNumber("abc"), undefined);
  46. assert.equal(toNumber("12abc"), undefined);
  47. });
  48. test("toNumber: non-finite numbers give undefined", () => {
  49. assert.equal(toNumber(Number.NaN), undefined);
  50. assert.equal(toNumber(Number.POSITIVE_INFINITY), undefined);
  51. assert.equal(toNumber("Infinity"), undefined);
  52. });
  53. test("toNumber: other types give undefined", () => {
  54. for (const value of [null, undefined, true, {}, [], [5]]) {
  55. assert.equal(toNumber(value), undefined, `input ${JSON.stringify(value)}`);
  56. }
  57. });