Preserve partial creation replies on stream failure
Some checks failed
CI / verify (push) Has been cancelled
Some checks failed
CI / verify (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
/* @vitest-environment jsdom */
|
||||
|
||||
import { act, render, screen, waitFor } from '@testing-library/react';
|
||||
import { useEffect } from 'react';
|
||||
import { expect, test, vi } from 'vitest';
|
||||
|
||||
import { usePlatformCreationAgentFlowController } from './usePlatformCreationAgentFlowController';
|
||||
|
||||
type TestSession = {
|
||||
sessionId: string;
|
||||
messages: Array<{
|
||||
id: string;
|
||||
role: string;
|
||||
kind?: string;
|
||||
text: string;
|
||||
createdAt?: string;
|
||||
}>;
|
||||
};
|
||||
|
||||
type TestMessagePayload = {
|
||||
clientMessageId: string;
|
||||
text: string;
|
||||
};
|
||||
|
||||
function TestHarness({
|
||||
streamMessage,
|
||||
}: {
|
||||
streamMessage: (
|
||||
sessionId: string,
|
||||
payload: TestMessagePayload,
|
||||
options?: { onUpdate?: (text: string) => void },
|
||||
) => Promise<TestSession>;
|
||||
}) {
|
||||
const flow = usePlatformCreationAgentFlowController<
|
||||
TestSession,
|
||||
Record<string, never>,
|
||||
{ session: TestSession },
|
||||
TestMessagePayload,
|
||||
{ action: string },
|
||||
{ session: TestSession }
|
||||
>({
|
||||
client: {
|
||||
createSession: async () => ({
|
||||
session: {
|
||||
sessionId: 'session-1',
|
||||
messages: [],
|
||||
},
|
||||
}),
|
||||
getSession: async () => ({
|
||||
session: {
|
||||
sessionId: 'session-1',
|
||||
messages: [],
|
||||
},
|
||||
}),
|
||||
streamMessage,
|
||||
executeAction: async () => ({
|
||||
session: {
|
||||
sessionId: 'session-1',
|
||||
messages: [],
|
||||
},
|
||||
}),
|
||||
selectSession: (response) => response.session,
|
||||
},
|
||||
createPayload: {},
|
||||
workspaceStage: 'match3d-agent-workspace',
|
||||
resultStage: 'match3d-result',
|
||||
platformStage: 'platform',
|
||||
isCompileAction: () => false,
|
||||
resolveErrorMessage: (error, fallback) =>
|
||||
error instanceof Error ? error.message : fallback,
|
||||
errorMessages: {
|
||||
open: '打开失败',
|
||||
restoreMissingSession: '缺少会话',
|
||||
restore: '恢复失败',
|
||||
submit: '发送失败',
|
||||
execute: '执行失败',
|
||||
},
|
||||
enterCreateTab: () => {},
|
||||
setSelectionStage: () => {},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
void flow.openWorkspace({});
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
void flow.submitMessage({
|
||||
clientMessageId: 'client-message-1',
|
||||
text: '做一个办公室文具方洞挑战',
|
||||
});
|
||||
}}
|
||||
>
|
||||
发送
|
||||
</button>
|
||||
<div data-testid="messages">
|
||||
{flow.session?.messages.map((message) => (
|
||||
<div key={message.id}>{`${message.role}:${message.kind}:${message.text}`}</div>
|
||||
))}
|
||||
</div>
|
||||
{flow.error ? <div>{flow.error}</div> : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
test('creation agent flow preserves streamed assistant text when stream fails', async () => {
|
||||
const streamMessage = vi.fn(async (_sessionId, _payload, options) => {
|
||||
options?.onUpdate?.('先把方洞万能的反差定住。');
|
||||
throw new Error('方洞挑战聊天生成失败:LLM 请求超时');
|
||||
});
|
||||
|
||||
render(<TestHarness streamMessage={streamMessage} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByRole('button', { name: '发送' })).toBeTruthy();
|
||||
});
|
||||
|
||||
await act(async () => {
|
||||
screen.getByRole('button', { name: '发送' }).click();
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(
|
||||
screen.getByText('方洞挑战聊天生成失败:LLM 请求超时'),
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
expect(screen.getByTestId('messages').textContent).toContain(
|
||||
'user:chat:做一个办公室文具方洞挑战',
|
||||
);
|
||||
expect(screen.getByTestId('messages').textContent).toContain(
|
||||
'assistant:warning:先把方洞万能的反差定住。',
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user