-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPlanRegistrationHelperTests.cs
More file actions
880 lines (824 loc) · 42 KB
/
PlanRegistrationHelperTests.cs
File metadata and controls
880 lines (824 loc) · 42 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
using System;
using Microting.TimePlanningBase.Infrastructure.Data.Entities;
using NUnit.Framework;
using TimePlanning.Pn.Infrastructure.Helpers;
namespace TimePlanning.Pn.Test;
[TestFixture]
public class PlanRegistrationHelperTests
{
[TestCase(DayOfWeek.Monday, 180, 30, 2, 60, 12, 96, 13)]
[TestCase(DayOfWeek.Tuesday, 180, 30, 2, 60, 24, 30, 0)]
[TestCase(DayOfWeek.Wednesday, 180, 30, 2, 60, 96, 192, 13)]
[TestCase(DayOfWeek.Thursday, 180, 30, 2, 60, 96, 132, 7)]
[TestCase(DayOfWeek.Friday, 180, 30, 2, 60, 96, 132, 7)]
[TestCase(DayOfWeek.Saturday, 120, 30, 2, 60, 96, 132, 7)]
[TestCase(DayOfWeek.Sunday, 120, 30, 2, 60, 96, 192, 13)]
public void CalculatePauseAutoBreakCalculationActive_SetsPause1Id_Correctly(
DayOfWeek dayOfWeek,
int breakMinutesDivider,
int breakMinutesPrDivider,
int minutesActualAtWorkDivisor,
int breakMinutesUpperLimit,
int startId,
int stopId,
int expectedPause1Id)
{
// Arrange
var assignedSite = new AssignedSite
{
AutoBreakCalculationActive = true,
MondayBreakMinutesDivider = breakMinutesDivider,
MondayBreakMinutesPrDivider = breakMinutesPrDivider,
MondayBreakMinutesUpperLimit = breakMinutesUpperLimit,
TuesdayBreakMinutesDivider = breakMinutesDivider,
TuesdayBreakMinutesPrDivider = breakMinutesPrDivider,
TuesdayBreakMinutesUpperLimit = breakMinutesUpperLimit,
WednesdayBreakMinutesDivider = breakMinutesDivider,
WednesdayBreakMinutesPrDivider = breakMinutesPrDivider,
WednesdayBreakMinutesUpperLimit = breakMinutesUpperLimit,
ThursdayBreakMinutesDivider = breakMinutesDivider,
ThursdayBreakMinutesPrDivider = breakMinutesPrDivider,
ThursdayBreakMinutesUpperLimit = breakMinutesUpperLimit,
FridayBreakMinutesDivider = breakMinutesDivider,
FridayBreakMinutesPrDivider = breakMinutesPrDivider,
FridayBreakMinutesUpperLimit = breakMinutesUpperLimit,
SaturdayBreakMinutesDivider = breakMinutesDivider,
SaturdayBreakMinutesPrDivider = breakMinutesPrDivider,
SaturdayBreakMinutesUpperLimit = breakMinutesUpperLimit,
SundayBreakMinutesDivider = breakMinutesDivider,
SundayBreakMinutesPrDivider = breakMinutesPrDivider,
SundayBreakMinutesUpperLimit = breakMinutesUpperLimit,
};
var planning = new PlanRegistration
{
Date = DateTime.Today.AddDays(dayOfWeek - DateTime.Today.DayOfWeek),
Start1Id = startId,
Stop1Id = stopId,
Start2Id = 0,
Stop2Id = 0,
Start3Id = 0,
Stop3Id = 0,
Start4Id = 0,
Stop4Id = 0,
Start5Id = 0,
Stop5Id = 0
};
// Calculate expected Pause1Id
var minutesActualAtWork = (stopId - startId) * 5;
var numberOfBreaks = minutesActualAtWork / breakMinutesDivider;
var breakTime = numberOfBreaks * breakMinutesPrDivider;
// var expectedPause1Id = breakTime < breakMinutesUpperLimit ? breakTime : breakMinutesUpperLimit;
// Act
var result = PlanRegistrationHelper.CalculatePauseAutoBreakCalculationActive(assignedSite, planning);
// Assert
// Assert.That(expectedPause1Id, result.Pause1Id, $"Failed for {dayOfWeek}");
Assert.That(result.Pause1Id, Is.EqualTo(expectedPause1Id), $"Failed for {dayOfWeek}");
}
/// <summary>
/// Phase 0 plumbing test: proves that toggling
/// <see cref="AssignedSite.UseOneMinuteIntervals"/> on/off has no observable
/// effect today. The new if-branch in
/// <see cref="PlanRegistrationHelper.CalculatePauseAutoBreakCalculationActive"/>
/// is a TODO stub that falls through to the existing 5-minute logic, so
/// pause IDs must be byte-identical between flag-on and flag-off runs.
/// </summary>
[Test]
public void CalculatePauseAutoBreakCalculationActive_FlagOnAndFlagOff_ReturnSamePauseId()
{
// Arrange — identical seed for both runs except for the flag.
AssignedSite BuildAssignedSite(bool useOneMinuteIntervals) => new()
{
UseOneMinuteIntervals = useOneMinuteIntervals,
AutoBreakCalculationActive = true,
MondayBreakMinutesDivider = 180,
MondayBreakMinutesPrDivider = 30,
MondayBreakMinutesUpperLimit = 60,
TuesdayBreakMinutesDivider = 180,
TuesdayBreakMinutesPrDivider = 30,
TuesdayBreakMinutesUpperLimit = 60,
WednesdayBreakMinutesDivider = 180,
WednesdayBreakMinutesPrDivider = 30,
WednesdayBreakMinutesUpperLimit = 60,
ThursdayBreakMinutesDivider = 180,
ThursdayBreakMinutesPrDivider = 30,
ThursdayBreakMinutesUpperLimit = 60,
FridayBreakMinutesDivider = 180,
FridayBreakMinutesPrDivider = 30,
FridayBreakMinutesUpperLimit = 60,
SaturdayBreakMinutesDivider = 120,
SaturdayBreakMinutesPrDivider = 30,
SaturdayBreakMinutesUpperLimit = 60,
SundayBreakMinutesDivider = 120,
SundayBreakMinutesPrDivider = 30,
SundayBreakMinutesUpperLimit = 60,
};
PlanRegistration BuildPlanning() => new()
{
Date = DateTime.Today.AddDays(DayOfWeek.Wednesday - DateTime.Today.DayOfWeek),
Start1Id = 96,
Stop1Id = 192,
Start2Id = 0,
Stop2Id = 0,
Start3Id = 0,
Stop3Id = 0,
Start4Id = 0,
Stop4Id = 0,
Start5Id = 0,
Stop5Id = 0
};
// Act
var resultFlagOff =
PlanRegistrationHelper.CalculatePauseAutoBreakCalculationActive(BuildAssignedSite(false), BuildPlanning());
var resultFlagOn =
PlanRegistrationHelper.CalculatePauseAutoBreakCalculationActive(BuildAssignedSite(true), BuildPlanning());
// Assert — every pause ID is byte-identical between the two runs.
Assert.Multiple(() =>
{
Assert.That(resultFlagOn.Pause1Id, Is.EqualTo(resultFlagOff.Pause1Id), "Pause1Id must match flag-off run");
Assert.That(resultFlagOn.Pause2Id, Is.EqualTo(resultFlagOff.Pause2Id), "Pause2Id must match flag-off run");
Assert.That(resultFlagOn.Pause3Id, Is.EqualTo(resultFlagOff.Pause3Id), "Pause3Id must match flag-off run");
Assert.That(resultFlagOn.Pause4Id, Is.EqualTo(resultFlagOff.Pause4Id), "Pause4Id must match flag-off run");
Assert.That(resultFlagOn.Pause5Id, Is.EqualTo(resultFlagOff.Pause5Id), "Pause5Id must match flag-off run");
});
}
/// <summary>
/// Phase 0 plumbing test: the new 2-arg
/// <see cref="PlanRegistrationHelper.RecalculatePlanHoursFromShifts(PlanRegistration, bool)"/>
/// overload must delegate to the existing 1-arg version regardless of
/// the <c>useOneMinuteIntervals</c> flag, because planned-shift precision
/// stays minute-only per the rollout plan. So both flag values plus the
/// 1-arg call must produce identical PlanHours / PlanHoursInSeconds.
/// </summary>
[Test]
public void RecalculatePlanHoursFromShifts_FlagOnAndFlagOff_ReturnSamePlanHours()
{
// Arrange — three identical PlanRegistrations exercised down three paths.
PlanRegistration BuildPlan() => new()
{
PlannedStartOfShift1 = 420,
PlannedEndOfShift1 = 900,
PlannedBreakOfShift1 = 30,
};
var prOneArg = BuildPlan();
var prFlagOff = BuildPlan();
var prFlagOn = BuildPlan();
// Act
PlanRegistrationHelper.RecalculatePlanHoursFromShifts(prOneArg);
PlanRegistrationHelper.RecalculatePlanHoursFromShifts(prFlagOff, useOneMinuteIntervals: false);
PlanRegistrationHelper.RecalculatePlanHoursFromShifts(prFlagOn, useOneMinuteIntervals: true);
// Assert — all three paths produce identical totals.
Assert.Multiple(() =>
{
Assert.That(prFlagOff.PlanHours, Is.EqualTo(prOneArg.PlanHours),
"Flag-off 2-arg path must match 1-arg path");
Assert.That(prFlagOff.PlanHoursInSeconds, Is.EqualTo(prOneArg.PlanHoursInSeconds),
"Flag-off 2-arg PlanHoursInSeconds must match 1-arg");
Assert.That(prFlagOn.PlanHours, Is.EqualTo(prOneArg.PlanHours),
"Flag-on 2-arg path must match 1-arg path");
Assert.That(prFlagOn.PlanHoursInSeconds, Is.EqualTo(prOneArg.PlanHoursInSeconds),
"Flag-on 2-arg PlanHoursInSeconds must match 1-arg");
});
}
/// <summary>
/// Phase 0 plumbing test for the new
/// <c>RoundDownToNearestFiveMinutesAndFormat(DateTime, int, bool)</c>
/// overload on <c>TimePlanningWorkingHoursService</c>. The helper is
/// <c>private static</c> and there is no <c>InternalsVisibleTo</c>
/// attribute, so the only public consumer today is
/// <c>TimePlanningWorkingHoursService.ReadSimple</c>, which requires
/// the full SDK / DB fixture (sdkContext, baseDbContext, userService,
/// currentUser, sdkSiteWorker, etc.) that this test fixture does not
/// yet wire up. Per the rollout plan, mirror the existing
/// <c>[Ignore]</c> carve-out used elsewhere for pre-fixture tests so
/// the assertions are captured for future fixture work without
/// blocking Phase 0 CI. The intent is identical to the two tests
/// above: flag-on and flag-off must produce byte-identical strings
/// in Phase 0 because the new overload simply delegates to the
/// existing 2-arg helper.
/// </summary>
[Test]
[Ignore("Phase 0 carve-out: ReadSimple requires SDK/DB fixture not wired here; assertion captured for future fixture work.")]
public void RoundDownToNearestFiveMinutesAndFormat_FlagOnAndFlagOff_ReturnSameString()
{
// Arrange / Act / Assert (intent, to be enabled when fixture lands):
//
// var midnight = new DateTime(2026, 1, 5, 0, 0, 0);
// var resultFlagOff = service.ReadSimpleForTest(midnight, useOneMinuteIntervals: false);
// var resultFlagOn = service.ReadSimpleForTest(midnight, useOneMinuteIntervals: true);
// Assert.That(resultFlagOn.Start1StartedAt, Is.EqualTo(resultFlagOff.Start1StartedAt));
//
// Phase 0 contract: the new 3-arg private helper is a pass-through
// to the existing 2-arg helper, so all formatted strings must match
// byte-for-byte regardless of the flag.
Assert.Pass("Captured for future fixture work; see XML doc above.");
}
// ------------------------------------------------------------------
// Phase 1 — server-side write path: prefer DateTime stamps when flag on
// ------------------------------------------------------------------
//
// The fork lives in
// PlanRegistrationHelper.UpdatePlanRegistrationsInPeriod(...)
// around the `if (planRegistration.Start1Id > 289)` block. When the
// `Start1Id > 289` workaround divides the value back down, the
// existing path also overwrites `Start1StartedAt` from the snapped
// index (`Date.AddMinutes(Start1Id * 5)`). When
// `AssignedSite.UseOneMinuteIntervals` is true AND a precise stamp is
// already populated, that overwrite is skipped — the precise stamp is
// the source of truth in Phase 1.
//
// `UpdatePlanRegistrationsInPeriod` is async and takes
// `(planningsInPeriod, siteModel, dbContext, dbAssignedSite, logger,
// site, midnightOfDateFrom, midnightOfDateTo, options)`. Wiring those
// up requires the full DB / SDK / IPluginDbOptions / Site fixture
// that this `PlanRegistrationHelperTests` fixture deliberately keeps
// lightweight; per the rollout plan this is the documented carve-out
// pattern (see RoundDownToNearestFiveMinutesAndFormat above). The
// assertions below are captured here so that whoever lands the
// fuller fixture (or chooses to extend `PlanningServiceMultiShiftTests`
// / `PlanRegistrationHelperReadBySiteAndDateTests`) can flip the
// `[Ignore]` off without rewriting the contract.
/// <summary>
/// Phase 1 contract: when <c>UseOneMinuteIntervals</c> is on and a
/// precise <c>Start1StartedAt</c> stamp already lives on the row, the
/// `Start1Id > 289` workaround in
/// <see cref="PlanRegistrationHelper.UpdatePlanRegistrationsInPeriod"/>
/// must NOT overwrite the precise stamp with the 5-minute snap.
/// </summary>
[Test]
[Ignore("Phase 1 carve-out: UpdatePlanRegistrationsInPeriod requires logger/options/Site fixture not wired here; assertion captured for future fixture work.")]
public void UpdatePlanRegistrationsInPeriod_FlagOn_PreservesStartedAt()
{
// Arrange (intent, to be enabled when fixture lands):
//
// var date = new DateTime(2026, 5, 15, 0, 0, 0);
// var preciseStart = new DateTime(2026, 5, 15, 7, 3, 53);
//
// var dbAssignedSite = new AssignedSite { UseOneMinuteIntervals = true,
// Resigned = false,
// UseGoogleSheetAsDefault = false,
// SiteId = 900 };
// await dbAssignedSite.Create(TimePlanningPnDbContext);
//
// // Start1Id > 289 is required to enter the workaround branch.
// // 504 / (5+1) = 84 → AddMinutes(84*5) = +420min = 07:00 (the snap).
// var planning = new PlanRegistration {
// SdkSitId = 900,
// Date = date,
// Start1Id = 504,
// Start1StartedAt = preciseStart,
// Stop1Id = 0,
// };
// await planning.Create(TimePlanningPnDbContext);
//
// // Act
// await PlanRegistrationHelper.UpdatePlanRegistrationsInPeriod(
// new List<PlanRegistration> { new() { Id = planning.Id, Date = date } },
// new TimePlanningPlanningModel(), TimePlanningPnDbContext,
// dbAssignedSite, logger, site, date, date, options);
//
// // Assert — flag-on path keeps the precise 07:03:53 stamp.
// var reloaded = await TimePlanningPnDbContext.PlanRegistrations
// .AsNoTracking().FirstAsync(x => x.Id == planning.Id);
// Assert.That(reloaded.Start1StartedAt, Is.EqualTo(preciseStart));
Assert.Pass("Captured for future fixture work; see XML doc above.");
}
/// <summary>
/// Phase 1 regression-guard: with the flag OFF, the existing backfill
/// behavior must be byte-identical to before — `Start1StartedAt` is
/// recomputed from the corrected `Start1Id` index even if a precise
/// stamp had been seeded on the row.
/// </summary>
[Test]
[Ignore("Phase 1 carve-out: UpdatePlanRegistrationsInPeriod requires logger/options/Site fixture not wired here; assertion captured for future fixture work.")]
public void UpdatePlanRegistrationsInPeriod_FlagOff_BackfillsStartedAt_AsBefore()
{
// Arrange (intent, to be enabled when fixture lands):
//
// var date = new DateTime(2026, 5, 15, 0, 0, 0);
// var preciseStart = new DateTime(2026, 5, 15, 7, 3, 53);
// var expectedSnap = new DateTime(2026, 5, 15, 7, 0, 0);
//
// var dbAssignedSite = new AssignedSite { UseOneMinuteIntervals = false,
// Resigned = false,
// UseGoogleSheetAsDefault = false,
// SiteId = 901 };
//
// var planning = new PlanRegistration {
// SdkSitId = 901,
// Date = date,
// Start1Id = 504,
// Start1StartedAt = preciseStart,
// };
//
// // Act — same call as the flag-on test, just flag flipped off.
//
// // Assert — flag-off path overwrites Start1StartedAt to the 5-min snap.
// Assert.That(reloaded.Start1StartedAt, Is.EqualTo(expectedSnap));
Assert.Pass("Captured for future fixture work; see XML doc above.");
}
/// <summary>
/// Phase 1 fallback: with the flag ON but no precise stamp populated
/// (e.g. a legacy row without observed punch data), the existing
/// backfill must still run so the row gets a sensible
/// `Start1StartedAt` derived from the int index.
/// </summary>
[Test]
[Ignore("Phase 1 carve-out: UpdatePlanRegistrationsInPeriod requires logger/options/Site fixture not wired here; assertion captured for future fixture work.")]
public void UpdatePlanRegistrationsInPeriod_FlagOnButStartedAtNull_FallsBackToBackfill()
{
// Arrange (intent, to be enabled when fixture lands):
//
// var date = new DateTime(2026, 5, 15, 0, 0, 0);
// var expectedSnap = new DateTime(2026, 5, 15, 7, 0, 0);
//
// var dbAssignedSite = new AssignedSite { UseOneMinuteIntervals = true,
// Resigned = false,
// UseGoogleSheetAsDefault = false,
// SiteId = 902 };
//
// var planning = new PlanRegistration {
// SdkSitId = 902,
// Date = date,
// Start1Id = 504,
// Start1StartedAt = null, // legacy row, no precise stamp
// };
//
// // Act — flag is on, but since StartedAt is null the helper falls
// // through to the existing backfill so the row stays usable.
//
// // Assert — backfill ran exactly as in the flag-off path.
// Assert.That(reloaded.Start1StartedAt, Is.EqualTo(expectedSnap));
Assert.Pass("Captured for future fixture work; see XML doc above.");
}
/// <summary>
/// Phase 1 contract for the auto-break write path
/// (<see cref="PlanRegistrationHelper.CalculatePauseAutoBreakCalculationActive"/>).
/// Auto-break pauses are COMPUTED from day-of-week rules, not OBSERVED,
/// so even with <c>UseOneMinuteIntervals</c> on we deliberately do NOT
/// fabricate `Pause1StartedAt` / `Pause1StoppedAt` timestamps — there
/// is no real-world signal anchoring them. The legacy `Pause1Id` int
/// (in 5-minute multiples) stays the source of truth and therefore
/// must agree byte-for-byte with the flag-off path. DateTime pause
/// fields are reserved for OBSERVED pauses written via
/// `UpdateWorkingHour`.
/// </summary>
[Test]
public void CalculatePauseAutoBreakCalculationActive_FlagOn_LegacyAndDateTimeFieldsAgree()
{
// Arrange — identical seed for both runs except for the flag.
AssignedSite BuildAssignedSite(bool useOneMinuteIntervals) => new()
{
UseOneMinuteIntervals = useOneMinuteIntervals,
AutoBreakCalculationActive = true,
// Wednesday rule: every 180 min worked → 30 min pause, capped at 60.
WednesdayBreakMinutesDivider = 180,
WednesdayBreakMinutesPrDivider = 30,
WednesdayBreakMinutesUpperLimit = 60,
};
// 96..192 in 5-min ticks = 8:00..16:00 = 480 min worked.
// 480 / 180 = 2 breaks; 2 * 30 = 60 min ≤ 60 cap → Pause1Id = 60.
// After the trailing `/ 5 + 1` step that's `60 / 5 + 1 = 13`.
PlanRegistration BuildPlanning(DateTime? preciseStart, DateTime? preciseStop) => new()
{
Date = DateTime.Today.AddDays(DayOfWeek.Wednesday - DateTime.Today.DayOfWeek),
Start1Id = 96,
Stop1Id = 192,
Start1StartedAt = preciseStart,
Stop1StoppedAt = preciseStop,
};
var preciseStart = new DateTime(2026, 5, 13, 8, 0, 17);
var preciseStop = new DateTime(2026, 5, 13, 16, 0, 41);
// Act
var resultFlagOff = PlanRegistrationHelper.CalculatePauseAutoBreakCalculationActive(
BuildAssignedSite(false), BuildPlanning(preciseStart, preciseStop));
var resultFlagOn = PlanRegistrationHelper.CalculatePauseAutoBreakCalculationActive(
BuildAssignedSite(true), BuildPlanning(preciseStart, preciseStop));
Assert.Multiple(() =>
{
// Legacy int field is byte-identical between flag-on and flag-off.
Assert.That(resultFlagOn.Pause1Id, Is.EqualTo(resultFlagOff.Pause1Id),
"Pause1Id must match flag-off run (auto-break is computed, not observed)");
Assert.That(resultFlagOn.Pause1Id, Is.EqualTo(13),
"Pause1Id must remain 13 (60 min cap → /5+1 = 13)");
// Phase 1 deliberately does NOT fabricate DateTime pause stamps
// for an auto-computed pause; both runs leave them null.
Assert.That(resultFlagOn.Pause1StartedAt, Is.Null,
"Auto-break must not invent a Pause1StartedAt when flag on");
Assert.That(resultFlagOn.Pause1StoppedAt, Is.Null,
"Auto-break must not invent a Pause1StoppedAt when flag on");
Assert.That(resultFlagOff.Pause1StartedAt, Is.Null,
"Flag-off path must remain unchanged: no Pause1StartedAt");
Assert.That(resultFlagOff.Pause1StoppedAt, Is.Null,
"Flag-off path must remain unchanged: no Pause1StoppedAt");
// Sanity: if any DateTime fields WERE set in some future phase,
// they would need to agree with `Pause1Id * 5` minutes when
// anchored on Start1StartedAt. For Phase 1, both null is the
// documented expectation.
});
}
// ------------------------------------------------------------------
// Phase 2 — server-side calculations from DateTime + *InSeconds
// ------------------------------------------------------------------
//
// The fork lives in two new helpers on PlanRegistrationHelper:
// - ComputeNettoSecondsFromDateTimeShifts(PlanRegistration)
// - ApplyNettoFlexChainSecondPrecision(PlanRegistration, int, bool)
//
// When UseOneMinuteIntervals is on, every NettoHours/Flex/SumFlex compute
// site forks to these helpers; the *InSeconds int columns become the
// source of truth and the legacy double hour fields are back-derived as
// xField = xFieldInSeconds / 3600.0
//
// The flag-off path stays byte-identical to before — no edits to the
// existing 5-minute math.
/// <summary>
/// Phase 2 contract: with the flag ON and DateTime stamps populated for
/// shift 1 plus a legacy <c>Pause1Id</c> (no DateTime pause stamps), the
/// netto-seconds computation derives the work span from
/// <c>(Stop1StoppedAt - Start1StartedAt)</c> in seconds and subtracts
/// the legacy pause snap of <c>(Pause1Id - 1) * 5 * 60</c> seconds.
///
/// Seed: Start1StartedAt = 2026-05-15 07:03:53,
/// Stop1StoppedAt = 2026-05-15 15:30:11,
/// Pause1Id = 6 (= 25 min legacy snap = 1500 s).
/// Expected work seconds: 15:30:11 - 07:03:53 = 8 h 26 m 18 s = 30378 s.
/// Expected netto seconds: 30378 - 1500 = 28878 s.
/// </summary>
[Test]
public void NettoHours_FlagOn_DerivedFromDateTimeDeltasInSeconds()
{
var pr = new PlanRegistration
{
Date = new DateTime(2026, 5, 15, 0, 0, 0),
Start1Id = 0,
Stop1Id = 0,
Start1StartedAt = new DateTime(2026, 5, 15, 7, 3, 53),
Stop1StoppedAt = new DateTime(2026, 5, 15, 15, 30, 11),
Pause1Id = 6,
Pause1StartedAt = null,
Pause1StoppedAt = null,
};
var nettoSeconds = PlanRegistrationHelper.ComputeNettoSecondsFromDateTimeShifts(pr);
Assert.Multiple(() =>
{
Assert.That(nettoSeconds, Is.EqualTo(28878L),
"Netto seconds must be (Stop-Start)=30378 minus legacy pause 1500 = 28878");
Assert.That(nettoSeconds / 3600.0, Is.EqualTo(28878 / 3600.0).Within(0.0001),
"NettoHours back-derived from seconds = 28878/3600 ≈ 8.0217");
});
}
/// <summary>
/// Phase 2 regression-guard: the new compute helper handles the
/// pure-DateTime-pause path correctly. Seed both pause stamps and
/// confirm pause is computed from <c>(Pause1StoppedAt - Pause1StartedAt)</c>
/// in seconds, not from the legacy <c>Pause1Id</c> snap.
/// </summary>
[Test]
public void NettoHours_FlagOn_PauseDateTimeBeatsPauseId()
{
var pr = new PlanRegistration
{
Date = new DateTime(2026, 5, 15, 0, 0, 0),
Start1StartedAt = new DateTime(2026, 5, 15, 8, 0, 0),
Stop1StoppedAt = new DateTime(2026, 5, 15, 16, 0, 0),
// Legacy snap: would be 5 min; precise pause is 27 s.
Pause1Id = 2,
Pause1StartedAt = new DateTime(2026, 5, 15, 12, 0, 0),
Pause1StoppedAt = new DateTime(2026, 5, 15, 12, 0, 27),
};
var nettoSeconds = PlanRegistrationHelper.ComputeNettoSecondsFromDateTimeShifts(pr);
// 8h work = 28800 s; pause from DateTime = 27 s; netto = 28773 s.
Assert.That(nettoSeconds, Is.EqualTo(28773L),
"DateTime pause must trump legacy Pause1Id snap when both populated");
}
/// <summary>
/// Phase 2 chain test: with the flag ON, FlexInSeconds and
/// SumFlexEndInSeconds are derived from NettoHoursInSeconds via
/// the same formula as the flag-off path but in seconds:
/// FlexInSeconds = NettoHoursInSeconds - PlanHoursInSeconds
/// SumFlexEndInSeconds = SumFlexStartInSeconds + FlexInSeconds
/// - PaiedOutFlexInSeconds (when preceded)
/// The doubles are back-derived as xInSeconds / 3600.0.
///
/// Seed mirrors the NettoHours test: 28878 netto seconds (~8.022 h),
/// PlanHours = 8.0 ⇒ PlanHoursInSeconds = 28800. Carries
/// SumFlexStartInSeconds = 1800 (30 min). PaiedOutFlexInSeconds = 0.
/// Expected: FlexInSeconds = 78, SumFlexEndInSeconds = 1878.
/// </summary>
[Test]
public void FlexAndSumFlex_FlagOn_DerivedFromInSecondsChain()
{
var pr = new PlanRegistration
{
Date = new DateTime(2026, 5, 15, 0, 0, 0),
Start1StartedAt = new DateTime(2026, 5, 15, 7, 3, 53),
Stop1StoppedAt = new DateTime(2026, 5, 15, 15, 30, 11),
Pause1Id = 6,
PlanHours = 8.0,
PlanHoursInSeconds = 28800,
PaiedOutFlex = 0,
PaiedOutFlexInSeconds = 0,
NettoHoursOverrideActive = false,
};
// Pretend the previous day carried 30 min of positive flex forward.
const int sumFlexStartInSeconds = 1800;
PlanRegistrationHelper.ApplyNettoFlexChainSecondPrecision(
pr, sumFlexStartInSeconds, hasPreTimePlanning: true);
Assert.Multiple(() =>
{
Assert.That(pr.NettoHoursInSeconds, Is.EqualTo(28878),
"NettoHoursInSeconds must equal computed netto seconds");
Assert.That(pr.NettoHours, Is.EqualTo(28878 / 3600.0).Within(0.0001),
"NettoHours back-derived from NettoHoursInSeconds / 3600.0");
Assert.That(pr.FlexInSeconds, Is.EqualTo(28878 - 28800),
"FlexInSeconds = NettoHoursInSeconds - PlanHoursInSeconds = 78");
Assert.That(pr.Flex, Is.EqualTo(78 / 3600.0).Within(0.0001),
"Flex back-derived from FlexInSeconds / 3600.0");
Assert.That(pr.SumFlexStartInSeconds, Is.EqualTo(1800),
"SumFlexStartInSeconds carries the running balance from previous day");
Assert.That(pr.SumFlexStart, Is.EqualTo(0.5).Within(0.0001),
"SumFlexStart back-derived = 1800 / 3600 = 0.5");
Assert.That(pr.SumFlexEndInSeconds, Is.EqualTo(1800 + 78 - 0),
"SumFlexEndInSeconds = SumFlexStartInSeconds + FlexInSeconds - PaiedOutFlexInSeconds = 1878");
Assert.That(pr.SumFlexEnd, Is.EqualTo(1878 / 3600.0).Within(0.0001),
"SumFlexEnd back-derived from SumFlexEndInSeconds / 3600.0");
});
}
/// <summary>
/// Phase 2 chain test for the no-preceding-day case. With
/// <paramref name="hasPreTimePlanning"/> = false, SumFlexStart resets
/// to 0 and SumFlexEnd is derived directly from Flex - PaiedOutFlex.
/// </summary>
[Test]
public void SumFlex_FlagOn_NoPreceding_StartsAtZero()
{
var pr = new PlanRegistration
{
Date = new DateTime(2026, 5, 15, 0, 0, 0),
Start1StartedAt = new DateTime(2026, 5, 15, 8, 0, 0),
Stop1StoppedAt = new DateTime(2026, 5, 15, 16, 0, 0),
Pause1Id = 0,
PlanHours = 8.0,
PlanHoursInSeconds = 28800,
PaiedOutFlex = 0,
PaiedOutFlexInSeconds = 0,
};
PlanRegistrationHelper.ApplyNettoFlexChainSecondPrecision(
pr, sumFlexStartInSeconds: 0, hasPreTimePlanning: false);
Assert.Multiple(() =>
{
Assert.That(pr.NettoHoursInSeconds, Is.EqualTo(28800),
"8h work, 0 pause → 28800 s netto");
Assert.That(pr.SumFlexStartInSeconds, Is.EqualTo(0));
Assert.That(pr.SumFlexStart, Is.EqualTo(0.0));
Assert.That(pr.FlexInSeconds, Is.EqualTo(0),
"Worked exactly the planned hours → 0 flex");
Assert.That(pr.SumFlexEndInSeconds, Is.EqualTo(0));
Assert.That(pr.SumFlexEnd, Is.EqualTo(0.0));
});
}
/// <summary>
/// Phase 2: when the override is active, the chain uses
/// <c>NettoHoursOverride</c> (in hours) instead of computed
/// NettoHoursInSeconds. Mirrors the flag-off override semantics
/// sign-for-sign.
/// </summary>
[Test]
public void Flex_FlagOn_OverrideActive_UsesOverrideForChain()
{
var pr = new PlanRegistration
{
Date = new DateTime(2026, 5, 15, 0, 0, 0),
Start1StartedAt = new DateTime(2026, 5, 15, 8, 0, 0),
Stop1StoppedAt = new DateTime(2026, 5, 15, 16, 0, 0),
Pause1Id = 0,
PlanHours = 8.0,
PlanHoursInSeconds = 28800,
PaiedOutFlex = 0,
PaiedOutFlexInSeconds = 0,
NettoHoursOverride = 9.5,
NettoHoursOverrideActive = true,
};
PlanRegistrationHelper.ApplyNettoFlexChainSecondPrecision(
pr, sumFlexStartInSeconds: 0, hasPreTimePlanning: false);
Assert.Multiple(() =>
{
// Computed NettoHoursInSeconds is still based on the actual
// shift (28800 s = 8 h), independent of the override.
Assert.That(pr.NettoHoursInSeconds, Is.EqualTo(28800),
"NettoHoursInSeconds always reflects actual work, not override");
// Flex uses the override: 9.5 - 8.0 = 1.5 h = 5400 s.
Assert.That(pr.FlexInSeconds, Is.EqualTo(5400),
"FlexInSeconds = NettoHoursOverride*3600 - PlanHoursInSeconds");
Assert.That(pr.Flex, Is.EqualTo(1.5).Within(0.0001));
Assert.That(pr.SumFlexEndInSeconds, Is.EqualTo(5400),
"SumFlexEndInSeconds = 0 + 5400 - 0");
});
}
/// <summary>
/// Phase 2: PaiedOutFlexInSeconds reduces SumFlexEndInSeconds, and the
/// double <c>PaiedOutFlex</c> is the back-derived equivalent.
/// </summary>
[Test]
public void PaiedOutFlex_FlagOn_DerivedFromInSeconds()
{
var pr = new PlanRegistration
{
Date = new DateTime(2026, 5, 15, 0, 0, 0),
Start1StartedAt = new DateTime(2026, 5, 15, 8, 0, 0),
Stop1StoppedAt = new DateTime(2026, 5, 15, 16, 0, 0),
Pause1Id = 0,
PlanHours = 8.0,
PlanHoursInSeconds = 28800,
// 30 min paid out as flex.
PaiedOutFlex = 0.5,
PaiedOutFlexInSeconds = 1800,
};
PlanRegistrationHelper.ApplyNettoFlexChainSecondPrecision(
pr, sumFlexStartInSeconds: 0, hasPreTimePlanning: false);
Assert.Multiple(() =>
{
// Flex itself is 0 (worked exactly 8 h on a 8 h plan).
Assert.That(pr.FlexInSeconds, Is.EqualTo(0));
// SumFlexEnd = Flex - PaiedOutFlex = 0 - 1800 = -1800 s.
Assert.That(pr.SumFlexEndInSeconds, Is.EqualTo(-1800),
"SumFlexEndInSeconds reflects PaiedOutFlexInSeconds being deducted");
Assert.That(pr.SumFlexEnd, Is.EqualTo(-0.5).Within(0.0001),
"SumFlexEnd back-derived = -1800 / 3600 = -0.5");
// Double PaiedOutFlex == PaiedOutFlexInSeconds / 3600.0 is the
// intended invariant; verify the seed honors it.
Assert.That(pr.PaiedOutFlex, Is.EqualTo(pr.PaiedOutFlexInSeconds / 3600.0).Within(0.0001),
"PaiedOutFlex must equal PaiedOutFlexInSeconds / 3600.0");
});
}
/// <summary>
/// Phase 2 regression-guard for the flag-OFF path: the new helpers
/// are only invoked when <c>UseOneMinuteIntervals</c> is true; the
/// flag-off path stays byte-identical to current main. We verify that
/// by NOT calling the new helpers and asserting that a freshly-seeded
/// row's <c>NettoHoursInSeconds</c> stays at 0 — i.e. the helpers
/// don't run as a side effect of construction.
/// </summary>
[Test]
public void NettoHours_FlagOff_NewHelperNotInvoked()
{
var pr = new PlanRegistration
{
Date = new DateTime(2026, 5, 15, 0, 0, 0),
Start1StartedAt = new DateTime(2026, 5, 15, 7, 3, 53),
Stop1StoppedAt = new DateTime(2026, 5, 15, 15, 30, 11),
Pause1Id = 6,
};
// Without invoking ApplyNettoFlexChainSecondPrecision, the *InSeconds
// columns must remain at the entity defaults — proving the helper
// is the only writer in the flag-on path and the flag-off code
// hasn't been changed to invoke it implicitly.
Assert.Multiple(() =>
{
Assert.That(pr.NettoHoursInSeconds, Is.EqualTo(0));
Assert.That(pr.FlexInSeconds, Is.EqualTo(0));
Assert.That(pr.SumFlexStartInSeconds, Is.EqualTo(0));
Assert.That(pr.SumFlexEndInSeconds, Is.EqualTo(0));
});
}
/// <summary>
/// Phase 2 carve-out for service-level integration tests of the
/// <see cref="TimePlanning.Pn.Services.TimePlanningWorkingHoursService.TimePlanningWorkingHoursService.Index"/>
/// recompute. The <c>Index</c> endpoint requires the full SDK / DB /
/// userService / coreHelper / options / localizationService fixture
/// that this lightweight <c>PlanRegistrationHelperTests</c> file
/// deliberately does not wire up. Per the rollout plan the assertion
/// is captured here for future fixture work — same pattern as the
/// Phase 0 / Phase 1 carve-outs above.
/// </summary>
[Test]
[Ignore("Phase 2 carve-out: TimePlanningWorkingHoursService.Index requires SDK/DB/options fixture not wired here; assertion captured for future fixture work.")]
public void Index_FlagOn_DerivedFieldsConsistent()
{
// Arrange (intent, to be enabled when fixture lands):
//
// var assignedSite = new AssignedSite { UseOneMinuteIntervals = true,
// SiteId = 950 };
// await assignedSite.Create(TimePlanningPnDbContext);
//
// var planning = new PlanRegistration {
// SdkSitId = 950,
// Date = new DateTime(2026, 5, 15, 0, 0, 0),
// Start1StartedAt = new DateTime(2026, 5, 15, 7, 3, 53),
// Stop1StoppedAt = new DateTime(2026, 5, 15, 15, 30, 11),
// Pause1Id = 6,
// PlanHoursInSeconds = 28800,
// };
// await planning.Create(TimePlanningPnDbContext);
//
// // Act
// var result = await service.Index(new TimePlanningWorkingHoursRequestModel {
// SiteId = 950,
// DateFrom = new DateTime(2026, 5, 15, 0, 0, 0),
// DateTo = new DateTime(2026, 5, 15, 0, 0, 0),
// });
//
// // Assert — the doubles in the response must equal the *InSeconds
// // siblings divided by 3600.0 (back-derivation invariant).
// var row = result.Model.Single(x => x.Date == planning.Date);
// Assert.That(row.NettoHours, Is.EqualTo(row.NettoHoursInSeconds / 3600.0).Within(0.0001));
// Assert.That(row.FlexHours, Is.EqualTo(row.FlexInSeconds / 3600.0).Within(0.0001));
// Assert.That(row.SumFlexStart, Is.EqualTo(row.SumFlexStartInSeconds / 3600.0).Within(0.0001));
// Assert.That(row.SumFlexEnd, Is.EqualTo(row.SumFlexEndInSeconds / 3600.0).Within(0.0001));
Assert.Pass("Captured for future fixture work; see XML doc above.");
}
// ------------------------------------------------------------------
// Phase 4 — web admin display + Excel export: HH:mm:ss when flag on
// ------------------------------------------------------------------
//
// The fork lives in a private helper on
// TimePlanningWorkingHoursService.GetShiftTime(plr, shift, actualStamp, useOneMinuteIntervals)
// which is called from FillDataRow when emitting Excel cells. The
// existing 2-arg overload (Options[] lookup) is left untouched; the new
// 4-arg overload formats `actualStamp` as `HH:mm:ss` when both
// `useOneMinuteIntervals` and `actualStamp.HasValue` are true, and falls
// through to the 2-arg overload otherwise.
//
// Because the helper is `private` and the test fixture has no
// `InternalsVisibleTo` attribute, the contract is captured here as the
// documented carve-out (mirroring the Phase 0/1/2 patterns above) so
// that the assertion intent survives any future refactor that exposes
// the helper for direct testing.
/// <summary>
/// Phase 4 contract: when <c>UseOneMinuteIntervals</c> is on AND a
/// precise <c>actualStamp</c> is supplied, the new
/// <c>GetShiftTime(plr, shift, actualStamp, useOneMinuteIntervals)</c>
/// overload returns the stamp formatted as <c>HH:mm:ss</c> rather than
/// the legacy 5-minute <c>plr.Options[shift - 1]</c> lookup.
/// </summary>
[Test]
[Ignore("Phase 4 carve-out: GetShiftTime is private on TimePlanningWorkingHoursService and the fixture has no InternalsVisibleTo; assertion captured for future fixture work.")]
public void GetShiftTime_FlagOnWithActualStamp_ReturnsHHmmss()
{
// Arrange / Act / Assert (intent, to be enabled when the helper is
// exposed via InternalsVisibleTo or extracted to a testable seam):
//
// var plr = new PlanRegistration
// {
// Start1Id = 84, // 5-min idx 84 → "07:00" via Options[83]
// Start1StartedAt = new DateTime(2026, 5, 15, 7, 3, 53),
// };
// var resultFlagOn = service.GetShiftTimeForTest(plr, plr.Start1Id, plr.Start1StartedAt, useOneMinuteIntervals: true);
// var resultFlagOff = service.GetShiftTimeForTest(plr, plr.Start1Id, plr.Start1StartedAt, useOneMinuteIntervals: false);
//
// Assert.That(resultFlagOn, Is.EqualTo("07:03:53"));
// Assert.That(resultFlagOff, Is.EqualTo("07:00")); // Options[83] = legacy snap
Assert.Pass("Captured for future fixture work; see XML doc above.");
}
/// <summary>
/// Phase 4 contract: the Excel dashboard export
/// (<c>GenerateExcelDashboard</c>) emits <c>HH:mm:ss</c> string cells
/// for shift start/stop columns when the row's <c>AssignedSite</c> has
/// <c>UseOneMinuteIntervals</c> on, sourced from the precise
/// <c>Start1StartedAt</c>/<c>Stop1StoppedAt</c> stamps. With the flag
/// off, cell values must remain byte-identical to the legacy 5-minute
/// <c>Options[]</c> lookup.
/// </summary>
[Test]
[Ignore("Phase 4 carve-out: GenerateExcelDashboard requires the full Excel/SDK/DB fixture not wired here; assertion captured for future fixture work.")]
public void GenerateExcelDashboard_FlagOn_FormatsShiftCellsAsHHmmss()
{
// Arrange / Act / Assert (intent, to be enabled when fixture lands):
//
// var assignedSite = new AssignedSite { SiteId = 950, UseOneMinuteIntervals = true };
// var planning = new PlanRegistration
// {
// SdkSitId = 950,
// Date = new DateTime(2026, 5, 15, 0, 0, 0),
// Start1Id = 84,
// Stop1Id = 187,
// Start1StartedAt = new DateTime(2026, 5, 15, 7, 3, 53),
// Stop1StoppedAt = new DateTime(2026, 5, 15, 15, 30, 11),
// PlanHours = 8.0,
// };
// await assignedSite.Create(TimePlanningPnDbContext);
// await planning.Create(TimePlanningPnDbContext);
//
// var stream = await service.GenerateExcelDashboard(new TimePlanningWorkingHoursRequestModel
// {
// SiteId = 950,
// DateFrom = new DateTime(2026, 5, 15, 0, 0, 0),
// DateTo = new DateTime(2026, 5, 15, 0, 0, 0),
// });
//
// using var pkg = new ExcelPackage(stream);
// var sheet = pkg.Workbook.Worksheets.First();
// // Shift1Start column is the 8th cell on data row 2 (1-indexed).
// Assert.That(sheet.Cells[2, 8].Text, Is.EqualTo("07:03:53"));
// Assert.That(sheet.Cells[2, 9].Text, Is.EqualTo("15:30:11"));
//
// // Flip the flag back off → cells must show the legacy 5-min Options[] strings.
// assignedSite.UseOneMinuteIntervals = false;
// await assignedSite.Update(TimePlanningPnDbContext);
// var stream2 = await service.GenerateExcelDashboard(...);
// using var pkg2 = new ExcelPackage(stream2);
// Assert.That(pkg2.Workbook.Worksheets.First().Cells[2, 8].Text, Is.EqualTo("07:00"));
// Assert.That(pkg2.Workbook.Worksheets.First().Cells[2, 9].Text, Is.EqualTo("15:30"));
Assert.Pass("Captured for future fixture work; see XML doc above.");
}
}