-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathContentHandoverServiceTests.cs
More file actions
719 lines (619 loc) · 26.4 KB
/
Copy pathContentHandoverServiceTests.cs
File metadata and controls
719 lines (619 loc) · 26.4 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
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microting.EformAngularFrontendBase.Infrastructure.Data;
using Microting.eFormApi.BasePn.Abstractions;
using Microting.TimePlanningBase.Infrastructure.Data.Entities;
using NSubstitute;
using NUnit.Framework;
using TimePlanning.Pn.Infrastructure.Models.ContentHandover;
using TimePlanning.Pn.Services.ContentHandoverService;
using TimePlanning.Pn.Services.TimePlanningLocalizationService;
namespace TimePlanning.Pn.Test;
[TestFixture]
public class ContentHandoverServiceTests : TestBaseSetup
{
private IContentHandoverService _contentHandoverService;
private IUserService _userService;
private ITimePlanningLocalizationService _localizationService;
[SetUp]
public async Task SetUp()
{
await base.Setup();
_userService = Substitute.For<IUserService>();
_userService.UserId.Returns(1);
_localizationService = Substitute.For<ITimePlanningLocalizationService>();
_localizationService.GetString(Arg.Any<string>()).Returns(x => x[0]?.ToString());
// Provide a non-null BaseDbContext substitute so the ctor-injected field never NREs.
// Mirrors the pattern used in PlanRegistrationVersionHistoryTests.
var baseDbContext = Substitute.For<BaseDbContext>(new DbContextOptions<BaseDbContext>());
_contentHandoverService = new ContentHandoverService(
Substitute.For<Microsoft.Extensions.Logging.ILogger<ContentHandoverService>>(),
TimePlanningPnDbContext,
_userService,
_localizationService,
Substitute.For<Microting.eFormApi.BasePn.Abstractions.IEFormCoreService>(),
baseDbContext,
Substitute.For<TimePlanning.Pn.Services.PushNotificationService.IPushNotificationService>());
}
// GetHandoverEligibleCoworkersAsync exercises the real SDK MicrotingDbContext (Workers,
// SiteTags, SiteWorkers) AND the AngularFrontendBase Users table. Seeding all three against
// the existing mariadb testcontainer harness is non-trivial and outside the scope of this
// fix. These tests are placeholders for a follow-up task to wire real Worker/SiteTag/User
// seeding so the happy-path, gate-path, and worker-not-found paths can be covered.
[Test]
[Ignore("Follow-up: wire real sdk Worker/SiteTag + BaseDbContext.Users seeding for GetHandoverEligibleCoworkersAsync happy-path test")]
public Task GetHandoverEligibleCoworkersAsync_HappyPath_ReturnsCoworkerSharingTag()
{
return Task.CompletedTask;
}
[Test]
[Ignore("Follow-up: wire real sdk Worker/SiteTag + BaseDbContext.Users seeding for GetHandoverEligibleCoworkersAsync gate-path test")]
public Task GetHandoverEligibleCoworkersAsync_GatePath_ReturnsEmptyWhenNoSharedTag()
{
return Task.CompletedTask;
}
[Test]
[Ignore("Follow-up: wire real BaseDbContext.Users seeding for GetHandoverEligibleCoworkersAsync worker-not-found test")]
public Task GetHandoverEligibleCoworkersAsync_WorkerNotFound_ReturnsFailure()
{
return Task.CompletedTask;
}
[Test]
public async Task CreateAsync_CreatesHandoverRequest_WhenSourceHasContent()
{
// Arrange
var date = new DateTime(2024, 1, 1);
var sourcePR = new PlanRegistration
{
Date = date,
SdkSitId = 1,
PlanHoursInSeconds = 28800, // 8 hours
PlanText = "Important work",
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await sourcePR.Create(TimePlanningPnDbContext);
var targetPR = new PlanRegistration
{
Date = date,
SdkSitId = 2,
PlanHoursInSeconds = 0,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await targetPR.Create(TimePlanningPnDbContext);
var model = new ContentHandoverRequestCreateModel
{
ToSdkSitId = 2,
RequestComment = "Need to transfer work"
};
// Act
var result = await _contentHandoverService.CreateAsync(sourcePR.Id, model);
// Assert
Assert.That(result.Success, Is.True);
Assert.That(result.Model, Is.Not.Null);
Assert.That(result.Model.Count, Is.EqualTo(1));
Assert.That(result.Model[0].Status, Is.EqualTo("Pending"));
Assert.That(result.Model[0].FromSdkSitId, Is.EqualTo(1));
Assert.That(result.Model[0].ToSdkSitId, Is.EqualTo(2));
Assert.That(result.Model[0].ShiftIndex, Is.Null);
}
[Test]
public async Task CreateAsync_Fails_WhenSourceHasNoContent()
{
// Arrange
var date = new DateTime(2024, 1, 1);
var sourcePR = new PlanRegistration
{
Date = date,
SdkSitId = 1,
PlanHoursInSeconds = 0,
PlanText = null,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await sourcePR.Create(TimePlanningPnDbContext);
var targetPR = new PlanRegistration
{
Date = date,
SdkSitId = 2,
PlanHoursInSeconds = 0,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await targetPR.Create(TimePlanningPnDbContext);
var model = new ContentHandoverRequestCreateModel
{
ToSdkSitId = 2
};
// Act
var result = await _contentHandoverService.CreateAsync(sourcePR.Id, model);
// Assert
Assert.That(result.Success, Is.False);
Assert.That(result.Message, Is.EqualTo("SourcePlanRegistrationHasNoContent"));
}
[Test]
public async Task AcceptAsync_MovesContent_FromSourceToTarget()
{
// Arrange
var date = new DateTime(2024, 1, 1);
var sourcePR = new PlanRegistration
{
Date = date,
SdkSitId = 1,
PlanHours = 8,
PlanHoursInSeconds = 28800,
PlanText = "Important work",
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await sourcePR.Create(TimePlanningPnDbContext);
var targetPR = new PlanRegistration
{
Date = date,
SdkSitId = 2,
PlanHours = 0,
PlanHoursInSeconds = 0,
PlanText = null,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await targetPR.Create(TimePlanningPnDbContext);
var request = new PlanRegistrationContentHandoverRequest
{
FromSdkSitId = 1,
ToSdkSitId = 2,
Date = date,
FromPlanRegistrationId = sourcePR.Id,
ToPlanRegistrationId = targetPR.Id,
Status = HandoverRequestStatus.Pending,
RequestedAtUtc = DateTime.UtcNow,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await request.Create(TimePlanningPnDbContext);
var model = new ContentHandoverDecisionModel
{
DecisionComment = "Accepted"
};
// Act
var result = await _contentHandoverService.AcceptAsync(request.Id, 2, model);
// Assert
Console.WriteLine($"Result Success: {result.Success}, Message: {result.Message}");
Assert.That(result.Success, Is.True, $"Expected success but got error: {result.Message}");
// Verify content moved
var updatedSource = await TimePlanningPnDbContext.PlanRegistrations.FindAsync(sourcePR.Id);
var updatedTarget = await TimePlanningPnDbContext.PlanRegistrations.FindAsync(targetPR.Id);
Assert.That(updatedSource.PlanHoursInSeconds, Is.EqualTo(0));
Assert.That(updatedSource.PlanText, Is.Null);
Assert.That(updatedTarget.PlanHoursInSeconds, Is.EqualTo(28800));
Assert.That(updatedTarget.PlanText, Is.EqualTo("Important work"));
// Verify request status
var updatedRequest = await TimePlanningPnDbContext.PlanRegistrationContentHandoverRequests.FindAsync(request.Id);
Assert.That(updatedRequest.Status, Is.EqualTo(HandoverRequestStatus.Accepted));
}
[Test]
public async Task AcceptAsync_Fails_WhenTargetHasContent()
{
// Arrange
var date = new DateTime(2024, 1, 1);
var sourcePR = new PlanRegistration
{
Date = date,
SdkSitId = 1,
PlanHours = 8,
PlanHoursInSeconds = 28800,
PlanText = "Important work",
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await sourcePR.Create(TimePlanningPnDbContext);
var targetPR = new PlanRegistration
{
Date = date,
SdkSitId = 2,
PlanHours = 4,
PlanHoursInSeconds = 14400, // Target has content
PlanText = "Existing work",
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await targetPR.Create(TimePlanningPnDbContext);
var request = new PlanRegistrationContentHandoverRequest
{
FromSdkSitId = 1,
ToSdkSitId = 2,
Date = date,
FromPlanRegistrationId = sourcePR.Id,
ToPlanRegistrationId = targetPR.Id,
Status = HandoverRequestStatus.Pending,
RequestedAtUtc = DateTime.UtcNow,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await request.Create(TimePlanningPnDbContext);
var model = new ContentHandoverDecisionModel
{
DecisionComment = "Accepted"
};
// Act
var result = await _contentHandoverService.AcceptAsync(request.Id, 2, model);
// Assert
Assert.That(result.Success, Is.False);
Assert.That(result.Message, Is.EqualTo("TargetPlanRegistrationMustBeEmpty"));
}
[Test]
public async Task RejectAsync_ChangesStatus_WithoutMovingContent()
{
// Arrange
var date = new DateTime(2024, 1, 1);
var sourcePR = new PlanRegistration
{
Date = date,
SdkSitId = 1,
PlanHoursInSeconds = 28800,
PlanText = "Important work",
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await sourcePR.Create(TimePlanningPnDbContext);
var targetPR = new PlanRegistration
{
Date = date,
SdkSitId = 2,
PlanHoursInSeconds = 0,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await targetPR.Create(TimePlanningPnDbContext);
var request = new PlanRegistrationContentHandoverRequest
{
FromSdkSitId = 1,
ToSdkSitId = 2,
Date = date,
FromPlanRegistrationId = sourcePR.Id,
ToPlanRegistrationId = targetPR.Id,
Status = HandoverRequestStatus.Pending,
RequestedAtUtc = DateTime.UtcNow,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await request.Create(TimePlanningPnDbContext);
var model = new ContentHandoverDecisionModel
{
DecisionComment = "Rejected"
};
// Act
var result = await _contentHandoverService.RejectAsync(request.Id, 2, model);
// Assert
Assert.That(result.Success, Is.True);
// Verify status changed
var updatedRequest = await TimePlanningPnDbContext.PlanRegistrationContentHandoverRequests.FindAsync(request.Id);
Assert.That(updatedRequest.Status, Is.EqualTo(HandoverRequestStatus.Rejected));
// Verify content not moved
var updatedSource = await TimePlanningPnDbContext.PlanRegistrations.FindAsync(sourcePR.Id);
var updatedTarget = await TimePlanningPnDbContext.PlanRegistrations.FindAsync(targetPR.Id);
Assert.That(updatedSource.PlanHoursInSeconds, Is.EqualTo(28800));
Assert.That(updatedTarget.PlanHoursInSeconds, Is.EqualTo(0));
}
[Test]
public async Task CancelAsync_ChangesStatus_WhenRequestedBySourceWorker()
{
// Arrange
var date = new DateTime(2024, 1, 1);
var sourcePR = new PlanRegistration
{
Date = date,
SdkSitId = 1,
PlanHoursInSeconds = 28800,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await sourcePR.Create(TimePlanningPnDbContext);
var targetPR = new PlanRegistration
{
Date = date,
SdkSitId = 2,
PlanHoursInSeconds = 0,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await targetPR.Create(TimePlanningPnDbContext);
var request = new PlanRegistrationContentHandoverRequest
{
FromSdkSitId = 1,
ToSdkSitId = 2,
Date = date,
FromPlanRegistrationId = sourcePR.Id,
ToPlanRegistrationId = targetPR.Id,
Status = HandoverRequestStatus.Pending,
RequestedAtUtc = DateTime.UtcNow,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await request.Create(TimePlanningPnDbContext);
// Act
var result = await _contentHandoverService.CancelAsync(request.Id, 1);
// Assert
Assert.That(result.Success, Is.True);
var updatedRequest = await TimePlanningPnDbContext.PlanRegistrationContentHandoverRequests.FindAsync(request.Id);
Assert.That(updatedRequest.Status, Is.EqualTo(HandoverRequestStatus.Cancelled));
}
[Test]
[Ignore("Follow-up: GetInboxAsync now resolves caller site from JWT via BaseDbContext.Users → sdk Worker lookup. Requires real BaseDbContext seeding to test here. The flow is covered end-to-end by the Dart gRPC contract suite (test/integration/grpc_flows_test.dart).")]
public async Task GetInboxAsync_ReturnsPendingRequestsForReceiver()
{
// Arrange
var date = new DateTime(2024, 1, 1);
var sourcePR = new PlanRegistration
{
Date = date,
SdkSitId = 1,
PlanHoursInSeconds = 28800,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await sourcePR.Create(TimePlanningPnDbContext);
var targetPR = new PlanRegistration
{
Date = date,
SdkSitId = 2,
PlanHoursInSeconds = 0,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await targetPR.Create(TimePlanningPnDbContext);
var request = new PlanRegistrationContentHandoverRequest
{
FromSdkSitId = 1,
ToSdkSitId = 2,
Date = date,
FromPlanRegistrationId = sourcePR.Id,
ToPlanRegistrationId = targetPR.Id,
Status = HandoverRequestStatus.Pending,
RequestedAtUtc = DateTime.UtcNow,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await request.Create(TimePlanningPnDbContext);
// Act
var result = await _contentHandoverService.GetInboxAsync();
// Assert
Assert.That(result.Success, Is.True);
Assert.That(result.Model.Count, Is.EqualTo(1));
Assert.That(result.Model[0].Status, Is.EqualTo("Pending"));
}
[Test]
public async Task GetMineAsync_ReturnsRequestsFromSender()
{
// Arrange
var date = new DateTime(2024, 1, 1);
var sourcePR = new PlanRegistration
{
Date = date,
SdkSitId = 1,
PlanHoursInSeconds = 28800,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await sourcePR.Create(TimePlanningPnDbContext);
var targetPR = new PlanRegistration
{
Date = date,
SdkSitId = 2,
PlanHoursInSeconds = 0,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await targetPR.Create(TimePlanningPnDbContext);
var request = new PlanRegistrationContentHandoverRequest
{
FromSdkSitId = 1,
ToSdkSitId = 2,
Date = date,
FromPlanRegistrationId = sourcePR.Id,
ToPlanRegistrationId = targetPR.Id,
Status = HandoverRequestStatus.Pending,
RequestedAtUtc = DateTime.UtcNow,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await request.Create(TimePlanningPnDbContext);
// Act
var result = await _contentHandoverService.GetMineAsync(1);
// Assert
Assert.That(result.Success, Is.True);
Assert.That(result.Model.Count, Is.EqualTo(1));
Assert.That(result.Model[0].FromSdkSitId, Is.EqualTo(1));
}
// ---------------------------------------------------------------
// Partial-shift handover tests.
// ---------------------------------------------------------------
private async Task<(PlanRegistration source, PlanRegistration target)> SeedPartialShiftPairAsync(DateTime date)
{
// Accept path recomputes derived fields via PlanRegistrationHelper, which
// requires an AssignedSite row per SdkSitId. Seed both before the plan rows.
if (!await TimePlanningPnDbContext.AssignedSites.AnyAsync(a => a.SiteId == 1))
{
await new AssignedSite { SiteId = 1, CreatedByUserId = 1, UpdatedByUserId = 1 }
.Create(TimePlanningPnDbContext);
}
if (!await TimePlanningPnDbContext.AssignedSites.AnyAsync(a => a.SiteId == 2))
{
await new AssignedSite { SiteId = 2, CreatedByUserId = 1, UpdatedByUserId = 1 }
.Create(TimePlanningPnDbContext);
}
var source = new PlanRegistration
{
Date = date,
SdkSitId = 1,
PlannedStartOfShift1 = 8 * 60,
PlannedEndOfShift1 = 12 * 60,
PlannedStartOfShift2 = 13 * 60,
PlannedEndOfShift2 = 17 * 60,
PlannedStartOfShift3 = 18 * 60,
PlannedEndOfShift3 = 21 * 60,
PlanHoursInSeconds = 11 * 3600,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await source.Create(TimePlanningPnDbContext);
var target = new PlanRegistration
{
Date = date,
SdkSitId = 2,
CreatedByUserId = 1,
UpdatedByUserId = 1
};
await target.Create(TimePlanningPnDbContext);
return (source, target);
}
[Test]
public async Task CreateAsync_SingleShift_CreatesOneRowWithShiftIndex()
{
var date = new DateTime(2024, 2, 1);
var (source, _) = await SeedPartialShiftPairAsync(date);
var result = await _contentHandoverService.CreateAsync(source.Id,
new ContentHandoverRequestCreateModel { ToSdkSitId = 2, ShiftIndices = new() { 2 } });
Assert.That(result.Success, Is.True, result.Message);
Assert.That(result.Model.Count, Is.EqualTo(1));
Assert.That(result.Model[0].ShiftIndex, Is.EqualTo(2));
// Source/target planning untouched (pending only).
var s = await TimePlanningPnDbContext.PlanRegistrations.FindAsync(source.Id);
Assert.That(s.PlannedEndOfShift2, Is.EqualTo(17 * 60));
}
[Test]
public async Task CreateAsync_MultiShift_CreatesRowPerShift()
{
var date = new DateTime(2024, 2, 2);
var (source, _) = await SeedPartialShiftPairAsync(date);
var result = await _contentHandoverService.CreateAsync(source.Id,
new ContentHandoverRequestCreateModel { ToSdkSitId = 2, ShiftIndices = new() { 1, 3 } });
Assert.That(result.Success, Is.True, result.Message);
Assert.That(result.Model.Count, Is.EqualTo(2));
Assert.That(result.Model.Select(m => m.ShiftIndex).OrderBy(x => x),
Is.EqualTo(new int?[] { 1, 3 }));
}
[Test]
public async Task CreateAsync_MultiShift_AllOrNothing_WhenOneInvalid()
{
var date = new DateTime(2024, 2, 3);
var (source, _) = await SeedPartialShiftPairAsync(date);
// Shift 4 on source is empty — invalid.
var result = await _contentHandoverService.CreateAsync(source.Id,
new ContentHandoverRequestCreateModel { ToSdkSitId = 2, ShiftIndices = new() { 1, 4 } });
Assert.That(result.Success, Is.False);
// Per-shift error message should name the failing shift so the UI can
// show which one blocked the batch.
Assert.That(result.Message, Does.Contain("Shift 4"));
var rows = await TimePlanningPnDbContext.PlanRegistrationContentHandoverRequests
.Where(r => r.FromPlanRegistrationId == source.Id)
.ToListAsync();
Assert.That(rows, Is.Empty, "No rows should have been persisted (validation pre-flight blocks the whole batch)");
}
[Test]
public async Task CreateAsync_DifferentShifts_AreAllowed_SameShift_IsBlocked()
{
var date = new DateTime(2024, 2, 4);
var (source, _) = await SeedPartialShiftPairAsync(date);
var first = await _contentHandoverService.CreateAsync(source.Id,
new ContentHandoverRequestCreateModel { ToSdkSitId = 2, ShiftIndices = new() { 1 } });
Assert.That(first.Success, Is.True);
// Different shift: allowed.
var second = await _contentHandoverService.CreateAsync(source.Id,
new ContentHandoverRequestCreateModel { ToSdkSitId = 2, ShiftIndices = new() { 2 } });
Assert.That(second.Success, Is.True, second.Message);
// Same shift again: blocked.
var third = await _contentHandoverService.CreateAsync(source.Id,
new ContentHandoverRequestCreateModel { ToSdkSitId = 2, ShiftIndices = new() { 1 } });
Assert.That(third.Success, Is.False);
}
[Test]
public async Task CreateAsync_PendingFullDay_BlocksPartial_AndViceVersa()
{
var dateA = new DateTime(2024, 2, 5);
var (sourceA, _) = await SeedPartialShiftPairAsync(dateA);
var fullDay = await _contentHandoverService.CreateAsync(sourceA.Id,
new ContentHandoverRequestCreateModel { ToSdkSitId = 2 });
Assert.That(fullDay.Success, Is.True);
var partialBlocked = await _contentHandoverService.CreateAsync(sourceA.Id,
new ContentHandoverRequestCreateModel { ToSdkSitId = 2, ShiftIndices = new() { 1 } });
Assert.That(partialBlocked.Success, Is.False);
// Separate date to test the other direction.
var dateB = new DateTime(2024, 2, 6);
var (sourceB, _) = await SeedPartialShiftPairAsync(dateB);
var partial = await _contentHandoverService.CreateAsync(sourceB.Id,
new ContentHandoverRequestCreateModel { ToSdkSitId = 2, ShiftIndices = new() { 2 } });
Assert.That(partial.Success, Is.True);
var fullDayBlocked = await _contentHandoverService.CreateAsync(sourceB.Id,
new ContentHandoverRequestCreateModel { ToSdkSitId = 2 });
Assert.That(fullDayBlocked.Success, Is.False);
}
[Test]
public async Task AcceptAsync_SingleShift_MovesOnlyThatShift()
{
var date = new DateTime(2024, 2, 7);
var (source, target) = await SeedPartialShiftPairAsync(date);
var create = await _contentHandoverService.CreateAsync(source.Id,
new ContentHandoverRequestCreateModel { ToSdkSitId = 2, ShiftIndices = new() { 2 } });
Assert.That(create.Success, Is.True);
var requestId = create.Model[0].Id;
var accept = await _contentHandoverService.AcceptAsync(requestId, 2,
new ContentHandoverDecisionModel { DecisionComment = "ok" });
Assert.That(accept.Success, Is.True, accept.Message);
var s = await TimePlanningPnDbContext.PlanRegistrations.FindAsync(source.Id);
var t = await TimePlanningPnDbContext.PlanRegistrations.FindAsync(target.Id);
// Shift 2 moved
Assert.That(s.PlannedEndOfShift2, Is.EqualTo(0));
Assert.That(t.PlannedEndOfShift2, Is.EqualTo(17 * 60));
// Shift 1 and 3 untouched on source
Assert.That(s.PlannedEndOfShift1, Is.EqualTo(12 * 60));
Assert.That(s.PlannedEndOfShift3, Is.EqualTo(21 * 60));
}
[Test]
public async Task AcceptAllShifts_EquivalentToFullDay()
{
var date = new DateTime(2024, 2, 8);
var (source, target) = await SeedPartialShiftPairAsync(date);
var create = await _contentHandoverService.CreateAsync(source.Id,
new ContentHandoverRequestCreateModel { ToSdkSitId = 2, ShiftIndices = new() { 1, 2, 3 } });
Assert.That(create.Success, Is.True, create.Message);
foreach (var m in create.Model)
{
var res = await _contentHandoverService.AcceptAsync(m.Id, 2,
new ContentHandoverDecisionModel { DecisionComment = "ok" });
Assert.That(res.Success, Is.True, res.Message);
}
var s = await TimePlanningPnDbContext.PlanRegistrations.FindAsync(source.Id);
var t = await TimePlanningPnDbContext.PlanRegistrations.FindAsync(target.Id);
Assert.That(s.PlannedEndOfShift1, Is.EqualTo(0));
Assert.That(s.PlannedEndOfShift2, Is.EqualTo(0));
Assert.That(s.PlannedEndOfShift3, Is.EqualTo(0));
Assert.That(t.PlannedEndOfShift1, Is.EqualTo(12 * 60));
Assert.That(t.PlannedEndOfShift2, Is.EqualTo(17 * 60));
Assert.That(t.PlannedEndOfShift3, Is.EqualTo(21 * 60));
}
[Test]
public async Task RejectOneOfN_LeavesRemainingPending()
{
var date = new DateTime(2024, 2, 9);
var (source, _) = await SeedPartialShiftPairAsync(date);
var create = await _contentHandoverService.CreateAsync(source.Id,
new ContentHandoverRequestCreateModel { ToSdkSitId = 2, ShiftIndices = new() { 1, 2 } });
Assert.That(create.Success, Is.True);
var shift1Id = create.Model.Single(m => m.ShiftIndex == 1).Id;
var shift2Id = create.Model.Single(m => m.ShiftIndex == 2).Id;
var rej = await _contentHandoverService.RejectAsync(shift1Id, 2,
new ContentHandoverDecisionModel { DecisionComment = "no" });
Assert.That(rej.Success, Is.True);
var reloaded1 = await TimePlanningPnDbContext.PlanRegistrationContentHandoverRequests.FindAsync(shift1Id);
var reloaded2 = await TimePlanningPnDbContext.PlanRegistrationContentHandoverRequests.FindAsync(shift2Id);
Assert.That(reloaded1.Status, Is.EqualTo(HandoverRequestStatus.Rejected));
Assert.That(reloaded2.Status, Is.EqualTo(HandoverRequestStatus.Pending));
// Shift 1 still on source (not moved).
var s = await TimePlanningPnDbContext.PlanRegistrations.FindAsync(source.Id);
Assert.That(s.PlannedEndOfShift1, Is.EqualTo(12 * 60));
}
}