graph_state.test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const graph_state_1 = require("./graph_state");
  4. const test_helpers_1 = require("./util/test_helpers");
  5. const delta_registry_1 = require("./delta_registry");
  6. const delta_1 = require("./delta");
  7. const assert_1 = require("./util/assert");
  8. class MyGraphStateListener {
  9. createNode(ns) {
  10. console.log("created node", ns);
  11. }
  12. createValue(vs) {
  13. console.log("created value", vs);
  14. }
  15. deleteNode(id) {
  16. console.log("deleted node", id);
  17. }
  18. deleteValue(value) {
  19. console.log("deleted value", value);
  20. }
  21. createLinkToNode(sourceId, label, targetId) {
  22. console.log("created link to node", sourceId, label, targetId);
  23. }
  24. createLinkToValue(sourceId, label, targetValue) {
  25. console.log("created link to value", sourceId, label, targetValue);
  26. }
  27. deleteLink(sourceId, label) {
  28. console.log("deleted link", sourceId, label);
  29. }
  30. }
  31. describe("GraphState", () => {
  32. // need to test more scenarios!
  33. // taken from primitive delta test:
  34. it("Delete node with self-edge", () => {
  35. const graphState = new graph_state_1.GraphState();
  36. const registry = new delta_registry_1.DeltaRegistry();
  37. const getId = (0, test_helpers_1.mockUuid)();
  38. function newTransaction(deltas, description) {
  39. return registry.newTransaction(deltas, description, (0, delta_1.findTxDependencies)(deltas, graphState.deltas));
  40. }
  41. const myListener = new MyGraphStateListener();
  42. // Better not to create these primitive deltas by hand (is hard!). Instead, use 'getDeltasForSetEdge' and 'getDeltasForDelete' (see below).
  43. // const edgeCreation = registry.newEdgeUpdate(nodeCreation.createOutgoingEdge("label"), nodeCreation);
  44. // const edgeUpdate = registry.newEdgeUpdate(edgeCreation, null);
  45. // const nodeDeletion = registry.newNodeDeletion(nodeCreation, [edgeUpdate], [edgeUpdate]);
  46. (0, assert_1.assert)(graphState.nodes.size === 0, "Expected no nodes initially");
  47. const nodeId = getId();
  48. const nodeCreation = registry.newNodeCreation(nodeId);
  49. const comp0 = newTransaction([nodeCreation], "node creation");
  50. graphState.exec(comp0, myListener);
  51. (0, assert_1.assert)(graphState.nodes.size === 1, "Expected one node after NodeCreation");
  52. const nodeState = graphState.nodes.get(nodeId);
  53. const deltaForSetEdge = nodeState.getDeltaForSetEdge(registry, "x", 42);
  54. const comp1 = newTransaction([deltaForSetEdge], "set edge x == 42");
  55. graphState.exec(comp1, myListener);
  56. (0, assert_1.assert)(graphState.nodes.get(nodeId).outgoingDeltas.get("x") !== undefined, "Expected outgoing edge 'x' for node " + nodeId);
  57. (0, assert_1.assert)(graphState.nodes.get(nodeId).outgoing.get("x").value === 42, "Expected value of outgoing edge 'x' to be 42");
  58. const deltasForDelete = nodeState.getDeltasForDelete(registry);
  59. console.log({ deltasForDelete });
  60. const comp2 = newTransaction(deltasForDelete, "delete node (and edge)");
  61. graphState.exec(comp2, myListener);
  62. console.log(nodeState.outgoing.get("x"));
  63. // assert(nodeState!.outgoing.get("x") === null, "Exepected no outgoing edge 'x'");
  64. graphState.unexec(comp2, myListener);
  65. // assert((graphState.nodes.get(nodeId)!.outgoing.get("x") as IValueState).value === 42, "Expected value of outgoing edge 'x' to be 42");
  66. // assert(graphState.nodes.size === 0, "Expected no more nodes in graphState");
  67. // // graphState.exec(edgeCreation);
  68. // assert(graphState.nodes.size === 1, "Expected one node after EdgeCreation");
  69. // // assert(graphState.edges.length === 1, "Expected one edge after EdgeCreation");
  70. // graphState.exec(edgeUpdate);
  71. // assert(graphState.nodes.size === 1, "Expected one node after EdgeUpdate");
  72. // // assert(graphState.edges.length === 0, "Expected no edges after EdgeUpdate");
  73. // graphState.exec(nodeDeletion);
  74. // assert(graphState.nodes.size === 0, "Expected no nodes after NodeDeletion");
  75. // // assert(graphState.edges.length === 0, "Expected no edges after NodeDeletion");
  76. });
  77. });
  78. //# sourceMappingURL=graph_state.test.js.map