test(phase-0): 确认基线全绿并预置迭代验收测试契约
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
{
|
||||
"note": "readSource 钉子逐文件基线。两把尺子:pins=落在用例体内的读盘,reads=全文读点总数(含模块级夹具与 helper)。都只许减;pins 下降而 reads 没同步下降=把读盘挪出体内,不算改进。由 tests/source-pin-ratchet.test.ts 生成。",
|
||||
"totals": {
|
||||
"files": 250,
|
||||
"cases": 1748,
|
||||
"pins": 451,
|
||||
"reads": 644
|
||||
"files": 251,
|
||||
"cases": 1750,
|
||||
"pins": 453,
|
||||
"reads": 646
|
||||
},
|
||||
"files": {
|
||||
"admin-scaffold.test.ts": {
|
||||
@@ -337,6 +337,11 @@
|
||||
"pins": 0,
|
||||
"reads": 1
|
||||
},
|
||||
"pending-phase4-大档上传接线.test.ts": {
|
||||
"cases": 2,
|
||||
"pins": 2,
|
||||
"reads": 2
|
||||
},
|
||||
"pending-phase4-错误兜底与大文件上传.test.ts": {
|
||||
"cases": 1,
|
||||
"pins": 0,
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
// AC-11(R4):后管版本/教程大档上传接线的「源码尺子」那一半。
|
||||
//
|
||||
// Pending Phase 4 激活。今天 `src/pages/records/version-api.ts` 与 `tutorial-api.ts`
|
||||
// 都是 presign → 一次 PUT 直传(512MB 级也一样,蓝图 §1.2 R4 实读);本文件要求两条
|
||||
// 路径接上既有 `/api/files/chunk/**` 分片通道(断点续传/按片重试)。
|
||||
//
|
||||
// 跑法:npm test(node --test tests/*.test.ts)。
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { dirname, resolve } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
const here = dirname(fileURLToPath(import.meta.url))
|
||||
const root = resolve(here, '..')
|
||||
const SKIP = 'Pending Phase 4: 断点续传大档接线(AC-11)'
|
||||
|
||||
const LARGE_UPLOAD_FILES = [
|
||||
'src/pages/records/version-api.ts',
|
||||
'src/pages/records/tutorial-api.ts',
|
||||
]
|
||||
|
||||
test('后管版本/教程大档必须接分片通道', { skip: SKIP }, () => {
|
||||
for (const file of LARGE_UPLOAD_FILES) {
|
||||
const source = readFileSync(resolve(root, file), 'utf-8')
|
||||
assert.match(
|
||||
source,
|
||||
/files\/chunk/,
|
||||
`AC-11:${file} 仍是 presign 直传(512MB 断网从 0 重来,没有分片续传)`,
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
test('分片接线不得只落注释', { skip: SKIP }, () => {
|
||||
for (const file of LARGE_UPLOAD_FILES) {
|
||||
const source = readFileSync(resolve(root, file), 'utf-8')
|
||||
const codeLines = source
|
||||
.split('\n')
|
||||
.filter((line) => !line.trim().startsWith('//') && !line.trim().startsWith('*'))
|
||||
.join('\n')
|
||||
assert.match(codeLines, /files\/chunk/, `AC-11:${file} 的分片引用只出现在注释里`)
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,73 @@
|
||||
// AC-11(R4):后管大档上传接线的「行为用例」那一半。
|
||||
//
|
||||
// Pending Phase 4 激活。9MB 安装包(跨分片阈值)调 uploadSoftwareVersion,
|
||||
// 必须出现 POST /files/chunk/init;今天它走 presign → 一次 PUT 直传,
|
||||
// 512MB 断网只能从 0 重来。本文件不出网,mock 掉 http 与 axios 直传实例。
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
post: vi.fn(),
|
||||
get: vi.fn(),
|
||||
put: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
directPut: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('axios', () => ({
|
||||
default: {
|
||||
create: () => ({ put: mocks.directPut }),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@/api/http', () => ({
|
||||
http: { post: mocks.post, get: mocks.get, put: mocks.put, delete: mocks.delete },
|
||||
unwrap: (value: unknown) => {
|
||||
const envelope = value as { success?: boolean; data?: unknown; message?: string }
|
||||
if (envelope && typeof envelope === 'object' && 'success' in envelope) {
|
||||
if (!envelope.success) {
|
||||
throw new Error(envelope.message || '请求失败')
|
||||
}
|
||||
return envelope.data
|
||||
}
|
||||
return value
|
||||
},
|
||||
}))
|
||||
|
||||
import { uploadSoftwareVersion } from '@/pages/records/version-api.ts'
|
||||
|
||||
const SKIP = 'Pending Phase 4: 断点续传大档接线(AC-11)'
|
||||
|
||||
describe.skip('AC-11 后管大档上传', () => {
|
||||
it('9MB 安装包必须走分片 init', async () => {
|
||||
// 预签名与分片两套响应揉成一个超集:哪条实现路径先走都能拿到自己要的字段。
|
||||
mocks.post.mockResolvedValue({
|
||||
data: {
|
||||
success: true,
|
||||
data: {
|
||||
upload_url: 'https://oss.example/put',
|
||||
file_url: 'https://oss.example/pkg.zip',
|
||||
version: '9.9.9',
|
||||
object_key: 'client/versions/pkg.zip',
|
||||
upload_id: 'a'.repeat(32),
|
||||
uploaded_parts: [],
|
||||
item: null,
|
||||
},
|
||||
},
|
||||
})
|
||||
mocks.get.mockResolvedValue({
|
||||
data: { success: true, data: { upload_id: 'a'.repeat(32), uploaded_parts: [] } },
|
||||
})
|
||||
mocks.directPut.mockResolvedValue({})
|
||||
|
||||
const file = new Blob([new Uint8Array(9 * 1024 * 1024)], {
|
||||
type: 'application/octet-stream',
|
||||
})
|
||||
await uploadSoftwareVersion('9.9.9', file)
|
||||
|
||||
const urls = mocks.post.mock.calls.map(([url]) => String(url))
|
||||
expect(
|
||||
urls.some((url) => url.includes('/files/chunk/init')),
|
||||
`AC-11:9MB 安装包仍走 presign 直传,实际出口:${urls.join(', ')}`,
|
||||
).toBe(true)
|
||||
})
|
||||
})
|
||||
+421
@@ -0,0 +1,421 @@
|
||||
package com.nanri.aiimage.modules.evidence;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.common.security.DeviceSessionPolicy;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
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.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* AC-7/AC-13/AC-14(R3):证据件上云后的服务端落点与访问面契约。
|
||||
*
|
||||
* <p>Pending Phase 3 激活。Phase 0 预置的判据(接缝名是判据的一部分,改名必须同步改本文件
|
||||
* 并在蓝图 §5 登记):</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li>Flyway {@code V182+} 新增 {@code biz_task_evidence}:task_id/uid/evidence_type/source/
|
||||
* object_key/byte_size/content_type/sha256/created_at,按 task_id 关联但**不建外键**;</li>
|
||||
* <li>{@code application.yml} 的 {@code evidence:} 块显式落 bucket(独立桶,不得为
|
||||
* nanri-ai-images)、retention-days(必须有数字默认值)、key-prefix;</li>
|
||||
* <li>证据读取端点落在 {@code /api/admin/task-evidence/**}(files/content/download/delete),
|
||||
* 每个方法都走私有 {@code requireSuperAdmin(HttpServletRequest)}(对齐
|
||||
* {@code AdminDeviceLogController:167-175});</li>
|
||||
* <li>守卫行为三样本:无 token/bad token → 401、非超管 → 403、超管 → 放行;伪造 uid 由
|
||||
* files 端点的 uid 归属校验兜住(源码判据);</li>
|
||||
* <li>证据模块日志零命中 presign / 直链(AC-13);对象 key 带 UUID(铁律 10)。</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>四样本中的「合法 JWT 200」在 Phase 3 由真机/集成用例补齐(本文件钉守卫放行分支,
|
||||
* 蓝图 Phase 3 准出另见 {@code 鉴权四样本真机留痕})。</p>
|
||||
*/
|
||||
@Disabled("Pending Phase 3: 证据件上云与服务端落点(AC-7/AC-13/AC-14)")
|
||||
class EvidencePhase3ContractTest {
|
||||
|
||||
private static final Path MODULE = Path.of("").toAbsolutePath();
|
||||
private static final Path MAIN_JAVA = MODULE.resolve("src/main/java");
|
||||
private static final Path DB_DIR = MODULE.resolve("src/main/resources/db");
|
||||
private static final Path APP_YML = MODULE.resolve("src/main/resources/application.yml");
|
||||
private static final String CONTROLLER_FQCN =
|
||||
"com.nanri.aiimage.modules.evidence.controller.AdminTaskEvidenceController";
|
||||
private static final String EVIDENCE_PATH = "/api/admin/task-evidence";
|
||||
|
||||
@Test
|
||||
void 迁移必须新增证据表且字段与索引齐全() throws IOException {
|
||||
List<Path> migrations = new ArrayList<>();
|
||||
try (Stream<Path> stream = Files.list(DB_DIR)) {
|
||||
stream.filter(path -> path.getFileName().toString().matches("V\\d+__.*\\.sql"))
|
||||
.filter(path -> versionOf(path.getFileName().toString()) >= 182)
|
||||
.forEach(migrations::add);
|
||||
}
|
||||
assertFalse(migrations.isEmpty(), "AC-7:本迭代必须新增 V182+ 迁移(当前最高 V181)");
|
||||
|
||||
StringBuilder joined = new StringBuilder();
|
||||
for (Path migration : migrations) {
|
||||
joined.append(read(migration)).append('\n');
|
||||
}
|
||||
String all = joined.toString();
|
||||
Matcher table = Pattern.compile("(?is)create\\s+table[^;]*biz_task_evidence[^;]*;")
|
||||
.matcher(all);
|
||||
assertTrue(table.find(), "AC-7:V182+ 必须建 biz_task_evidence 表(蓝图 §3.3)");
|
||||
String ddl = table.group();
|
||||
for (String column : List.of("task_id", "uid", "evidence_type", "source", "object_key",
|
||||
"byte_size", "content_type", "sha256", "created_at")) {
|
||||
assertTrue(Pattern.compile("(?i)\\b" + column + "\\b").matcher(ddl).find(),
|
||||
"AC-7:证据表缺列 " + column);
|
||||
}
|
||||
assertFalse(ddl.toUpperCase().contains("FOREIGN KEY"),
|
||||
"AC-7:证据表按 task_id 关联但不建外键(全库唯一 FK 是 V80:47)");
|
||||
assertTrue(Pattern.compile("(?i)(index|key)\\s+\\w+\\s*\\(\\s*task_id").matcher(ddl).find(),
|
||||
"AC-7:必须有 task_id 索引(按任务关联查询)");
|
||||
assertTrue(Pattern.compile("(?i)(index|key)\\s+\\w+\\s*\\(\\s*uid").matcher(ddl).find(),
|
||||
"AC-7:必须有 uid 索引(数据范围/清理)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 证据桶与保留期必须显式落在applicationYml() throws IOException {
|
||||
String yaml = read(APP_YML);
|
||||
String block = yamlBlock(yaml, "evidence");
|
||||
assertNotNull(block, "AC-13:application.yml 必须有 evidence: 配置块(不许只落代码默认值)");
|
||||
|
||||
String bucket = scalar(block, "bucket");
|
||||
assertNotNull(bucket, "AC-13:必须配置证据桶");
|
||||
String bucketDefault = defaultValue(bucket);
|
||||
assertNotNull(bucketDefault, "AC-13:证据桶不得只有变量无默认值(部署漏配即隐形)");
|
||||
assertFalse(bucketDefault.contains("nanri-ai-images"),
|
||||
"AC-13:证据必须落独立桶,不得复用业务桶 nanri-ai-images");
|
||||
|
||||
String retention = scalar(block, "retention-days");
|
||||
assertNotNull(retention, "AC-13:必须配置保留天数");
|
||||
String retentionDefault = defaultValue(retention);
|
||||
assertNotNull(retentionDefault, "AC-13:保留天数必须有确定的数字默认值(天数要进判据)");
|
||||
assertTrue(Integer.parseInt(retentionDefault) >= 1, "AC-13:保留天数必须为正整数");
|
||||
|
||||
assertNotNull(scalar(block, "key-prefix"), "AC-13:必须配置对象 key 前缀");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 证据读取端点必须全部走super访问面() throws IOException {
|
||||
Path controller = evidenceController();
|
||||
String source = read(controller);
|
||||
assertTrue(source.contains(EVIDENCE_PATH),
|
||||
"AC-13:证据读取控制器必须落在 " + EVIDENCE_PATH);
|
||||
|
||||
List<String> bodies = mappedMethodBodies(source);
|
||||
assertTrue(bodies.size() >= 4,
|
||||
"AC-13:证据读取至少要有 files/content/download/delete 四个端点,实扫 " + bodies.size());
|
||||
for (String body : bodies) {
|
||||
assertTrue(body.contains("requireSuperAdmin"),
|
||||
"AC-13:证据读取端点必须逐个走 requireSuperAdmin(非 super 一律 403)\n" + body);
|
||||
}
|
||||
assertTrue(source.contains("isSuper"),
|
||||
"AC-13:超管判定必须复用 DeviceSessionPolicy.isSuper 单出口");
|
||||
assertFalse(source.toLowerCase().contains("permitall"),
|
||||
"AC-13:证据读取端点不得 permitAll");
|
||||
|
||||
for (String endpoint : List.of("\"/files\"", "\"/content\"", "\"/download\"", "\"/delete\"")) {
|
||||
assertTrue(source.contains(endpoint), "AC-13:缺端点 " + endpoint);
|
||||
}
|
||||
|
||||
String filesBody = bodies.get(0);
|
||||
assertTrue(filesBody.contains("uid"),
|
||||
"AC-13:列表端点必须支持按 uid 收窄(伪造 uid 的归属校验入口)");
|
||||
assertTrue(filesBody.contains("operator") || filesBody.contains("getId"),
|
||||
"AC-13:uid 过滤必须与当前操作者归属对撞(防伪造 uid 越权读)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 守卫行为三样本_无token坏token401_非超管403_超管放行() throws Exception {
|
||||
Object controller = newEvidenceController();
|
||||
AdminAuthSupport auth = authSupportOf(controller);
|
||||
Method guard = superAdminGuard(controller.getClass());
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
|
||||
when(auth.requireAdmin(any(HttpServletRequest.class)))
|
||||
.thenThrow(new BusinessException(401, "未登录"));
|
||||
Throwable noToken = invoke(guard, controller, request);
|
||||
assertInstanceOf(BusinessException.class, noToken, "AC-13:无 token 必须 401");
|
||||
assertEquals(Integer.valueOf(401), ((BusinessException) noToken).getCode());
|
||||
|
||||
when(auth.requireAdmin(any(HttpServletRequest.class)))
|
||||
.thenThrow(new BusinessException(401, "登录已过期,请重新登录"));
|
||||
Throwable badToken = invoke(guard, controller, request);
|
||||
assertInstanceOf(BusinessException.class, badToken, "AC-13:坏 token 必须 401");
|
||||
assertEquals(Integer.valueOf(401), ((BusinessException) badToken).getCode());
|
||||
|
||||
AdminUserEntity normalAdmin = new AdminUserEntity();
|
||||
normalAdmin.setId(2L);
|
||||
normalAdmin.setUsername("ops-admin");
|
||||
normalAdmin.setRole("admin");
|
||||
normalAdmin.setIsAdmin(1);
|
||||
when(auth.requireAdmin(any(HttpServletRequest.class))).thenReturn(normalAdmin);
|
||||
when(auth.currentRole(normalAdmin)).thenReturn("admin");
|
||||
Throwable forbidden = invoke(guard, controller, request);
|
||||
assertInstanceOf(BusinessException.class, forbidden, "AC-13:非超管必须 403");
|
||||
assertEquals(Integer.valueOf(403), ((BusinessException) forbidden).getCode());
|
||||
|
||||
AdminUserEntity superAdmin = new AdminUserEntity();
|
||||
superAdmin.setId(1L);
|
||||
superAdmin.setUsername("root");
|
||||
superAdmin.setRole(DeviceSessionPolicy.SUPER_ADMIN);
|
||||
superAdmin.setIsAdmin(1);
|
||||
when(auth.requireAdmin(any(HttpServletRequest.class))).thenReturn(superAdmin);
|
||||
when(auth.currentRole(superAdmin)).thenReturn(DeviceSessionPolicy.SUPER_ADMIN);
|
||||
Object returned = guard.invoke(controller, request);
|
||||
assertSame(superAdmin, returned, "AC-13:超管必须放行并返回操作者");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 登记必须按taskId关联且对象key带uuid() throws IOException {
|
||||
String all = evidenceSources();
|
||||
assertTrue(Pattern.compile("(?i)task[_-]?id").matcher(all).find(),
|
||||
"AC-7:证据登记/落表必须按 taskId 关联");
|
||||
assertTrue(all.contains("objectKey") || all.contains("object_key"),
|
||||
"AC-7:登记必须记录 objectKey");
|
||||
assertTrue(all.contains("UUID"),
|
||||
"AC-13(铁律 10):对象 key 必须带 UUID");
|
||||
assertTrue(all.contains("sha256") || all.contains("SHA-256"),
|
||||
"AC-7:登记必须带摘要(防篡改/去重)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 证据模块日志零命中presign与直链() throws IOException {
|
||||
List<Path> files = new ArrayList<>();
|
||||
try (Stream<Path> stream = Files.walk(MAIN_JAVA)) {
|
||||
stream.filter(path -> path.toString().endsWith(".java"))
|
||||
.filter(EvidencePhase3ContractTest::isEvidenceSource)
|
||||
.forEach(files::add);
|
||||
}
|
||||
assertFalse(files.isEmpty(), "AC-13:未找到证据模块源码(命名/落点未按契约)");
|
||||
List<String> leaks = new ArrayList<>();
|
||||
for (Path file : files) {
|
||||
for (String line : read(file).split("\\R")) {
|
||||
String lower = line.toLowerCase();
|
||||
boolean logLine = lower.contains("log.info") || lower.contains("log.warn")
|
||||
|| lower.contains("log.error") || lower.contains("log.debug");
|
||||
boolean urlLike = lower.contains("presign") || lower.contains("uploadurl")
|
||||
|| lower.contains("fileurl") || lower.contains("http://")
|
||||
|| lower.contains("https://");
|
||||
if (logLine && urlLike) {
|
||||
leaks.add(file.getFileName() + ": " + line.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
assertTrue(leaks.isEmpty(), "AC-13:证据模块日志输出了 presign/直链:\n" + String.join("\n", leaks));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- helpers
|
||||
|
||||
private static Path evidenceController() throws IOException {
|
||||
List<Path> hits = new ArrayList<>();
|
||||
try (Stream<Path> stream = Files.walk(MAIN_JAVA)) {
|
||||
stream.filter(path -> path.toString().endsWith(".java"))
|
||||
.filter(path -> {
|
||||
try {
|
||||
return read(path).contains(EVIDENCE_PATH);
|
||||
} catch (IOException ex) {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.forEach(hits::add);
|
||||
}
|
||||
assertFalse(hits.isEmpty(),
|
||||
"AC-13:未找到 " + EVIDENCE_PATH + " 的控制器源码(Phase 3 落点未按契约)");
|
||||
return hits.get(0);
|
||||
}
|
||||
|
||||
private static boolean isEvidenceSource(Path path) {
|
||||
try {
|
||||
String source = read(path);
|
||||
return source.contains("task-evidence") || source.contains("TaskEvidence")
|
||||
|| source.contains("biz_task_evidence");
|
||||
} catch (IOException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static String evidenceSources() throws IOException {
|
||||
List<Path> files = new ArrayList<>();
|
||||
try (Stream<Path> stream = Files.walk(MAIN_JAVA)) {
|
||||
stream.filter(path -> path.toString().endsWith(".java"))
|
||||
.filter(EvidencePhase3ContractTest::isEvidenceSource)
|
||||
.forEach(files::add);
|
||||
}
|
||||
assertFalse(files.isEmpty(), "AC-7:未找到证据模块源码");
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Path file : files) {
|
||||
builder.append(read(file)).append('\n');
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private static Object newEvidenceController() throws Exception {
|
||||
Class<?> controllerClass = Class.forName(CONTROLLER_FQCN);
|
||||
Constructor<?> constructor = Arrays.stream(controllerClass.getDeclaredConstructors())
|
||||
.max(Comparator.comparingInt(candidate -> candidate.getParameterCount()))
|
||||
.orElseThrow();
|
||||
Class<?>[] types = constructor.getParameterTypes();
|
||||
Object[] args = new Object[types.length];
|
||||
for (int index = 0; index < types.length; index++) {
|
||||
if (types[index].isPrimitive()) {
|
||||
fail("AC-13:控制器构造器不应有原始类型参数: " + types[index]);
|
||||
}
|
||||
args[index] = AdminAuthSupport.class.isAssignableFrom(types[index])
|
||||
? mock(AdminAuthSupport.class)
|
||||
: mock(types[index]);
|
||||
}
|
||||
constructor.setAccessible(true);
|
||||
return constructor.newInstance(args);
|
||||
}
|
||||
|
||||
private static AdminAuthSupport authSupportOf(Object controller) throws IllegalAccessException {
|
||||
for (java.lang.reflect.Field field : controller.getClass().getDeclaredFields()) {
|
||||
if (AdminAuthSupport.class.isAssignableFrom(field.getType())) {
|
||||
field.setAccessible(true);
|
||||
return (AdminAuthSupport) field.get(controller);
|
||||
}
|
||||
}
|
||||
fail("AC-13:证据控制器必须注入 AdminAuthSupport(与 AdminDeviceLogController 同口径)");
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Method superAdminGuard(Class<?> controllerClass) {
|
||||
for (Method method : controllerClass.getDeclaredMethods()) {
|
||||
if (method.getName().equals("requireSuperAdmin")
|
||||
&& method.getParameterCount() == 1
|
||||
&& method.getParameterTypes()[0].isAssignableFrom(HttpServletRequest.class)) {
|
||||
method.setAccessible(true);
|
||||
return method;
|
||||
}
|
||||
}
|
||||
throw new AssertionError(
|
||||
"AC-13:证据控制器必须含私有 requireSuperAdmin(HttpServletRequest)(对齐 AdminDeviceLogController:167-175)");
|
||||
}
|
||||
|
||||
private static Throwable invoke(Method method, Object target, Object... args) throws Exception {
|
||||
try {
|
||||
method.invoke(target, args);
|
||||
return null;
|
||||
} catch (InvocationTargetException ex) {
|
||||
return ex.getTargetException();
|
||||
}
|
||||
}
|
||||
|
||||
private static List<String> mappedMethodBodies(String source) {
|
||||
String[] lines = source.split("\\R");
|
||||
Pattern mapping = Pattern.compile("@(Get|Post|Put|Delete|Patch)Mapping");
|
||||
List<String> bodies = new ArrayList<>();
|
||||
for (int index = 0; index < lines.length; index++) {
|
||||
if (!mapping.matcher(lines[index]).find()) {
|
||||
continue;
|
||||
}
|
||||
int cursor = index;
|
||||
while (cursor < lines.length && !lines[cursor].contains("{") && !lines[cursor].contains(";")) {
|
||||
cursor++;
|
||||
}
|
||||
if (cursor >= lines.length || lines[cursor].contains(";")) {
|
||||
continue;
|
||||
}
|
||||
int depth = 0;
|
||||
StringBuilder body = new StringBuilder();
|
||||
for (int line = cursor; line < lines.length; line++) {
|
||||
for (char ch : lines[line].toCharArray()) {
|
||||
if (ch == '{') {
|
||||
depth++;
|
||||
} else if (ch == '}') {
|
||||
depth--;
|
||||
}
|
||||
}
|
||||
body.append(lines[line]).append('\n');
|
||||
if (depth == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
bodies.add(body.toString());
|
||||
index = cursor;
|
||||
}
|
||||
return bodies;
|
||||
}
|
||||
|
||||
private static String yamlBlock(String yaml, String key) {
|
||||
String[] lines = yaml.split("\\R");
|
||||
Pattern header = Pattern.compile("^(\\s*)" + Pattern.quote(key) + ":\\s*$");
|
||||
int start = -1;
|
||||
int indent = -1;
|
||||
for (int index = 0; index < lines.length; index++) {
|
||||
Matcher matcher = header.matcher(lines[index]);
|
||||
if (matcher.matches()) {
|
||||
start = index;
|
||||
indent = matcher.group(1).length();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (start < 0) {
|
||||
return null;
|
||||
}
|
||||
StringBuilder block = new StringBuilder();
|
||||
for (int index = start + 1; index < lines.length; index++) {
|
||||
String line = lines[index];
|
||||
if (line.trim().isEmpty() || line.trim().startsWith("#")) {
|
||||
block.append(line).append('\n');
|
||||
continue;
|
||||
}
|
||||
int currentIndent = line.length() - line.replaceAll("^\\s+", "").length();
|
||||
if (currentIndent <= indent) {
|
||||
break;
|
||||
}
|
||||
block.append(line).append('\n');
|
||||
}
|
||||
return block.toString();
|
||||
}
|
||||
|
||||
private static String scalar(String block, String key) {
|
||||
Matcher matcher = Pattern.compile("(?m)^\\s*" + Pattern.quote(key) + ":\\s*(\\S.*)$")
|
||||
.matcher(block);
|
||||
return matcher.find() ? matcher.group(1).trim() : null;
|
||||
}
|
||||
|
||||
private static String defaultValue(String value) {
|
||||
Matcher env = Pattern.compile("^\\$\\{[^:}]+:([^}]*)\\}$").matcher(value);
|
||||
if (env.matches()) {
|
||||
String fallback = env.group(1).trim();
|
||||
return fallback.isEmpty() ? null : fallback;
|
||||
}
|
||||
return value.startsWith("${") ? null : value;
|
||||
}
|
||||
|
||||
private static long versionOf(String fileName) {
|
||||
Matcher matcher = Pattern.compile("^V(\\d+)__").matcher(fileName);
|
||||
return matcher.find() ? Long.parseLong(matcher.group(1)) : -1;
|
||||
}
|
||||
|
||||
private static String read(Path path) throws IOException {
|
||||
return Files.readString(path);
|
||||
}
|
||||
}
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
package com.nanri.aiimage.modules.file.service;
|
||||
|
||||
import com.nanri.aiimage.config.StorageProperties;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* AC-10(R5):stale 分片会话必须由周期任务回收,不能等人 init 时顺手清。
|
||||
*
|
||||
* <p>Pending Phase 4 激活。今天 {@code ChunkedUploadService.cleanupStaleSessions()} 的
|
||||
* 唯一调用点在 {@code init()} 里(:86),没有任何 {@code @Scheduled};断掉的上传会永久占盘,
|
||||
* 而 {@code LocalTempCleanupService} 的射程只有 source/result/transientPayload,
|
||||
* {@code chunk-uploads} 不在其列。</p>
|
||||
*
|
||||
* <p>接缝契约(改名必须同步改本文件并登记蓝图 §5):</p>
|
||||
* <ul>
|
||||
* <li>回收入口挂在 {@link ChunkedUploadService} 上,方法名不限,但必须有
|
||||
* {@link Scheduled} 注解——**摘掉注解即判红**(本文件第一条断言就是它);</li>
|
||||
* <li>「无人 init 也会清」:本用例先造 stale + fresh 两个会话,回拨 stale 的目录时间后
|
||||
* <b>不再 init</b>,直接反射调用被注解的周期方法;</li>
|
||||
* <li>近 N 分钟有片写入的会话不得误清(fresh 会话必须原样保留)。</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>分片会话只有盘上 meta/part,无 DB 行;证据表的 DB 行保留期判据见
|
||||
* {@code EvidencePhase3ContractTest}(AC-13 配置块)。</p>
|
||||
*/
|
||||
@Disabled("Pending Phase 4: 服务端残骸回收补洞(AC-10)")
|
||||
class ChunkedUploadReclaimContractTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private ChunkedUploadService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
StorageProperties properties = new StorageProperties();
|
||||
properties.setLocalTempDir(tempDir.toString());
|
||||
properties.setCleanupEnabled(true);
|
||||
service = new ChunkedUploadService(properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 无人init也必须由周期任务回收stale会话() throws Exception {
|
||||
List<Method> scheduled = scheduledMethods();
|
||||
assertFalse(scheduled.isEmpty(),
|
||||
"AC-10:ChunkedUploadService 上必须挂 @Scheduled 回收方法(摘掉注解即判红)");
|
||||
|
||||
ChunkedUploadService.ChunkSession stale = service.init("stale.bin", 2, 10, "evidence");
|
||||
ChunkedUploadService.ChunkSession fresh = service.init("fresh.bin", 2, 10, "evidence");
|
||||
Path staleDir = sessionDir(stale.uploadId());
|
||||
Path freshDir = sessionDir(fresh.uploadId());
|
||||
assertTrue(Files.isDirectory(staleDir));
|
||||
|
||||
// 回拨 stale 的目录时间(cleanupStaleSessions 以目录 mtime 判龄),fresh 保持当前时间
|
||||
Instant old = Instant.now().minus(30, ChronoUnit.HOURS);
|
||||
Files.setLastModifiedTime(staleDir, FileTime.from(old));
|
||||
|
||||
for (Method method : scheduled) {
|
||||
method.setAccessible(true);
|
||||
try {
|
||||
method.invoke(service);
|
||||
} catch (InvocationTargetException ex) {
|
||||
throw new AssertionError("AC-10:周期回收方法自身抛异常", ex.getTargetException());
|
||||
}
|
||||
}
|
||||
|
||||
assertFalse(Files.exists(staleDir),
|
||||
"AC-10:stale 会话仍在盘上(今天只有 init 时才清,断掉的上传永久占盘)");
|
||||
assertTrue(Files.isDirectory(freshDir),
|
||||
"AC-10:近 N 分钟有写入的会话被误清(用户大档会从 0 重传)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 统一清理必须覆盖chunkUploads目录() throws Exception {
|
||||
Path chunkDir = tempDir.resolve("chunk-uploads").resolve("c".repeat(32));
|
||||
Files.createDirectories(chunkDir);
|
||||
Path part = chunkDir.resolve("part-0");
|
||||
Files.writeString(part, "x");
|
||||
Instant old = Instant.now().minus(10, ChronoUnit.DAYS);
|
||||
setTimes(part, old);
|
||||
setTimes(chunkDir, old);
|
||||
|
||||
StorageProperties properties = new StorageProperties();
|
||||
properties.setLocalTempDir(tempDir.toString());
|
||||
properties.setCleanupEnabled(true);
|
||||
properties.setSourceRetentionHours(24);
|
||||
properties.setResultRetentionHours(24);
|
||||
properties.setTransientPayloadRetentionHours(72);
|
||||
new LocalTempCleanupService(properties).cleanupLocalTempDir();
|
||||
|
||||
assertFalse(Files.exists(chunkDir),
|
||||
"AC-10:chunk-uploads 残骸未纳入统一清理(LocalTempCleanupService 射程外的洞)");
|
||||
}
|
||||
|
||||
private List<Method> scheduledMethods() {
|
||||
List<Method> methods = new ArrayList<>();
|
||||
for (Class<?> type = ChunkedUploadService.class; type != null && type != Object.class;
|
||||
type = type.getSuperclass()) {
|
||||
Arrays.stream(type.getDeclaredMethods())
|
||||
.filter(method -> method.isAnnotationPresent(Scheduled.class))
|
||||
.forEach(methods::add);
|
||||
}
|
||||
return methods;
|
||||
}
|
||||
|
||||
private Path sessionDir(String uploadId) {
|
||||
return tempDir.resolve("chunk-uploads").resolve(uploadId);
|
||||
}
|
||||
|
||||
private static void setTimes(Path path, Instant instant) throws Exception {
|
||||
FileTime time = FileTime.from(instant);
|
||||
Files.getFileAttributeView(path,
|
||||
java.nio.file.attribute.BasicFileAttributeView.class)
|
||||
.setTimes(time, time, null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// AC-11(R4):用户端大档上传接线的「行为用例」那一半。
|
||||
//
|
||||
// Pending Phase 4 激活。9MB 视频(跨 8MB 分片阈值)调 uploadImageVideoMedia,
|
||||
// 必须出现 POST /files/chunk/init;今天它只打 POST /files/upload(单次 120s)。
|
||||
//
|
||||
// 本文件走 vitest(tests/mount/**),用 mock 的 http 出口对撞 URL 序列,不出网。
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
post: vi.fn(),
|
||||
get: vi.fn(),
|
||||
put: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
deleteFn: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/shared/api/http.ts', () => ({
|
||||
http: mocks,
|
||||
post: mocks.post,
|
||||
get: mocks.get,
|
||||
put: mocks.put,
|
||||
del: mocks.deleteFn,
|
||||
unwrapJavaResponse: (value: unknown) => value,
|
||||
}))
|
||||
|
||||
import { uploadImageVideoMedia } from '@/shared/api/types/modules/image-video.ts'
|
||||
|
||||
const SKIP = 'Pending Phase 4: 断点续传大档接线(AC-11)'
|
||||
|
||||
describe.skip('AC-11 用户端大档上传', () => {
|
||||
it('9MB 视频必须走分片 init', async () => {
|
||||
const payload = {
|
||||
success: true,
|
||||
data: {
|
||||
url: 'https://oss.example/evidence.mp4',
|
||||
objectKey: 'image-video/obj.mp4',
|
||||
originalFilename: 'big.mp4',
|
||||
mediaType: 'video',
|
||||
upload_id: 'a'.repeat(32),
|
||||
uploaded_parts: [],
|
||||
},
|
||||
}
|
||||
mocks.post.mockResolvedValue(payload)
|
||||
mocks.get.mockResolvedValue({
|
||||
success: true,
|
||||
data: { upload_id: 'a'.repeat(32), uploaded_parts: [] },
|
||||
})
|
||||
|
||||
const file = new File([new Uint8Array(9 * 1024 * 1024)], 'big.mp4', { type: 'video/mp4' })
|
||||
await uploadImageVideoMedia(file)
|
||||
|
||||
const urls = mocks.post.mock.calls.map(([url]) => String(url))
|
||||
expect(
|
||||
urls.some((url) => url.includes('/files/chunk/init')),
|
||||
`AC-11:9MB 视频仍走单次上传,实际出口:${urls.join(', ')}`,
|
||||
).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,45 @@
|
||||
// AC-11(R4):用户端大档上传接线双口径的「源码尺子」那一半。
|
||||
//
|
||||
// Pending Phase 4 激活。今天 `src/shared/api/upload.ts::uploadTempFileToJava` 已实现
|
||||
// init→part→status→complete 全套分片,但**零生产调用方**(蓝图 §1.2 R4 实读);
|
||||
// `image-video.ts:215 uploadImageVideoMedia` 仍是一次 multipart+固定 120s。
|
||||
// 本文件钉两件事:
|
||||
// 1. uploadTempFileToJava 的调用方数从 0 变 ≥1;
|
||||
// 2. 图片视频大档路径接上分片(uploadTempFileToJava 或 /files/chunk)。
|
||||
//
|
||||
// 跑法:npm test(node --test tests/*.test.ts;本文件在顶层,不进 vitest 的 glob)。
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { dirname, resolve, relative, sep } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
import { listSourceFiles } from './helpers/task-terminal-ast-ruler.ts'
|
||||
|
||||
const here = dirname(fileURLToPath(import.meta.url))
|
||||
const root = resolve(here, '..')
|
||||
const srcFiles = listSourceFiles(resolve(root, 'src'))
|
||||
const rel = (path: string) => relative(root, path).split(sep).join('/')
|
||||
const SKIP = 'Pending Phase 4: 断点续传大档接线(AC-11)'
|
||||
|
||||
test('uploadTempFileToJava 必须有生产调用方(调用方数 ≥1)', { skip: SKIP }, () => {
|
||||
assert.ok(srcFiles.length > 50, `源码扫描面异常:只扫到 ${srcFiles.length} 个文件`)
|
||||
const callers = srcFiles
|
||||
.map(rel)
|
||||
.filter((file) => file !== 'src/shared/api/upload.ts')
|
||||
.filter((file) => readFileSync(resolve(root, file), 'utf-8').includes('uploadTempFileToJava'))
|
||||
assert.ok(
|
||||
callers.length >= 1,
|
||||
`AC-11:uploadTempFileToJava 仍是零生产调用方(扫过 ${srcFiles.length} 个源码文件)`,
|
||||
)
|
||||
})
|
||||
|
||||
test('图片视频大档上传必须接分片通道', { skip: SKIP }, () => {
|
||||
const file = 'src/shared/api/types/modules/image-video.ts'
|
||||
const source = readFileSync(resolve(root, file), 'utf-8')
|
||||
assert.match(
|
||||
source,
|
||||
/uploadTempFileToJava|files\/chunk/,
|
||||
'AC-11:uploadImageVideoMedia 仍是一次 multipart+固定 120s(大档一断就从 0 重传)',
|
||||
)
|
||||
})
|
||||
Reference in New Issue
Block a user