|
| 1 | +/** |
| 2 | + * useTestRunner Hook |
| 3 | + * |
| 4 | + * Encapsulates test execution lifecycle via server-side execution. |
| 5 | + * Handles framework detection/selection, flow creation, SSE subscription, |
| 6 | + * and server-side process spawning with live output streaming. |
| 7 | + */ |
| 8 | + |
| 9 | +import { type Accessor, createSignal } from 'solid-js'; |
| 10 | +import { useObserver, useSDK } from '../context'; |
| 11 | +import type { TestFrameworkOption } from '../sdk'; |
| 12 | +import { useFlowSubscription } from './use-flow-subscription'; |
| 13 | + |
| 14 | +export interface TestRunnerOptions { |
| 15 | + onFrameworkDialogNeeded: ( |
| 16 | + testPath: string, |
| 17 | + options: TestFrameworkOption[], |
| 18 | + onSelect: (frameworkId: string) => void |
| 19 | + ) => void; |
| 20 | +} |
| 21 | + |
| 22 | +export interface TestRunnerReturn { |
| 23 | + runTest: (testPath: string) => Promise<void>; |
| 24 | + cancelTest: () => void; |
| 25 | + isRunning: Accessor<boolean>; |
| 26 | + cleanup: () => void; |
| 27 | +} |
| 28 | + |
| 29 | +export function useTestRunner(options: TestRunnerOptions): TestRunnerReturn { |
| 30 | + const sdk = useSDK(); |
| 31 | + const observer = useObserver(); |
| 32 | + const flowSubscription = useFlowSubscription(); |
| 33 | + |
| 34 | + let currentRunId: string | undefined; |
| 35 | + const [isStarting, setIsStarting] = createSignal(false); |
| 36 | + |
| 37 | + async function startTest(testPath: string, frameworkId?: string) { |
| 38 | + let flowId: string | undefined; |
| 39 | + try { |
| 40 | + const createdFlow = await sdk.createFlow(`Test: ${testPath}`); |
| 41 | + flowId = createdFlow.flowId; |
| 42 | + |
| 43 | + observer.setState('flowId', flowId); |
| 44 | + flowSubscription.subscribe(flowId); |
| 45 | + |
| 46 | + const { runId } = await sdk.runTest(testPath, frameworkId, flowId); |
| 47 | + currentRunId = runId; |
| 48 | + |
| 49 | + if (!observer.state.runningScript) { |
| 50 | + observer.setState('runningScript', { |
| 51 | + path: testPath, |
| 52 | + pid: 0, |
| 53 | + startedAt: Date.now() |
| 54 | + }); |
| 55 | + } |
| 56 | + } catch (err) { |
| 57 | + console.error('Failed to start test:', err); |
| 58 | + if (flowId) { |
| 59 | + flowSubscription.unsubscribe(); |
| 60 | + observer.setState('flowId', undefined); |
| 61 | + } |
| 62 | + observer.setState('sseStatus', 'error'); |
| 63 | + currentRunId = undefined; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + async function handleRunTest(testPath: string) { |
| 68 | + if (observer.state.runningScript || isStarting()) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + setIsStarting(true); |
| 73 | + observer.reset(); |
| 74 | + |
| 75 | + try { |
| 76 | + const { detected, options: frameworkOptions } = await sdk.getTestFrameworks(testPath); |
| 77 | + |
| 78 | + if (detected) { |
| 79 | + await startTest(testPath, detected); |
| 80 | + } else { |
| 81 | + options.onFrameworkDialogNeeded(testPath, frameworkOptions, (selectedFrameworkId) => { |
| 82 | + void startTest(testPath, selectedFrameworkId); |
| 83 | + }); |
| 84 | + } |
| 85 | + } catch (err) { |
| 86 | + console.error('Failed to get test frameworks:', err); |
| 87 | + observer.setState('sseStatus', 'error'); |
| 88 | + } finally { |
| 89 | + setIsStarting(false); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + async function cancelTest() { |
| 94 | + if (currentRunId) { |
| 95 | + try { |
| 96 | + await sdk.cancelTest(currentRunId); |
| 97 | + } catch { |
| 98 | + // Test may have already finished |
| 99 | + } |
| 100 | + currentRunId = undefined; |
| 101 | + } |
| 102 | + setIsStarting(false); |
| 103 | + flowSubscription.unsubscribe(); |
| 104 | + } |
| 105 | + |
| 106 | + function cleanup() { |
| 107 | + if (currentRunId) { |
| 108 | + sdk.cancelTest(currentRunId).catch(() => {}); |
| 109 | + currentRunId = undefined; |
| 110 | + } |
| 111 | + flowSubscription.cleanup(); |
| 112 | + setIsStarting(false); |
| 113 | + } |
| 114 | + |
| 115 | + return { |
| 116 | + runTest: handleRunTest, |
| 117 | + cancelTest, |
| 118 | + isRunning: () => isStarting() || !!observer.state.runningScript, |
| 119 | + cleanup |
| 120 | + }; |
| 121 | +} |
0 commit comments