1
This commit is contained in:
116
src/services/match3dGeneratedModelCache.test.ts
Normal file
116
src/services/match3dGeneratedModelCache.test.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import { afterEach, describe, expect, test, vi } from 'vitest';
|
||||
|
||||
import { setStoredAccessToken, clearStoredAccessToken } from './apiClient';
|
||||
import {
|
||||
clearMatch3DGeneratedModelBytesCache,
|
||||
getMatch3DGeneratedModelAssetSources,
|
||||
preloadMatch3DGeneratedModelAssets,
|
||||
readMatch3DGeneratedModelBytes,
|
||||
} from './match3dGeneratedModelCache';
|
||||
|
||||
describe('match3dGeneratedModelCache', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
clearMatch3DGeneratedModelBytesCache();
|
||||
clearStoredAccessToken({ emit: false });
|
||||
});
|
||||
|
||||
test('预加载生成模型字节并复用本地缓存', async () => {
|
||||
setStoredAccessToken('test-access-token', { emit: false });
|
||||
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
|
||||
new Response(new Uint8Array([103, 108, 84, 70]), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'model/gltf-binary',
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
await preloadMatch3DGeneratedModelAssets(
|
||||
[
|
||||
{
|
||||
itemId: 'match3d-item-1',
|
||||
itemName: '草莓',
|
||||
imageSrc: null,
|
||||
imageObjectKey: null,
|
||||
modelSrc:
|
||||
'/generated-match3d-assets/session/profile/items/match3d-item-1/model.glb',
|
||||
modelObjectKey: null,
|
||||
modelFileName: 'strawberry.glb',
|
||||
taskUuid: null,
|
||||
subscriptionKey: null,
|
||||
status: 'model_ready',
|
||||
error: null,
|
||||
},
|
||||
],
|
||||
{ expireSeconds: 300 },
|
||||
);
|
||||
const bytes = await readMatch3DGeneratedModelBytes(
|
||||
'/generated-match3d-assets/session/profile/items/match3d-item-1/model.glb',
|
||||
{ expireSeconds: 300 },
|
||||
);
|
||||
|
||||
expect(Array.from(new Uint8Array(bytes))).toEqual([103, 108, 84, 70]);
|
||||
expect(globalThis.fetch).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('模型源列表会去重并兼容 modelObjectKey', () => {
|
||||
const sources = getMatch3DGeneratedModelAssetSources([
|
||||
{
|
||||
itemId: 'match3d-item-1',
|
||||
itemName: '草莓',
|
||||
imageSrc: null,
|
||||
imageObjectKey: null,
|
||||
modelSrc: null,
|
||||
modelObjectKey:
|
||||
'generated-match3d-assets/session/profile/items/match3d-item-1/model.glb',
|
||||
modelFileName: 'strawberry.glb',
|
||||
taskUuid: null,
|
||||
subscriptionKey: null,
|
||||
status: 'model_ready',
|
||||
error: null,
|
||||
},
|
||||
{
|
||||
itemId: 'match3d-item-1-duplicate',
|
||||
itemName: '草莓副本',
|
||||
imageSrc: null,
|
||||
imageObjectKey: null,
|
||||
modelSrc:
|
||||
'generated-match3d-assets/session/profile/items/match3d-item-1/model.glb',
|
||||
modelObjectKey: null,
|
||||
modelFileName: 'strawberry.glb',
|
||||
taskUuid: null,
|
||||
subscriptionKey: null,
|
||||
status: 'model_ready',
|
||||
error: null,
|
||||
},
|
||||
]);
|
||||
|
||||
expect(sources).toEqual([
|
||||
'generated-match3d-assets/session/profile/items/match3d-item-1/model.glb',
|
||||
]);
|
||||
});
|
||||
|
||||
test('同时存在外部 modelSrc 和平台 modelObjectKey 时优先预加载平台对象', () => {
|
||||
const sources = getMatch3DGeneratedModelAssetSources([
|
||||
{
|
||||
itemId: 'match3d-item-legacy',
|
||||
itemName: '苹果',
|
||||
imageSrc: null,
|
||||
imageObjectKey: null,
|
||||
modelSrc: 'https://rodin.example.com/expired/model.glb',
|
||||
modelObjectKey:
|
||||
'generated-match3d-assets/session/profile/items/match3d-item-legacy/model.glb',
|
||||
modelFileName: 'apple.glb',
|
||||
taskUuid: null,
|
||||
subscriptionKey: null,
|
||||
status: 'model_ready',
|
||||
error: null,
|
||||
},
|
||||
]);
|
||||
|
||||
expect(sources).toEqual([
|
||||
'generated-match3d-assets/session/profile/items/match3d-item-legacy/model.glb',
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user