using NSubstitute;
using Service.AI;
using Service.Transaction;
using System.Text.Json;
namespace WebApi.Test.Service;
///
/// SmartHandleService 单元测试
///
public class SmartHandleServiceTest : BaseTest
{
private readonly IOpenAiService _openAiService;
private readonly ITransactionRecordRepository _transactionRecordRepository;
private readonly ITransactionStatisticsService _transactionStatisticsService;
private readonly ITextSegmentService _textSegmentService;
private readonly ILogger _logger;
private readonly ITransactionCategoryRepository _categoryRepository;
private readonly IConfigService _configService;
private readonly IClassificationIconPromptProvider _iconPromptProvider;
private readonly SmartHandleService _service;
public SmartHandleServiceTest()
{
_openAiService = Substitute.For();
_transactionRecordRepository = Substitute.For();
_transactionStatisticsService = Substitute.For();
_textSegmentService = Substitute.For();
_logger = Substitute.For>();
_categoryRepository = Substitute.For();
_configService = Substitute.For();
_iconPromptProvider = Substitute.For();
_service = new SmartHandleService(
_transactionRecordRepository,
_transactionStatisticsService,
_textSegmentService,
_logger,
_categoryRepository,
_openAiService,
_configService,
_iconPromptProvider
);
}
#region GenerateSingleCategoryIconAsync Tests
[Fact]
public async Task GenerateSingleCategoryIconAsync_AI返回带Markdown标记的JSON_应成功解析()
{
// Arrange
const string categoryName = "餐饮";
const TransactionType categoryType = TransactionType.Expense;
const string aiResponseWithMarkdown = """
```json
[""]
```
""";
_iconPromptProvider.GetSingleIconPrompt(categoryName, categoryType).Returns("system prompt");
_openAiService.ChatAsync(Arg.Any(), Arg.Any(), Arg.Any())
.Returns(aiResponseWithMarkdown);
// Act
var result = await _service.GenerateSingleCategoryIconAsync(categoryName, categoryType);
// Assert
result.Should().NotBeNull();
result.Should().Contain(""]
""";
_iconPromptProvider.GetSingleIconPrompt(categoryName, categoryType).Returns("system prompt");
_openAiService.ChatAsync(Arg.Any(), Arg.Any(), Arg.Any())
.Returns(aiResponsePureJson);
// Act
var result = await _service.GenerateSingleCategoryIconAsync(categoryName, categoryType);
// Assert
result.Should().NotBeNull();
result.Should().Contain("