@@ -1402,6 +1402,7 @@ public async Task Post_TestBatch_MarksBatchAsSuperseded_IfNoImagesFound()
14021402
14031403 var dbBatch = await dbContext . Batches . SingleAsync ( b => b . Id == 201 ) ;
14041404 dbBatch . Superseded . Should ( ) . BeTrue ( ) ;
1405+ dbBatch . Finished . Should ( ) . NotBeNull ( "Batch was marked as finished" ) ;
14051406
14061407 A . CallTo ( ( ) =>
14071408 NotificationSender . SendBatchCompletedMessage (
@@ -1410,6 +1411,33 @@ public async Task Post_TestBatch_MarksBatchAsSuperseded_IfNoImagesFound()
14101411 . MustHaveHappened ( ) ;
14111412 }
14121413
1414+ [ Fact ]
1415+ public async Task Post_TestBatch_MarksBatchAsSuperseded_IfNoImagesFound_NoNotificationRaisedIfAlreadyFinished ( )
1416+ {
1417+ // Arrange
1418+ const int batchId = 2011 ;
1419+ await dbContext . Batches . AddTestBatch ( batchId , finished : DateTime . UtcNow ) ;
1420+ await dbContext . SaveChangesAsync ( ) ;
1421+ var path = $ "customers/99/queue/batches/{ batchId } /test";
1422+
1423+ // Act
1424+ var response = await httpClient . AsCustomer ( ) . PostAsync ( path , null ! ) ;
1425+
1426+ // Assert
1427+ response . StatusCode . Should ( ) . Be ( HttpStatusCode . OK ) ;
1428+ var jsonDoc = JsonDocument . Parse ( await response . Content . ReadAsStringAsync ( ) ) ;
1429+ jsonDoc . RootElement . GetProperty ( "success" ) . GetBoolean ( ) . Should ( ) . BeTrue ( ) ;
1430+
1431+ var dbBatch = await dbContext . Batches . SingleAsync ( b => b . Id == batchId ) ;
1432+ dbBatch . Superseded . Should ( ) . BeTrue ( ) ;
1433+
1434+ A . CallTo ( ( ) =>
1435+ NotificationSender . SendBatchCompletedMessage (
1436+ A < Batch > . That . Matches ( b => b . Id == dbBatch . Id ) ,
1437+ A < CancellationToken > . _ ) )
1438+ . MustNotHaveHappened ( ) ;
1439+ }
1440+
14131441 [ Fact ]
14141442 public async Task Post_TestBatch_MarksBatchAsComplete_IfImagesFoundAndAllFinished_AndBatchNotFinished ( )
14151443 {
@@ -1511,6 +1539,40 @@ public async Task Post_TestBatch_DoesNotChangeBatchFinished_IfImagesFoundAndAllF
15111539 . MustNotHaveHappened ( ) ;
15121540 }
15131541
1542+ [ Fact ]
1543+ public async Task Post_TestBatch_DoesNotFinishBatch_IfImagesFoundAndAllFinished_ButFinishedBeforeBatchSubmitted ( )
1544+ {
1545+ // This test covers the case where existing Assets were reprocessed, they have been finished in the past but this
1546+ // was prior to batch creation
1547+ const int batch = 206 ;
1548+ var submitted = DateTime . UtcNow . AddDays ( 3 ) ;
1549+ await dbContext . Batches . AddTestBatch ( batch , count : 3 , submitted : submitted ) ;
1550+ await dbContext . Images . AddTestAsset ( AssetId . FromString ( "2/1/june" ) , batch : batch , finished : DateTime . UtcNow ) ;
1551+ await dbContext . Images . AddTestAsset ( AssetId . FromString ( "2/1/hunger" ) , batch : batch , finished : DateTime . UtcNow ) ;
1552+ await dbContext . Images . AddTestAsset ( AssetId . FromString ( "2/1/grace" ) , batch : batch , finished : DateTime . UtcNow ) ;
1553+ await dbContext . SaveChangesAsync ( ) ;
1554+ var path = $ "customers/99/queue/batches/{ batch } /test";
1555+
1556+ // Act
1557+ var response = await httpClient . AsCustomer ( ) . PostAsync ( path , null ! ) ;
1558+
1559+ // Assert
1560+ response . StatusCode . Should ( ) . Be ( HttpStatusCode . OK ) ;
1561+ var jsonDoc = JsonDocument . Parse ( await response . Content . ReadAsStringAsync ( ) ) ;
1562+ jsonDoc . RootElement . GetProperty ( "success" ) . GetBoolean ( ) . Should ( ) . BeFalse ( ) ;
1563+
1564+ var dbBatch = await dbContext . Batches . SingleAsync ( b => b . Id == batch ) ;
1565+ dbBatch . Superseded . Should ( ) . BeFalse ( ) ;
1566+ dbBatch . Finished . Should ( ) . BeNull ( "All images are completed but they completed before submission" ) ;
1567+ dbBatch . Count . Should ( ) . Be ( 3 ) ;
1568+
1569+ A . CallTo ( ( ) =>
1570+ NotificationSender . SendBatchCompletedMessage (
1571+ A < Batch > . That . Matches ( b => b . Id == dbBatch . Id ) ,
1572+ A < CancellationToken > . _ ) )
1573+ . MustNotHaveHappened ( ) ;
1574+ }
1575+
15141576 [ Fact ]
15151577 public async Task Post_CreateBatch_201_IfImageOptimisationPolicySetForImage_AndLegacyEnabled ( )
15161578 {
0 commit comments