Files
makelore/tests/unit/image-workspace-api.test.ts
brother7 3d9dd14918 feat: 统一 AI 设计 Workspace 与生成任务链路
需求:以设计项目组织固定设计 Agent 对话、方向确认和图片视频任务。

实现:新增 Works Square 云端适配与开发态本地适配,统一 Host API、Quote 确认、任务轮询及私有媒体 Range 代理。
2026-07-31 13:56:55 +08:00

114 lines
3.8 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { AppError } from '@/lib/error-model';
import {
ensureHostApiToken,
getHostApiBase,
hostApiFetch,
} from '@/lib/host-api';
import {
confirmImageWorkspaceGeneration,
createImageWorkspaceProject,
fetchImageWorkspace,
ImageWorkspaceApiError,
resolveImageWorkspaceAssetUrl,
sendImageWorkspaceMessage,
} from '@/lib/image-workspace';
vi.mock('@/lib/host-api', () => ({
hostApiFetch: vi.fn(),
ensureHostApiToken: vi.fn(),
getHostApiBase: vi.fn(),
}));
const hostApiFetchMock = vi.mocked(hostApiFetch);
const ensureHostApiTokenMock = vi.mocked(ensureHostApiToken);
const getHostApiBaseMock = vi.mocked(getHostApiBase);
const bootstrap = {
capabilities: {
conversation: true,
generation: true,
image: true,
video: false,
},
workspaces: [],
};
describe('AI design renderer API boundary', () => {
beforeEach(() => {
hostApiFetchMock.mockReset();
hostApiFetchMock.mockResolvedValue({ success: true, status: 200, data: bootstrap });
ensureHostApiTokenMock.mockReset();
ensureHostApiTokenMock.mockResolvedValue('host-token');
getHostApiBaseMock.mockReset();
getHostApiBaseMock.mockReturnValue('http://127.0.0.1:13210');
});
it('uses only the Main-owned route and never forwards a Works Square token', async () => {
await fetchImageWorkspace();
expect(hostApiFetchMock).toHaveBeenCalledWith('/api/works/image-workspace', {});
});
it('creates an idempotent Workspace with a trimmed user-visible title', async () => {
await createImageWorkspaceProject(' 角色设计 ', 'workspace-client-1');
expect(hostApiFetchMock).toHaveBeenCalledWith('/api/works/image-workspace/workspaces', {
method: 'POST',
body: JSON.stringify({
clientWorkspaceId: 'workspace-client-1',
title: '角色设计',
}),
});
});
it('sends conversation turns with the current turn revision and no provider settings', async () => {
await sendImageWorkspaceMessage('workspace/one', 4, ' 继续调整构图 ');
const [path, init] = hostApiFetchMock.mock.calls[0];
expect(path).toBe('/api/works/image-workspace/workspaces/workspace%2Fone/messages');
expect(init).toMatchObject({ method: 'POST' });
expect(JSON.parse(String(init?.body))).toMatchObject({
expectedTurnRevision: 4,
message: '继续调整构图',
attachmentAssetIds: [],
clientTurnId: expect.stringMatching(/^turn-/),
});
expect(String(init?.body)).not.toMatch(/model|resolution|outputCount/);
});
it('confirms a Quote through a separate structured action route', async () => {
await confirmImageWorkspaceGeneration('workspace-one', 5, 'quote/one');
const [path, init] = hostApiFetchMock.mock.calls[0];
expect(path).toBe(
'/api/works/image-workspace/workspaces/workspace-one/quotes/quote%2Fone/confirm',
);
expect(JSON.parse(String(init?.body))).toMatchObject({
expectedTurnRevision: 5,
clientTurnId: expect.stringMatching(/^turn-/),
});
});
it('preserves structured Main error codes for revision recovery', async () => {
hostApiFetchMock.mockRejectedValueOnce(new AppError(
'UNKNOWN',
'Design workspace revision has changed',
undefined,
{ status: 409, backendCode: 'workspace_revision_conflict' },
));
await expect(fetchImageWorkspace()).rejects.toMatchObject<ImageWorkspaceApiError>({
status: 409,
code: 'workspace_revision_conflict',
});
});
it('builds a loopback media URL with only the Host API session token', async () => {
await expect(resolveImageWorkspaceAssetUrl('/api/works/image-workspace/assets/one'))
.resolves.toBe(
'http://127.0.0.1:13210/api/works/image-workspace/assets/one?token=host-token',
);
});
});