-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoders.test.coffee
More file actions
1067 lines (765 loc) · 39.8 KB
/
coders.test.coffee
File metadata and controls
1067 lines (765 loc) · 39.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Tests for G-code generation methods
Polyslice = require('../../index')
describe 'G-code Generation (Coders)', ->
slicer = null
beforeEach ->
slicer = new Polyslice({
progressCallback: null # Disable progress output during tests
})
describe 'Movement Commands', ->
test 'should generate autohome G-code', ->
gcode = slicer.codeAutohome()
expect(gcode).toBe('G28\n')
xcodeHome = slicer.codeAutohome(true, false, false)
expect(xcodeHome).toBe('G28 X\n')
test 'should generate workspace plane G-code', ->
expect(slicer.codeWorkspacePlane()).toBe('G17\n') # XY plane.
slicer.setWorkspacePlane('XZ')
expect(slicer.codeWorkspacePlane()).toBe('G18\n')
slicer.setWorkspacePlane('YZ')
expect(slicer.codeWorkspacePlane()).toBe('G19\n')
test 'should generate linear movement G-code', ->
moveCode = slicer.codeLinearMovement(10, 20, 5)
expect(moveCode).toBe('G0 X10 Y20 Z5\n') # G0 for non-extruding move.
extrudeCode = slicer.codeLinearMovement(10, 20, 5, 0.1, 1500) # Feedrate in mm/min.
expect(extrudeCode).toBe('G1 X10 Y20 Z5 E0.1 F1500\n') # G1 for extruding move.
test 'should generate arc movement G-code', ->
# Test clockwise arc.
arcCode = slicer.codeArcMovement("clockwise", 10, 10, null, null, null, null, 5, 0)
expect(arcCode).toBe('G2 X10 Y10 I5 J0\n')
# Test counterclockwise arc.
arcCode = slicer.codeArcMovement("counterclockwise", 5, 5, null, null, null, null, 2, 2)
expect(arcCode).toBe('G3 X5 Y5 I2 J2\n')
describe 'Unit Commands', ->
test 'should generate length unit G-code', ->
expect(slicer.codeLengthUnit()).toBe('G21\n') # millimeters.
slicer.setLengthUnit('inches')
expect(slicer.codeLengthUnit()).toBe('G20\n')
test 'should generate temperature unit G-code', ->
expect(slicer.codeTemperatureUnit()).toBe('M149 C\n') # celsius.
slicer.setTemperatureUnit('fahrenheit')
expect(slicer.codeTemperatureUnit()).toBe('M149 F\n')
slicer.setTemperatureUnit('kelvin')
expect(slicer.codeTemperatureUnit()).toBe('M149 K\n')
describe 'Temperature Commands', ->
test 'should generate temperature G-code', ->
nozzleCode = slicer.codeNozzleTemperature(200, false)
expect(nozzleCode).toBe('M104 S200\n') # Set without waiting.
bedCode = slicer.codeBedTemperature(60, false)
expect(bedCode).toBe('M140 S60\n') # Set without waiting.
describe 'Fan Commands', ->
test 'should generate fan speed G-code', ->
fanCode = slicer.codeFanSpeed(50)
expect(fanCode).toBe('M106 S127\n') # 50% * 2.55 = 127.5, rounded to 127.
fanOffCode = slicer.codeFanSpeed(0)
expect(fanOffCode).toBe('M107\n') # Fan off.
describe 'Control Commands', ->
test 'should generate message G-code', ->
messageCode = slicer.codeMessage("Test Message")
expect(messageCode).toBe('M117 Test Message\n')
test 'should generate wait G-code', ->
waitCode = slicer.codeWait()
expect(waitCode).toBe('M400\n')
test 'should generate dwell G-code', ->
dwellCode = slicer.codeDwell(1000)
expect(dwellCode).toBe('M0 P1000\n') # Interruptible dwell for 1000ms.
nonInterruptibleDwell = slicer.codeDwell(500, false)
expect(nonInterruptibleDwell).toBe('G4 P500\n') # Non-interruptible dwell for 500ms.
describe 'Retraction Commands', ->
test 'should generate retraction G-code', ->
result = slicer.codeRetract()
expect(result).toBe('G1 E-1 F2400\n') # Default 1mm at 40mm/s (2400mm/min).
# Test custom values.
result = slicer.codeRetract(2.0, 50)
expect(result).toBe('G1 E-2 F3000\n')
# Test zero retraction.
result = slicer.codeRetract(0, 50)
expect(result).toBe('') # Should return empty string.
test 'should generate unretract G-code', ->
result = slicer.codeUnretract()
expect(result).toBe('G1 E1 F2400\n') # Default 1mm at 40mm/s.
# Test custom values.
result = slicer.codeUnretract(1.5, 30)
expect(result).toBe('G1 E1.5 F1800\n')
describe 'Print Sequence Commands', ->
test 'should generate test strip G-code', ->
result = slicer.codeTestStrip()
expect(result).toContain('G92 E0') # Reset extruder.
expect(result).toContain('G0 Z2') # Move Z up.
expect(result).toContain('G0 X10 Y10 Z0.25') # Move to start (X-axis now).
expect(result).toContain('E15') # First line extrusion.
expect(result).toContain('E30') # Second line cumulative extrusion.
expect(result).toContain('G0 Z2') # Lift nozzle.
test 'should generate test strip with custom dimensions', ->
result = slicer.codeTestStrip(80, 0.4, 0.4)
expect(result).toContain('G0 X10 Y10 Z0.4') # Custom height.
expect(result).toContain('X90') # Custom length (10 + 80) along X-axis.
expect(result).toContain('G0 Z2') # Lift nozzle.
test 'should generate pre-print sequence', ->
slicer.setNozzleTemperature(200)
slicer.setBedTemperature(60)
slicer.setFanSpeed(100)
slicer.setMetadata(false) # Disable metadata for simpler test.
slicer.setVerbose(false) # Disable verbose for simpler test.
result = slicer.codePrePrint()
expect(result).toContain('M104 S200') # Start heating nozzle.
expect(result).toContain('M140 S60') # Start heating bed.
expect(result).toContain('G28') # Autohome (while heating).
expect(result).toContain('G0 Z10') # Back off bed.
expect(result).toContain('M109 R200') # Wait for nozzle temperature.
expect(result).toContain('M190 R60') # Wait for bed temperature.
expect(result).toContain('M82') # Absolute extrusion mode.
expect(result).toContain('G17') # Workspace plane.
expect(result).toContain('G21') # Length unit.
expect(result).toContain('G92 E0') # Reset extruder.
expect(result).toContain('E-5') # Final retract before print.
test 'should generate pre-print without test strip', ->
slicer.setTestStrip(false)
slicer.setVerbose(false)
result = slicer.codePrePrint()
expect(result).not.toContain('X10 Y10') # Test strip starting position.
test 'should generate pre-print with test strip', ->
slicer.setTestStrip(true)
slicer.setVerbose(false)
result = slicer.codePrePrint()
expect(result).toContain('X10 Y10') # Test strip starting position (X-axis).
test 'should generate pre-print with simultaneous heating and autohome', ->
slicer.setNozzleTemperature(200)
slicer.setBedTemperature(60)
slicer.setMetadata(false)
slicer.setVerbose(false)
result = slicer.codePrePrint()
expect(result).toContain('M104 S200') # Start nozzle heating (no wait).
expect(result).toContain('M140 S60') # Start bed heating (no wait).
expect(result).toContain('G28') # Autohome (simultaneous with heating).
expect(result).toContain('M109 R200') # Wait for nozzle.
expect(result).toContain('M190 R60') # Wait for bed.
test 'should generate post-print sequence', ->
slicer.setVerbose(false)
result = slicer.codePostPrint()
expect(result).toContain('M107') # Turn off fan.
expect(result).toContain('G91') # Relative positioning.
expect(result).toContain('G0 X5 Y5') # Wipe move (default is now true).
expect(result).toContain('E-2') # Retract.
expect(result).toContain('G1 Z10') # Raise nozzle.
expect(result).toContain('G90') # Absolute positioning.
expect(result).toContain('G28 X Y') # Home X and Y.
expect(result).toContain('M104 S0') # Turn off nozzle.
expect(result).toContain('M140 S0') # Turn off bed.
expect(result).toContain('M84 X Y E') # Disable steppers.
expect(result).toContain('M300') # Buzzer command present (triple beep).
expect(result).toContain('S420') # Frequency 420Hz.
test 'should generate post-print without buzzer', ->
slicer.setBuzzer(false)
slicer.setVerbose(false)
result = slicer.codePostPrint()
expect(result).not.toContain('M300') # No buzzer.
expect(result).toContain('M84 X Y E') # Disable steppers.
test 'should generate post-print with wipe nozzle', ->
slicer.setWipeNozzle(true)
slicer.setVerbose(false)
result = slicer.codePostPrint()
expect(result).toContain('G0 X5 Y5') # Wipe move.
# Verify wipe comes before retract and raise Z.
wipeIndex = result.indexOf('G0 X5 Y5')
retractIndex = result.indexOf('G1 Z10 E-2')
expect(wipeIndex).toBeGreaterThan(-1)
expect(retractIndex).toBeGreaterThan(-1)
expect(wipeIndex).toBeLessThan(retractIndex) # Wipe should come before retract/raise.
test 'should generate post-print without wipe nozzle', ->
slicer.setWipeNozzle(false)
slicer.setVerbose(false)
result = slicer.codePostPrint()
expect(result).not.toContain('G0 X5 Y5') # No wipe move.
test 'should use smart wipe when enabled with mesh data', ->
slicer.setWipeNozzle(true)
slicer.setSmartWipeNozzle(true)
slicer.setVerbose(false)
slicer.setRetractionDistance(1.0)
# Simulate mesh boundary data from slicing.
slicer.lastLayerEndPoint = { x: 5, y: 0, z: 5 }
slicer._meshBounds = { min: { x: -5, y: -5 }, max: { x: 5, y: 5 } }
slicer.centerOffsetX = 100
slicer.centerOffsetY = 100
result = slicer.codePostPrint()
# Should NOT contain simple wipe.
expect(result).not.toContain('G0 X5 Y5')
# Should contain smart wipe with retraction.
# The exact coordinates depend on the smart wipe logic, but should have relative move and retraction.
expect(result).toContain('G91') # Relative positioning.
# Should contain retraction during wipe (negative E value).
lines = result.split('\n')
foundSmartWipe = false
for line in lines
if line.includes('G1') and line.includes('E-') and (line.includes('X') or line.includes('Y'))
foundSmartWipe = true
break
expect(foundSmartWipe).toBe(true)
test 'should fall back to simple wipe when smart wipe disabled', ->
slicer.setWipeNozzle(true)
slicer.setSmartWipeNozzle(false)
slicer.setVerbose(false)
result = slicer.codePostPrint()
expect(result).toContain('G0 X5 Y5') # Simple wipe move.
test 'should fall back to simple wipe when mesh data not available', ->
slicer.setWipeNozzle(true)
slicer.setSmartWipeNozzle(true)
slicer.setVerbose(false)
# No mesh data available.
slicer.lastLayerEndPoint = null
slicer._meshBounds = null
result = slicer.codePostPrint()
expect(result).toContain('G0 X5 Y5') # Fallback to simple wipe.
test 'should handle smart wipe retraction correctly', ->
slicer.setWipeNozzle(true)
slicer.setSmartWipeNozzle(true)
slicer.setVerbose(false)
slicer.setRetractionDistance(1.5)
# Simulate mesh boundary data.
slicer.lastLayerEndPoint = { x: 5, y: 0, z: 5 }
slicer._meshBounds = { min: { x: -5, y: -5 }, max: { x: 5, y: 5 } }
slicer.centerOffsetX = 100
slicer.centerOffsetY = 100
result = slicer.codePostPrint()
# Should have retraction equal to configured distance during wipe.
expect(result).toContain('E-1.5')
# After smart wipe with retraction, the Z raise should NOT have additional retraction.
lines = result.split('\n')
foundSmartWipe = false
smartWipeIndex = -1
for line, index in lines
if line.includes('G1') and line.includes('E-1.5')
foundSmartWipe = true
smartWipeIndex = index
break
expect(foundSmartWipe).toBe(true)
# Check that the Z raise after smart wipe doesn't have E-2 (which would be double retraction).
# It should only have Z10 without additional retraction.
foundRaiseZ = false
for i in [smartWipeIndex + 1...lines.length]
if lines[i].includes('Z10') and not lines[i].includes('E-')
foundRaiseZ = true
break
expect(foundRaiseZ).toBe(true)
test 'should generate metadata header', ->
Printer = require('../../config/printer/printer')
Filament = require('../../config/filament/filament')
printer = new Printer('Ender3')
filament = new Filament('GenericPLA')
metadataSlicer = new Polyslice({
progressCallback: null
printer: printer
filament: filament
metadata: true
})
result = metadataSlicer.codeMetadata()
pkg = require('../../../package.json')
expect(result).toContain('; Generated by Polyslice')
expect(result).toContain('; Version: ' + pkg.version)
expect(result).not.toContain('; Version: Unknown')
expect(result).toContain('; Timestamp:')
expect(result).toContain('; Repository: https://github.com/jgphilpott/polyslice')
expect(result).toContain('; Printer: Ender3')
expect(result).toContain('; Filament: Generic PLA (pla)')
expect(result).toContain('; Nozzle Temp:')
expect(result).toContain('; Bed Temp:')
expect(result).toContain('; Layer Height:')
test 'should include metadata in pre-print when enabled', ->
slicer.setMetadata(true)
result = slicer.codePrePrint()
expect(result).toContain('; Generated by Polyslice')
test 'should not include metadata in pre-print when disabled', ->
slicer.setMetadata(false)
result = slicer.codePrePrint()
expect(result).not.toContain('; Generated by Polyslice')
test 'should include print statistics in metadata after slicing', ->
THREE = require('three')
Printer = require('../../config/printer/printer')
Filament = require('../../config/filament/filament')
# Create a simple test cube.
geometry = new THREE.BoxGeometry(10, 10, 10)
material = new THREE.MeshBasicMaterial({ color: 0x00ff00 })
mesh = new THREE.Mesh(geometry, material)
printer = new Printer('Ender3')
filament = new Filament('GenericPLA')
slicerWithStats = new Polyslice({
progressCallback: null
printer: printer
filament: filament
metadata: true
verbose: false
})
# Slice the mesh.
gcode = slicerWithStats.slice(mesh)
# Verify metadata contains print statistics.
expect(gcode).toContain('; Total Layers:')
expect(gcode).toContain('; Filament Length:')
expect(gcode).toContain('; Material Volume:')
expect(gcode).toContain('; Material Weight:')
expect(gcode).toContain('; Estimated Print Time:')
# Verify statistics are populated on slicer instance.
expect(slicerWithStats.totalLayers).toBeGreaterThan(0)
expect(slicerWithStats.totalFilamentLength).toBeGreaterThan(0)
test 'should respect metadata field config settings', ->
THREE = require('three')
Printer = require('../../config/printer/printer')
Filament = require('../../config/filament/filament')
# Create a simple test cube.
geometry = new THREE.BoxGeometry(10, 10, 10)
material = new THREE.MeshBasicMaterial({ color: 0x00ff00 })
mesh = new THREE.Mesh(geometry, material)
printer = new Printer('Ender3')
filament = new Filament('GenericPLA')
# Test with all new metadata fields enabled (default).
slicerEnabled = new Polyslice({
progressCallback: null
printer: printer
filament: filament
metadata: true
verbose: false
})
gcodeEnabled = slicerEnabled.slice(mesh)
# Verify new metadata fields are present.
expect(gcodeEnabled).toContain('; Flavor: Marlin')
expect(gcodeEnabled).toContain('; Infill Density:')
expect(gcodeEnabled).toContain('; Infill Pattern:')
expect(gcodeEnabled).toContain('; Wall Count:')
expect(gcodeEnabled).toContain('; Support:')
expect(gcodeEnabled).toContain('; Adhesion:')
expect(gcodeEnabled).toContain('; Perimeter Speed:')
expect(gcodeEnabled).toContain('; Infill Speed:')
expect(gcodeEnabled).toContain('; Travel Speed:')
# Note: Bounding box is added after slicing, not checked here.
# Test with new metadata fields disabled.
slicerDisabled = new Polyslice({
progressCallback: null
printer: printer
filament: filament
metadata: true
metadataFlavor: false
metadataInfillDensity: false
metadataInfillPattern: false
metadataWallCount: false
metadataSupport: false
metadataAdhesion: false
metadataSpeeds: false
metadataBoundingBox: false
verbose: false
})
gcodeDisabled = slicerDisabled.slice(mesh)
# Verify new metadata fields are NOT present.
expect(gcodeDisabled).not.toContain('; Flavor: Marlin')
expect(gcodeDisabled).not.toContain('; Infill Density:')
expect(gcodeDisabled).not.toContain('; Infill Pattern:')
expect(gcodeDisabled).not.toContain('; Wall Count:')
expect(gcodeDisabled).not.toContain('; Support:')
expect(gcodeDisabled).not.toContain('; Adhesion:')
expect(gcodeDisabled).not.toContain('; Perimeter Speed:')
expect(gcodeDisabled).not.toContain('; Infill Speed:')
expect(gcodeDisabled).not.toContain('; Travel Speed:')
# Note: Bounding box is added after slicing, not checked here.
# But old metadata fields should still be present (didn't disable them).
expect(gcodeDisabled).toContain('; Generated by Polyslice')
expect(gcodeDisabled).toContain('; Version:')
test 'should calculate print time correctly', ->
THREE = require('three')
# Create a simple test cube.
geometry = new THREE.BoxGeometry(5, 5, 5)
material = new THREE.MeshBasicMaterial({ color: 0x00ff00 })
mesh = new THREE.Mesh(geometry, material)
timeTestSlicer = new Polyslice({
progressCallback: null
metadata: true
verbose: false
layerHeight: 0.2
})
# Slice the mesh.
gcode = timeTestSlicer.slice(mesh)
# Extract estimated print time from metadata.
lines = gcode.split('\n')
timeLine = null
for line in lines
if line.includes('; Estimated Print Time:')
timeLine = line
break
expect(timeLine).not.toBeNull()
# Verify time format is HH:MM:SS.
expect(timeLine).toMatch(/\d{2}:\d{2}:\d{2}/)
test 'should format time correctly', ->
coders = require('./coders')
# Test various time durations.
expect(coders.formatTime(0)).toBe('00:00:00')
expect(coders.formatTime(61)).toBe('00:01:01')
expect(coders.formatTime(3661)).toBe('01:01:01')
expect(coders.formatTime(36000)).toBe('10:00:00')
# Test negative and invalid values.
expect(coders.formatTime(-100)).toBe('00:00:00')
expect(coders.formatTime(NaN)).toBe('00:00:00')
expect(coders.formatTime(null)).toBe('00:00:00')
expect(coders.formatTime(undefined)).toBe('00:00:00')
expect(coders.formatTime(36000)).toBe('10:00:00')
test 'should calculate arc movement time correctly', ->
coders = require('./coders')
Polyslice = require('../../polyslice')
# Create a slicer with test G-code containing arc movements
testSlicer = new Polyslice({progressCallback: null})
# Simulate G-code with arc movements
# G2/G3 with I/J parameters (center format)
testSlicer.gcode = """
G90
G21
G28
M109 S200
M190 S60
G92 E0
G1 X0 Y0 Z0.2 F3000
G1 X10 Y0 E1 F1800
G2 X20 Y10 I10 J0 E2 F1800
G1 X20 Y20 E3 F1800
G3 X10 Y30 I-10 J0 E4 F1800
"""
# Calculate print time
printTime = coders.calculatePrintTime(testSlicer)
# Print time should be greater than zero
expect(printTime).toBeGreaterThan(0)
# The arc movements should take longer than if they were straight lines
# Arc from (10,0) to (20,10) with center offset I=10, J=0
# Radius = 10mm, this is a quarter circle, arc length = π*10/2 ≈ 15.7mm
# Straight line distance would be sqrt(100+100) ≈ 14.1mm
# So arc should add more time
# Verify time includes heating estimates (30s + 60s = 90s)
expect(printTime).toBeGreaterThan(90)
test 'should handle relative positioning mode (G91)', ->
coders = require('./coders')
Polyslice = require('../../polyslice')
# Create a slicer with test G-code using relative positioning
testSlicer = new Polyslice({progressCallback: null})
# Test G-code with mixed absolute and relative positioning
testSlicer.gcode = """
G90
G21
G28
M109 S200
M190 S60
G92 E0
G1 X10 Y10 Z0.2 F3000
G91
G1 X5 Y0 E1 F1800
G1 X0 Y5 E2 F1800
G1 X-5 Y0 E3 F1800
G90
G1 X0 Y0 E4 F1800
"""
# Calculate print time
printTime = coders.calculatePrintTime(testSlicer)
# Print time should be greater than zero
expect(printTime).toBeGreaterThan(0)
# Verify time includes heating estimates (30s + 60s = 90s)
expect(printTime).toBeGreaterThan(90)
# Calculate expected move distances
# Absolute: (0,0,0) -> (10,10,0.2) = sqrt(100+100+0.04) ≈ 14.14mm
# Relative: (10,10) -> (15,10) = 5mm
# Relative: (15,10) -> (15,15) = 5mm
# Relative: (15,15) -> (10,15) = 5mm
# Absolute: (10,15) -> (0,0) = sqrt(100+225) ≈ 18.03mm
# Total distance ≈ 47mm at 1800mm/min = 30mm/s -> ~1.57s
# Plus heating (90s) = ~91.57s
expect(printTime).toBeGreaterThan(91)
expect(printTime).toBeLessThan(93)
# Verify time includes heating estimates (30s + 60s = 90s)
expect(printTime).toBeGreaterThan(90)
test 'should calculate material volume and weight', ->
THREE = require('three')
Filament = require('../../config/filament/filament')
# Create a test cube.
geometry = new THREE.BoxGeometry(10, 10, 10)
material = new THREE.MeshBasicMaterial({ color: 0x00ff00 })
mesh = new THREE.Mesh(geometry, material)
filament = new Filament('GenericPLA')
materialTestSlicer = new Polyslice({
progressCallback: null
filament: filament
metadata: true
verbose: false
})
# Slice the mesh.
gcode = materialTestSlicer.slice(mesh)
# Verify material calculations are present.
expect(gcode).toContain('; Material Volume:')
expect(gcode).toContain('mm³')
expect(gcode).toContain('; Material Weight:')
expect(gcode).toContain('g')
# Extract values to verify they're reasonable.
lines = gcode.split('\n')
volumeLine = null
weightLine = null
for line in lines
if line.includes('; Material Volume:')
volumeLine = line
else if line.includes('; Material Weight:')
weightLine = line
expect(volumeLine).not.toBeNull()
expect(weightLine).not.toBeNull()
# Verify values are positive numbers.
volumeMatch = volumeLine.match(/(\d+\.?\d*)mm³/)
weightMatch = weightLine.match(/(\d+\.?\d*)g/)
expect(volumeMatch).not.toBeNull()
expect(weightMatch).not.toBeNull()
expect(parseFloat(volumeMatch[1])).toBeGreaterThan(0)
expect(parseFloat(weightMatch[1])).toBeGreaterThan(0)
test 'should respect individual metadata field settings', ->
THREE = require('three')
Printer = require('../../config/printer/printer')
Filament = require('../../config/filament/filament')
# Create a test cube.
geometry = new THREE.BoxGeometry(10, 10, 10)
material = new THREE.MeshBasicMaterial({ color: 0x00ff00 })
mesh = new THREE.Mesh(geometry, material)
printer = new Printer('Ender3')
filament = new Filament('GenericPLA')
# Create slicer with all metadata fields disabled except a few.
# Note: metadataTitle is always enabled when metadata is true.
customMetadataSlicer = new Polyslice({
progressCallback: null
printer: printer
filament: filament
metadata: true
verbose: false
metadataVersion: false # Disable
metadataTimestamp: false # Disable
metadataRepository: false # Disable
metadataPrinter: true # Keep enabled
metadataFilament: false # Disable
metadataNozzleTemp: false # Disable
metadataBedTemp: false # Disable
metadataLayerHeight: false # Disable
metadataTotalLayers: true # Keep enabled
metadataFilamentLength: true # Keep enabled
metadataMaterialVolume: false # Disable
metadataMaterialWeight: false # Disable
metadataPrintTime: true # Keep enabled
})
# Slice the mesh.
gcode = customMetadataSlicer.slice(mesh)
# Verify enabled fields are present (title is always present).
expect(gcode).toContain('; Generated by Polyslice')
expect(gcode).toContain('; Printer: Ender3')
expect(gcode).toContain('; Total Layers:')
expect(gcode).toContain('; Filament Length:')
expect(gcode).toContain('; Estimated Print Time:')
# Verify disabled fields are NOT present.
expect(gcode).not.toContain('; Version:')
expect(gcode).not.toContain('; Timestamp:')
expect(gcode).not.toContain('; Repository:')
expect(gcode).not.toContain('; Filament: Generic PLA')
expect(gcode).not.toContain('; Nozzle Temp:')
expect(gcode).not.toContain('; Bed Temp:')
expect(gcode).not.toContain('; Layer Height:')
expect(gcode).not.toContain('; Material Volume:')
expect(gcode).not.toContain('; Material Weight:')
describe 'Coder Methods Side Effects', ->
test 'should not modify slicer state when generating temperature G-code', ->
# Set initial values.
slicer.setNozzleTemperature(200)
slicer.setBedTemperature(60)
# Generate G-code with different values.
slicer.codeNozzleTemperature(0, false)
slicer.codeBedTemperature(0, false)
# Verify original values are preserved.
expect(slicer.nozzleTemperature).toBe(200)
expect(slicer.bedTemperature).toBe(60)
test 'should not modify slicer state when generating fan G-code', ->
# Set initial value.
slicer.setFanSpeed(100)
# Generate G-code with different value.
slicer.codeFanSpeed(0)
# Verify original value is preserved.
expect(slicer.fanSpeed).toBe(100)
test 'should use passed values instead of config for G-code generation', ->
# Set config values.
slicer.setNozzleTemperature(200)
slicer.setBedTemperature(60)
slicer.setFanSpeed(100)
# Generate G-code with different values.
nozzleCode = slicer.codeNozzleTemperature(150, false)
bedCode = slicer.codeBedTemperature(40, false)
fanCode = slicer.codeFanSpeed(50)
# Verify G-code uses passed values, not config values.
expect(nozzleCode).toBe('M104 S150\n')
expect(bedCode).toBe('M140 S40\n')
expect(fanCode).toBe('M106 S127\n') # 50% = 127
# Verify config values are still unchanged.
expect(slicer.nozzleTemperature).toBe(200)
expect(slicer.bedTemperature).toBe(60)
expect(slicer.fanSpeed).toBe(100)
test 'should preserve temperature and fan settings across multiple slice calls', ->
THREE = require('three')
# Create a simple cube mesh.
geometry = new THREE.BoxGeometry(10, 10, 10)
material = new THREE.MeshBasicMaterial()
mesh = new THREE.Mesh(geometry, material)
mesh.position.set(0, 0, 5)
mesh.updateMatrixWorld()
# Configure slicer with specific temperatures and fan speed.
slicer.setNozzleTemperature(200)
slicer.setBedTemperature(60)
slicer.setFanSpeed(100)
# First slice.
result1 = slicer.slice(mesh)
# Verify first slice has correct heating commands.
expect(result1).toContain('M104 S200') # Start heating nozzle.
expect(result1).toContain('M109 R200') # Wait for nozzle temp.
expect(result1).toContain('M140 S60') # Start heating bed.
expect(result1).toContain('M190 R60') # Wait for bed temp.
expect(result1).toContain('M106 S255') # Fan on (100% = 255).
# Second slice (should have identical pre-print sequence).
result2 = slicer.slice(mesh)
# Verify second slice has the SAME heating commands.
expect(result2).toContain('M104 S200') # Start heating nozzle.
expect(result2).toContain('M109 R200') # Wait for nozzle temp.
expect(result2).toContain('M140 S60') # Start heating bed.
expect(result2).toContain('M190 R60') # Wait for bed temp.
expect(result2).toContain('M106 S255') # Fan on (100% = 255).
# Verify slicer settings are still correct after both slices.
expect(slicer.nozzleTemperature).toBe(200)
expect(slicer.bedTemperature).toBe(60)
expect(slicer.fanSpeed).toBe(100)
describe 'G-code Precision Formatting', ->
coders = require('./coders')
describe 'formatPrecision helper', ->
test 'should format numbers to specified decimal places', ->
# Test with various precision levels.
expect(coders.formatPrecision(1.23456789, 0)).toBe('1')
expect(coders.formatPrecision(1.23456789, 1)).toBe('1.2')
expect(coders.formatPrecision(1.23456789, 2)).toBe('1.23')
expect(coders.formatPrecision(1.23456789, 3)).toBe('1.235')
expect(coders.formatPrecision(1.23456789, 5)).toBe('1.23457')
test 'should remove trailing zeros', ->
# Trailing zeros should be removed.
expect(coders.formatPrecision(1.0, 3)).toBe('1')
expect(coders.formatPrecision(1.5, 3)).toBe('1.5')
expect(coders.formatPrecision(1.50, 3)).toBe('1.5')
expect(coders.formatPrecision(1.500, 3)).toBe('1.5')
expect(coders.formatPrecision(10.0, 2)).toBe('10')
test 'should handle zero correctly', ->
expect(coders.formatPrecision(0, 0)).toBe('0')
expect(coders.formatPrecision(0, 3)).toBe('0')
expect(coders.formatPrecision(0.0, 5)).toBe('0')
test 'should handle negative numbers', ->
expect(coders.formatPrecision(-1.23456, 2)).toBe('-1.23')
expect(coders.formatPrecision(-5.0, 3)).toBe('-5')
expect(coders.formatPrecision(-0.001, 3)).toBe('-0.001')
test 'should round correctly', ->
# Test rounding behavior.
expect(coders.formatPrecision(1.235, 2)).toBe('1.24')
expect(coders.formatPrecision(1.234, 2)).toBe('1.23')
expect(coders.formatPrecision(1.995, 2)).toBe('2')
expect(coders.formatPrecision(0.0005, 3)).toBe('0.001')
test 'should handle invalid input', ->
# Invalid inputs should be returned as-is.
expect(coders.formatPrecision(NaN, 3)).toBe(NaN)
expect(coders.formatPrecision(null, 3)).toBe(null)
expect(coders.formatPrecision(undefined, 3)).toBe(undefined)
describe 'Default precision settings', ->
test 'should use default precision values', ->
expect(slicer.getCoordinatePrecision()).toBe(3)
expect(slicer.getExtrusionPrecision()).toBe(5)
expect(slicer.getFeedratePrecision()).toBe(0)
test 'should format coordinates with 3 decimals by default', ->
gcode = coders.codeLinearMovement(slicer, 100.123456789, 200.987654321, 0.001)
expect(gcode).toContain('X100.123')
expect(gcode).toContain('Y200.988')
expect(gcode).toContain('Z0.001')
test 'should format extrusion with 5 decimals by default', ->
gcode = coders.codeLinearMovement(slicer, 100, 100, 0.2, 1.23456789)
expect(gcode).toContain('E1.23457')
test 'should format feedrate with 0 decimals by default', ->
gcode = coders.codeLinearMovement(slicer, 100, 100, 0.2, null, 1800.567)
expect(gcode).toContain('F1801')
describe 'Custom precision settings', ->
test 'should accept custom coordinate precision', ->
slicer.setCoordinatePrecision(2)
expect(slicer.getCoordinatePrecision()).toBe(2)
gcode = coders.codeLinearMovement(slicer, 100.123456, 200.987654, 0.001)
expect(gcode).toContain('X100.12')
expect(gcode).toContain('Y200.99')
expect(gcode).toContain('Z0')
test 'should accept custom extrusion precision', ->
slicer.setExtrusionPrecision(3)
expect(slicer.getExtrusionPrecision()).toBe(3)
gcode = coders.codeLinearMovement(slicer, 100, 100, 0.2, 1.23456789)
expect(gcode).toContain('E1.235')
test 'should accept custom feedrate precision', ->
slicer.setFeedratePrecision(2)
expect(slicer.getFeedratePrecision()).toBe(2)
gcode = coders.codeLinearMovement(slicer, 100, 100, 0.2, null, 1800.567)
expect(gcode).toContain('F1800.57')
test 'should validate precision range (0-10)', ->
# Valid range.
slicer.setCoordinatePrecision(0)
expect(slicer.getCoordinatePrecision()).toBe(0)
slicer.setCoordinatePrecision(10)
expect(slicer.getCoordinatePrecision()).toBe(10)
# Invalid values should be ignored.
slicer.setCoordinatePrecision(15)
expect(slicer.getCoordinatePrecision()).toBe(10) # Should remain at last valid value.
slicer.setCoordinatePrecision(-1)
expect(slicer.getCoordinatePrecision()).toBe(10) # Should remain at last valid value.
test 'should floor non-integer precision values', ->
slicer.setCoordinatePrecision(3.7)
expect(slicer.getCoordinatePrecision()).toBe(3)
slicer.setExtrusionPrecision(5.2)
expect(slicer.getExtrusionPrecision()).toBe(5)
describe 'Method chaining', ->
test 'should support method chaining for precision setters', ->
result = slicer
.setCoordinatePrecision(2)
.setExtrusionPrecision(4)
.setFeedratePrecision(1)