Files
Genarrative/apps/ai-game-creator-shell/tests/resourceClassificationPanel.test.tsx
T
suzmii e1044546ad 接入素材删除入口并收敛资源命令后的 manifest 重载
- ResourceClassificationPanel 增加删除动作:首次点击进入确认态,二次点击才执行,避免误触不可逆操作
- 面板标题与按钮明确写出只移除登记、素材文件保留在磁盘
- 删除失败时把客户端的拒绝原因原样呈现(例如已被运行槽位绑定),并退出确认态
- index.tsx 抽出 reloadManifestAfterAssetCommand:资源分类更新与素材删除共用同一条重载路径,提交标识分别用 asset-classification / asset-delete 前缀
- 补 2 条面板测试:两次点击才删除且入参带 expectedProjectRevision、被运行槽位拒绝时不触发 onDeleted
2026-09-10 10:53:13 +08:00

247 lines
7.3 KiB
TypeScript

// @vitest-environment jsdom
import { cleanup, render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { afterEach, describe, expect, test, vi } from 'vitest';
import type { GameCreationAppAssetManifestEntry } from '../../../packages/shared/src/contracts/gameCreationApp';
import { ResourceClassificationPanel } from '../src/view/project-development/ResourceClassificationPanel';
const asset: GameCreationAppAssetManifestEntry = {
id: 'asset-hero',
kind: 'character',
mediaType: 'image/png',
localPath: 'assets/hero.png',
source: { kind: 'uploaded' },
category: 'character',
tags: ['主角'],
};
function installInvoke(
implementation: (command: string, args?: unknown) => Promise<unknown>,
) {
const invoke = vi.fn(implementation);
(
window as unknown as {
__TAURI__?: { core?: { invoke?: typeof invoke } };
}
).__TAURI__ = { core: { invoke } };
return invoke;
}
function removeInvoke() {
delete (
window as unknown as {
__TAURI__?: { core?: { invoke?: unknown } };
}
).__TAURI__;
}
afterEach(() => {
cleanup();
removeInvoke();
});
describe('ResourceClassificationPanel', () => {
test('writes the category and normalized tags through the controlled native command', async () => {
const user = userEvent.setup();
const onSaved = vi.fn();
const invoke = installInvoke(async (command) => {
if (command === 'get_local_game_project_revision') {
return { revision: 7 };
}
if (command === 'update_local_project_resource_classification') {
return {
asset: { ...asset, category: 'scene', tags: ['主舞台', '日夜'] },
committedProjectRevision: 8,
};
}
throw new Error(`unexpected command: ${command}`);
});
render(
<ResourceClassificationPanel
projectPath="C:/project"
projectId="project-1"
asset={asset}
onClose={vi.fn()}
onSaved={onSaved}
onDeleted={vi.fn()}
/>,
);
await user.click(screen.getByRole('button', { name: '场景与环境' }));
const tagsField = screen.getByLabelText('资源标签');
await user.clear(tagsField);
await user.type(tagsField, ' 主舞台 ,日夜,主舞台、');
await user.click(screen.getByRole('button', { name: '保存' }));
await waitFor(() => {
expect(onSaved).toHaveBeenCalledTimes(1);
});
expect(invoke).toHaveBeenCalledWith(
'update_local_project_resource_classification',
expect.objectContaining({
input: expect.objectContaining({
projectPath: 'C:/project',
expectedProjectId: 'project-1',
expectedProjectRevision: 7,
assetId: 'asset-hero',
category: 'scene',
}),
}),
);
const [, args] = invoke.mock.calls.find(
([command]) => command === 'update_local_project_resource_classification',
)!;
expect((args as { input: { tags: string[] } }).input.tags).toEqual([
'主舞台',
'日夜',
]);
expect(onSaved.mock.calls[0]?.[0]).toEqual({
asset: { ...asset, category: 'scene', tags: ['主舞台', '日夜'] },
committedProjectRevision: 8,
});
});
test('surfaces the native rejection without reporting a save', async () => {
const user = userEvent.setup();
const onSaved = vi.fn();
installInvoke(async (command) => {
if (command === 'get_local_game_project_revision') {
return { revision: 7 };
}
throw '非法资源分类:future-category';
});
render(
<ResourceClassificationPanel
projectPath="C:/project"
projectId="project-1"
asset={asset}
onClose={vi.fn()}
onSaved={onSaved}
onDeleted={vi.fn()}
/>,
);
await user.click(screen.getByRole('button', { name: '保存' }));
await screen.findByRole('alert');
expect(screen.getByRole('alert').textContent).toContain('非法资源分类');
expect(onSaved).not.toHaveBeenCalled();
});
test('does not attempt a write outside the native client', async () => {
const user = userEvent.setup();
const onSaved = vi.fn();
render(
<ResourceClassificationPanel
projectPath="C:/project"
projectId="project-1"
asset={asset}
onClose={vi.fn()}
onSaved={onSaved}
onDeleted={vi.fn()}
/>,
);
await user.click(screen.getByRole('button', { name: '保存' }));
await screen.findByRole('alert');
expect(screen.getByRole('alert').textContent).toContain('客户端');
expect(onSaved).not.toHaveBeenCalled();
});
test('deletes the asset registration only after an explicit confirmation', async () => {
const user = userEvent.setup();
const onDeleted = vi.fn();
const invoke = installInvoke(async (command) => {
if (command === 'get_local_game_project_revision') {
return { revision: 9 };
}
if (command === 'delete_local_project_asset') {
return {
assetId: 'asset-hero',
localPath: 'assets/hero.png',
committedProjectRevision: 10,
fileRetained: true,
};
}
throw new Error(`unexpected command: ${command}`);
});
render(
<ResourceClassificationPanel
projectPath="C:/project"
projectId="project-1"
asset={asset}
onClose={vi.fn()}
onSaved={vi.fn()}
onDeleted={onDeleted}
/>,
);
// 第一次点击只进入确认态,不得直接发起删除。
await user.click(
screen.getByRole('button', { name: '删除资源登记(保留素材文件)' }),
);
expect(onDeleted).not.toHaveBeenCalled();
expect(
invoke.mock.calls.some(
([command]) => command === 'delete_local_project_asset',
),
).toBe(false);
await user.click(screen.getByRole('button', { name: '确认删除资源登记' }));
await waitFor(() => {
expect(onDeleted).toHaveBeenCalledTimes(1);
});
expect(invoke).toHaveBeenCalledWith('delete_local_project_asset', {
input: {
projectPath: 'C:/project',
expectedProjectId: 'project-1',
expectedProjectRevision: 9,
assetId: 'asset-hero',
},
});
expect(onDeleted.mock.calls[0]?.[0]).toEqual({
assetId: 'asset-hero',
localPath: 'assets/hero.png',
committedProjectRevision: 10,
fileRetained: true,
});
});
test('surfaces the runtime rejection when the asset is still bound to a slot', async () => {
const user = userEvent.setup();
const onDeleted = vi.fn();
installInvoke(async (command) => {
if (command === 'get_local_game_project_revision') {
return { revision: 9 };
}
throw '素材 asset-hero 已被版本 initial-1 的运行槽位 hero 绑定,不能删除';
});
render(
<ResourceClassificationPanel
projectPath="C:/project"
projectId="project-1"
asset={asset}
onClose={vi.fn()}
onSaved={vi.fn()}
onDeleted={onDeleted}
/>,
);
await user.click(
screen.getByRole('button', { name: '删除资源登记(保留素材文件)' }),
);
await user.click(screen.getByRole('button', { name: '确认删除资源登记' }));
await screen.findByRole('alert');
expect(screen.getByRole('alert').textContent).toContain('运行槽位');
expect(onDeleted).not.toHaveBeenCalled();
});
});