Files
KJD/test/GoodsComplianceSmoke.cs
2026-11-09 10:50:48 +08:00

80 lines
4.7 KiB
C#

using AllBase.Helper;
using AllBase;
using AllBase.Model;
using System.Collections.Generic;
using System;
using System.Linq;
using Work.Services;
// Compile against the built Work.dll, AllBase.dll and Newtonsoft.Json.dll and run in an isolated directory.
internal static class GoodsComplianceSmoke
{
private static void Check(bool passed)
{
if (!passed) throw new Exception("商品合规自测失败");
}
public static void Main()
{
var user = Guid.NewGuid().ToString();
BaseClass.UserName = user;
GlobalConfig.GlobalSettings.GoodsCompliance = new ComplianceOptions { Keywords = "测试" };
var cloud = new ComplianceGoods { id = "123", type = ComplianceGoods.Cloud };
var library = new ComplianceGoods { id = "123", type = ComplianceGoods.Library };
var attempted = new List<ComplianceGoods>();
var failures = new List<string>();
var batch = GoodsComplianceService.DetectSelectedAsync(new[] { cloud, cloud, library }, item =>
{
attempted.Add(item);
throw new InvalidOperationException("读取失败");
}, (id, status) => { if (status == "失败:读取失败") failures.Add(id); }).GetAwaiter().GetResult();
Check(attempted.SequenceEqual(new[] { cloud, library }) && failures.Count == 2);
var file = System.IO.Directory.GetFiles(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
"data", "config"), "goods_compliance_" + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(user)) + ".json").Single();
Check(Newtonsoft.Json.JsonConvert.DeserializeObject<List<ComplianceGoods>>(System.IO.File.ReadAllText(file))
.Select(x => x.type).SequenceEqual(new[] { cloud.type, library.type }));
Check(batch.Safe == 0 && batch.Risk == 0 && batch.Failed == 2 && batch.Unfinished == 0);
using (var stop = new System.Threading.CancellationTokenSource())
{
attempted.Clear();
var stopped = GoodsComplianceService.DetectSelectedAsync(new[] { cloud, library }, item =>
{
attempted.Add(item);
stop.Cancel();
return new Product { ProductId = item.id };
}, (id, status) => { }, stop.Token).GetAwaiter().GetResult();
Check(attempted.SequenceEqual(new[] { cloud }) && stopped.Unfinished == 2 && stopped.Failed == 0);
Check(Newtonsoft.Json.JsonConvert.DeserializeObject<List<ComplianceGoods>>(System.IO.File.ReadAllText(file)).Count == 2);
}
var words = GoodsComplianceService.Keywords("广告,测试,abc 促销;ABC|示例");
Check(words.Length == 5);
Check(GoodsComplianceService.Matches("ABC广告", words).SequenceEqual(new[] { "广告", "abc" }));
Check(GoodsComplianceService.Matches("普通商品", words).Length == 0);
// Exercise detection without calling the reporting API.
var detect = typeof(GoodsComplianceService).GetMethod("DetectProductAsync",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
var titleHit = ((System.Threading.Tasks.Task<ComplianceResult>)detect.Invoke(null, new object[]
{
new Product { ProductId = "title", Title = "ABC广告" }, new ComplianceOptions(), words, null,
System.Threading.CancellationToken.None
})).GetAwaiter().GetResult();
Check(titleHit.Status == "有风险" && titleHit.Summary == "有风险:标题命中关键词:广告,abc");
var invalidImage = new Product { ProductId = "bad-image", MainImages = new List<string> { "file:///image.jpg" } };
var statuses = new List<string>();
GoodsComplianceService.DetectSelectedAsync(new[] { cloud }, item => invalidImage,
(id, status) => statuses.Add(status)).GetAwaiter().GetResult();
Check(statuses.SequenceEqual(new[] { "读取详情中", "正在检测第 1 张图片", "失败:第 1 张图片检测失败:图片链接必须为 HTTP 或 HTTPS 地址。" }));
var products = new[] { new Product(), new Product { ProductId = "empty" }, invalidImage };
var callbacks = new List<Product>();
GoodsComplianceService.DetectAsync(products, new ComplianceOptions { Keywords = "测试" }, (product, result) =>
{
Check(result.Error != null);
if (product == invalidImage) Check(result.Error.Contains("第 1 张图片") && result.Error.Contains("HTTP"));
callbacks.Add(product);
}).GetAwaiter().GetResult();
Check(callbacks.SequenceEqual(products));
System.IO.File.Delete(file);
Console.WriteLine("商品合规队列与匹配自测通过");
}
}