init
This commit is contained in:
commit
5f8428a318
1688 files changed
+39031
No files matched your search
@@ -0,0 +1,25 @@
|
||||
using RecordBot.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace RecordBot.Tests;
|
||||
|
||||
public class DashScopeImageEndpointTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(
|
||||
"https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||
"https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation")]
|
||||
[InlineData(
|
||||
"https://dashscope.aliyuncs.com/compatible-mode/v1/",
|
||||
"https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation")]
|
||||
[InlineData(
|
||||
"https://dashscope.aliyuncs.com/api/v1",
|
||||
"https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation")]
|
||||
[InlineData(
|
||||
"",
|
||||
"https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation")]
|
||||
public void ResolveDashScopeImageEndpoint_FromChatBaseUrl(string baseUrl, string expected)
|
||||
{
|
||||
Assert.Equal(expected, QwenIntentClient.ResolveDashScopeImageEndpoint(baseUrl).ToString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using RecordBot.Options;
|
||||
using RecordBot.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace RecordBot.Tests;
|
||||
|
||||
public class GrokImageEndpointTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("https://wawazz.xyz/v1", false, "https://wawazz.xyz/v1/images/generations")]
|
||||
[InlineData("https://wawazz.xyz/v1/", false, "https://wawazz.xyz/v1/images/generations")]
|
||||
[InlineData("https://wawazz.xyz/v1", true, "https://wawazz.xyz/v1/images/edits")]
|
||||
[InlineData("", false, "https://wawazz.xyz/v1/images/generations")]
|
||||
public void ResolveImagesEndpoint_Works(string baseUrl, bool edits, string expected)
|
||||
{
|
||||
Assert.Equal(expected, GrokImageClient.ResolveImagesEndpoint(baseUrl, edits).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildRequestBody_IncludesAspectAndResolution()
|
||||
{
|
||||
var grok = new GrokImageOptions
|
||||
{
|
||||
AspectRatio = "9:16",
|
||||
Resolution = "2k",
|
||||
};
|
||||
var body = GrokImageClient.BuildRequestBody(grok, "grok-imagine-image-2.0", "一只猫", null);
|
||||
Assert.Equal("grok-imagine-image-2.0", body["model"]);
|
||||
Assert.Equal("一只猫", body["prompt"]);
|
||||
Assert.Equal("9:16", body["aspect_ratio"]);
|
||||
Assert.Equal("2k", body["resolution"]);
|
||||
Assert.Equal("b64_json", body["response_format"]);
|
||||
Assert.False(body.ContainsKey("image"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildRequestBody_WithReference_AddsImage()
|
||||
{
|
||||
var grok = new GrokImageOptions { AspectRatio = "auto", Resolution = "1k" };
|
||||
var pngHeader = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
|
||||
var body = GrokImageClient.BuildRequestBody(grok, "grok-imagine-image-quality", "改成水彩", pngHeader);
|
||||
Assert.True(body.ContainsKey("image"));
|
||||
Assert.Equal("auto", body["aspect_ratio"]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using RecordBot.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace RecordBot.Tests;
|
||||
|
||||
public class ImageGenPromptTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("画图 一只橘猫", "一只橘猫")]
|
||||
[InlineData("画图:赛博朋克城市", "赛博朋克城市")]
|
||||
[InlineData("画一张水彩风景", "水彩风景")]
|
||||
[InlineData("生成图片 海边日落", "海边日落")]
|
||||
[InlineData("帮我画一只狗", "一只狗")]
|
||||
[InlineData("draw an image a red balloon", "a red balloon")]
|
||||
[InlineData("画图", "")]
|
||||
public void TryExtract_Works(string input, string expected)
|
||||
{
|
||||
Assert.Equal(expected, ImageGenPrompt.TryExtract(input));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("今天天气怎么样")]
|
||||
[InlineData("随便记一笔")]
|
||||
[InlineData("提醒我明天开会")]
|
||||
public void TryExtract_NonDraw_ReturnsNull(string input)
|
||||
{
|
||||
Assert.Null(ImageGenPrompt.TryExtract(input));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryExtract_MultilinePrompt_Works()
|
||||
{
|
||||
var input = "画图 古老丛林深处,扭曲的巨树。\n风格:暗黑奇幻、细节丰富。\n光影压抑。";
|
||||
var got = ImageGenPrompt.TryExtract(input);
|
||||
Assert.NotNull(got);
|
||||
Assert.Contains("古老丛林深处", got);
|
||||
Assert.Contains("风格:暗黑奇幻", got);
|
||||
Assert.Contains("光影压抑", got);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("绘图 一只橘猫", "一只橘猫")]
|
||||
[InlineData("绘图:赛博朋克城市", "赛博朋克城市")]
|
||||
[InlineData("绘图", "")]
|
||||
[InlineData("grok imagine a red balloon", "a red balloon")]
|
||||
public void TryExtractGrok_Works(string input, string expected)
|
||||
{
|
||||
Assert.Equal(expected, ImageGenPrompt.TryExtractGrok(input));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("画图 一只橘猫")]
|
||||
[InlineData("生成图片 海边日落")]
|
||||
[InlineData("今天天气怎么样")]
|
||||
public void TryExtractGrok_DoesNotMatchQwenTriggers(string input)
|
||||
{
|
||||
Assert.Null(ImageGenPrompt.TryExtractGrok(input));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryExtractGrok_MultilinePrompt_Works()
|
||||
{
|
||||
var input = "绘图 古老丛林深处,扭曲的巨树。\n风格:暗黑奇幻。";
|
||||
var got = ImageGenPrompt.TryExtractGrok(input);
|
||||
Assert.NotNull(got);
|
||||
Assert.Contains("古老丛林深处", got);
|
||||
Assert.Contains("风格:暗黑奇幻", got);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using RecordBot.Options;
|
||||
using RecordBot.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace RecordBot.Tests;
|
||||
|
||||
public class KeywordClassifierTests
|
||||
{
|
||||
private readonly KeywordClassifier _classifier =
|
||||
new(Microsoft.Extensions.Options.Options.Create(new RecordBotOptions()));
|
||||
|
||||
[Theory]
|
||||
[InlineData("#支出 午餐 35", "支出", "午餐 35", 35.0)]
|
||||
[InlineData("支出:买菜 42.5", "支出", "买菜 42.5", 42.5)]
|
||||
[InlineData("待办 明天交房租", "待办", "明天交房租", null)]
|
||||
[InlineData("随便记一笔", "笔记", "随便记一笔", null)]
|
||||
[InlineData("提醒我10月15日还信用卡", "待办", "我10月15日还信用卡", null)]
|
||||
public void ClassifyText_Works(string input, string category, string content, double? amount)
|
||||
{
|
||||
var r = _classifier.ClassifyText(input);
|
||||
Assert.False(r.IsCommand);
|
||||
Assert.Equal(category, r.Category);
|
||||
Assert.Equal(content, r.Content);
|
||||
if (amount is null)
|
||||
Assert.Null(r.Amount);
|
||||
else
|
||||
Assert.Equal((decimal)amount, r.Amount);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("帮助", "help")]
|
||||
[InlineData("查询", "list")]
|
||||
[InlineData("统计", "stats")]
|
||||
[InlineData("聊天模式", "mode_chat")]
|
||||
[InlineData("联网聊天模式", "mode_chat_online")]
|
||||
[InlineData("记录模式", "mode_record")]
|
||||
[InlineData("当前模式", "mode_status")]
|
||||
[InlineData("我的提醒", "reminders")]
|
||||
[InlineData("取消提醒 3", "cancel_reminder:3")]
|
||||
[InlineData("我的待办", "todos")]
|
||||
[InlineData("导出本月", "export_month")]
|
||||
[InlineData("删除 12", "delete_record:12")]
|
||||
[InlineData("删除上一条", "delete_last")]
|
||||
[InlineData("改金额 12 35", "set_amount:12:35")]
|
||||
[InlineData("改分类 12 支出", "set_category:12:支出")]
|
||||
[InlineData("完成 3", "complete_todo:3")]
|
||||
[InlineData("完成待办 3", "complete_todo:3")]
|
||||
[InlineData("延期 3 明天", "defer_todo:3:明天")]
|
||||
[InlineData("今天", "list_range:today")]
|
||||
[InlineData("昨天", "list_range:yesterday")]
|
||||
[InlineData("本周", "list_range:this_week")]
|
||||
[InlineData("上周花了多少", "stats_range:last_week")]
|
||||
[InlineData("昨天午餐", "list_range:yesterday:午餐")]
|
||||
[InlineData("画图 一只橘猫", "draw_image:一只橘猫")]
|
||||
[InlineData("生成图片 海边日落", "draw_image:海边日落")]
|
||||
[InlineData("画图 第一行\n第二行风格", "draw_image:第一行\n第二行风格")]
|
||||
[InlineData("绘图 一只橘猫", "draw_grok_image:一只橘猫")]
|
||||
[InlineData("绘图:海边日落", "draw_grok_image:海边日落")]
|
||||
[InlineData("绘图 第一行\n第二行风格", "draw_grok_image:第一行\n第二行风格")]
|
||||
public void Commands_Detected(string input, string command)
|
||||
{
|
||||
var r = _classifier.ClassifyText(input);
|
||||
Assert.True(r.IsCommand);
|
||||
Assert.Equal(command, r.Command);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Today_With_Purchase_Is_Not_Command()
|
||||
{
|
||||
var r = _classifier.ClassifyText("今天买了菜花了30元");
|
||||
Assert.False(r.IsCommand);
|
||||
Assert.Equal("支出", r.Category);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("今天是几号?")]
|
||||
[InlineData("今天的日期")]
|
||||
[InlineData("昨天星期几")]
|
||||
public void Today_Date_Questions_Are_Not_Range_Commands(string input)
|
||||
{
|
||||
var r = _classifier.ClassifyText(input);
|
||||
Assert.False(r.IsCommand);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using RecordBot.Options;
|
||||
using Xunit;
|
||||
|
||||
namespace RecordBot.Tests;
|
||||
|
||||
public class LlmVisionTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("qwen3.5-omni-plus", true, true)]
|
||||
[InlineData("qwen-vl-plus", true, false)]
|
||||
[InlineData("qwen3.8-max", false, false)]
|
||||
[InlineData("qwen-plus", false, false)]
|
||||
public void ModelFlags(string model, bool vision, bool stream)
|
||||
{
|
||||
var opts = new LlmOptions { Model = model };
|
||||
Assert.Equal(vision, opts.CanSeeImages);
|
||||
Assert.Equal(stream, opts.RequiresStream);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using RecordBot.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace RecordBot.Tests;
|
||||
|
||||
public class PendingDrawCoordinatorTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task CompleteWaitWithImage_UnblocksWaiter_WithoutCancelRace()
|
||||
{
|
||||
var c = new PendingDrawCoordinator();
|
||||
var (_, imageTask) = c.BeginWait("u1", "改成水彩", null);
|
||||
|
||||
var bytes = new byte[] { 1, 2, 3 };
|
||||
var job = c.TryCompleteWaitWithImage("u1", bytes);
|
||||
Assert.NotNull(job);
|
||||
Assert.Equal("改成水彩", job!.Prompt);
|
||||
|
||||
// Simulate scheduler: WhenAny with a long delay — imageTask should win with bytes.
|
||||
var delay = Task.Delay(TimeSpan.FromSeconds(5));
|
||||
var completed = await Task.WhenAny(imageTask, delay);
|
||||
Assert.Same(imageTask, completed);
|
||||
Assert.True(imageTask.IsCompletedSuccessfully);
|
||||
Assert.Equal(bytes, await imageTask);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CompleteAfterWhenAnyStarted_StillYieldsBytes()
|
||||
{
|
||||
var c = new PendingDrawCoordinator();
|
||||
var (_, imageTask) = c.BeginWait("u2", "一只猫", null);
|
||||
|
||||
var wait = Task.WhenAny(imageTask, Task.Delay(TimeSpan.FromSeconds(2)));
|
||||
await Task.Delay(20);
|
||||
Assert.NotNull(c.TryCompleteWaitWithImage("u2", [9, 9]));
|
||||
|
||||
await wait;
|
||||
Assert.True(imageTask.IsCompletedSuccessfully);
|
||||
Assert.Equal(new byte[] { 9, 9 }, await imageTask);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CompleteWait_WithoutWait_StashesRecent()
|
||||
{
|
||||
var c = new PendingDrawCoordinator();
|
||||
Assert.Null(c.TryCompleteWaitWithImage("nobody", [1, 2]));
|
||||
var got = c.TryTakeRecentImage("nobody", TimeSpan.FromSeconds(5));
|
||||
Assert.Equal(new byte[] { 1, 2 }, got);
|
||||
Assert.Null(c.TryTakeRecentImage("nobody", TimeSpan.FromSeconds(5)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\RecordBot\RecordBot.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,87 @@
|
||||
using RecordBot.Data;
|
||||
using RecordBot.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace RecordBot.Tests;
|
||||
|
||||
public class ReminderTimeParserTests
|
||||
{
|
||||
private static readonly DateTimeOffset Now =
|
||||
new(2026, 10, 15, 20, 0, 0, TimeSpan.FromHours(8));
|
||||
|
||||
[Theory]
|
||||
[InlineData("提醒我明天8点50打卡", true, "打卡", 16, 8, 50)]
|
||||
[InlineData("提醒我明天08:50打卡", true, "打卡", 16, 8, 50)]
|
||||
[InlineData("叫我明天9点开会", true, "开会", 16, 9, 0)]
|
||||
[InlineData("提醒我后天10点半出门", true, "出门", 17, 10, 30)]
|
||||
[InlineData("提醒我30分钟后喝水", true, "喝水", 15, 20, 30)]
|
||||
[InlineData("明天六点四十提醒我起床", true, "起床", 16, 6, 40)]
|
||||
public void TryParseLocal_CommonPhrases(
|
||||
string input,
|
||||
bool ok,
|
||||
string content,
|
||||
int day,
|
||||
int hour,
|
||||
int minute)
|
||||
{
|
||||
var success = ReminderParser.TryParseLocal(input, Now, out var result);
|
||||
Assert.Equal(ok, success);
|
||||
Assert.Equal(ok, result.Ok);
|
||||
if (!ok) return;
|
||||
|
||||
Assert.Contains(content, result.Content);
|
||||
Assert.Equal(day, result.FireAtLocal.Day);
|
||||
Assert.Equal(hour, result.FireAtLocal.Hour);
|
||||
Assert.Equal(minute, result.FireAtLocal.Minute);
|
||||
Assert.Equal(ReminderRecurrence.None, result.Recurrence);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryParseLocal_DailyChineseTime()
|
||||
{
|
||||
// Now is 20:00 on Oct 15 → next 08:50 is Oct 16
|
||||
var ok = ReminderParser.TryParseLocal("每天早上八点五十提醒我打卡", Now, out var result);
|
||||
Assert.True(ok);
|
||||
Assert.True(result.Ok);
|
||||
Assert.Equal(ReminderRecurrence.Daily, result.Recurrence);
|
||||
Assert.Contains("打卡", result.Content);
|
||||
Assert.Equal(16, result.FireAtLocal.Day);
|
||||
Assert.Equal(8, result.FireAtLocal.Hour);
|
||||
Assert.Equal(50, result.FireAtLocal.Minute);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryParseLocal_DailySameDayIfStillFuture()
|
||||
{
|
||||
var morning = new DateTimeOffset(2026, 10, 15, 7, 0, 0, TimeSpan.FromHours(8));
|
||||
var ok = ReminderParser.TryParseLocal("每天早上8点50提醒我打卡", morning, out var result);
|
||||
Assert.True(ok);
|
||||
Assert.Equal(ReminderRecurrence.Daily, result.Recurrence);
|
||||
Assert.Equal(15, result.FireAtLocal.Day);
|
||||
Assert.Equal(8, result.FireAtLocal.Hour);
|
||||
Assert.Equal(50, result.FireAtLocal.Minute);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("六", 6)]
|
||||
[InlineData("十", 10)]
|
||||
[InlineData("十二", 12)]
|
||||
[InlineData("四十", 40)]
|
||||
[InlineData("五十", 50)]
|
||||
public void TryParseChineseInt_Works(string input, int expected)
|
||||
{
|
||||
Assert.True(ReminderParser.TryParseChineseInt(input, out var n));
|
||||
Assert.Equal(expected, n);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("提醒我明天8点50打卡", true)]
|
||||
[InlineData("叫我5分钟后", true)]
|
||||
[InlineData("每天早上八点五十提醒我打卡", true)]
|
||||
[InlineData("午餐花了35", false)]
|
||||
[InlineData("待办 买菜", false)]
|
||||
public void LooksLikeReminder_Works(string input, bool expected)
|
||||
{
|
||||
Assert.Equal(expected, ReminderParser.LooksLikeReminder(input));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user