build-flow.api/tests/BuildFlow.Rag.Tests/LlmRerankParsingTests.cs

54 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BuildFlow.Infrastructure.AI;
using FluentAssertions;
using Xunit;
namespace BuildFlow.Rag.Tests;
/// <summary>
/// LlmRerankService.ParseOrderedIds 纯解析单测:不依赖 PG/LLM,覆盖围栏容忍与幻觉拒绝。
/// </summary>
public class LlmRerankParsingTests
{
private static readonly List<(Guid ChunkId, string Content)> Candidates =
[
(Guid.NewGuid(), "内容甲"),
(Guid.NewGuid(), "内容乙"),
(Guid.NewGuid(), "内容丙"),
];
[Fact]
public void PlainArray_ParsesInOrder()
{
var result = LlmRerankService.ParseOrderedIds("[3,1,2]", Candidates);
result.Should().NotBeNull();
result![0].Should().Be(Candidates[2].ChunkId);
result[1].Should().Be(Candidates[0].ChunkId);
result[2].Should().Be(Candidates[1].ChunkId);
}
[Fact]
public void JsonFenceAndProse_Tolerated()
{
var result = LlmRerankService.ParseOrderedIds("好的,排序结果如下:\n```json\n[2, 1]\n```\n希望有帮助", Candidates);
result.Should().NotBeNull();
result![0].Should().Be(Candidates[1].ChunkId);
result[1].Should().Be(Candidates[0].ChunkId);
}
[Fact]
public void OutOfRangeNumber_RejectedAsNull()
{
// 模型幻觉输出编号 4(越界)→ 整体不可信,降级
LlmRerankService.ParseOrderedIds("[1, 4]", Candidates).Should().BeNull();
LlmRerankService.ParseOrderedIds("[0, 1]", Candidates).Should().BeNull();
}
[Fact]
public void MalformedText_ReturnsNull()
{
LlmRerankService.ParseOrderedIds("无法排序", Candidates).Should().BeNull();
LlmRerankService.ParseOrderedIds("[1, \"两\"]", Candidates).Should().BeNull();
LlmRerankService.ParseOrderedIds("[]", Candidates).Should().BeNull();
}
}