import { test } from "@siteimprove/alfa-test" ; import { Graph } from "../src/graph" ; const graph = Graph . from ( [ [ "foo" , [ "bar" , "baz" ] ] , [ "bar" , [ ] ] , [ "baz" , [ "foo" ] ] , ] ) ; test ( "#connect() connects two nodes in a graph" , ( t ) => { t . deepEqual ( graph . connect ( "bar" , "baz" ) . toArray ( ) , [ [ "baz" , [ "foo" ] ] , [ "foo" , [ "baz" , "bar" ] ] , [ "bar" , [ "baz" ] ] , ] ) ; } ) ; test ( "#disconnect() disconnects two nodes in a graph" , ( t ) => { t . deepEqual ( graph . disconnect ( "foo" , "bar" ) . toArray ( ) , [ [ "baz" , [ "foo" ] ] , [ "foo" , [ "baz" ] ] , [ "bar" , [ ] ] , ] ) ; } ) ; test ( "#delete() removes a node from a graph" , ( t ) => { t . deepEqual ( graph . delete ( "bar" ) . toArray ( ) , [ [ "baz" , [ "foo" ] ] , [ "foo" , [ "baz" ] ] , ] ) ; } ) ; test ( "#traverse() traverses the subgraph rooted at a node" , ( t ) => { t . deepEqual ( graph . traverse ( "baz" ) . toJSON ( ) , [ [ "foo" , "baz" ] , [ "bar" , "foo" ] , ] ) ; } ) ; test ( "#traverse() traverses the subgraph rooted at a node depth-first" , ( t ) => { const graph = Graph . from ( [ [ 1 , [ 2 , 5 ] ] , [ 2 , [ 3 , 4 ] ] , [ 4 , [ 2 ] ] , [ 5 , [ 6 , 7 ] ] , [ 7 , [ 1 ] ] , ] ) ; t . deepEqual ( graph . traverse ( 1 , Graph . DepthFirst ) . toJSON ( ) , [ [ 5 , 1 ] , [ 6 , 5 ] , [ 7 , 5 ] , [ 2 , 1 ] , [ 4 , 2 ] , [ 3 , 2 ] , ] ) ; } ) ; test ( "#traverse() traverses the subgraph rooted at a node breadth-first" , ( t ) => { const graph = Graph . from ( [ [ 1 , [ 2 , 5 ] ] , [ 2 , [ 3 , 4 ] ] , [ 4 , [ 2 ] ] , [ 5 , [ 6 , 7 ] ] , [ 7 , [ 1 ] ] , ] ) ; t . deepEqual ( graph . traverse ( 1 , Graph . BreadthFirst ) . toJSON ( ) , [ [ 2 , 1 ] , [ 5 , 1 ] , [ 3 , 2 ] , [ 4 , 2 ] , [ 7 , 5 ] , [ 6 , 5 ] , ] ) ; } ) ; test ( "#path() returns the path from one node to another" , ( t ) => { t . deepEqual ( graph . path ( "foo" , "bar" ) . toJSON ( ) , [ "bar" ] ) ; t . deepEqual ( graph . path ( "baz" , "bar" ) . toJSON ( ) , [ "foo" , "bar" ] ) ; t . deepEqual ( graph . path ( "bar" , "baz" ) . toJSON ( ) , [ ] ) ; } ) ; test ( "#hasPath() checks if there's a path from one node to another" , ( t ) => { t . equal ( graph . hasPath ( "foo" , "bar" ) , true ) ; t . equal ( graph . hasPath ( "baz" , "bar" ) , true ) ; t . equal ( graph . hasPath ( "bar" , "baz" ) , false ) ; } ) ; test ( "#sort() topologically sorts an acyclic graph" , ( t ) => { const graph = Graph . from ( [ [ 1 , [ 2 ] ] , [ 2 , [ 3 , 4 ] ] , [ 4 , [ 3 ] ] , [ 5 , [ 4 ] ] , ] ) ; t . deepEqual ( [ ... graph . sort ( ) ] , [ 5 , 1 , 2 , 4 , 3 ] ) ; } ) ; test ( "#sort() yields nothing for a cyclic graph" , ( t ) => { const graph = Graph . from ( [ [ 1 , [ 2 ] ] , [ 2 , [ 3 , 4 ] ] , [ 4 , [ 1 ] ] , ] ) ; t . deepEqual ( [ ... graph . sort ( ) ] , [ ] ) ; } ) ;