Files
Genarrative/apps/ai-game-creator-shell/tests/gameDistributionPublish.test.ts
T
suzmii 3aa3e33ceb
Project CI / AI game creator shell Rust crates (pull_request) Failing after 56s
Project CI / Backend tests (pull_request) Failing after 23s
Project CI / AI game creator shell Rust lane 1/2 (pull_request) Failing after 5m7s
Project CI / Repository checks (pull_request) Failing after 21s
Project CI / Frontend tests (pull_request) Has been cancelled
Project CI / AI game creator shell web tests (pull_request) Has been cancelled
Project CI / AI game creator shell Rust lane 2/2 (pull_request) Has been cancelled
Project CI / Native shell tests (pull_request) Has been cancelled
实现统一项目发行版本
以 projectVersion 作为 AGC 唯一用户版本,支持同版本重复提交和版本回退。

保留 publicationRevision 并发校验与旧客户端自动递增兼容。
2026-10-03 16:41:04 +08:00

420 lines
14 KiB
TypeScript

// @vitest-environment jsdom
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import type { GameCreationAppManifest } from '../../../packages/shared/src/contracts/gameCreationApp';
import type { TauriInvoke } from '../src/app/types';
import {
generateGameDistributionCover,
publishLocalProjectGame,
readGameCoverGenerationPrice,
readGameDistributionPublication,
saveGameDistributionProjectVersion,
suggestGameDistributionPublishMetadata,
} from '../src/services/gameDistributionPublish';
const MANIFEST = {
projectId: 'local-proj-1',
name: '星轨防线',
goal: '守住轨道城',
} as unknown as GameCreationAppManifest;
const PUBLISH_RESULT = {
gameId: 'game_1',
versionId: 'gamever_1',
versionNumber: 1,
status: 'pending_review',
packageSha256: 'a'.repeat(64),
packageSizeBytes: 1024,
fileCount: 1,
};
type InvokeCall = {
command: string;
args: Record<string, unknown> | undefined;
};
function installNativeInvoke(
implementation: (command: string, args?: Record<string, unknown>) => unknown,
) {
const calls: InvokeCall[] = [];
const invoke = vi.fn(
async (command: string, args?: Record<string, unknown>) => {
calls.push({ command, args });
return implementation(command, args);
},
) as unknown as TauriInvoke;
return { invoke, calls };
}
beforeEach(() => {
window.localStorage.clear();
});
afterEach(() => {
vi.useRealTimers();
delete (window as unknown as { __TAURI__?: unknown }).__TAURI__;
});
describe('Rust 发布 facade', () => {
test('发布只通过一个 native command 传递本地路径、资料和幂等键', async () => {
const { invoke, calls } = installNativeInvoke((command) => {
if (command === 'publish_local_project_game') return PUBLISH_RESULT;
throw new Error(`未预期的命令:${command}`);
});
const result = await publishLocalProjectGame({
invoke,
projectPath: '/tmp/project',
packageRelativePath: 'exports/playtest-package-1.zip',
manifest: MANIFEST,
metadata: {
coverAssetId: 'asset_cover',
screenshots: ['asset_shot_1', 'asset_shot_2'],
},
idempotencyKey: 'agc-publish-test',
});
expect(result).toEqual(PUBLISH_RESULT);
expect(calls).toHaveLength(1);
expect(calls[0]?.command).toBe('publish_local_project_game');
expect(calls[0]?.args).toMatchObject({
projectPath: '/tmp/project',
packageRelativePath: 'exports/playtest-package-1.zip',
idempotencyKey: 'agc-publish-test',
metadata: {
localProjectId: 'local-proj-1',
coverAssetId: 'asset_cover',
screenshots: ['asset_shot_1', 'asset_shot_2'],
},
});
expect(calls[0]?.args).not.toHaveProperty('accessToken');
expect(calls[0]?.args).not.toHaveProperty('apiBaseUrl');
expect(calls[0]?.args).not.toHaveProperty('packageBytes');
});
test('未显式给出根幂等键时不生成键,交由原生账本解析', async () => {
const { invoke, calls } = installNativeInvoke((command) => {
if (command === 'publish_local_project_game') return PUBLISH_RESULT;
throw new Error(`未预期的命令:${command}`);
});
await publishLocalProjectGame({
invoke,
projectPath: '/tmp/project',
packageRelativePath: 'exports/playtest-package-1.zip',
manifest: MANIFEST,
metadata: { coverAssetId: 'asset_cover' },
});
expect(calls).toHaveLength(1);
// 渲染层不铸键也不落盘:Rust 按「账号 + 本地项目 + 包摘要」的账本解析根幂等键,
// 这样响应丢失、重启与分片续传才会落在同一次服务端尝试上。
expect(calls[0]?.args).not.toHaveProperty('idempotencyKey');
expect(calls[0]?.args).toMatchObject({
projectPath: '/tmp/project',
packageRelativePath: 'exports/playtest-package-1.zip',
metadata: { localProjectId: 'local-proj-1' },
});
});
test('缺少本地项目标识时在发起 native command 前失败关闭', async () => {
const invoke = vi.fn() as unknown as TauriInvoke;
await expect(
publishLocalProjectGame({
invoke,
projectPath: '/tmp/project',
packageRelativePath: 'exports/playtest-package-1.zip',
manifest: { ...MANIFEST, projectId: ' ' } as GameCreationAppManifest,
metadata: { coverAssetId: 'asset_cover' },
}),
).rejects.toThrow('发布需要本地项目标识');
expect(invoke).not.toHaveBeenCalled();
});
test('缺少封面时在创建 native 发布任务前失败关闭', async () => {
const invoke = vi.fn() as unknown as TauriInvoke;
await expect(
publishLocalProjectGame({
invoke,
projectPath: '/tmp/project',
packageRelativePath: 'exports/playtest-package-1.zip',
manifest: MANIFEST,
metadata: { coverAssetId: ' ' },
}),
).rejects.toThrow('请先选择游戏封面');
expect(invoke).not.toHaveBeenCalled();
});
test('截图超过 6 张时在 native command 前失败关闭', async () => {
const invoke = vi.fn() as unknown as TauriInvoke;
await expect(
publishLocalProjectGame({
invoke,
projectPath: '/tmp/project',
packageRelativePath: 'exports/playtest-package-1.zip',
manifest: MANIFEST,
metadata: {
coverAssetId: 'asset_cover',
screenshots: Array.from(
{ length: 7 },
(_, index) => `asset_${index}`,
),
},
}),
).rejects.toThrow('游戏截图最多 6 张');
expect(invoke).not.toHaveBeenCalled();
});
test('资料建议和价格均通过 native command', async () => {
const { invoke, calls } = installNativeInvoke((command) => {
if (command === 'suggest_game_distribution_publish_metadata') {
return { summary: '驾驶炮台守住轨道城', category: '策略' };
}
if (command === 'read_game_cover_generation_price') return 5;
throw new Error(`未预期的命令:${command}`);
});
await expect(
suggestGameDistributionPublishMetadata(
{
name: '星轨防线',
goal: '守住轨道城',
context:
'当前任务与状态:原型已完成;现有素材:image:assets/hero.png',
},
invoke,
),
).resolves.toEqual({ summary: '驾驶炮台守住轨道城', category: '策略' });
await expect(
readGameCoverGenerationPrice(
{ model: 'gpt-image-2', imageSize: '2K' },
invoke,
),
).resolves.toBe(5);
expect(calls).toEqual([
{
command: 'suggest_game_distribution_publish_metadata',
args: {
name: '星轨防线',
goal: '守住轨道城',
context:
'当前任务与状态:原型已完成;现有素材:image:assets/hero.png',
},
},
{
command: 'read_game_cover_generation_price',
args: { model: 'gpt-image-2', imageSize: '2K' },
},
]);
});
test('封面生成由 Rust 等待队列后一次性返回已登记素材', async () => {
const { invoke, calls } = installNativeInvoke((command) => {
if (command === 'generate_game_distribution_cover') {
return {
assetObjectId: 'asset_generated_cover',
previewUrl: 'https://assets.example.com/generated-cover.png',
taskId: 'task_cover_1',
model: 'gpt-image-2',
};
}
throw new Error(`未预期的命令:${command}`);
});
await expect(
generateGameDistributionCover(
{
prompt: '为《星轨防线》生成游戏封面',
model: 'gpt-image-2',
aspectRatio: '16:9',
imageSize: '2K',
},
invoke,
),
).resolves.toEqual({
assetObjectId: 'asset_generated_cover',
previewUrl: 'https://assets.example.com/generated-cover.png',
taskId: 'task_cover_1',
model: 'gpt-image-2',
});
expect(calls).toEqual([
{
command: 'generate_game_distribution_cover',
args: {
prompt: '为《星轨防线》生成游戏封面',
model: 'gpt-image-2',
aspectRatio: '16:9',
imageSize: '2K',
assetLabel: '游戏封面',
},
},
]);
});
test('发布状态恢复通过单个 native command 传递项目路径与三态结果', async () => {
const updateResult = {
state: 'update',
binding: {
accountId: 'user-1',
apiBaseUrl: 'https://dev.test',
gameId: 'game_9',
revision: 7,
latestVersionId: 'gamever_2',
latestVersionNumber: 2,
status: 'pending_review',
},
refreshed: true,
draft: null,
visibility: 'published',
};
const { invoke, calls } = installNativeInvoke((command) => {
if (command === 'read_game_distribution_publication') {
return updateResult;
}
throw new Error(`未预期的命令:${command}`);
});
await expect(
readGameDistributionPublication({ projectPath: '/tmp/project' }, invoke),
).resolves.toEqual(updateResult);
expect(calls).toEqual([
{
command: 'read_game_distribution_publication',
args: { projectPath: '/tmp/project' },
},
]);
});
test('恢复结果缺少已知三态时失败关闭,不把未知结果当成首次发布', async () => {
const { invoke } = installNativeInvoke(() => undefined);
await expect(
readGameDistributionPublication({ projectPath: '/tmp/project' }, invoke),
).rejects.toThrow('发布状态回读结果无效');
});
test('更新发布会带上原 gameId、项目版本与公开修订号', async () => {
const { invoke, calls } = installNativeInvoke(() => PUBLISH_RESULT);
await publishLocalProjectGame({
invoke,
projectPath: '/tmp/project',
packageRelativePath: 'exports/playtest-package-1.zip',
manifest: MANIFEST,
metadata: { coverAssetId: 'asset_cover' },
gameId: 'game_9',
versionNumber: 3,
expectedPublicationRevision: 7,
});
expect(calls[0]?.args).toMatchObject({
gameId: 'game_9',
versionNumber: 3,
expectedPublicationRevision: 7,
});
});
test('未显式给出项目版本时沿用清单里的唯一发行版本', async () => {
const { invoke, calls } = installNativeInvoke(() => PUBLISH_RESULT);
// 旧清单没有 projectVersion:首版回落 1,不把「没写」变成服务端自动递增。
await publishLocalProjectGame({
invoke,
projectPath: '/tmp/project',
packageRelativePath: 'exports/playtest-package-1.zip',
manifest: MANIFEST,
metadata: { coverAssetId: 'asset_cover' },
});
expect(calls[0]?.args).toMatchObject({ versionNumber: 1 });
// 清单写过的项目版本就是这次提交的标签;同一标签重复提交由服务端接受。
await publishLocalProjectGame({
invoke,
projectPath: '/tmp/project',
packageRelativePath: 'exports/playtest-package-1.zip',
manifest: {
...MANIFEST,
projectVersion: 4,
} as unknown as GameCreationAppManifest,
metadata: { coverAssetId: 'asset_cover' },
gameId: 'game_9',
expectedPublicationRevision: 7,
});
expect(calls[1]?.args).toMatchObject({
gameId: 'game_9',
versionNumber: 4,
expectedPublicationRevision: 7,
});
});
test('更新发布缺少公开修订号时在 native command 前失败关闭', async () => {
const invoke = vi.fn() as unknown as TauriInvoke;
await expect(
publishLocalProjectGame({
invoke,
projectPath: '/tmp/project',
packageRelativePath: 'exports/playtest-package-1.zip',
manifest: MANIFEST,
metadata: { coverAssetId: 'asset_cover' },
gameId: 'game_9',
versionNumber: 3,
}),
).rejects.toThrow('缺少公开修订号');
expect(invoke).not.toHaveBeenCalled();
});
test('项目版本不是正整数安全整数时在 native command 前失败关闭', async () => {
const invoke = vi.fn() as unknown as TauriInvoke;
for (const versionNumber of [0, -1, 1.5, Number.MAX_SAFE_INTEGER + 1]) {
await expect(
publishLocalProjectGame({
invoke,
projectPath: '/tmp/project',
packageRelativePath: 'exports/playtest-package-1.zip',
manifest: MANIFEST,
metadata: { coverAssetId: 'asset_cover' },
versionNumber,
}),
).rejects.toThrow('项目版本必须是大于 0 的安全整数');
}
expect(invoke).not.toHaveBeenCalled();
});
test('项目版本写回清单通过单个 native command 并回传整份 manifest', async () => {
const savedManifest = {
...MANIFEST,
projectVersion: 6,
} as unknown as GameCreationAppManifest;
const { invoke, calls } = installNativeInvoke(() => savedManifest);
const updated = await saveGameDistributionProjectVersion(
{ projectPath: '/tmp/project', projectVersion: 6 },
invoke,
);
expect(updated).toEqual(savedManifest);
expect(calls).toEqual([
{
command: 'update_game_distribution_project_version',
args: { projectPath: '/tmp/project', projectVersion: 6 },
},
]);
});
test('项目版本写回在非法输入时失败关闭', async () => {
const invoke = vi.fn() as unknown as TauriInvoke;
await expect(
saveGameDistributionProjectVersion(
{ projectPath: '/tmp/project', projectVersion: 0 },
invoke,
),
).rejects.toThrow('项目版本必须是大于 0 的安全整数');
await expect(
saveGameDistributionProjectVersion(
{ projectPath: ' ', projectVersion: 2 },
invoke,
),
).rejects.toThrow('发布需要绑定本地项目');
expect(invoke).not.toHaveBeenCalled();
});
});