TidGi-Desktop/features/stepDefinitions/agent.ts
2025-08-18 22:52:52 +08:00

66 lines
2.5 KiB
TypeScript

import { After, Given, Then } from '@cucumber/cucumber';
import { MockOpenAIServer } from '../supports/mockOpenAI';
import type { ApplicationWorld } from './application';
// Agent-specific Given steps
Given('I have started the mock OpenAI server', async function(this: ApplicationWorld) {
// Start mock OpenAI server with fixed port 15121 for consistency
this.mockOpenAIServer = new MockOpenAIServer(15121);
await this.mockOpenAIServer.start();
});
// Agent-specific cleanup - only for agent scenarios
After({ tags: '@agent' }, async function(this: ApplicationWorld) {
// Stop mock OpenAI server
if (this.mockOpenAIServer) {
await this.mockOpenAIServer.stop();
this.mockOpenAIServer = undefined;
}
});
// Only keep agent-specific steps that can't use generic ones
Then('I should see {int} messages in chat history', async function(this: ApplicationWorld, expectedCount: number) {
const currentWindow = this.currentWindow || this.mainWindow;
if (!currentWindow) {
throw new Error('No current window is available');
}
// Use precise selector based on the provided HTML structure
const messageSelector = '[data-testid="message-bubble"]';
try {
// Wait for messages to reach expected count, checking periodically for streaming
for (let attempt = 1; attempt <= expectedCount * 3; attempt++) {
try {
// Wait for at least one message to exist
await currentWindow.waitForSelector(messageSelector, { timeout: 5000 });
// Count current messages
const messages = currentWindow.locator(messageSelector);
const currentCount = await messages.count();
if (currentCount === expectedCount) {
return;
} else if (currentCount > expectedCount) {
throw new Error(`Expected ${expectedCount} messages but found ${currentCount} (too many)`);
}
// If not enough messages yet, wait a bit more for streaming
if (attempt < expectedCount * 3) {
await currentWindow.waitForTimeout(2000);
}
} catch (timeoutError) {
if (attempt === expectedCount * 3) {
throw timeoutError;
}
}
}
// Final attempt to get the count
const finalCount = await currentWindow.locator(messageSelector).count();
throw new Error(`Expected ${expectedCount} messages but found ${finalCount} after waiting for streaming to complete`);
} catch (error) {
throw new Error(`Could not find expected ${expectedCount} messages. Error: ${(error as Error).message}`);
}
});