feat(phase-4): 建单凭据需求按 task type 登记进 TaskModuleRegistry(B72-01)
`/newApi` 是整包转发的透传面(客户端只搬字节),载荷里有什么凭据需求客户端猜不出来; 铁律 27 的建单闸门此前只有两处各写一枚枚举,"这一类要什么凭据"没有一处正本。本笔把它 收成表:`TaskModuleRegistry.Module` 新增 `submitSecret`(16 类逐类登记), `requireSubmitSecret` 新增按 type 的入口,enum 形态降为包内原语——包外再想硬编一把密钥 就编译不过,只能上册表。 两处判断写死在代码里: - **登记为空是结论,不是漏填**。外观专利/货源查询要 LLM 密钥;其余几类缺的是代理, 而缺代理在执行侧是「直连降级」不是注定失败(铁律 27 边界 c),拦了会把现在能跑的功能 判死——所以本笔只登记、不新增任何拦截,`enqueue_json` 那半还等"这台机器直连能不能 采到"的实测读数(B72-01 后半欠账原样留着)。 - **空登记调到这里当场 IllegalStateException,不静默放行**。空转的闸门比没有闸门更坏, 它让下一个人以为这条链已经防住了(门禁 1 同族)。同理 `submitSecretModuleOf` 对不在 清单里的 type 抛错而不回落:「没登记」与「登记过=不需要」不许揉成同一个值。 判据:`TaskModuleCoverageTest` +3 例(登记表与实际问闸门的类型双向对撞/每个模块都 登记过/未知类型当场抛,源码扫描按 ConsoleWideningGuardrailTest 的 mainRoot 口径, 定位不到即抛不空扫)+`UserApiSecretServiceTest` +4 例(按 type 拒/放行不读库/ 空登记抛 IllegalStateException 且不碰库/未注册抛 IllegalArgumentException)。 两处快照测试跟着改到类型形态(`anyString()` 消歧重载,摘掉不再使用的 enum 导入)。 反向验证:外观专利不登记→2 红;给 COLLECT_DATA 顺手加登记→2 红;静默放行→1 红;复原复绿。 行为对现网零变化:两个调用点问的还是同一把钥匙、同一句文案。 门禁:`mvn -o test-compile` rc=0;`mvn -o test` 整量 5274/0 失败/0 错误/19 skipped BUILD SUCCESS(Phase 3 在册 5267+本笔 7)。
This commit is contained in:
+54
-17
@@ -1,8 +1,11 @@
|
||||
package com.nanri.aiimage.common.module;
|
||||
|
||||
import com.nanri.aiimage.modules.usersecret.support.UserSecretModule;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -16,6 +19,12 @@ import java.util.stream.Collectors;
|
||||
*
|
||||
* 心跳与陈旧判死两处是代码分支驱动(switch / 逐模块委派),无法纯数据派生,
|
||||
* 新增模块时仍需实现对应 Handler,测试会提示缺失。
|
||||
*
|
||||
* 建单凭据({@code submitSecret},B72-01)同样登记在本表:「这一类任务要拿用户的哪把密钥跑」
|
||||
* 的唯一出处。`/newApi` 是整包转发的透传面,客户端猜不出载荷要什么凭据,只能由服务端按 type
|
||||
* 逐类登记。这里刻意区分两件事:登记了 = 建单口必须问这把;`Optional.empty()` = **登记过的结论**
|
||||
* 「这一类建单时不需要用户凭据」,不是漏填——`TaskModuleCoverageTest` 把「问了闸门却没登记」与
|
||||
* 「登记了却没问闸门」两个方向都钉红,空壳闸门比没闸门更坏(铁律 27 边界 c)。
|
||||
*/
|
||||
public final class TaskModuleRegistry {
|
||||
|
||||
@@ -31,28 +40,34 @@ public final class TaskModuleRegistry {
|
||||
* @param selfScheduledStaleCheck 该模块的陈旧判死自带更快的调度(publish 60s / collect-data 30s),
|
||||
* 刻意不并入 2 分钟一轮的巡检线——并入会显著拉长判死时延。
|
||||
* 这类模块的"不在委托名单"是设计差异,不是漏接;由覆盖面测试固定。
|
||||
* @param submitSecret 建单口要问的那把用户密钥;{@code Optional.empty()} 是**登记过的结论**
|
||||
* "这一类不缺凭据也能跑"(例:缺代理在本机执行类是直连降级,
|
||||
* 不是注定失败——铁律 27 边界 c,拦了会把现在能跑的功能判死)。
|
||||
*/
|
||||
public record Module(String type, String label, boolean resultFileJob, boolean ageCleanup,
|
||||
boolean delegatedStaleCheck, boolean selfScheduledStaleCheck) {
|
||||
boolean delegatedStaleCheck, boolean selfScheduledStaleCheck,
|
||||
Optional<UserSecretModule> submitSecret) {
|
||||
}
|
||||
|
||||
private static final List<Module> MODULES = List.of(
|
||||
new Module("PUBLISH", "上架", true, false, false, true),
|
||||
new Module("DEDUPE", "数据去重", false, true, false, false),
|
||||
new Module("SPLIT", "数据拆分", false, true, false, false),
|
||||
new Module("CONVERT", "格式转换", false, true, false, false),
|
||||
new Module("DELETE_BRAND", "删除ASIN", true, true, false, false),
|
||||
new Module("PRODUCT_RISK_RESOLVE", "商品风险解决", true, true, false, false),
|
||||
new Module("PRICE_TRACK", "跟价", true, true, false, false),
|
||||
new Module("SHOP_MATCH", "定时匹配", true, true, false, false),
|
||||
new Module("PATROL_DELETE", "巡店删除", true, true, false, false),
|
||||
new Module("QUERY_ASIN", "查询ASIN", true, true, false, false),
|
||||
new Module("WITHDRAW", "取款", true, true, false, false),
|
||||
new Module("APPEARANCE_PATENT", "外观专利检测", true, true, true, false),
|
||||
new Module("SIMILAR_ASIN", "货源查询", true, true, true, false),
|
||||
new Module("COLLECT_DATA", "采集数据", true, true, false, true),
|
||||
new Module("SHOP_DATA_CRAWL", "店铺数据抓取", true, false, true, false),
|
||||
new Module("BRAND", "品牌检测", true, false, true, false)
|
||||
new Module("PUBLISH", "上架", true, false, false, true, Optional.empty()),
|
||||
new Module("DEDUPE", "数据去重", false, true, false, false, Optional.empty()),
|
||||
new Module("SPLIT", "数据拆分", false, true, false, false, Optional.empty()),
|
||||
new Module("CONVERT", "格式转换", false, true, false, false, Optional.empty()),
|
||||
new Module("DELETE_BRAND", "删除ASIN", true, true, false, false, Optional.empty()),
|
||||
new Module("PRODUCT_RISK_RESOLVE", "商品风险解决", true, true, false, false, Optional.empty()),
|
||||
new Module("PRICE_TRACK", "跟价", true, true, false, false, Optional.empty()),
|
||||
new Module("SHOP_MATCH", "定时匹配", true, true, false, false, Optional.empty()),
|
||||
new Module("PATROL_DELETE", "巡店删除", true, true, false, false, Optional.empty()),
|
||||
new Module("QUERY_ASIN", "查询ASIN", true, true, false, false, Optional.empty()),
|
||||
new Module("WITHDRAW", "取款", true, true, false, false, Optional.empty()),
|
||||
new Module("APPEARANCE_PATENT", "外观专利检测", true, true, true, false,
|
||||
Optional.of(UserSecretModule.APPEARANCE_PATENT)),
|
||||
new Module("SIMILAR_ASIN", "货源查询", true, true, true, false,
|
||||
Optional.of(UserSecretModule.SIMILAR_ASIN)),
|
||||
new Module("COLLECT_DATA", "采集数据", true, true, false, true, Optional.empty()),
|
||||
new Module("SHOP_DATA_CRAWL", "店铺数据抓取", true, false, true, false, Optional.empty()),
|
||||
new Module("BRAND", "品牌检测", true, false, true, false, Optional.empty())
|
||||
);
|
||||
|
||||
private TaskModuleRegistry() {
|
||||
@@ -100,6 +115,28 @@ public final class TaskModuleRegistry {
|
||||
.collect(Collectors.toUnmodifiableSet());
|
||||
}
|
||||
|
||||
/**
|
||||
* 建单口这一类任务要问的那把用户密钥(B72-01 的唯一出处)。
|
||||
*
|
||||
* @throws IllegalArgumentException moduleType 根本不在清单里(拼错的常量名)。
|
||||
* 刻意不跟 {@link #labelOf} 一样回落原值:「没登记」与「登记过=不需要」是两件事,
|
||||
* 揉成同一个空值会让调用方把漏接当成结论。
|
||||
*/
|
||||
public static Optional<UserSecretModule> submitSecretModuleOf(String moduleType) {
|
||||
for (Module module : MODULES) {
|
||||
if (module.type().equals(moduleType)) {
|
||||
return module.submitSecret();
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("模块不在清单里: " + moduleType);
|
||||
}
|
||||
|
||||
/** 登记了建单凭据的模块(覆盖面测试用它对撞「实际在建单口问闸门的类型」)。 */
|
||||
public static Set<String> submitSecretModuleTypes() {
|
||||
return MODULES.stream().filter(module -> module.submitSecret().isPresent())
|
||||
.map(Module::type).collect(Collectors.toUnmodifiableSet());
|
||||
}
|
||||
|
||||
public static String labelOf(String moduleType) {
|
||||
for (Module module : MODULES) {
|
||||
if (module.type().equals(moduleType)) {
|
||||
|
||||
+3
-2
@@ -189,10 +189,11 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
|
||||
|
||||
public AppearancePatentParseVo parseAndCreateTask(AppearancePatentParseRequest request, String deviceId) {
|
||||
// 密钥闸门排在解析之前:Excel 解析与建单四段都要花时间与配额,而缺密钥的单跑完也只是
|
||||
// 一张没有 LLM 结论的表(判据见 UserApiSecretService#requireSubmitSecret)。
|
||||
// 一张没有 LLM 结论的表(判据见 UserApiSecretService#requireSubmitSecret;
|
||||
// 这一类要哪把密钥登记在 TaskModuleRegistry,不在本文件写死)。
|
||||
requireUserId(request.getUserId());
|
||||
userApiSecretService.requireSubmitSecret(request.getUserId(),
|
||||
UserSecretModule.APPEARANCE_PATENT, request.getApiKey());
|
||||
MODULE_TYPE, request.getApiKey());
|
||||
return parsePipeline().parseAndCreateTask(request, deviceId);
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -76,7 +76,6 @@ import com.nanri.aiimage.modules.task.service.support.TaskFailureLedger;
|
||||
import com.nanri.aiimage.modules.task.service.TaskVisibleFailureNotice;
|
||||
import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
|
||||
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
|
||||
import com.nanri.aiimage.modules.usersecret.support.UserSecretModule;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -393,9 +392,10 @@ public class SimilarAsinTaskService implements SimilarAsinPipelineHost {
|
||||
throw new BusinessException("user_id 不合法");
|
||||
}
|
||||
// 密钥闸门排在解析之前:解析、建单四段都要花时间与配额,而缺密钥的单跑完只是
|
||||
// 一张没有 LLM 结论的表(判据见 UserApiSecretService#requireSubmitSecret)。
|
||||
// 一张没有 LLM 结论的表(判据见 UserApiSecretService#requireSubmitSecret;
|
||||
// 这一类要哪把密钥登记在 TaskModuleRegistry,不在本文件写死)。
|
||||
userApiSecretService.requireSubmitSecret(request.getUserId(),
|
||||
UserSecretModule.SIMILAR_ASIN, request.getApiKey());
|
||||
MODULE_TYPE, request.getApiKey());
|
||||
if (request.getFiles() == null || request.getFiles().isEmpty()) {
|
||||
throw new BusinessException("请先上传 Excel 文件");
|
||||
}
|
||||
|
||||
+23
-1
@@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.usersecret.service;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.module.TaskModuleRegistry;
|
||||
import com.nanri.aiimage.common.text.SecretMasking;
|
||||
import com.nanri.aiimage.modules.admin.spi.UserSecretCleanupPort;
|
||||
import com.nanri.aiimage.common.security.ShopCredentialCryptoService;
|
||||
@@ -302,8 +303,11 @@ public class UserApiSecretService implements UserSecretCleanupPort {
|
||||
* 比一句「没配密钥」的失败更难发现。
|
||||
*
|
||||
* <p>调用方须先校验 userId(各模块自己的入口文案为准),这里不重复判。
|
||||
*
|
||||
* <p>包外不许直接调这一枚:模块→密钥的登记只认 {@link TaskModuleRegistry},
|
||||
* 跨包的入口是下面的按 type 版本(B72-01)。
|
||||
*/
|
||||
public void requireSubmitSecret(Long userId, UserSecretModule module, String callerSuppliedValue) {
|
||||
void requireSubmitSecret(Long userId, UserSecretModule module, String callerSuppliedValue) {
|
||||
if (hasText(callerSuppliedValue)) {
|
||||
return;
|
||||
}
|
||||
@@ -314,6 +318,24 @@ public class UserApiSecretService implements UserSecretCleanupPort {
|
||||
+ "缺了只会得到一张没有检测结论的结果表。请先在「密钥设置」里填好并保存。");
|
||||
}
|
||||
|
||||
/**
|
||||
* 建单前置(按 task type 问):这一类任务要拿用户的哪把密钥,唯一出处是
|
||||
* {@link TaskModuleRegistry#submitSecretModuleOf(String)}——不是调用方各写一枚枚举,
|
||||
* 更不是客户端猜({@code /newApi} 是整包转发的透传面,载荷里有什么凭据需求只有服务端按
|
||||
* type 登记才说得清,B72-01)。
|
||||
*
|
||||
* <p>登记成「这一类建单不需要用户凭据」的类型调到这里<b>当场 IllegalStateException</b>,
|
||||
* 不静默放行:空转的闸门比没有闸门更坏,它让下一个人以为这条链已经防住了。
|
||||
* 要把某类任务纳入闸门,先在被判死的形状上有实证、再上册表,不要反过来。
|
||||
*/
|
||||
public void requireSubmitSecret(Long userId, String moduleType, String callerSuppliedValue) {
|
||||
UserSecretModule module = TaskModuleRegistry.submitSecretModuleOf(moduleType)
|
||||
.orElseThrow(() -> new IllegalStateException("模块「" + moduleType
|
||||
+ "」在 TaskModuleRegistry 登记为「建单不需要用户凭据」,不该调这道闸门:"
|
||||
+ "要么把这一类真要的那把密钥登记进表,要么把闸门从这条链上摘掉"));
|
||||
requireSubmitSecret(userId, module, callerSuppliedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 任务执行兜底读取「货源」:未配置返回空串(调用方按模块默认值处理)。
|
||||
* 代理模块额外做链接域名识别,兜住老数据(只有链接、没有供应商列)的存量行。
|
||||
|
||||
+2
-4
@@ -33,7 +33,6 @@ import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
|
||||
import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
|
||||
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
|
||||
import com.nanri.aiimage.modules.usersecret.support.UserSecretModule;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
@@ -502,7 +501,7 @@ class AppearancePatentCreateTaskSnapshotTest {
|
||||
Harness harness = harnessWithFiles("uploads/main.xlsx", mixedWorkbook(), null, null);
|
||||
doThrow(new com.nanri.aiimage.common.exception.BusinessException("该账号还没有配置外观专利密钥"))
|
||||
.when(harness.userApiSecretService)
|
||||
.requireSubmitSecret(any(), any(), any());
|
||||
.requireSubmitSecret(any(), anyString(), any());
|
||||
|
||||
assertThrows(com.nanri.aiimage.common.exception.BusinessException.class,
|
||||
() -> harness.service().parseAndCreateTask(
|
||||
@@ -519,8 +518,7 @@ class AppearancePatentCreateTaskSnapshotTest {
|
||||
|
||||
harness.service().parseAndCreateTask(request(7L, List.of(source("uploads/main.xlsx", "17.xlsx"))), "d");
|
||||
|
||||
verify(harness.userApiSecretService).requireSubmitSecret(7L,
|
||||
UserSecretModule.APPEARANCE_PATENT, "sk-test-0001");
|
||||
verify(harness.userApiSecretService).requireSubmitSecret(7L, "APPEARANCE_PATENT", "sk-test-0001");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+140
@@ -9,12 +9,23 @@ import com.nanri.aiimage.config.ModuleCleanupProperties;
|
||||
import com.nanri.aiimage.config.TaskFileJobConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
@@ -110,4 +121,133 @@ class TaskModuleCoverageTest {
|
||||
assertTrue(keys.contains("APPEARANCE_PATENT"));
|
||||
assertTrue(keys.contains("BRAND"));
|
||||
}
|
||||
|
||||
// ---------- 建单凭据登记(B72-01,Phase 4) ----------
|
||||
|
||||
/**
|
||||
* 「这一类任务建单要拿用户的哪把密钥」登记表,必须与**实际在建单口问闸门的类型**一模一样。
|
||||
*
|
||||
* <p>两个方向都得红,缺一个方向这张表就会烂成摆设:
|
||||
* <ul>
|
||||
* <li>登记了却没人问 = 空转的登记表,下一个人照它判断"这类已经防住了"会做错事
|
||||
* (门禁 1 反 stub 的同一家族:声称保护但不保护);</li>
|
||||
* <li>问了却没登记 = 运行时 {@code IllegalStateException}({@code UserApiSecretService}
|
||||
* 对空登记刻意不静默放行),这里提前把它抓成红,不留到线上。</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>为什么要有这张表:{@code /newApi} 是整包转发的透传面(客户端只搬字节),
|
||||
* 载荷里有什么凭据需求客户端猜不出来,只能由服务端按 type 逐类登记(B72-01)。
|
||||
*/
|
||||
@Test
|
||||
void submitSecretDeclarationsCoverExactlyTheGatedTypes() {
|
||||
Set<String> gated = gatedSubmitSecretModuleTypes();
|
||||
|
||||
assertFalse(gated.isEmpty(), "一个建单闸门都没扫到 = 源码路径写错了,整支护栏在空转");
|
||||
assertEquals(TaskModuleRegistry.submitSecretModuleTypes(), gated,
|
||||
"登记表与建单口实际问闸门的类型不一致(唯一出处=TaskModuleRegistry.submitSecret)");
|
||||
}
|
||||
|
||||
/**
|
||||
* 登记为「不需要凭据」是**结论**而不是漏填,改这个结论要拍板不要顺手。
|
||||
*
|
||||
* <p>缺代理的几类(采集数据/店铺数据抓取/品牌检测/跟价…)在执行侧是「直连降级」,
|
||||
* 不是注定失败——铁律 27 边界 (c) 明确不得顺手拦,拦了会把现在能跑的功能判死。
|
||||
* 要动这一档,先有"这台机器直连能不能采到"的实测读数(欠账记在 B72-01 后半)。
|
||||
*/
|
||||
@Test
|
||||
void everyRegisteredModuleDeclaresItsCredentialNeed() {
|
||||
Set<String> declared = new LinkedHashSet<>();
|
||||
for (String moduleType : EXPECTED_MODULES) {
|
||||
// 不在清单里的类型这里会抛,所以这一轮循环同时钉住「每个模块都登记过」
|
||||
TaskModuleRegistry.submitSecretModuleOf(moduleType).ifPresent(secret -> declared.add(moduleType));
|
||||
}
|
||||
|
||||
assertEquals(Set.of("APPEARANCE_PATENT", "SIMILAR_ASIN"), declared,
|
||||
"建单口要问密钥的只有这两类(外观专利/货源查询的 LLM 检测)。其余几类缺的是代理,"
|
||||
+ "而缺代理在执行侧是「直连降级」不是注定失败——铁律 27 边界 (c) 不得顺手拦,"
|
||||
+ "拦了会把现在能跑的功能判死;要改这一档先有实测读数,拍板走 BACKLOG");
|
||||
}
|
||||
|
||||
/** 「没登记」与「登记过=不需要」不许揉成同一个值:不在清单里的 type 必须抛,不能回 null。 */
|
||||
@Test
|
||||
void unknownModuleTypeFailsLoudInsteadOfLookingLikeADeclaration() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> TaskModuleRegistry.submitSecretModuleOf("APPEARANCE_PATENTT"),
|
||||
"拼错的类型名被当成「不需要凭据」放行,等于闸门静默消失");
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫主源码里每一处 {@code .requireSubmitSecret(...)} 调用,取出它问的那个 task type。
|
||||
*
|
||||
* <p>只认带点号的调用:方法自己的声明、以及服务内部 enum 形态的转调都不带点号,
|
||||
* 数进去会把「实现」当成「调用方」而虚增分母(与 {@code SourceScanTest} 的声明/调用点同族坑)。
|
||||
*/
|
||||
private static Set<String> gatedSubmitSecretModuleTypes() {
|
||||
Set<String> gated = new LinkedHashSet<>();
|
||||
Pattern call = Pattern.compile("\\.requireSubmitSecret\\([^,]+,\\s*([A-Za-z_\"]+),");
|
||||
Pattern ownType = Pattern.compile("MODULE_TYPE\\s*=\\s*\"([A-Z_]+)\"");
|
||||
for (Path file : mainJavaFiles()) {
|
||||
String code = withoutCommentLines(read(file));
|
||||
Matcher matcher = call.matcher(code);
|
||||
if (!matcher.find()) {
|
||||
continue;
|
||||
}
|
||||
do {
|
||||
String token = matcher.group(1);
|
||||
if (token.startsWith("\"")) {
|
||||
gated.add(token.substring(1, token.length() - 1));
|
||||
continue;
|
||||
}
|
||||
assertTrue("MODULE_TYPE".equals(token),
|
||||
file.getFileName() + " 传给建单闸门的第二个参数只认类型常量或字面量: " + token);
|
||||
Matcher own = ownType.matcher(code);
|
||||
assertTrue(own.find(),
|
||||
file.getFileName() + " 调了闸门却没声明 MODULE_TYPE,登记表对不上号");
|
||||
gated.add(own.group(1));
|
||||
} while (matcher.find());
|
||||
}
|
||||
return gated;
|
||||
}
|
||||
|
||||
/** 注释行与 javadoc 续行不算锚点:在注释里提一句方法名不等于接了线。 */
|
||||
private static String withoutCommentLines(String source) {
|
||||
return source.lines()
|
||||
.map(String::trim)
|
||||
.filter(line -> !line.startsWith("//") && !line.startsWith("*")
|
||||
&& !line.startsWith("/*") && !line.startsWith("*/"))
|
||||
.collect(Collectors.joining("\n"));
|
||||
}
|
||||
|
||||
private static List<Path> mainJavaFiles() {
|
||||
try (Stream<Path> walk = Files.walk(mainRoot())) {
|
||||
return walk.filter(Files::isRegularFile)
|
||||
.filter(p -> p.getFileName().toString().endsWith(".java"))
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("扫描主源码失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/** 路径解析照抄 {@code common/security/ConsoleWideningGuardrailTest}:定位不到必须抛,不许空扫。 */
|
||||
private static Path mainRoot() {
|
||||
Path here = Path.of("").toAbsolutePath();
|
||||
Path direct = here.resolve("src/main/java/com/nanri/aiimage");
|
||||
if (Files.isDirectory(direct)) {
|
||||
return direct;
|
||||
}
|
||||
Path nested = here.resolve("backend-java").resolve("src/main/java/com/nanri/aiimage");
|
||||
if (Files.isDirectory(nested)) {
|
||||
return nested;
|
||||
}
|
||||
throw new IllegalStateException("无法定位 backend-java 源码目录(cwd=" + here + ")");
|
||||
}
|
||||
|
||||
private static String read(Path file) {
|
||||
try {
|
||||
return Files.readString(file, StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("读取失败: " + file, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-3
@@ -31,7 +31,6 @@ import com.nanri.aiimage.modules.task.service.TaskHeartbeatPositionService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
|
||||
import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
|
||||
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
|
||||
import com.nanri.aiimage.modules.usersecret.support.UserSecretModule;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
@@ -408,7 +407,7 @@ class SimilarAsinCreateTaskSnapshotTest {
|
||||
@Test
|
||||
void missingSecretRejectsBeforeParse() {
|
||||
doThrow(new BusinessException("该账号还没有配置货源查询密钥"))
|
||||
.when(userApiSecretService).requireSubmitSecret(any(), any(), any());
|
||||
.when(userApiSecretService).requireSubmitSecret(any(), anyString(), any());
|
||||
|
||||
assertThrows(BusinessException.class, () -> service.parseAndCreateTask(
|
||||
request(List.of(source("uploads/20261023/one.xlsx", "one.xlsx"))), "snapshot-device"));
|
||||
@@ -424,7 +423,7 @@ class SimilarAsinCreateTaskSnapshotTest {
|
||||
assertThrows(BusinessException.class, () -> service.parseAndCreateTask(
|
||||
request(List.of(source("uploads/20261023/one.xlsx", "one.xlsx"))), "snapshot-device"));
|
||||
|
||||
verify(userApiSecretService).requireSubmitSecret(9L, UserSecretModule.SIMILAR_ASIN, "unit-test-key");
|
||||
verify(userApiSecretService).requireSubmitSecret(9L, "SIMILAR_ASIN", "unit-test-key");
|
||||
}
|
||||
|
||||
/** 只在显式点名时写 golden:默认跑测试永远不许改是非源。 */
|
||||
|
||||
+48
@@ -366,6 +366,54 @@ class UserApiSecretServiceTest {
|
||||
service.requireSubmitSecret(7L, UserSecretModule.APPEARANCE_PATENT, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 task type 问闸门(B72-01):要哪把密钥的出处只有 {@code TaskModuleRegistry},
|
||||
* 调用方不再各写一枚枚举——{@code /newApi} 是整包转发的透传面,客户端猜不出载荷需求,
|
||||
* 服务端也不许两处各判各的(铁律 27 边界 a「同源」)。
|
||||
*/
|
||||
@Test
|
||||
void requireSubmitSecretByTypeResolvesTheModuleFromTheRegistry() {
|
||||
UserApiSecretService service = newService();
|
||||
when(mapper.selectOne(any())).thenReturn(null);
|
||||
|
||||
BusinessException error = assertThrows(BusinessException.class,
|
||||
() -> service.requireSubmitSecret(7L, "SIMILAR_ASIN", " "));
|
||||
|
||||
// 文案仍然点名"缺哪把、去哪配",且那把钥匙是登记表选出来的而不是调用方给的
|
||||
assertThat(error.getMessage()).contains("货源查询密钥").contains("密钥设置");
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireSubmitSecretByTypePassesWhenCallerCarriesKey() {
|
||||
UserApiSecretService service = newService();
|
||||
|
||||
service.requireSubmitSecret(7L, "APPEARANCE_PATENT", "sk-caller");
|
||||
|
||||
verify(mapper, never()).selectOne(any());
|
||||
}
|
||||
|
||||
/** 登记为「这一类不需要用户凭据」的类型调到这里必须炸,不能静默放行成空闸门。 */
|
||||
@Test
|
||||
void requireSubmitSecretByTypeRefusesTypeDeclaredAsNeedingNoCredential() {
|
||||
UserApiSecretService service = newService();
|
||||
|
||||
IllegalStateException error = assertThrows(IllegalStateException.class,
|
||||
() -> service.requireSubmitSecret(7L, "COLLECT_DATA", null));
|
||||
|
||||
assertThat(error.getMessage()).contains("COLLECT_DATA");
|
||||
// 空转的闸门比没有闸门更坏:走到这里一字节都不该碰库
|
||||
verify(mapper, never()).selectOne(any());
|
||||
}
|
||||
|
||||
/** 「没登记」与「登记过=不需要」是两个结论:不在清单里的类型名当场抛,不许当成空需求。 */
|
||||
@Test
|
||||
void requireSubmitSecretByTypeRejectsModuleThatIsNotRegistered() {
|
||||
UserApiSecretService service = newService();
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> service.requireSubmitSecret(7L, "APPEARANCE_PATENTT", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void migrateSkipsWhenServerValueExists() {
|
||||
UserApiSecretService service = newService();
|
||||
|
||||
Reference in New Issue
Block a user