Skip to content

Commit 897b1eb

Browse files
committed
refactor: replace console.log with debug utility in tests (#494)
# Reduce Test Verbosity with Conditional Debug Logging This PR introduces a debug logging utility to reduce console noise during test runs while preserving the ability to see detailed logs when needed. Key changes: - Added a new `debug.ts` helper with a `log()` function that only outputs when `DEBUG=1` or `VERBOSE=1` environment variables are set - Replaced all `console.log()` calls in E2E tests with the new `log()` function - Updated the global setup script to use the same logging pattern - Simplified the global setup output to show only essential information by default This change makes test output cleaner and more focused on actual test results while maintaining the ability to see detailed logs when debugging by running tests with `DEBUG=1`.
1 parent f9bfb15 commit 897b1eb

6 files changed

Lines changed: 75 additions & 57 deletions

File tree

pkgs/client/__tests__/e2e/concurrent-operations.test.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { FlowRunStatus, FlowStepStatus } from '../../src/lib/types.js';
88
import { PgflowSqlClient } from '@pgflow/core';
99
import { readAndStart } from '../helpers/polling.js';
1010
import { cleanupFlow } from '../helpers/cleanup.js';
11+
import { log } from '../helpers/debug.js';
1112

1213
describe('Concurrent Operations Tests', () => {
1314
it(
@@ -36,15 +37,15 @@ describe('Concurrent Operations Tests', () => {
3637
});
3738

3839
// Start flows sequentially to avoid overwhelming the system
39-
console.log('=== Starting flows ===');
40+
log('=== Starting flows ===');
4041
const run1 = await pgflowClient.startFlow(flow1.slug, { data: 'flow1-data' });
4142
const run2 = await pgflowClient.startFlow(flow2.slug, { data: 'flow2-data' });
4243

4344
expect(run1.flow_slug).toBe(flow1.slug);
4445
expect(run2.flow_slug).toBe(flow2.slug);
4546

4647
// Get and complete tasks from both flows
47-
console.log('=== Completing steps ===');
48+
log('=== Completing steps ===');
4849

4950
const tasks1 = await readAndStart(sql, sqlClient, flow1.slug, 1, 5);
5051
const tasks2 = await readAndStart(sql, sqlClient, flow2.slug, 1, 5);
@@ -69,16 +70,16 @@ describe('Concurrent Operations Tests', () => {
6970
// Debug: Check database state directly
7071
const dbState1 = await sql`SELECT status, output FROM pgflow.step_tasks WHERE run_id = ${run1.run_id}::uuid`;
7172
const dbState2 = await sql`SELECT status, output FROM pgflow.step_tasks WHERE run_id = ${run2.run_id}::uuid`;
72-
console.log('DB State 1:', dbState1);
73-
console.log('DB State 2:', dbState2);
73+
log('DB State 1:', dbState1);
74+
log('DB State 2:', dbState2);
7475

7576
// Verify database shows completion (since realtime events may be delayed)
7677
expect(dbState1[0].status).toBe('completed');
7778
expect(dbState2[0].status).toBe('completed');
7879
expect(dbState1[0].output).toEqual({ result: 'flow1-completed' });
7980
expect(dbState2[0].output).toEqual({ result: 'flow2-completed' });
8081

81-
console.log('=== Concurrent flows completed successfully ===');
82+
log('=== Concurrent flows completed successfully ===');
8283
await supabaseClient.removeAllChannels();
8384
}),
8485
40000
@@ -164,9 +165,9 @@ describe('Concurrent Operations Tests', () => {
164165
expect(observerRun2!.step('shared_step').output).toEqual(stepOutput);
165166

166167
// Log event counts for debugging (realtime delivery can be unreliable in tests)
167-
console.log('Client 1 events:', client1Events.length);
168-
console.log('Client 2 events:', client2Events.length);
169-
console.log('Client 3 events:', client3Events.length);
168+
log('Client 1 events:', client1Events.length);
169+
log('Client 2 events:', client2Events.length);
170+
log('Client 3 events:', client3Events.length);
170171

171172
// Don't assert on event count - final state verification is more important
172173
// The test already verified all clients have correct final state above
@@ -251,7 +252,7 @@ describe('Concurrent Operations Tests', () => {
251252
expect(dbState[0].output).toEqual(expectedOutput);
252253
}
253254

254-
console.log('=== All concurrent instances completed without conflicts ===');
255+
log('=== All concurrent instances completed without conflicts ===');
255256
await supabaseClient.removeAllChannels();
256257
}),
257258
40000
@@ -332,7 +333,7 @@ describe('Concurrent Operations Tests', () => {
332333
expect(dbStateA[0].output.result).toContain('flow-a');
333334
expect(dbStateB[0].output.result).toContain('flow-b');
334335

335-
console.log('=== Flow isolation maintained successfully ===');
336+
log('=== Flow isolation maintained successfully ===');
336337
await supabaseClient.removeAllChannels();
337338
}),
338339
40000

pkgs/client/__tests__/e2e/full-stack-dsl.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Flow } from '@pgflow/dsl';
99
import { compileFlow } from '@pgflow/dsl';
1010
import { readAndStart } from '../helpers/polling.js';
1111
import { cleanupFlow } from '../helpers/cleanup.js';
12+
import { log } from '../helpers/debug.js';
1213

1314
describe('Full Stack DSL Integration', () => {
1415
it(
@@ -41,7 +42,7 @@ describe('Full Stack DSL Integration', () => {
4142

4243
// 2. Compile to SQL
4344
const flowSql = compileFlow(SimpleFlow);
44-
console.log('Generated SQL statements:', flowSql);
45+
log('Generated SQL statements:', flowSql);
4546

4647
// 3. Execute SQL to create flow definition
4748
for (const statement of flowSql) {
@@ -90,7 +91,7 @@ describe('Full Stack DSL Integration', () => {
9091
expect(run.input).toEqual(input);
9192

9293
// 7. Execute the complete flow lifecycle
93-
console.log('=== Step 1: Completing fetch step ===');
94+
log('=== Step 1: Completing fetch step ===');
9495
let tasks = await readAndStart(sql, sqlClient, SimpleFlow.slug, 1, 5);
9596
expect(tasks).toHaveLength(1);
9697
expect(tasks[0].step_slug).toBe('fetch');
@@ -106,7 +107,7 @@ describe('Full Stack DSL Integration', () => {
106107
expect(fetchStep.status).toBe(FlowStepStatus.Completed);
107108
expect(fetchStep.output).toEqual(fetchOutput);
108109

109-
console.log('=== Step 2: Completing process step ===');
110+
log('=== Step 2: Completing process step ===');
110111
tasks = await readAndStart(sql, sqlClient, SimpleFlow.slug, 1, 5);
111112
expect(tasks).toHaveLength(1);
112113
expect(tasks[0].step_slug).toBe('process');
@@ -127,7 +128,7 @@ describe('Full Stack DSL Integration', () => {
127128
expect(processStep.status).toBe(FlowStepStatus.Completed);
128129
expect(processStep.output).toEqual(processOutput);
129130

130-
console.log('=== Step 3: Completing save step ===');
131+
log('=== Step 3: Completing save step ===');
131132
tasks = await readAndStart(sql, sqlClient, SimpleFlow.slug, 1, 5);
132133
expect(tasks).toHaveLength(1);
133134
expect(tasks[0].step_slug).toBe('save');
@@ -157,7 +158,7 @@ describe('Full Stack DSL Integration', () => {
157158
expect(run.status).toBe(FlowRunStatus.Completed);
158159
expect(run.remaining_steps).toBe(0);
159160

160-
console.log('=== Full Stack DSL Test Completed Successfully ===');
161+
log('=== Full Stack DSL Test Completed Successfully ===');
161162
await supabaseClient.removeAllChannels();
162163
}),
163164
30000

pkgs/client/__tests__/e2e/happy-path-e2e.test.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { FlowRunStatus, FlowStepStatus } from '../../src/lib/types.js';
88
import { PgflowSqlClient } from '@pgflow/core';
99
import { readAndStart } from '../helpers/polling.js';
1010
import { cleanupFlow } from '../helpers/cleanup.js';
11+
import { log } from '../helpers/debug.js';
1112

1213
describe('Happy Path E2E Integration', () => {
1314
it(
@@ -40,7 +41,7 @@ describe('Happy Path E2E Integration', () => {
4041

4142
// Listen to all events for verification
4243
run.on('*', (event) => {
43-
console.log('Run event received:', event);
44+
log('Run event received:', event);
4445
receivedRunEvents.push(event);
4546
});
4647

@@ -49,17 +50,17 @@ describe('Happy Path E2E Integration', () => {
4950
const saveStep = run.step('save');
5051

5152
fetchStep.on('*', (event) => {
52-
console.log('Fetch step event:', event);
53+
log('Fetch step event:', event);
5354
receivedStepEvents.push({ step: 'fetch', event });
5455
});
5556

5657
processStep.on('*', (event) => {
57-
console.log('Process step event:', event);
58+
log('Process step event:', event);
5859
receivedStepEvents.push({ step: 'process', event });
5960
});
6061

6162
saveStep.on('*', (event) => {
62-
console.log('Save step event:', event);
63+
log('Save step event:', event);
6364
receivedStepEvents.push({ step: 'save', event });
6465
});
6566

@@ -68,7 +69,7 @@ describe('Happy Path E2E Integration', () => {
6869
expect(run.input).toEqual(input);
6970

7071
// Step 1: Complete fetch step
71-
console.log('=== Step 1: Completing fetch step ===');
72+
log('=== Step 1: Completing fetch step ===');
7273
let tasks = await readAndStart(sql, sqlClient, testFlow.slug, 1, 5);
7374
expect(tasks).toHaveLength(1);
7475
expect(tasks[0].step_slug).toBe('fetch');
@@ -83,7 +84,7 @@ describe('Happy Path E2E Integration', () => {
8384
expect(fetchStep.output).toEqual(fetchOutput);
8485

8586
// Step 2: Complete process step (should now be available)
86-
console.log('=== Step 2: Completing process step ===');
87+
log('=== Step 2: Completing process step ===');
8788
tasks = await readAndStart(sql, sqlClient, testFlow.slug, 1, 5);
8889
expect(tasks).toHaveLength(1);
8990
expect(tasks[0].step_slug).toBe('process');
@@ -103,28 +104,28 @@ describe('Happy Path E2E Integration', () => {
103104
expect(processStep.output).toEqual(processOutput);
104105

105106
// Step 3: Complete save step (should now be available)
106-
console.log('=== Step 3: Completing save step ===');
107+
log('=== Step 3: Completing save step ===');
107108

108109
// Debug: Check dependencies in database
109110
const deps = await sql`
110111
SELECT * FROM pgflow.deps WHERE flow_slug = ${testFlow.slug}
111112
ORDER BY dep_slug, step_slug
112113
`;
113-
console.log('Dependencies in database:', deps);
114+
log('Dependencies in database:', deps);
114115

115116
// Debug: Check step_tasks status
116117
const stepTasks = await sql`
117118
SELECT step_slug, status, output FROM pgflow.step_tasks
118119
WHERE run_id = ${run.run_id}::uuid
119120
ORDER BY step_slug
120121
`;
121-
console.log('Step tasks status:', stepTasks);
122+
log('Step tasks status:', stepTasks);
122123

123124
tasks = await readAndStart(sql, sqlClient, testFlow.slug, 1, 5);
124125
expect(tasks).toHaveLength(1);
125126
expect(tasks[0].step_slug).toBe('save');
126127

127-
console.log('Save task input:', JSON.stringify(tasks[0].input, null, 2));
128+
log('Save task input:', JSON.stringify(tasks[0].input, null, 2));
128129

129130
expect(tasks[0].input.run).toEqual(input);
130131
expect(tasks[0].input.process).toEqual(processOutput);
@@ -150,9 +151,9 @@ describe('Happy Path E2E Integration', () => {
150151
expect(run.output).toEqual({ save: saveOutput }); // Final step output becomes run output
151152

152153
// Verify we received realtime events
153-
console.log('=== Event Verification ===');
154-
console.log('Total run events received:', receivedRunEvents.length);
155-
console.log('Total step events received:', receivedStepEvents.length);
154+
log('=== Event Verification ===');
155+
log('Total run events received:', receivedRunEvents.length);
156+
log('Total step events received:', receivedStepEvents.length);
156157

157158
// Should have received at least the completion event
158159
// Note: run:started event may fire before listeners are established
@@ -183,7 +184,7 @@ describe('Happy Path E2E Integration', () => {
183184
const remainingTasks = await readAndStart(sql, sqlClient, testFlow.slug, 1, 1);
184185
expect(remainingTasks).toHaveLength(0);
185186

186-
console.log('=== Happy Path E2E Test Completed Successfully ===');
187+
log('=== Happy Path E2E Test Completed Successfully ===');
187188
await supabaseClient.removeAllChannels();
188189
}),
189190
30000 // Allow extra time for 3-step completion

pkgs/client/__tests__/e2e/network-resilience.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { FlowStepStatus } from '../../src/lib/types.js';
88
import { PgflowSqlClient } from '@pgflow/core';
99
import { readAndStart } from '../helpers/polling.js';
1010
import { cleanupFlow } from '../helpers/cleanup.js';
11+
import { log } from '../helpers/debug.js';
1112

1213
describe('Network Resilience Tests', () => {
1314
it(
@@ -61,7 +62,7 @@ describe('Network Resilience Tests', () => {
6162
expect(stepOne.status).toBe(FlowStepStatus.Completed);
6263

6364
// Simulate network disconnection by unsubscribing from all channels
64-
console.log('=== Simulating network disconnection ===');
65+
log('=== Simulating network disconnection ===');
6566
await supabaseClient.removeAllChannels();
6667

6768
// Wait a bit to ensure disconnection
@@ -76,7 +77,7 @@ describe('Network Resilience Tests', () => {
7677
await sqlClient.completeTask(tasks[0], stepTwoOutput);
7778

7879
// Reconnect by creating a new run instance (simulates app restart/reconnection)
79-
console.log('=== Simulating reconnection ===');
80+
log('=== Simulating reconnection ===');
8081
const reconnectedRun = await pgflowClient.getRun(run.run_id);
8182
expect(reconnectedRun).toBeTruthy();
8283

@@ -114,8 +115,8 @@ describe('Network Resilience Tests', () => {
114115
expect(dbState[1].status).toBe('completed');
115116
expect(dbState[1].output).toEqual(stepTwoOutput);
116117

117-
console.log('Events before disconnect:', eventsBeforeDisconnect.length);
118-
console.log('Events after reconnect:', eventsAfterReconnect.length);
118+
log('Events before disconnect:', eventsBeforeDisconnect.length);
119+
log('Events after reconnect:', eventsAfterReconnect.length);
119120

120121
await supabaseClient.removeAllChannels();
121122
}),
@@ -148,7 +149,7 @@ describe('Network Resilience Tests', () => {
148149
if (channel) {
149150
channel.subscribe((status: string) => {
150151
connectionEvents.push(status);
151-
console.log('Channel status changed:', status);
152+
log('Channel status changed:', status);
152153
});
153154
}
154155

@@ -165,7 +166,7 @@ describe('Network Resilience Tests', () => {
165166
expect(step.status).toBe(FlowStepStatus.Completed);
166167
expect(step.output).toEqual(stepOutput);
167168

168-
console.log('Connection events recorded:', connectionEvents);
169+
log('Connection events recorded:', connectionEvents);
169170

170171
await supabaseClient.removeAllChannels();
171172
}),
@@ -195,7 +196,7 @@ describe('Network Resilience Tests', () => {
195196

196197
// Simulate poor network by introducing delays and multiple reconnections
197198
for (let i = 0; i < 3; i++) {
198-
console.log(`=== Connection cycle ${i + 1} ===`);
199+
log(`=== Connection cycle ${i + 1} ===`);
199200

200201
// Short connection period
201202
await new Promise((resolve) => setTimeout(resolve, 100));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Debug logging utility for e2e tests.
3+
* Enable verbose output by setting DEBUG=1 or VERBOSE=1 environment variable.
4+
*/
5+
export const DEBUG = process.env.DEBUG === '1' || process.env.VERBOSE === '1';
6+
7+
export const log = (...args: unknown[]) => {
8+
if (DEBUG) {
9+
console.log(...args);
10+
}
11+
};

0 commit comments

Comments
 (0)