| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import {GraphState, GraphStateListener, INodeState, IValueState} from "./graph_state";
- import {mockUuid} from "./util/test_helpers";
- import {DeltaRegistry} from "./delta_registry";
- import {findTxDependencies, TargetValue} from "./delta";
- import {assert} from "./util/assert";
- class MyGraphStateListener implements GraphStateListener {
- createNode(ns: INodeState) {
- console.log("created node", ns);
- }
- createValue(vs: IValueState) {
- console.log("created value", vs);
- }
- deleteNode(id) {
- console.log("deleted node", id);
- }
- deleteValue(value) {
- console.log("deleted value", value);
- }
- createLinkToNode(sourceId, label: string, targetId) {
- console.log("created link to node", sourceId, label, targetId);
- }
- createLinkToValue(sourceId, label: string, targetValue) {
- console.log("created link to value", sourceId, label, targetValue);
- }
- deleteLink(sourceId, label: string) {
- console.log("deleted link", sourceId, label);
- }
- }
- describe("GraphState", () => {
- // need to test more scenarios!
- // taken from primitive delta test:
- it("Delete node with self-edge", () => {
- const graphState = new GraphState();
- const registry = new DeltaRegistry();
- const getId = mockUuid();
- function newTransaction(deltas, description) {
- return registry.newTransaction(deltas, description, findTxDependencies(deltas, graphState.deltas));
- }
- const myListener = new MyGraphStateListener();
- // Better not to create these primitive deltas by hand (is hard!). Instead, use 'getDeltasForSetEdge' and 'getDeltasForDelete' (see below).
-
- // const edgeCreation = registry.newEdgeUpdate(nodeCreation.createOutgoingEdge("label"), nodeCreation);
- // const edgeUpdate = registry.newEdgeUpdate(edgeCreation, null);
- // const nodeDeletion = registry.newNodeDeletion(nodeCreation, [edgeUpdate], [edgeUpdate]);
- assert(graphState.nodes.size === 0, "Expected no nodes initially");
- const nodeId = getId();
- const nodeCreation = registry.newNodeCreation(nodeId);
- const comp0 = newTransaction([nodeCreation], "node creation");
- graphState.exec(comp0, myListener);
- assert(graphState.nodes.size === 1, "Expected one node after NodeCreation");
- const nodeState = graphState.nodes.get(nodeId);
- const deltaForSetEdge = nodeState!.getDeltaForSetEdge(registry, "x", 42);
- const comp1 = newTransaction([deltaForSetEdge], "set edge x == 42");
- graphState.exec(comp1, myListener);
- assert(graphState.nodes.get(nodeId)!.outgoingDeltas.get("x") !== undefined, "Expected outgoing edge 'x' for node " + nodeId);
- assert((graphState.nodes.get(nodeId)!.outgoing.get("x") as IValueState).value === 42, "Expected value of outgoing edge 'x' to be 42");
- const deltasForDelete = nodeState!.getDeltasForDelete(registry);
- console.log({deltasForDelete})
- const comp2 = newTransaction(deltasForDelete, "delete node (and edge)");
- graphState.exec(comp2, myListener);
- console.log(nodeState!.outgoing.get("x"))
- // assert(nodeState!.outgoing.get("x") === null, "Exepected no outgoing edge 'x'");
- graphState.unexec(comp2, myListener);
- // assert((graphState.nodes.get(nodeId)!.outgoing.get("x") as IValueState).value === 42, "Expected value of outgoing edge 'x' to be 42");
- // assert(graphState.nodes.size === 0, "Expected no more nodes in graphState");
- // // graphState.exec(edgeCreation);
- // assert(graphState.nodes.size === 1, "Expected one node after EdgeCreation");
- // // assert(graphState.edges.length === 1, "Expected one edge after EdgeCreation");
- // graphState.exec(edgeUpdate);
- // assert(graphState.nodes.size === 1, "Expected one node after EdgeUpdate");
- // // assert(graphState.edges.length === 0, "Expected no edges after EdgeUpdate");
- // graphState.exec(nodeDeletion);
- // assert(graphState.nodes.size === 0, "Expected no nodes after NodeDeletion");
- // // assert(graphState.edges.length === 0, "Expected no edges after NodeDeletion");
- });
- });
|