graph_state.test.ts 3.9 KB

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