@@ -11,12 +11,11 @@ import { color } from "@synnaxlabs/x";
1111import { describe , expect , it } from "vitest" ;
1212import { z } from "zod" ;
1313
14+ import { reduce , reduceAll } from "@/schematic/actions" ;
1415import {
1516 type Action ,
1617 actionZ ,
17- addNode ,
18- reduce ,
19- reduceAll ,
18+ setNode ,
2019 removeEdge ,
2120 removeNode ,
2221 setAuthority ,
@@ -83,28 +82,31 @@ describe("schematic reducer", () => {
8382 } ) ;
8483 } ) ;
8584
86- describe ( "addNode " , ( ) => {
87- it ( "should append the node to the end of the slice" , ( ) => {
85+ describe ( "setNode " , ( ) => {
86+ it ( "should append the node to the end of the slice when no node has the same key " , ( ) => {
8887 const state = empty ( { nodes : [ node ( "n1" , 0 , 0 ) ] } ) ;
89- const out = reduceAll ( state , [ addNode ( { node : node ( "n2" , 1 , 2 ) } ) ] ) ;
88+ const out = reduceAll ( state , [ setNode ( { node : node ( "n2" , 1 , 2 ) } ) ] ) ;
9089 expect ( out . nodes ) . toEqual ( [ node ( "n1" , 0 , 0 ) , node ( "n2" , 1 , 2 ) ] ) ;
9190 } ) ;
9291 it ( "should write config under the node's key when config is non-undefined" , ( ) => {
9392 const out = reduceAll ( empty ( ) , [
94- addNode ( { node : node ( "n1" , 0 , 0 ) , config : { label : "Pump" , color : "#f00" } } ) ,
93+ setNode ( { node : node ( "n1" , 0 , 0 ) , config : { label : "Pump" , color : "#f00" } } ) ,
9594 ] ) ;
9695 expect ( out . configs ) . toEqual ( { n1 : { label : "Pump" , color : "#f00" } } ) ;
9796 } ) ;
9897 it ( "should leave configs untouched when the action's config is undefined" , ( ) => {
99- const out = reduceAll ( empty ( ) , [ addNode ( { node : node ( "n1" , 0 , 0 ) } ) ] ) ;
98+ const out = reduceAll ( empty ( ) , [ setNode ( { node : node ( "n1" , 0 , 0 ) } ) ] ) ;
10099 expect ( out . configs ) . toEqual ( { } ) ;
101100 } ) ;
102- it ( "should append a duplicate-key node, locking current behavior" , ( ) => {
103- const state = empty ( { nodes : [ node ( "n1" , 0 , 0 ) ] } ) ;
104- const out = reduceAll ( state , [ addNode ( { node : node ( "n1" , 9 , 9 ) } ) ] ) ;
105- expect ( out . nodes ) . toHaveLength ( 2 ) ;
101+ it ( "should replace an existing node in place when the key already exists, preserving slice index" , ( ) => {
102+ const state = empty ( {
103+ nodes : [ node ( "n1" , 0 , 0 ) , node ( "n2" , 1 , 1 ) , node ( "n3" , 2 , 2 ) ] ,
104+ } ) ;
105+ const out = reduceAll ( state , [ setNode ( { node : node ( "n2" , 9 , 9 ) } ) ] ) ;
106+ expect ( out . nodes ) . toHaveLength ( 3 ) ;
106107 expect ( out . nodes [ 0 ] ) . toEqual ( node ( "n1" , 0 , 0 ) ) ;
107- expect ( out . nodes [ 1 ] ) . toEqual ( node ( "n1" , 9 , 9 ) ) ;
108+ expect ( out . nodes [ 1 ] ) . toEqual ( node ( "n2" , 9 , 9 ) ) ;
109+ expect ( out . nodes [ 2 ] ) . toEqual ( node ( "n3" , 2 , 2 ) ) ;
108110 } ) ;
109111 } ) ;
110112
@@ -253,9 +255,9 @@ describe("schematic reducer", () => {
253255
254256 it ( "should build a complete graph from an empty schematic" , ( ) => {
255257 const out = reduceAll ( empty ( ) , [
256- addNode ( { node : node ( "pump" , 0 , 0 ) } ) ,
257- addNode ( { node : node ( "valve" , 100 , 0 ) } ) ,
258- addNode ( { node : node ( "tank" , 200 , 0 ) } ) ,
258+ setNode ( { node : node ( "pump" , 0 , 0 ) } ) ,
259+ setNode ( { node : node ( "valve" , 100 , 0 ) } ) ,
260+ setNode ( { node : node ( "tank" , 200 , 0 ) } ) ,
259261 setEdge ( { edge : edge ( "e1" , "pump" , "out" , "valve" , "in" ) } ) ,
260262 setEdge ( { edge : edge ( "e2" , "valve" , "out" , "tank" , "in" ) } ) ,
261263 setConfig ( { key : "pump" , config : { label : "Main Pump" } } ) ,
@@ -277,7 +279,7 @@ describe("schematic reducer", () => {
277279 } ) ;
278280 const out = reduceAll ( state , [
279281 removeNode ( { key : "n1" } ) ,
280- addNode ( { node : node ( "n1" , 50 , 50 ) } ) ,
282+ setNode ( { node : node ( "n1" , 50 , 50 ) } ) ,
281283 ] ) ;
282284 expect ( out . nodes ) . toHaveLength ( 2 ) ;
283285 expect ( out . nodes [ 1 ] ) . toEqual ( node ( "n1" , 50 , 50 ) ) ;
@@ -296,7 +298,7 @@ describe("schematic reducer", () => {
296298 const state = empty ( ) ;
297299 const actions : Action [ ] = [ ] ;
298300 for ( let i = 0 ; i < 5 ; i ++ )
299- actions . push ( addNode ( { node : node ( `n${ i } ` , i * 100 , 0 ) } ) ) ;
301+ actions . push ( setNode ( { node : node ( `n${ i } ` , i * 100 , 0 ) } ) ) ;
300302 for ( let i = 0 ; i < 5 ; i ++ ) {
301303 actions . push (
302304 setNodePosition ( { key : `n${ i } ` , position : { x : i * 100 , y : 50 } } ) ,
@@ -338,7 +340,7 @@ describe("schematic reducer", () => {
338340
339341 describe ( "single-action reduce" , ( ) => {
340342 it ( "should apply a single action without wrapping it in an array" , ( ) => {
341- const out = reduce ( empty ( ) , addNode ( { node : node ( "n1" , 1 , 2 ) } ) ) ;
343+ const out = reduce ( empty ( ) , setNode ( { node : node ( "n1" , 1 , 2 ) } ) ) ;
342344 expect ( out . nodes ) . toEqual ( [ node ( "n1" , 1 , 2 ) ] ) ;
343345 } ) ;
344346 } ) ;
0 commit comments