TidGi-Desktop/features/stepDefinitions/logging.ts
lin onetwo 1cd266d8fe test: implement scenario isolation for E2E tests
- Isolate each test scenario in test-artifacts/{scenarioSlug}/ directory
- Use dynamic ports for mock OpenAI server to avoid port conflicts
- Log VIEW_LOADED event via did-finish-load in main process (more reliable)
- Search all .log files when waiting for log markers
- Increase timeout for log marker steps to 15 seconds
- Fix ts-node cache issues by clearing cache before tests
- Move application launch to individual scenarios (required for mock server setup)

All 45 E2E test scenarios now pass consistently.
2026-01-17 01:19:53 +08:00

64 lines
2.1 KiB
TypeScript

import { After, DataTable, Then, When } from '@cucumber/cucumber';
import fs from 'fs';
import path from 'path';
import { MockOAuthServer } from '../supports/mockOAuthServer';
import { ApplicationWorld } from './application';
Then('I should find log entries containing', async function(this: ApplicationWorld, dataTable: DataTable | undefined) {
const expectedRows = dataTable?.raw().map((r: string[]) => r[0]);
// Use scenario-specific logs directory
const scenarioRoot = path.resolve(process.cwd(), 'test-artifacts', this.scenarioSlug);
const logsDir = path.resolve(scenarioRoot, 'userData-test', 'logs');
// Consider all log files in logs directory (including wiki logs like wiki-2025-10-25.log)
const files = fs.readdirSync(logsDir).filter((f) => f.endsWith('.log'));
const missing = expectedRows?.filter((expectedRow: string) => {
// Check if any log file contains this expected content
return !files.some((file) => {
try {
const content = fs.readFileSync(path.join(logsDir, file), 'utf8');
return content.includes(expectedRow);
} catch {
return false;
}
});
});
if (missing?.length) {
throw new Error(`Missing expected log messages in ${files.length} log file(s)`);
}
});
// OAuth Server Steps
When('I start Mock OAuth Server on port {int}', async function(this: ApplicationWorld, port: number) {
this.mockOAuthServer = new MockOAuthServer(
{ clientId: 'test-client-id' },
port,
);
await this.mockOAuthServer.start();
});
When('I stop Mock OAuth Server', async function(this: ApplicationWorld) {
if (this.mockOAuthServer) {
await this.mockOAuthServer.stop();
this.mockOAuthServer = undefined;
}
});
// Clean up Mock OAuth Server after @oauth tests
After({ tags: '@oauth' }, async function(this: ApplicationWorld) {
if (this.mockOAuthServer) {
try {
await Promise.race([
this.mockOAuthServer.stop(),
new Promise<void>((resolve) => setTimeout(resolve, 2000)),
]);
} catch {
// Ignore errors during cleanup
} finally {
this.mockOAuthServer = undefined;
}
}
});