@@ -12,7 +12,7 @@ import {
1212 onFloor ,
1313} from "." ;
1414
15- const { LEFT , RIGHT } = Direction ;
15+ const { LEFT , RIGHT , FRONT , BACK } = Direction ;
1616
1717describe ( "Building.validity" , ( ) => {
1818 it ( "marks valid buildings correctly" , ( ) => {
@@ -29,7 +29,7 @@ describe("Building.validity", () => {
2929 new Room ( "a" ) ,
3030 new Room ( "b" ) ,
3131 new Turn ( RIGHT ) ,
32- new Fork ( LEFT , "a" , " ") ,
32+ new Room ( "c ") ,
3333 ] ) ,
3434 ] )
3535 )
@@ -81,7 +81,7 @@ describe("Building.validity", () => {
8181 new Room ( "b" ) ,
8282 new Stairs ( LEFT , onFloor ( "c" , 4 ) ) ,
8383 ] ) ,
84- new Hallway ( [ new Room ( "a" ) , new Stairs ( RIGHT , onFloor ( "b " , 1 ) ) ] ) ,
84+ new Hallway ( [ new Room ( "a" ) , new Stairs ( RIGHT , onFloor ( "c " , 1 ) ) ] ) ,
8585 ] )
8686 )
8787 ) . toEqual (
@@ -92,6 +92,226 @@ describe("Building.validity", () => {
9292 ) ;
9393 } ) ;
9494
95+ it ( "marks buildings with duplicated nodes as invalid" , ( ) => {
96+ expect (
97+ isValidBuilding (
98+ new Building ( [
99+ new Hallway ( [ new Room ( "a" ) , new Room ( "b" ) , new Fork ( LEFT , "a" , "" ) ] ) ,
100+ new Hallway ( [ new Room ( "z" ) , new Fork ( RIGHT , "a" , "" ) ] ) ,
101+ ] )
102+ )
103+ ) . toEqual (
104+ expect . objectContaining ( {
105+ valid : false ,
106+ reason :
107+ "There's more than one Fork with the nodeId 'a'. One of them should probably be a reverseConnection." ,
108+ } )
109+ ) ;
110+
111+ expect (
112+ isValidBuilding (
113+ new Building ( [
114+ new Hallway ( [
115+ new Room ( "a" ) ,
116+ new Room ( "b" ) ,
117+ new Fork ( LEFT , reverseConnection ( "a" ) , "" ) ,
118+ ] ) ,
119+ new Hallway ( [
120+ new Room ( "z" ) ,
121+ new Fork ( RIGHT , reverseConnection ( "a" ) , "" ) ,
122+ ] ) ,
123+ ] )
124+ )
125+ ) . toEqual (
126+ expect . objectContaining ( {
127+ valid : false ,
128+ reason :
129+ "There's more than one Fork with the nodeId reverseConnection('a'). One of them should probably not be a reverseConnection." ,
130+ } )
131+ ) ;
132+
133+ expect (
134+ isValidBuilding (
135+ new Building ( [
136+ new Hallway ( [
137+ new Room ( "a" ) ,
138+ new Room ( "b" ) ,
139+ new Stairs ( LEFT , onFloor ( "a" , 1 ) ) ,
140+ ] ) ,
141+ new Hallway ( [ new Room ( "z" ) , new Stairs ( LEFT , onFloor ( "a" , 1 ) ) ] ) ,
142+ ] )
143+ )
144+ ) . toEqual (
145+ expect . objectContaining ( {
146+ valid : false ,
147+ reason :
148+ "There's more than one Stairs node with the name onFloor('a', 1). " +
149+ "One of the Stairs in the staircase should probably be on a different floor." ,
150+ } )
151+ ) ;
152+ } ) ;
153+
154+ it ( "doesn't allow unmatched Forks with no reverseConnection" , ( ) => {
155+ expect (
156+ isValidBuilding (
157+ new Building ( [
158+ new Hallway ( [ new Room ( "a" ) , new Room ( "b" ) , new Fork ( LEFT , "a" , "" ) ] ) ,
159+ ] )
160+ )
161+ ) . toEqual (
162+ expect . objectContaining ( {
163+ valid : false ,
164+ reason :
165+ "There's a Fork with the nodeId 'a' that doesn't have a reverseConnection. " +
166+ "You need to either add a Fork somewhere else with the nodeId reverseConnection('a') to connect it to this node, or remove this node." ,
167+ } )
168+ ) ;
169+
170+ expect (
171+ isValidBuilding (
172+ new Building ( [
173+ new Hallway ( [
174+ new Room ( "a" ) ,
175+ new Room ( "b" ) ,
176+ new Fork ( LEFT , reverseConnection ( "a" ) , "" ) ,
177+ ] ) ,
178+ ] )
179+ )
180+ ) . toEqual (
181+ expect . objectContaining ( {
182+ valid : false ,
183+ reason :
184+ "There's a Fork with the nodeId reverseConnection('a') that doesn't have a regular connection. " +
185+ "You need to either add a Fork somewhere else with the nodeId 'a' to connect it to this node, or remove this node." ,
186+ } )
187+ ) ;
188+ } ) ;
189+
190+ it ( "doesn't allow lone Stairs with no other floors in the staircase" , ( ) => {
191+ expect (
192+ isValidBuilding (
193+ new Building ( [
194+ new Hallway ( [
195+ new Room ( "a" ) ,
196+ new Room ( "b" ) ,
197+ new Stairs ( LEFT , onFloor ( "a" , 3 ) ) ,
198+ ] ) ,
199+ ] )
200+ )
201+ ) . toEqual (
202+ expect . objectContaining ( {
203+ valid : false ,
204+ reason :
205+ "There are Stairs with the nodeId onFloor('a', 3) with no corresponding Stairs on a different floor. " +
206+ "You need to either add Stairs somewhere else with the same name and a different floor, or remove this node." ,
207+ } )
208+ ) ;
209+ } ) ;
210+
211+ it ( "checks that rooms marked BACK are in the back and rooms marked FRONT are in the front" , ( ) => {
212+ expect (
213+ isValidBuilding (
214+ new Building ( [
215+ new Hallway ( [
216+ new Room ( "a" , BACK ) ,
217+ new Room ( "b" ) ,
218+ new Turn ( RIGHT ) ,
219+ new Room ( "c" ) ,
220+ ] ) ,
221+ ] )
222+ )
223+ ) . toEqual ( expect . objectContaining ( { valid : true } ) ) ;
224+
225+ expect (
226+ isValidBuilding (
227+ new Building ( [
228+ new Hallway ( [
229+ new Room ( "a" ) ,
230+ new Room ( "b" ) ,
231+ new Turn ( RIGHT ) ,
232+ new Room ( "c" , BACK ) ,
233+ ] ) ,
234+ ] )
235+ )
236+ ) . toEqual (
237+ expect . objectContaining ( {
238+ valid : false ,
239+ reason :
240+ "The element at position 3 of the Hallway at position 0 has the side BACK, but it is not the first element of the hallway" ,
241+ } )
242+ ) ;
243+
244+ expect (
245+ isValidBuilding (
246+ new Building ( [
247+ new Hallway ( [
248+ new Room ( "a" ) ,
249+ new Room ( "b" , BACK ) ,
250+ new Turn ( RIGHT ) ,
251+ new Room ( "c" ) ,
252+ ] ) ,
253+ ] )
254+ )
255+ ) . toEqual (
256+ expect . objectContaining ( {
257+ valid : false ,
258+ reason :
259+ "The element at position 1 of the Hallway at position 0 has the side BACK, but it is not the first element of the hallway" ,
260+ } )
261+ ) ;
262+
263+ expect (
264+ isValidBuilding (
265+ new Building ( [
266+ new Hallway ( [
267+ new Room ( "a" ) ,
268+ new Room ( "b" ) ,
269+ new Turn ( RIGHT ) ,
270+ new Room ( "c" , FRONT ) ,
271+ ] ) ,
272+ ] )
273+ )
274+ ) . toEqual ( expect . objectContaining ( { valid : true } ) ) ;
275+
276+ expect (
277+ isValidBuilding (
278+ new Building ( [
279+ new Hallway ( [
280+ new Room ( "a" , FRONT ) ,
281+ new Room ( "b" ) ,
282+ new Turn ( RIGHT ) ,
283+ new Room ( "c" ) ,
284+ ] ) ,
285+ ] )
286+ )
287+ ) . toEqual (
288+ expect . objectContaining ( {
289+ valid : false ,
290+ reason :
291+ "The element at position 0 of the Hallway at position 0 has the side FRONT, but it is not the last element of the hallway" ,
292+ } )
293+ ) ;
294+
295+ expect (
296+ isValidBuilding (
297+ new Building ( [
298+ new Hallway ( [
299+ new Room ( "a" ) ,
300+ new Room ( "b" , FRONT ) ,
301+ new Turn ( RIGHT ) ,
302+ new Room ( "c" ) ,
303+ ] ) ,
304+ ] )
305+ )
306+ ) . toEqual (
307+ expect . objectContaining ( {
308+ valid : false ,
309+ reason :
310+ "The element at position 1 of the Hallway at position 0 has the side FRONT, but it is not the last element of the hallway" ,
311+ } )
312+ ) ;
313+ } ) ;
314+
95315 it ( "marks buildings with negative weights as invalid" , ( ) => {
96316 expect (
97317 isValidBuilding (
@@ -128,7 +348,8 @@ describe("Building.validity", () => {
128348 ) . toEqual (
129349 expect . objectContaining ( {
130350 valid : false ,
131- reason : "The hallway at index 2 has no nodes (Forks or Stairs)" ,
351+ reason :
352+ "The hallway at index 2 has no nodes (Forks or Stairs) to connect it to the rest of the building." ,
132353 } )
133354 ) ;
134355 } ) ;
@@ -140,9 +361,13 @@ describe("Building.validity", () => {
140361 new Hallway ( [ new Room ( "a" ) , new Room ( "b" ) , new Fork ( LEFT , "a" , "" ) ] ) ,
141362 new Hallway ( [
142363 new Room ( "z" ) ,
143- new Fork ( RIGHT , reverseConnection ( "b " ) , "" ) ,
364+ new Fork ( RIGHT , reverseConnection ( "a " ) , "" ) ,
144365 ] ) ,
145366 new Hallway ( [ new Room ( "c" ) , new Fork ( RIGHT , "8" , "" ) ] ) ,
367+ new Hallway ( [
368+ new Room ( "f" ) ,
369+ new Fork ( FRONT , reverseConnection ( "8" ) , "" ) ,
370+ ] ) ,
146371 ] )
147372 )
148373 ) . toEqual (
@@ -153,4 +378,46 @@ describe("Building.validity", () => {
153378 } )
154379 ) ;
155380 } ) ;
381+
382+ it ( "doesn't allow a Turn at the back or front of a Hallway" , ( ) => {
383+ expect (
384+ isValidBuilding (
385+ new Building ( [
386+ new Hallway ( [
387+ new Room ( "a" , BACK ) ,
388+ new Room ( "b" ) ,
389+ new Turn ( RIGHT ) ,
390+ new Room ( "c" ) ,
391+ new Turn ( LEFT ) ,
392+ ] ) ,
393+ ] )
394+ )
395+ ) . toEqual (
396+ expect . objectContaining ( {
397+ valid : false ,
398+ reason :
399+ "There last element of the Hallway at position 0 is a Turn. There is no reason to include a Turn here because it will never be passed." ,
400+ } )
401+ ) ;
402+
403+ expect (
404+ isValidBuilding (
405+ new Building ( [
406+ new Hallway ( [
407+ new Turn ( LEFT ) ,
408+ new Room ( "a" ) ,
409+ new Room ( "b" ) ,
410+ new Turn ( RIGHT ) ,
411+ new Room ( "c" ) ,
412+ ] ) ,
413+ ] )
414+ )
415+ ) . toEqual (
416+ expect . objectContaining ( {
417+ valid : false ,
418+ reason :
419+ "There first element of the Hallway at position 0 is a Turn. There is no reason to include a Turn here because it will never be passed." ,
420+ } )
421+ ) ;
422+ } ) ;
156423} ) ;
0 commit comments