State_test.res.mjs

  1// Generated by ReScript, PLEASE EDIT WITH CARE
  2
  3import * as Yjs from "yjs";
  4import * as State from "../src/State.res.mjs";
  5import * as Vitest from "vitest";
  6import * as Waypoint from "../src/ffi/Waypoint.res.mjs";
  7import * as Core__Option from "@rescript/core/lib/es6/src/Core__Option.res.mjs";
  8
  9function pushWaypoint(yarray, waypoint) {
 10  (((ya, wp) => { ya.push([wp]); })(yarray, waypoint));
 11}
 12
 13function makeWaypointJSON(id, lon, lat, label, gid, country, from, cons, nonroutable, param) {
 14  let dict = Object.fromEntries([
 15    [
 16      "type",
 17      "waypoint"
 18    ],
 19    [
 20      "id",
 21      id
 22    ],
 23    [
 24      "point",
 25      [
 26        lon,
 27        lat
 28      ]
 29    ],
 30    [
 31      "label",
 32      label
 33    ],
 34    [
 35      "gid",
 36      gid
 37    ],
 38    [
 39      "country",
 40      country
 41    ]
 42  ]);
 43  if (from !== undefined) {
 44    dict["from"] = from;
 45  }
 46  if (cons !== undefined) {
 47    dict["cons"] = cons.map(prim => prim);
 48  }
 49  dict["nonroutable"] = Core__Option.getOr(nonroutable, false);
 50  return dict;
 51}
 52
 53function setupObserver(docID, ydoc) {
 54  let waypointsArray = ydoc.getArray("waypoints");
 55  let waypointsSignal = State.getOrCreateWaypointsSignal(docID);
 56  waypointsArray.observe(_event => {
 57    let jsonArray = waypointsArray.toJSON();
 58    let waypoints = Waypoint.fromYjsArray(jsonArray);
 59    waypointsSignal.value = waypoints;
 60  });
 61  return waypointsSignal;
 62}
 63
 64Vitest.describe("State.waypoints Map", () => {
 65  Vitest.test("signal starts empty for new docID", async t => {
 66    let signal = State.getOrCreateWaypointsSignal("doc1");
 67    t.expect(signal.value.length).toEqual(0);
 68  });
 69  Vitest.test("getOrCreateWaypointsSignal returns same signal for same docID", async t => {
 70    let signal1 = State.getOrCreateWaypointsSignal("doc2");
 71    let signal2 = State.getOrCreateWaypointsSignal("doc2");
 72    t.expect(signal1).toBe(signal2);
 73  });
 74  Vitest.test("getOrCreateWaypointsSignal returns different signals for different docIDs", async t => {
 75    let signal1 = State.getOrCreateWaypointsSignal("doc3");
 76    let signal2 = State.getOrCreateWaypointsSignal("doc4");
 77    t.expect(signal1).not.toBe(signal2);
 78  });
 79  Vitest.test("observer updates signal when waypoints are added", async t => {
 80    let doc = new Yjs.Doc();
 81    let waypointsSignal = setupObserver("doc5", doc);
 82    let waypointsArray = doc.getArray("waypoints");
 83    let wp1 = makeWaypointJSON("wp1", 85.324, 27.717, "Kathmandu", "kathmandu_gid", "NP", undefined, undefined, undefined, undefined);
 84    pushWaypoint(waypointsArray, wp1);
 85    let waypoints = waypointsSignal.value;
 86    t.expect(waypoints.length).toEqual(1);
 87    let wp = waypoints[0];
 88    if (wp !== undefined) {
 89      t.expect(wp.id).toEqual("wp1");
 90      t.expect(wp.point).toEqual([
 91        85.324,
 92        27.717
 93      ]);
 94      t.expect(wp.label).toEqual("Kathmandu");
 95      t.expect(wp.gid).toEqual("kathmandu_gid");
 96      t.expect(wp.country).toEqual("NP");
 97    } else {
 98      t.expect("waypoint not found").toEqual("");
 99    }
100  });
101  Vitest.test("multiple documents have independent waypoints", async t => {
102    let docA = new Yjs.Doc();
103    let docB = new Yjs.Doc();
104    let signalA = setupObserver("docA", docA);
105    let signalB = setupObserver("docB", docB);
106    let arrayA = docA.getArray("waypoints");
107    let arrayB = docB.getArray("waypoints");
108    let wpA = makeWaypointJSON("wpA", 85.0, 27.0, "Location A", "gidA", "NP", undefined, undefined, undefined, undefined);
109    pushWaypoint(arrayA, wpA);
110    t.expect(signalA.value.length).toEqual(1);
111    t.expect(signalB.value.length).toEqual(0);
112    let wpB = makeWaypointJSON("wpB", 83.0, 28.0, "Location B", "gidB", "NP", undefined, undefined, undefined, undefined);
113    pushWaypoint(arrayB, wpB);
114    t.expect(signalA.value.length).toEqual(1);
115    t.expect(signalB.value.length).toEqual(1);
116  });
117  Vitest.test("deserializes constraints correctly", async t => {
118    let doc = new Yjs.Doc();
119    let waypointsSignal = setupObserver("doc6", doc);
120    let waypointsArray = doc.getArray("waypoints");
121    let wp = makeWaypointJSON("wp2", 83.0, 28.0, "Pokhara", "pokhara_gid", "NP", undefined, [
122      "adv:high",
123      "max-speed:80"
124    ], undefined, undefined);
125    pushWaypoint(waypointsArray, wp);
126    let waypoints = waypointsSignal.value;
127    t.expect(waypoints.length).toEqual(1);
128    let wp$1 = waypoints[0];
129    if (wp$1 !== undefined) {
130      t.expect(wp$1.cons.length).toEqual(2);
131      let c1 = wp$1.cons[0];
132      if (c1 !== undefined) {
133        t.expect(c1).toEqual("adv:high");
134        return;
135      } else {
136        return;
137      }
138    }
139    t.expect("waypoint not found").toEqual("");
140  });
141});
142
143export {
144  pushWaypoint,
145  makeWaypointJSON,
146  setupObserver,
147}
148/*  Not a pure module */