feat(jd-publish): 尺码表空格 AI 补值端点 POST /size-values(B184-05 拍板「必填项一定要填,采集没有的用 AI 生成」)——JdSizeChartAiService 按 外观专利→货源查询 模块顺序取用户登记为龙抬头渠道的 LLM 密钥代调 chat/completions(enable_thinking/thinking_budget 按 LlmChannel 登记,gm-3.1-flash-lite),密钥不出服务端;解析 choices[0].message.content 归一 行→列→值序列(inputCount 长度对齐、区间倒挂/非数值/超范围整格丢弃、字符串数值兼容、markdown 围栏剥离),任何故障 WARN+空映射不抛错(客户端按身高比例公式回退,LongtaitouBalanceClient 同形降级纪律);JdPublishController 端点带 requireSessionOwnerUserId 会话身份。判据 JdSizeChartAiServiceTest 12 支(解析直测不 mock HTTP,LongtaitouBalanceClientTest 同形),mvn -o test-compile rc=0

This commit is contained in:
2026-11-21 17:16:25 +08:00
parent 0a769fa8d3
commit 66946c2391
4 changed files with 459 additions and 0 deletions
@@ -0,0 +1,162 @@
package com.nanri.aiimage.modules.jd.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.modules.jd.model.dto.JdSizeChartValuesRequest;
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* 尺码表空格 AI 补值的口径判据(B184-05,2026-11-21 拍板「必填项一定要填,
* 采集没有的用 AI 生成」)。
*
* <p>上游 HTTP 不 mock(与 {@code LongtaitouBalanceClientTest} 同形:抽包可见解析
* 方法直测)。这一格要守的是:模型输出可以直接落进京麦数值输入框——<b>宁缺勿错</b>,
* 数量不够/区间倒挂/非数值的格整格丢弃(客户端按比例公式回退),绝不给用户一张
* 「看着填满了但数值离谱」的尺码表。</p>
*/
class JdSizeChartAiServiceTest {
private final UserApiSecretService secretService = mock(UserApiSecretService.class);
private final JdSizeChartAiService service =
new JdSizeChartAiService(secretService, new ObjectMapper());
private static JdSizeChartValuesRequest.Gap gap(String row, String column, int inputCount) {
JdSizeChartValuesRequest.Gap gap = new JdSizeChartValuesRequest.Gap();
gap.setRow(row);
gap.setColumn(column);
gap.setInputCount(inputCount);
return gap;
}
@Test
void 模型数值输出解析成行列值序列() {
String content = "{\"S\": {\"shoulder\": [47.2], \"bust\": [80.3, 81.5]},"
+ " \"M\": {\"length\": [68.5]}}";
Map<String, Map<String, List<String>>> values = service.parseValues(content, List.of(
gap("S", "shoulder", 1), gap("S", "bust", 2), gap("M", "length", 1)));
assertThat(values.get("S").get("shoulder")).containsExactly("47.2");
assertThat(values.get("S").get("bust")).containsExactly("80.3", "81.5");
assertThat(values.get("M").get("length")).containsExactly("68.5");
}
@Test
void 字符串数值与markdown围栏照常解析() {
String content = "```json\n{\"XL\": {\"sleeve\": [\"57.1\"], \"weight\": [\"62.5\", \"70\"]}}\n```";
Map<String, Map<String, List<String>>> values = service.parseValues(content, List.of(
gap("XL", "sleeve", 1), gap("XL", "weight", 2)));
assertThat(values.get("XL").get("sleeve")).containsExactly("57.1");
assertThat(values.get("XL").get("weight")).containsExactly("62.5", "70.0");
}
@Test
void 值数量不足inputCount的格整格丢弃() {
String content = "{\"S\": {\"bust\": [80.3]}}";
assertThat(service.parseValues(content,
List.of(gap("S", "bust", 2)))).isEmpty();
}
@Test
void 区间倒挂的格丢弃不给半截值() {
String content = "{\"M\": {\"bust\": [81.5, 80.3]}}";
assertThat(service.parseValues(content,
List.of(gap("M", "bust", 2)))).isEmpty();
}
@Test
void 非数值超范围与零值格丢弃() {
String content = "{\"S\": {\"shoulder\": [\"肩宽\"]}, \"M\": {\"shoulder\": [-3]},"
+ " \"L\": {\"shoulder\": [0]}, \"XL\": {\"shoulder\": [900]}}";
Map<String, Map<String, List<String>>> values = service.parseValues(content, List.of(
gap("S", "shoulder", 1), gap("M", "shoulder", 1),
gap("L", "shoulder", 1), gap("XL", "shoulder", 1)));
assertThat(values).isEmpty();
}
@Test
void 垃圾输出返回空映射不抛异常() {
assertThat(service.parseValues("", List.of(gap("S", "bust", 1)))).isEmpty();
assertThat(service.parseValues("这是一句话", List.of(gap("S", "bust", 1)))).isEmpty();
assertThat(service.parseValues("[1,2,3]", List.of(gap("S", "bust", 1)))).isEmpty();
}
@Test
void prompt带类目上下文与格清单() {
JdSizeChartValuesRequest request = new JdSizeChartValuesRequest();
request.setGaps(List.of(gap("S", "shoulder", 1), gap("S", "bust", 2)));
request.setCategoryPath("男士套头毛衣");
String prompt = service.buildPrompt(request, request.getGaps());
assertThat(prompt).contains("男士套头毛衣").contains("S: ")
.contains("shoulder(要1个值)").contains("bust(要2个值)")
.contains("区间左值必须小于右值").contains("JSON");
}
@Test
void 无categoryPath的prompt照常构造() {
JdSizeChartValuesRequest request = new JdSizeChartValuesRequest();
request.setGaps(List.of(gap("M", "length", 1)));
assertThat(service.buildPrompt(request, request.getGaps()))
.contains("M: ").doesNotContain("null");
}
@Test
void 龙抬头密钥按模块顺序取用() {
when(secretService.findChannel(eq(7L), eq("appearance-patent"))).thenReturn("longtaitou");
when(secretService.findPlainValue(eq(7L), eq("appearance-patent"))).thenReturn("sk-ltt-1");
assertThat(service.findLongtaitouKey(7L)).isEqualTo("sk-ltt-1");
}
@Test
void 外观专利渠道不匹配时回落货源查询模块() {
when(secretService.findChannel(eq(8L), eq("appearance-patent"))).thenReturn("t8star");
when(secretService.findPlainValue(eq(8L), eq("appearance-patent"))).thenReturn("sk-t8");
when(secretService.findChannel(eq(8L), eq("similar-asin"))).thenReturn("longtaitou");
when(secretService.findPlainValue(eq(8L), eq("similar-asin"))).thenReturn("sk-ltt-2");
assertThat(service.findLongtaitouKey(8L)).isEqualTo("sk-ltt-2");
}
@Test
void 密钥登记为空或无龙抬头时降级公式回退() {
when(secretService.findChannel(eq(9L), anyString())).thenReturn(null);
assertThat(service.findLongtaitouKey(9L)).isEmpty();
when(secretService.findChannel(eq(10L), eq("appearance-patent"))).thenReturn("longtaitou");
when(secretService.findPlainValue(eq(10L), eq("appearance-patent"))).thenReturn("");
assertThat(service.findLongtaitouKey(10L)).isEmpty();
}
@Test
void generate对脏入参与无密钥一律空映射() {
JdSizeChartValuesRequest request = new JdSizeChartValuesRequest();
request.setGaps(List.of(gap("S", "bust", 1)));
assertThat(service.generate(null, request)).isEmpty();
assertThat(service.generate(1L, null)).isEmpty();
assertThat(service.generate(1L, new JdSizeChartValuesRequest())).isEmpty();
when(secretService.findChannel(any(), anyString())).thenReturn(null);
assertThat(service.generate(11L, request)).isEmpty();
}
}