Skip to content

Commit a060e7a

Browse files
committed
feat: Update unit test target file names to include "Tests" suffix and refactor service resolution logic
1 parent 8825e90 commit a060e7a

File tree

7 files changed

+102
-86
lines changed

7 files changed

+102
-86
lines changed

Modules/Intent.Modules.AI.UnitTests/Tasks/GenerateCqrsHandlerUnitTestsWithAITask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public string Execute(params string[] args)
9191
{
9292
["inputFilesJson"] = jsonInput,
9393
["userProvidedContext"] = userProvidedContext,
94-
["targetFileName"] = queryModel.Name + "Handler",
94+
["targetFileName"] = queryModel.Name + "HandlerTests",
9595
["mockFramework"] = UnitTestHelpers.GetMockFramework(_applicationConfigurationProvider),
9696
["slnRelativePath"] = "/" + string.Join('/', queryModel.GetParentPath().Select(x => x.Name)),
9797
["fileChangesSchema"] = FileChangesSchema.GetPromptInstructions()

Modules/Intent.Modules.AI.UnitTests/Tasks/GenerateDomainEventHandlerUnitTestWithAITask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public string Execute(params string[] args)
8888
{
8989
["inputFilesJson"] = jsonInput,
9090
["userProvidedContext"] = userProvidedContext,
91-
["targetFileName"] = eventHandlerElement.Name,
91+
["targetFileName"] = eventHandlerElement.Name + "Tests",
9292
["mockFramework"] = UnitTestHelpers.GetMockFramework(_applicationConfigurationProvider),
9393
["slnRelativePath"] = "/" + string.Join('/', eventHandlerElement.GetParentPath().Select(x => x.Name)),
9494
["fileChangesSchema"] = FileChangesSchema.GetPromptInstructions()

Modules/Intent.Modules.AI.UnitTests/Tasks/GenerateDomainServiceUnitTestsWithAITask.cs

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -68,51 +68,10 @@ public string Execute(params string[] args)
6868
var kernel = _intentSemanticKernelFactory.BuildSemanticKernel(modelId, provider, null);
6969

7070
var domainDesigner = _metadataManager.GetDesigner(applicationId, "Domain");
71-
72-
// Try to find the domain service operation
73-
var domainServiceOperation = domainDesigner.GetElementsOfType(SpecializationTypeIds.DomainServiceOperation)
71+
var domainService = domainDesigner.GetElementsOfType(SpecializationTypeIds.DomainService)
7472
.FirstOrDefault(x => x.Id == elementId);
75-
76-
// If not found as operation, try finding it as a domain service
77-
if (domainServiceOperation == null)
78-
{
79-
var domainService = domainDesigner.GetElementsOfType(SpecializationTypeIds.DomainService)
80-
.FirstOrDefault(x => x.Id == elementId);
81-
82-
if (domainService != null)
83-
{
84-
// Element ID is the service itself, find its operations
85-
domainServiceOperation = domainService.ChildElements
86-
.FirstOrDefault(o => o.SpecializationTypeId == SpecializationTypeIds.DomainServiceOperation);
87-
}
88-
}
89-
90-
if (domainServiceOperation == null)
91-
{
92-
// Diagnostic logging
93-
Logging.Log.Warning($"Looking for element ID: {elementId}");
94-
var domainServices = domainDesigner.GetElementsOfType(SpecializationTypeIds.DomainService).ToList();
95-
Logging.Log.Warning($"DomainService elements found: {domainServices.Count}");
96-
foreach (var ds in domainServices)
97-
{
98-
Logging.Log.Warning($" - {ds.Name} (ID: {ds.Id}, Type: {ds.SpecializationTypeId})");
99-
foreach (var child in ds.ChildElements)
100-
{
101-
Logging.Log.Warning($" - Child: {child.Name} (ID: {child.Id}, Type: {child.SpecializationTypeId})");
102-
}
103-
}
104-
105-
var operations = domainDesigner.GetElementsOfType(SpecializationTypeIds.DomainServiceOperation).ToList();
106-
Logging.Log.Warning($"DomainServiceOperation elements found: {operations.Count}");
107-
foreach (var op in operations)
108-
{
109-
Logging.Log.Warning($" - {op.Name} (ID: {op.Id})");
110-
}
111-
112-
throw new Exception($"An element was selected to be executed upon but could not be found. Please ensure you have saved your designer and try again.");
113-
}
11473

115-
var inputFiles = GetInputFiles(applicationId, domainServiceOperation);
74+
var inputFiles = GetInputFiles(applicationId, domainService);
11675
Logging.Log.Info($"Context files included: {inputFiles.Count} files");
11776
Logging.Log.Debug($"Files: {string.Join(", ", inputFiles.Select(f => Path.GetFileName(f.FilePath)))}");
11877

@@ -123,9 +82,9 @@ public string Execute(params string[] args)
12382
{
12483
["inputFilesJson"] = jsonInput,
12584
["userProvidedContext"] = userProvidedContext,
126-
["targetFileName"] = domainServiceOperation.Name,
85+
["targetFileName"] = domainService.Name + "Tests",
12786
["mockFramework"] = UnitTestHelpers.GetMockFramework(_applicationConfigurationProvider),
128-
["slnRelativePath"] = "/" + string.Join('/', domainServiceOperation.GetParentPath().Select(x => x.Name)),
87+
["slnRelativePath"] = "/" + string.Join('/', domainService.GetParentPath().Select(x => x.Name)),
12988
["fileChangesSchema"] = FileChangesSchema.GetPromptInstructions()
13089
});
13190

@@ -297,13 +256,13 @@ public class {DomainServiceName}Tests
297256
{
298257
private readonly Mock<IRepository1> _repository1Mock;
299258
private readonly Mock<IRepository2> _repository2Mock; // If service uses multiple repositories
300-
private readonly {DomainServiceName} _sut;
259+
private readonly {DomainServiceName} _service;
301260
302261
public {DomainServiceName}Tests()
303262
{
304263
_repository1Mock = new Mock<IRepository1>();
305264
_repository2Mock = new Mock<IRepository2>(); // Only if needed
306-
_sut = new {DomainServiceName}(_repository1Mock.Object, _repository2Mock.Object);
265+
_service = new {DomainServiceName}(_repository1Mock.Object, _repository2Mock.Object);
307266
}
308267
309268
// Test methods follow...
@@ -576,17 +535,32 @@ public async Task CalculateCommissionAsync_AppliesCap_ForHighValueTransaction()
576535
return promptTemplate;
577536
}
578537

579-
private List<ICodebaseFile> GetInputFiles(string applicationId, IElement domainServiceOperation)
538+
private List<ICodebaseFile> GetInputFiles(string applicationId, IElement domainService)
580539
{
581540
var filesProvider = _applicationConfigurationProvider.GeneratedFilesProvider();
582541

583542
// PRIMARY: Domain service implementation and interface
584-
var domainService = domainServiceOperation.ParentElement;
585543
var inputFiles = filesProvider.GetFilesForMetadata(domainService).ToList();
586-
inputFiles.AddRange(filesProvider.GetFilesForMetadata(domainServiceOperation));
544+
inputFiles.AddRange(filesProvider.GetFilesForMetadata(domainService));
545+
546+
// Include each domain service operation and its parameter types (DTO + Class)
547+
foreach (var operation in domainService.ChildElements.Where(x => x.SpecializationTypeId == SpecializationTypeIds.DomainServiceOperation))
548+
{
549+
inputFiles.AddRange(filesProvider.GetFilesForMetadata(operation));
550+
// DTO parameters
551+
foreach (var paramDto in operation.ChildElements.Where(p => p.TypeReference?.Element?.SpecializationTypeId == SpecializationTypeIds.Dto).Select(p => p.TypeReference.Element))
552+
{
553+
inputFiles.AddRange(filesProvider.GetFilesForMetadata(paramDto));
554+
}
555+
// Class (domain entity/value object) parameters
556+
foreach (var paramClass in operation.ChildElements.Where(p => p.TypeReference?.Element?.SpecializationTypeId == SpecializationTypeIds.Class).Select(p => p.TypeReference.Element))
557+
{
558+
inputFiles.AddRange(filesProvider.GetFilesForMetadata(paramClass));
559+
}
560+
}
587561

588562
// DOMAIN: Include related entities, value objects, and their repositories
589-
var relatedElements = UnitTestHelpers.GetRelatedElements(domainServiceOperation);
563+
var relatedElements = UnitTestHelpers.GetRelatedElements(domainService);
590564
foreach (var relatedElement in relatedElements)
591565
{
592566
inputFiles.AddRange(filesProvider.GetFilesForMetadata(relatedElement));

Modules/Intent.Modules.AI.UnitTests/Tasks/GenerateIntegrationEventHandlerUnitTestsWithAITask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public string Execute(params string[] args)
8888
{
8989
["inputFilesJson"] = jsonInput,
9090
["userProvidedContext"] = userProvidedContext,
91-
["targetFileName"] = eventHandlerElement.Name,
91+
["targetFileName"] = eventHandlerElement.Name + "Tests",
9292
["mockFramework"] = UnitTestHelpers.GetMockFramework(_applicationConfigurationProvider),
9393
["slnRelativePath"] = "/" + string.Join('/', eventHandlerElement.GetParentPath().Select(x => x.Name)),
9494
["fileChangesSchema"] = FileChangesSchema.GetPromptInstructions()

Modules/Intent.Modules.AI.UnitTests/Tasks/GenerateServiceUnitTestsWithAITask.cs

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,28 @@ public string Execute(params string[] args)
7272
var kernel = _intentSemanticKernelFactory.BuildSemanticKernel(modelId, provider, null);
7373

7474
var servicesDesigner = _metadataManager.GetDesigner(applicationId, "Services");
75-
var queryModel = servicesDesigner.GetElementsOfType(SpecializationTypeIds.Service)
76-
.SelectMany(s => s.ChildElements.Where(o => o.SpecializationTypeId == SpecializationTypeIds.Operation))
77-
.FirstOrDefault(x => x.Id == elementId);
78-
if (queryModel == null)
75+
// Attempt to resolve the element ID as a Service first.
76+
var serviceModel = servicesDesigner.GetElementsOfType(SpecializationTypeIds.Service)
77+
.FirstOrDefault(s => s.Id == elementId);
78+
IElement? selectedOperation = null;
79+
if (serviceModel == null)
7980
{
80-
throw new Exception($"An element was selected to be executed upon but could not be found. Please ensure you have saved your designer and try again.");
81+
// If not a Service, attempt to resolve as an Operation belonging to any Service.
82+
selectedOperation = servicesDesigner.GetElementsOfType(SpecializationTypeIds.Service)
83+
.SelectMany(s => s.ChildElements.Where(o => o.SpecializationTypeId == SpecializationTypeIds.Operation))
84+
.FirstOrDefault(o => o.Id == elementId);
85+
if (selectedOperation != null)
86+
{
87+
serviceModel = selectedOperation.ParentElement;
88+
}
89+
}
90+
if (serviceModel == null)
91+
{
92+
throw new Exception("An element was selected to be executed upon but could not be found. Please ensure you have saved your designer and try again.");
8193
}
82-
var inputFiles = GetInputFiles(queryModel);
94+
95+
var inputFiles = GetInputFiles(serviceModel);
96+
Logging.Log.Info($"Resolved service: {serviceModel.Name} (Selected operation: {selectedOperation?.Name ?? "All"})");
8397
Logging.Log.Info($"Context files included: {inputFiles.Count} files");
8498
Logging.Log.Debug($"Files: {string.Join(", ", inputFiles.Select(f => Path.GetFileName(f.FilePath)))}");
8599

@@ -90,9 +104,9 @@ public string Execute(params string[] args)
90104
{
91105
["inputFilesJson"] = jsonInput,
92106
["userProvidedContext"] = userProvidedContext,
93-
["targetFileName"] = queryModel.Name,
107+
["targetFileName"] = serviceModel.Name + "Tests",
94108
["mockFramework"] = UnitTestHelpers.GetMockFramework(_applicationConfigurationProvider),
95-
["slnRelativePath"] = "/" + string.Join('/', queryModel.GetParentPath().Select(x => x.Name)),
109+
["slnRelativePath"] = "/" + string.Join('/', serviceModel.GetParentPath().Select(x => x.Name)),
96110
["fileChangesSchema"] = FileChangesSchema.GetPromptInstructions()
97111
});
98112

@@ -258,13 +272,13 @@ public class {ServiceName}Tests
258272
{
259273
private readonly Mock<IRepository> _repositoryMock;
260274
private readonly Mock<IMapper> _mapperMock; // Only if service uses IMapper
261-
private readonly {ServiceName} _sut;
275+
private readonly {ServiceName} _service;
262276
263277
public {ServiceName}Tests()
264278
{
265279
_repositoryMock = new Mock<IRepository>();
266280
_mapperMock = new Mock<IMapper>(); // Only if needed
267-
_sut = new {ServiceName}(_repositoryMock.Object, _mapperMock.Object);
281+
_service = new {ServiceName}(_repositoryMock.Object, _mapperMock.Object);
268282
}
269283
270284
// Test methods follow...
@@ -296,7 +310,7 @@ public async Task CreateProduct_AddsProductAndReturnsId()
296310
.ReturnsAsync(1);
297311
298312
// Act
299-
var result = await _sut.CreateProduct(dto);
313+
var result = await _service.CreateProduct(dto);
300314
301315
// Assert
302316
Assert.Equal(expectedId, result);
@@ -324,7 +338,7 @@ public async Task UpdateProduct_UpdatesProduct_WhenFound()
324338
.ReturnsAsync(existingProduct);
325339
326340
// Act
327-
await _sut.UpdateProduct(productId, dto);
341+
await _service.UpdateProduct(productId, dto);
328342
329343
// Assert
330344
Assert.Equal("NewName", existingProduct.Name);
@@ -342,7 +356,7 @@ public async Task UpdateProduct_ThrowsNotFoundException_WhenProductNotFound()
342356
.ReturnsAsync((Product)null);
343357
344358
// Act & Assert
345-
await Assert.ThrowsAsync<NotFoundException>(() => _sut.UpdateProduct(dto.Id, dto));
359+
await Assert.ThrowsAsync<NotFoundException>(() => _service.UpdateProduct(dto.Id, dto));
346360
}
347361
```
348362
@@ -360,7 +374,7 @@ public async Task DeleteProduct_RemovesProduct_WhenFound()
360374
.ReturnsAsync(product);
361375
362376
// Act
363-
await _sut.DeleteProduct(productId);
377+
await _service.DeleteProduct(productId);
364378
365379
// Assert
366380
_productRepositoryMock.Verify(x => x.FindByIdAsync(productId, It.IsAny<CancellationToken>()), Times.Once);
@@ -377,7 +391,7 @@ public async Task DeleteProduct_ThrowsNotFoundException_WhenNotFound()
377391
.ReturnsAsync((Product)null);
378392
379393
// Act & Assert
380-
await Assert.ThrowsAsync<NotFoundException>(() => _sut.DeleteProduct(productId));
394+
await Assert.ThrowsAsync<NotFoundException>(() => _service.DeleteProduct(productId));
381395
}
382396
```
383397
@@ -402,7 +416,7 @@ public async Task FindProductById_ReturnsDto_WhenFound()
402416
.Returns(expectedDto);
403417
404418
// Act
405-
var result = await _sut.FindProductById(productId);
419+
var result = await _service.FindProductById(productId);
406420
407421
// Assert
408422
Assert.NotNull(result);
@@ -423,7 +437,7 @@ public async Task FindProductById_ThrowsNotFoundException_WhenNotFound()
423437
.ReturnsAsync((Product)null);
424438
425439
// Act & Assert
426-
await Assert.ThrowsAsync<NotFoundException>(() => _sut.FindProductById(productId));
440+
await Assert.ThrowsAsync<NotFoundException>(() => _service.FindProductById(productId));
427441
}
428442
```
429443
@@ -451,7 +465,7 @@ public async Task FindProducts_ReturnsAllProducts()
451465
.Returns((Product p) => ProductDto.Create("", "", 0, "", p.Id));
452466
453467
// Act
454-
var result = await _sut.FindProducts();
468+
var result = await _service.FindProducts();
455469
456470
// Assert
457471
Assert.NotNull(result);
@@ -497,7 +511,7 @@ public async Task FindProductsByCategory_ReturnsOnlyMatchingProducts()
497511
.Returns((Product p) => ProductDto.Create("", "", 0, "", Guid.NewGuid()));
498512
499513
// Act
500-
var result = await _sut.FindProductsByCategory(categoryId);
514+
var result = await _service.FindProductsByCategory(categoryId);
501515
502516
// Assert - This single test verifies: predicate logic, repository call, mapper call, result correctness
503517
Assert.NotNull(result);
@@ -528,7 +542,7 @@ public async Task FindProductsByCategory_ReturnsEmpty_WhenNoProductsMatchFilter(
528542
});
529543
530544
// Act
531-
var result = await _sut.FindProductsByCategory(categoryId);
545+
var result = await _service.FindProductsByCategory(categoryId);
532546
533547
// Assert
534548
Assert.Empty(result);
@@ -564,7 +578,7 @@ public async Task FindActiveProducts_ReturnsProducts_WhenProductsMatchCriteria()
564578
.Returns((Product p) => ProductDto.Create("", "", 0, "", p.Id));
565579
566580
// Act
567-
var result = await _sut.FindActiveProducts(categoryId);
581+
var result = await _service.FindActiveProducts(categoryId);
568582
569583
// Assert
570584
Assert.NotNull(result);
@@ -588,7 +602,7 @@ public async Task FindActiveProducts_ReturnsEmpty_WhenNoProductsMatchCriteria()
588602
.ReturnsAsync(new List<Product>());
589603
590604
// Act
591-
var result = await _sut.FindActiveProducts(categoryId);
605+
var result = await _service.FindActiveProducts(categoryId);
592606
593607
// Assert
594608
Assert.NotNull(result);
@@ -652,7 +666,7 @@ public async Task GetCompletedTransactions_ReturnsTransactions_WhenTransactionsM
652666
});
653667
654668
// Act
655-
var result = await _sut.GetCompletedTransactions(fromDate, toDate);
669+
var result = await _service.GetCompletedTransactions(fromDate, toDate);
656670
657671
// Assert
658672
Assert.NotNull(result);
@@ -689,7 +703,7 @@ public async Task GetCompletedTransactions_ReturnsEmpty_WhenNoTransactionsMatchC
689703
});
690704
691705
// Act
692-
var result = await _sut.GetCompletedTransactions(fromDate, toDate);
706+
var result = await _service.GetCompletedTransactions(fromDate, toDate);
693707
694708
// Assert
695709
Assert.NotNull(result);
@@ -716,10 +730,16 @@ private List<ICodebaseFile> GetInputFiles(IElement service)
716730
{
717731
inputFiles.AddRange(filesProvider.GetFilesForMetadata(operation.TypeReference.Element));
718732
}
733+
// Include DTO parameter types
719734
foreach (var paramType in operation.ChildElements.Where(x => x.TypeReference?.Element?.SpecializationTypeId == SpecializationTypeIds.Dto).Select(x => x.TypeReference.Element))
720735
{
721736
inputFiles.AddRange(filesProvider.GetFilesForMetadata(paramType));
722737
}
738+
// Include Class (domain entity / value object) parameter types
739+
foreach (var paramClass in operation.ChildElements.Where(x => x.TypeReference?.Element?.SpecializationTypeId == SpecializationTypeIds.Class).Select(x => x.TypeReference.Element))
740+
{
741+
inputFiles.AddRange(filesProvider.GetFilesForMetadata(paramClass));
742+
}
723743

724744
inputFiles.AddRange(UnitTestHelpers.GetRelatedElements(operation).SelectMany(x => filesProvider.GetFilesForMetadata(x)));
725745
}

0 commit comments

Comments
 (0)