feat(公司tab): 接线 视频任务记录——company_id 走 DataReadScope,detail/zip 用同一把 ReadScope 门住

Task 8 的前半页(生成记录那半页 BLOCKED,见账本裁定)。
- controller 接住 requireAdminOrInternal 的返回值算范围,page 侧 readScope(operator, companyId)
  .userIdsOr(base):显式 user_id 与公司 tab 是交集,不是替换。
- mapper 的 userId 单值条件保留、并列 userIds IN <foreach>;null=不筛,空集在 service 首行早退,
  绝不把 [] 交给 mapper(渲染成 IN () 是 500)。
- selectAdminZipRows 补 t.user_id:download-zip 的闸门要不越权就得先拿得到行主人。
- detail 的 404 文案拆开:404 只留给"真查不到行",越权是 403。zip 整单任一条越界 ⇒ 403,
  不借 X-Archive-Error-Count 的"部分失败"语义盖越权。
- 前端 company_id 落在 builder 之后、发请求之前的唯一一处(normalizeHistoryParams 那类白名单
  构造会把键静默丢掉);页面 watch(activeId) + 读 requestCompanyId。

验证(我自己复跑,不是转述):干净树定向 7/0(新 5 支 + G2 2 支)、全量前端 1726/1725/1
(G1 缺口清单 8 页→7 页,admin_image_video_tasks 已消失)、vue-tsc rc=0。
This commit is contained in:
2026-10-05 07:51:19 +08:00
parent 3e95ea9b3f
commit 4a8c05329a
7 changed files with 223 additions and 20 deletions
@@ -0,0 +1,136 @@
package com.nanri.aiimage.modules.imagevideo.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.modules.imagevideo.mapper.ImageVideoAdminTaskMapper;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* 公司 tab 的成员集合怎么进这条查询(Task 8)。
*
* <p>范围本身在 controller 里由 {@code DataReadScope} 算,service 只收算好的
* {@code List<Long> userIds}:null = 不筛(超管没选公司,今天的行为),
* 空集 = 这家公司今天没有成员账号(必须空页,绝不许退化成不筛)。</p>
*/
class ImageVideoAdminTaskServiceTest {
private final ImageVideoAdminTaskMapper mapper = mock(ImageVideoAdminTaskMapper.class);
private final ImageVideoAdminTaskService service =
new ImageVideoAdminTaskService(mapper, new ObjectMapper());
@Test
@DisplayName("超管选了公司:userId 的单值过滤之外必须多出成员集合")
@SuppressWarnings("unchecked")
void companyMembersGoToBothPageAndCountQueries() {
when(mapper.selectAdminTaskPage(any(), any(), any(), any(), any(), any(), any(), anyInt(), anyInt()))
.thenReturn(List.of());
when(mapper.countAdminTaskPage(any(), any(), any(), any(), any(), any(), any())).thenReturn(0L);
service.page(null, null, null, null, null, null, 1, 20, List.of(11L, 12L));
// 关键:行集与总数必须拿到同一个范围。只换 page 不换 count ⇒ "总数对不上但没人报错"。
ArgumentCaptor<List<Long>> rowsCap = ArgumentCaptor.forClass((Class) List.class);
ArgumentCaptor<List<Long>> cntCap = ArgumentCaptor.forClass((Class) List.class);
verify(mapper).selectAdminTaskPage(any(), rowsCap.capture(), any(), any(), any(), any(), any(),
anyInt(), anyInt());
verify(mapper).countAdminTaskPage(any(), cntCap.capture(), any(), any(), any(), any(), any());
assertThat(rowsCap.getValue()).containsExactlyInAnyOrder(11L, 12L);
assertThat(cntCap.getValue()).containsExactlyInAnyOrder(11L, 12L);
}
@Test
@DisplayName("不筛时传 null,不是空集——空集在 mapper 那里会渲染出 IN () 语法错")
void unfilteredPassesNullNotEmptyList() {
when(mapper.selectAdminTaskPage(any(), any(), any(), any(), any(), any(), any(), anyInt(), anyInt()))
.thenReturn(List.of());
service.page(null, null, null, null, null, null, 1, 20, null);
verify(mapper).selectAdminTaskPage(any(), isNull(), any(), any(), any(), any(), any(),
anyInt(), anyInt());
}
@Test
@DisplayName("成员集为空=空页,两支 mapper 一支都不许碰(退化成不筛就是越权)")
void emptyMemberSetShortCircuitsWithoutTouchingMapper() {
Map<String, Object> result = service.page(null, null, null, null, null, null, 3, 20, List.of());
assertThat(result)
.containsEntry("items", List.of())
.containsEntry("total", 0L)
.containsEntry("page", 3L)
.containsEntry("page_size", 20L);
verify(mapper, never()).selectAdminTaskPage(any(), any(), any(), any(), any(), any(), any(),
anyInt(), anyInt());
verify(mapper, never()).countAdminTaskPage(any(), any(), any(), any(), any(), any(), any());
}
@Test
@DisplayName("detail:行主人不在成员集里=403,真查不到行才是 404;不筛(null)时照旧放行")
void detailGatesRowOwnerAndKeepsNotFoundForMissingRow() {
when(mapper.selectAdminTaskDetail(7L)).thenReturn(videoRow(7L, 99L));
when(mapper.selectAdminTaskDetail(8L)).thenReturn(null);
assertThatThrownBy(() -> service.detail(7L, List.of(11L, 12L)))
.isInstanceOf(BusinessException.class)
.hasMessage("该任务不属于当前公司")
.extracting(e -> ((BusinessException) e).getCode())
.isEqualTo(403);
assertThatThrownBy(() -> service.detail(8L, List.of(11L)))
.isInstanceOf(BusinessException.class)
.hasMessage("视频任务不存在")
.extracting(e -> ((BusinessException) e).getCode())
.isEqualTo(404);
// 主人落在成员集里 ⇒ 读得到;集合为 null(超管没选公司)⇒ 同一条行也读得到。
assertThat(service.detail(7L, List.of(99L)).get("item")).isNotNull();
assertThat(service.detail(7L, null).get("item")).isNotNull();
}
@Test
@DisplayName("download-zip:任一行越界整单 403,不许塞进 X-Archive-Error-Count 的部分失败")
void zipRejectsWholeRequestWhenAnyRowIsOutOfScope() throws Exception {
when(mapper.selectAdminZipRows(List.of(7L, 8L)))
.thenReturn(List.of(videoRow(7L, 11L), videoRow(8L, 99L)));
List<ImageVideoAdminTaskService.VideoSelection> selections =
List.of(new ImageVideoAdminTaskService.VideoSelection(7L, 0),
new ImageVideoAdminTaskService.VideoSelection(8L, 0));
assertThatThrownBy(() -> service.buildZip(selections, List.of(11L, 12L)))
.isInstanceOf(BusinessException.class)
.hasMessage("该任务不属于当前公司")
.extracting(e -> ((BusinessException) e).getCode())
.isEqualTo(403);
// 在范围内的行照旧走"打包"语义(这里没有可下载的视频 ⇒ 记一条错误,不是 403)。
ImageVideoAdminTaskService.ZipBundle bundle =
service.buildZip(List.of(new ImageVideoAdminTaskService.VideoSelection(7L, 0)), List.of(11L));
java.nio.file.Files.deleteIfExists(bundle.file());
assertThat(bundle.fileCount()).isZero();
assertThat(bundle.errorCount()).isEqualTo(1);
}
/** 一条最小可用的任务行:user_id 是闸门唯一的判据,video_urls_json 留空好让 zip 不碰网络。 */
private Map<String, Object> videoRow(long taskId, Long ownerId) {
Map<String, Object> row = new LinkedHashMap<>();
row.put("id", taskId);
row.put("user_id", ownerId);
row.put("video_urls_json", "[]");
return row;
}
}