CyberStrikeAI/web/frontend/tests/vulnerability-retest.spec.mjs

130 lines
7.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { expect, label, screenshot, test, visit, CONVERSATION, VULNERABILITY } from './fixtures.mjs';
const BRANCH_ID = 'fixture-retest-branch';
const RETEST_PATH = `/api/vulnerabilities/${VULNERABILITY.id}/retest`;
const DEFAULT_PROMPT = label('vulnerabilityRetest.defaultPrompt',
'请帮我对漏洞 {{vulnerability_id}} 进行验证,确认是否已修复。完成后更新该漏洞状态。');
const GUIDANCE = label('vulnerabilityRetest.guidanceHeading', '验证方式(来自漏洞记录的复测方式):');
const RESOLVED_PROMPT = `请帮我对漏洞 ${VULNERABILITY.id} 进行验证,确认是否已修复。完成后更新该漏洞状态。`;
const REPLY = '复测完成:示例路径已返回 403,漏洞已修复。';
const MODAL_TITLE = label('vulnerabilityRetest.title', '复测漏洞');
function branchConversation({ pendingPrompt = '' } = {}) {
return {
id: BRANCH_ID, title: `复测:${VULNERABILITY.title}`, projectId: VULNERABILITY.project_id,
createdAt: CONVERSATION.createdAt, updatedAt: CONVERSATION.updatedAt,
messages: [
{ id: 'fixture-retest-user', role: 'user', content: RESOLVED_PROMPT, createdAt: CONVERSATION.createdAt },
{ id: 'fixture-retest-assistant', role: 'assistant', content: REPLY, createdAt: CONVERSATION.createdAt }
],
retest: pendingPrompt ? {
conversation_id: BRANCH_ID, source_conversation_id: CONVERSATION.id,
vulnerability_id: VULNERABILITY.id, pending_prompt: pendingPrompt
} : undefined
};
}
function mockRetestStream(mockApi) {
mockApi.respond('GET', `/api/conversations/${BRANCH_ID}`, { json: branchConversation() });
mockApi.respond('GET', `/api/conversations/${BRANCH_ID}/plan-tasks`,
{ json: { tasks: [], total: 0, completed: 0, running: false } });
mockApi.respond('POST', '/api/agent/stream', () => {
const events = [
{ type: 'conversation', data: { conversationId: BRANCH_ID } },
{ type: 'response_start', data: { conversationId: BRANCH_ID } },
{ type: 'response_delta', message: REPLY, data: { accumulated: REPLY } },
{ type: 'response', message: REPLY, data: { conversationId: BRANCH_ID, messageId: 'fixture-retest-assistant', finalizable: true } },
{ type: 'done', data: {} }
];
return {
contentType: 'text/event-stream; charset=utf-8',
body: events.map(event => `data: ${JSON.stringify(event)}\n\n`).join('') + 'data: [DONE]\n\n'
};
});
}
test('detail drawer retest creates a branch and auto-sends the resolved prompt', async ({ page, mockApi }, testInfo) => {
mockRetestStream(mockApi);
mockApi.respond('POST', RETEST_PATH, { json: {
conversation: branchConversation(),
prompt: RESOLVED_PROMPT,
vulnerability_id: VULNERABILITY.id,
source_conversation_id: CONVERSATION.id
} });
await visit(page, `vulnerabilities?id=${VULNERABILITY.id}`);
const drawer = page.getByRole('dialog');
await expect(drawer).toContainText(VULNERABILITY.title);
const retestButton = drawer.getByRole('button', { name: /复测$/ });
await expect(retestButton).toBeEnabled();
await retestButton.click();
const modal = page.getByRole('dialog', { name: MODAL_TITLE });
await expect(modal).toBeVisible();
await expect(modal).toContainText(VULNERABILITY.title);
const textarea = modal.locator('textarea');
await expect(textarea).toHaveValue(`${DEFAULT_PROMPT}\n\n${GUIDANCE}\n${VULNERABILITY.retest_notes}`);
await screenshot(page, testInfo, 'vulnerability-retest-modal');
await modal.getByRole('button', { name: label('vulnerabilityRetest.confirm', '确认并开始复测'), exact: true }).click();
// 分支创建请求携带弹窗里的提示词(占位符交给服务端解析)。
await expect.poll(() => mockApi.calls('POST', RETEST_PATH).length).toBe(1);
expect(mockApi.calls('POST', RETEST_PATH)[0].body).toEqual({
prompt: `${DEFAULT_PROMPT}\n\n${GUIDANCE}\n${VULNERABILITY.retest_notes}`
});
// 跳转到分支会话并自动发送解析后的提示词。
await expect.poll(() => mockApi.calls('POST', '/api/agent/stream').length).toBe(1);
expect(mockApi.calls('POST', '/api/agent/stream')[0].body).toMatchObject({
message: RESOLVED_PROMPT,
conversationId: BRANCH_ID
});
const chat = page.locator('#main-content');
await expect(chat).toContainText(REPLY);
const composer = chat.getByRole('textbox', { name: label('chat.composerLabel', '对话内容'), exact: true });
await expect(composer).toHaveValue('');
expect(await page.evaluate(() => sessionStorage.getItem('csai-retest-autosend'))).toBe(null);
await screenshot(page, testInfo, 'vulnerability-retest-autosend');
expect(mockApi.writes().map(write => write.path)).toEqual([RETEST_PATH, '/api/agent/stream']);
});
test('opening a retest branch manually restores the pending prompt without auto-sending', async ({ page, mockApi }) => {
mockApi.respond('GET', `/api/conversations/${BRANCH_ID}`, { json: branchConversation({ pendingPrompt: RESOLVED_PROMPT }) });
mockApi.respond('GET', `/api/conversations/${BRANCH_ID}/plan-tasks`,
{ json: { tasks: [], total: 0, completed: 0, running: false } });
await page.goto(`/static/app/#/chat?conversation=${BRANCH_ID}`);
const main = await page.locator('#main-content');
await expect(main).toContainText(`复测:${VULNERABILITY.title}`);
const composer = main.getByRole('textbox', { name: label('chat.composerLabel', '对话内容'), exact: true });
await expect(composer).toHaveValue(RESOLVED_PROMPT);
expect(mockApi.writes()).toEqual([]);
});
test('retest button stays disabled without a source conversation and reports server errors', async ({ page, mockApi }) => {
const standalone = { ...VULNERABILITY, id: 'fixture-retest-standalone', conversation_id: '' };
mockApi.respond('GET', `/api/vulnerabilities/${standalone.id}/context`,
{ json: { vulnerability: standalone, history: [], members: [], assets: [] } });
mockApi.respond('GET', `/api/vulnerabilities/${standalone.id}`, { json: standalone });
await visit(page, `vulnerabilities?id=${standalone.id}`);
const drawer = page.getByRole('dialog');
await expect(drawer).toContainText(standalone.title);
await expect(drawer.getByRole('button', { name: /复测$/ })).toBeDisabled();
// 服务器拒绝(如原始对话仍在执行)时错误留在弹窗内,可调整后重试。
await page.goto(`/static/app/#/vulnerabilities?id=${VULNERABILITY.id}`);
await page.reload();
const retryDrawer = page.getByRole('dialog');
await expect(retryDrawer).toContainText(VULNERABILITY.title);
await retryDrawer.getByRole('button', { name: /复测$/ }).click();
mockApi.fail('POST', RETEST_PATH, '原始对话仍在执行,请等待完成或停止后再复测');
const modal = page.getByRole('dialog', { name: MODAL_TITLE });
await modal.getByRole('button', { name: label('vulnerabilityRetest.confirm', '确认并开始复测'), exact: true }).click();
await expect(modal).toContainText('原始对话仍在执行,请等待完成或停止后再复测');
await expect(modal).toBeVisible();
expect(page.url()).toContain('/vulnerabilities');
});