两个真实缺陷,都由本轮的「跨端边界验收」暴露:
一、上传落盘:去重命中时删掉了唯一副本(T-WEB-017)
persistTempFile(动作 / 模型 / 场景上传走的通路)在 sha256 去重命中时直接
safeUnlink(tempPath) 并返回旧记录 —— 一旦记录指向的路径不存在(换过 STORAGE_DIR、
只恢复数据库、手工删过文件),上传仍返回 201,但后续动作预处理 500
(实测 ENOENT .../<sha>.vmd)、模型分析与缩略图静默失败。持久化通路 persistBuffer
早在上一轮就补了「文件缺失则按原路径补写」,这条通路漏了;现在同样处理:
先 existsSync(absolutePath(existing)),缺失则按记录里的原路径 rename(跨设备退化为
copyFile)补写,只有确认目标存在才删临时文件。
新增 test/storage-persist.spec.ts(5 项):首次上传 / 去重命中且文件在 /
去重命中但文件已丢失(补写而非删除)/ persistBuffer 回归 / 空文件拒绝。
反向验证:临时还原修复后第 3 条立刻失败,恢复后 5 项全过。
二、不可用模型的原因归因不准确(T-WEB-017)
unusableReason() 只要 stats 里有 humanoid 数字就归因于 humanoid,于是一个直接上传的
.glb(骨架完全合规、drivable=true、只是没有 VRM 容器)会被写成
「humanoid 不达标(19/22)」,把审核引向"重新绑定骨骼"而不是"以 VRM 交付"。
实测(跨端验收用真实 PMX→Assimp→GLB,886 KB):
修复前 → humanoid 不达标(19/22),未产出 VRM
修复后 → 未产出 VRM(格式为 GLB;humanoid 覆盖 19/22 已达标),需重新以 VRM 交付才能进入客户端目录
现在按 drivable 区分:不满足 16 根必需骨才归因 humanoid;达标但没 VRM 容器则明确说"已达标"。
新增 2 项单测(model-usability 5 → 7 项)。
三、跨端边界验收(Windows × Web)新增两类场景,双方都断言
* 不可用模型:真实 PMX 用 Assimp 转 GLB 后入库(等价"转换只产出 GLB")→
usable=false、原因准确、**不进入 catalog/export**、defaultModelId 不变;
包内客户端的 bundle.models 里没有任何非 VRM 模型;
* 场景 config 只给 {physics:{gravity:{y:-9.8}}} → 服务端补齐默认
(碰撞体 auto、地面 0.6/45°、静风、相机 follow、环境光 1、出生点 1 条),
客户端解析后逐项一致。
实测:npm run verify 全绿(契约 6 项 / 35 路由 / 版本 0.7.5 / 27 文件 309 项测试 /
build / 产物级实体注册核对 / 启动冒烟);跨端集成验收在**发布包起的服务端**上全部通过。
版本 0.7.4 → 0.7.5。
147 lines
5.6 KiB
TypeScript
147 lines
5.6 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
||
|
||
import { CatalogService } from '../src/catalog/catalog.service';
|
||
import { ModelFormat, PublishStatus, SourceType } from '../src/common/enums';
|
||
import type { CharacterModel } from '../src/entities/model.entity';
|
||
import { isUsableModel, toModelDto, unusableReason } from '../src/models/model.serializer';
|
||
|
||
/**
|
||
* v0.5.0「模型一律转 VRM」的可用性门槛。
|
||
*
|
||
* 规则(contracts/model-formats.md 1.1):所有上传模型都转 VRM;
|
||
* humanoid 不达标时回退交付 GLB —— 这种模型**可入库、可审核,但不可用**,
|
||
* **不进入 `catalog/export`**(用户明确选择"入库但标记不可用,不进已发布目录")。
|
||
*/
|
||
|
||
function model(overrides: Partial<CharacterModel> = {}): CharacterModel {
|
||
return {
|
||
id: 1,
|
||
name: 'model',
|
||
format: ModelFormat.Vrm,
|
||
vrmVersion: '1.0',
|
||
isPrimary: false,
|
||
fileAssetId: 1,
|
||
sourceAssetId: null,
|
||
thumbnailAssetId: null,
|
||
conversionNote: null,
|
||
stats: null,
|
||
sourceType: SourceType.Other,
|
||
sourceName: 'x',
|
||
sourceUrl: 'x',
|
||
license: 'x',
|
||
author: 'x',
|
||
status: PublishStatus.Published,
|
||
createdAt: new Date('2026-01-01T00:00:00Z'),
|
||
updatedAt: new Date('2026-01-01T00:00:00Z'),
|
||
...overrides,
|
||
} as CharacterModel;
|
||
}
|
||
|
||
describe('模型可用性(usable)', () => {
|
||
it('VRM → 可用,且没有不可用原因', () => {
|
||
const vrm = model({ format: ModelFormat.Vrm });
|
||
expect(isUsableModel(vrm)).toBe(true);
|
||
expect(unusableReason(vrm)).toBeNull();
|
||
expect(toModelDto(vrm, { fileUrl: '/f', thumbnailUrl: null }).usable).toBe(true);
|
||
});
|
||
|
||
it('GLB(humanoid 不达标)→ 不可用,原因带 humanoid 覆盖度', () => {
|
||
const glb = model({
|
||
format: ModelFormat.Glb,
|
||
vrmVersion: null,
|
||
stats: { humanoidMapped: 0, humanoidTotal: 22, drivable: false } as Record<string, unknown>,
|
||
});
|
||
expect(isUsableModel(glb)).toBe(false);
|
||
expect(unusableReason(glb)).toBe('humanoid 不达标(0/22),未产出 VRM');
|
||
const dto = toModelDto(glb, { fileUrl: '/f', thumbnailUrl: null });
|
||
expect(dto.usable).toBe(false);
|
||
expect(dto.unusableReason).toContain('0/22');
|
||
});
|
||
|
||
it('GLB 但 **humanoid 已达标** → 不能归因成「humanoid 不达标」(归因准确)', () => {
|
||
// 直接上传的 .glb:骨架合规(drivable=true)、只是没有 VRM 容器。
|
||
// 早期实现会写成「humanoid 不达标(19/22)」,把审核引向错误的修复方向。
|
||
const glb = model({
|
||
format: ModelFormat.Glb,
|
||
vrmVersion: null,
|
||
stats: {
|
||
humanoidMapped: 19,
|
||
humanoidTotal: 22,
|
||
drivable: true,
|
||
missingRequiredBones: [],
|
||
} as Record<string, unknown>,
|
||
});
|
||
const reason = unusableReason(glb);
|
||
expect(reason).not.toContain('humanoid 不达标');
|
||
expect(reason).toContain('19/22');
|
||
expect(reason).toContain('已达标');
|
||
expect(reason).toContain('未产出 VRM');
|
||
});
|
||
|
||
it('GLB 且 humanoid 未达标(drivable=false)→ 仍归因于 humanoid', () => {
|
||
const glb = model({
|
||
format: ModelFormat.Glb,
|
||
vrmVersion: null,
|
||
stats: {
|
||
humanoidMapped: 12,
|
||
humanoidTotal: 22,
|
||
drivable: false,
|
||
missingRequiredBones: ['leftHand'],
|
||
} as Record<string, unknown>,
|
||
});
|
||
expect(unusableReason(glb)).toBe('humanoid 不达标(12/22),未产出 VRM');
|
||
});
|
||
|
||
it('GLB 且没有 stats → 给出通用原因(不抛错)', () => {
|
||
const glb = model({ format: ModelFormat.Glb, vrmVersion: null, stats: null });
|
||
expect(unusableReason(glb)).toContain('未产出 VRM');
|
||
});
|
||
});
|
||
|
||
describe('目录导出只含可用模型', () => {
|
||
function buildService(captured: { modelsWhere?: unknown }): CatalogService {
|
||
const songs = { find: vi.fn().mockResolvedValue([]) };
|
||
const motions = { find: vi.fn().mockResolvedValue([]) };
|
||
const models = {
|
||
find: vi.fn().mockImplementation((options: { where: unknown }) => {
|
||
captured.modelsWhere = options.where;
|
||
return Promise.resolve([model({ id: 7, format: ModelFormat.Vrm })]);
|
||
}),
|
||
};
|
||
const songsService = { toDtos: vi.fn().mockResolvedValue([]) };
|
||
const motionsService = { toDtos: vi.fn().mockResolvedValue([]) };
|
||
const scenesService = { published: vi.fn().mockResolvedValue([]) };
|
||
const modelsService = {
|
||
toDtos: vi.fn().mockImplementation((rows: CharacterModel[]) =>
|
||
rows.map((row) => toModelDto(row, { fileUrl: '/f', thumbnailUrl: null })),
|
||
),
|
||
resolveDefaultModelId: vi.fn().mockResolvedValue(7),
|
||
};
|
||
const config = { contractVersions: { 'rest-api': '0.5.0' } };
|
||
return new CatalogService(
|
||
songs as never,
|
||
motions as never,
|
||
models as never,
|
||
songsService as never,
|
||
motionsService as never,
|
||
modelsService as never,
|
||
scenesService as never,
|
||
config as never,
|
||
);
|
||
}
|
||
|
||
it('查询条件限定 status=published 且 format=vrm', async () => {
|
||
const captured: { modelsWhere?: unknown } = {};
|
||
const bundle = await buildService(captured).exportBundle();
|
||
expect(captured.modelsWhere).toEqual({ status: PublishStatus.Published, format: ModelFormat.Vrm });
|
||
expect(bundle.models.map((m) => m.id)).toEqual([7]);
|
||
expect(bundle.models.every((m) => m.usable)).toBe(true);
|
||
});
|
||
|
||
it('目录指纹同样只统计可用模型(否则客户端会因"不可见变化"反复刷新)', async () => {
|
||
const captured: { modelsWhere?: unknown } = {};
|
||
await buildService(captured).version();
|
||
expect(captured.modelsWhere).toEqual({ status: PublishStatus.Published, format: ModelFormat.Vrm });
|
||
});
|
||
});
|