TidGi-Desktop/features/stepDefinitions/agent.ts
lin onetwo 5f990700ce rename
2025-08-17 23:58:44 +08:00

73 lines
2.9 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();
console.log(`Mock OpenAI server running at: ${this.mockOpenAIServer.baseUrl}`);
});
// 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"]';
console.log(`Looking for ${expectedCount} messages with selector: ${messageSelector}`);
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();
console.log(`Attempt ${attempt}: Found ${currentCount} messages (expecting ${expectedCount})`);
if (currentCount === expectedCount) {
console.log(`✓ Found exactly ${expectedCount} messages in chat history`);
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;
}
console.log(`Waiting for more messages... (attempt ${attempt})`);
}
}
// 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}`);
}
});