|
| 1 | +import { assertEquals } from '@std/assert'; |
| 2 | +import { FlowWorkerLifecycle } from '../../src/flow/FlowWorkerLifecycle.ts'; |
| 3 | +import { Queries, type EnsureFlowCompiledResult } from '../../src/core/Queries.ts'; |
| 4 | +import type { WorkerRow } from '../../src/core/types.ts'; |
| 5 | +import { Flow, type FlowShape } from '@pgflow/dsl'; |
| 6 | +import type { Logger } from '../../src/platform/types.ts'; |
| 7 | +import type { postgres } from '../sql.ts'; |
| 8 | + |
| 9 | +// Mock Queries |
| 10 | +class MockQueries extends Queries { |
| 11 | + public ensureFlowCompiledCallCount = 0; |
| 12 | + |
| 13 | + constructor() { |
| 14 | + // Pass null as sql since we'll override all methods |
| 15 | + super(null as unknown as postgres.Sql); |
| 16 | + } |
| 17 | + |
| 18 | + override onWorkerStarted(params: { workerId: string; edgeFunctionName: string; queueName: string }): Promise<WorkerRow> { |
| 19 | + return Promise.resolve({ |
| 20 | + worker_id: params.workerId, |
| 21 | + queue_name: params.queueName, |
| 22 | + function_name: params.edgeFunctionName, |
| 23 | + started_at: new Date().toISOString(), |
| 24 | + deprecated_at: null, |
| 25 | + last_heartbeat_at: new Date().toISOString(), |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + override sendHeartbeat(_workerRow: WorkerRow): Promise<{ is_deprecated: boolean }> { |
| 30 | + return Promise.resolve({ is_deprecated: false }); |
| 31 | + } |
| 32 | + |
| 33 | + override ensureFlowCompiled( |
| 34 | + _flowSlug: string, |
| 35 | + _shape: FlowShape, |
| 36 | + _mode: 'development' | 'production' |
| 37 | + ): Promise<EnsureFlowCompiledResult> { |
| 38 | + this.ensureFlowCompiledCallCount++; |
| 39 | + return Promise.resolve({ status: 'verified', differences: [] }); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// Real Flow for testing - using the DSL to create a valid flow |
| 44 | +const TestFlow = new Flow<{ value: number }>({ slug: 'test_flow' }) |
| 45 | + .step({ slug: 'step1' }, (input) => input.run.value); |
| 46 | + |
| 47 | +const createMockFlow = () => TestFlow; |
| 48 | + |
| 49 | +const createLogger = (): Logger => ({ |
| 50 | + debug: () => {}, |
| 51 | + info: () => {}, |
| 52 | + error: () => {}, |
| 53 | + warn: () => {}, |
| 54 | +}); |
| 55 | + |
| 56 | +Deno.test('FlowWorkerLifecycle - calls ensureFlowCompiled when ensureCompiledOnStartup is true', async () => { |
| 57 | + const mockQueries = new MockQueries(); |
| 58 | + const mockFlow = createMockFlow(); |
| 59 | + const logger = createLogger(); |
| 60 | + |
| 61 | + const lifecycle = new FlowWorkerLifecycle(mockQueries, mockFlow, logger, { |
| 62 | + ensureCompiledOnStartup: true |
| 63 | + }); |
| 64 | + |
| 65 | + await lifecycle.acknowledgeStart({ |
| 66 | + workerId: 'test-worker-id', |
| 67 | + edgeFunctionName: 'test-function', |
| 68 | + }); |
| 69 | + |
| 70 | + assertEquals(mockQueries.ensureFlowCompiledCallCount, 1, 'ensureFlowCompiled should be called once'); |
| 71 | +}); |
| 72 | + |
| 73 | +Deno.test('FlowWorkerLifecycle - skips ensureFlowCompiled when ensureCompiledOnStartup is false', async () => { |
| 74 | + const mockQueries = new MockQueries(); |
| 75 | + const mockFlow = createMockFlow(); |
| 76 | + const logger = createLogger(); |
| 77 | + |
| 78 | + const lifecycle = new FlowWorkerLifecycle(mockQueries, mockFlow, logger, { |
| 79 | + ensureCompiledOnStartup: false |
| 80 | + }); |
| 81 | + |
| 82 | + await lifecycle.acknowledgeStart({ |
| 83 | + workerId: 'test-worker-id', |
| 84 | + edgeFunctionName: 'test-function', |
| 85 | + }); |
| 86 | + |
| 87 | + assertEquals(mockQueries.ensureFlowCompiledCallCount, 0, 'ensureFlowCompiled should NOT be called'); |
| 88 | +}); |
| 89 | + |
| 90 | +Deno.test('FlowWorkerLifecycle - calls ensureFlowCompiled by default (no config)', async () => { |
| 91 | + const mockQueries = new MockQueries(); |
| 92 | + const mockFlow = createMockFlow(); |
| 93 | + const logger = createLogger(); |
| 94 | + |
| 95 | + const lifecycle = new FlowWorkerLifecycle(mockQueries, mockFlow, logger); |
| 96 | + |
| 97 | + await lifecycle.acknowledgeStart({ |
| 98 | + workerId: 'test-worker-id', |
| 99 | + edgeFunctionName: 'test-function', |
| 100 | + }); |
| 101 | + |
| 102 | + assertEquals(mockQueries.ensureFlowCompiledCallCount, 1, 'ensureFlowCompiled should be called by default'); |
| 103 | +}); |
| 104 | + |
| 105 | +Deno.test('FlowWorkerLifecycle - calls ensureFlowCompiled by default (empty config)', async () => { |
| 106 | + const mockQueries = new MockQueries(); |
| 107 | + const mockFlow = createMockFlow(); |
| 108 | + const logger = createLogger(); |
| 109 | + |
| 110 | + const lifecycle = new FlowWorkerLifecycle(mockQueries, mockFlow, logger, {}); |
| 111 | + |
| 112 | + await lifecycle.acknowledgeStart({ |
| 113 | + workerId: 'test-worker-id', |
| 114 | + edgeFunctionName: 'test-function', |
| 115 | + }); |
| 116 | + |
| 117 | + assertEquals(mockQueries.ensureFlowCompiledCallCount, 1, 'ensureFlowCompiled should be called with empty config'); |
| 118 | +}); |
| 119 | + |
| 120 | +Deno.test('FlowWorkerLifecycle - logs skip message when ensureCompiledOnStartup is false', async () => { |
| 121 | + const logs: string[] = []; |
| 122 | + const testLogger: Logger = { |
| 123 | + debug: () => {}, |
| 124 | + info: (msg: string) => logs.push(msg), |
| 125 | + error: () => {}, |
| 126 | + warn: () => {}, |
| 127 | + }; |
| 128 | + |
| 129 | + const mockQueries = new MockQueries(); |
| 130 | + const mockFlow = createMockFlow(); |
| 131 | + |
| 132 | + const lifecycle = new FlowWorkerLifecycle(mockQueries, mockFlow, testLogger, { |
| 133 | + ensureCompiledOnStartup: false |
| 134 | + }); |
| 135 | + |
| 136 | + await lifecycle.acknowledgeStart({ |
| 137 | + workerId: 'test-worker-id', |
| 138 | + edgeFunctionName: 'test-function', |
| 139 | + }); |
| 140 | + |
| 141 | + const skipLog = logs.find(log => log.includes('Skipping compilation')); |
| 142 | + assertEquals(skipLog !== undefined, true, 'Should log skip message'); |
| 143 | + assertEquals(skipLog?.includes('ensureCompiledOnStartup=false'), true, 'Skip message should mention the config flag'); |
| 144 | +}); |
| 145 | + |
| 146 | +Deno.test('FlowWorkerLifecycle - does not log skip message when ensureCompiledOnStartup is true', async () => { |
| 147 | + const logs: string[] = []; |
| 148 | + const testLogger: Logger = { |
| 149 | + debug: () => {}, |
| 150 | + info: (msg: string) => logs.push(msg), |
| 151 | + error: () => {}, |
| 152 | + warn: () => {}, |
| 153 | + }; |
| 154 | + |
| 155 | + const mockQueries = new MockQueries(); |
| 156 | + const mockFlow = createMockFlow(); |
| 157 | + |
| 158 | + const lifecycle = new FlowWorkerLifecycle(mockQueries, mockFlow, testLogger, { |
| 159 | + ensureCompiledOnStartup: true |
| 160 | + }); |
| 161 | + |
| 162 | + await lifecycle.acknowledgeStart({ |
| 163 | + workerId: 'test-worker-id', |
| 164 | + edgeFunctionName: 'test-function', |
| 165 | + }); |
| 166 | + |
| 167 | + const skipLog = logs.find(log => log.includes('Skipping compilation')); |
| 168 | + assertEquals(skipLog, undefined, 'Should NOT log skip message when compilation is enabled'); |
| 169 | +}); |
0 commit comments