|
| 1 | +import type { IpcMainEvent, WebContents } from 'electron'; |
| 2 | + |
| 3 | +import { startFiddle } from './fiddle-core'; |
| 4 | +import { ipcMainManager } from './ipc'; |
| 5 | +import { pushOutputLine } from './utils/push-output'; |
| 6 | +import { setVersion } from './utils/set-version'; |
| 7 | +import { RunResult, RunnableVersion } from '../interfaces'; |
| 8 | +import { IpcEvents } from '../ipc-events'; |
| 9 | +import { Bisector } from '../utils/bisect'; |
| 10 | + |
| 11 | +const resultString: Record<RunResult, string> = Object.freeze({ |
| 12 | + [RunResult.FAILURE]: '❌ failed', |
| 13 | + [RunResult.INVALID]: '❓ invalid', |
| 14 | + [RunResult.SUCCESS]: '✅ passed', |
| 15 | +}); |
| 16 | + |
| 17 | +/** |
| 18 | + * Bisect the current fiddle across the specified versions. |
| 19 | + * |
| 20 | + * @param versions - versions to bisect |
| 21 | + */ |
| 22 | +async function autobisect( |
| 23 | + webContents: WebContents, |
| 24 | + versions: Array<RunnableVersion>, |
| 25 | +) { |
| 26 | + try { |
| 27 | + ipcMainManager.send(IpcEvents.IS_AUTO_BISECTING, [true], webContents); |
| 28 | + await autobisectImpl(webContents, versions); |
| 29 | + } finally { |
| 30 | + ipcMainManager.send(IpcEvents.IS_AUTO_BISECTING, [false], webContents); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function autobisectImpl( |
| 35 | + webContents: WebContents, |
| 36 | + versions: Array<RunnableVersion>, |
| 37 | +) { |
| 38 | + const prefix = `Runner: autobisect`; |
| 39 | + |
| 40 | + // precondition: can't bisect unless we have >= 2 versions |
| 41 | + if (versions.length < 2) { |
| 42 | + pushOutputLine( |
| 43 | + webContents, |
| 44 | + `${prefix} needs at least two Electron versions`, |
| 45 | + ); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + const results: Map<string, RunResult> = new Map(); |
| 50 | + |
| 51 | + const runVersion = async (version: string) => { |
| 52 | + let result = results.get(version); |
| 53 | + if (result === undefined) { |
| 54 | + const pre = `${prefix} Electron ${version} -`; |
| 55 | + pushOutputLine(webContents, `${pre} setting version`); |
| 56 | + await setVersion(webContents, version); |
| 57 | + pushOutputLine(webContents, `${pre} starting test`); |
| 58 | + result = await startFiddle(webContents); |
| 59 | + results.set(version, result); |
| 60 | + pushOutputLine( |
| 61 | + webContents, |
| 62 | + `${pre} finished test ${resultString[result]}`, |
| 63 | + ); |
| 64 | + } |
| 65 | + return result; |
| 66 | + }; |
| 67 | + |
| 68 | + const bisector = new Bisector(versions); |
| 69 | + let targetVersion = bisector.getCurrentVersion(); |
| 70 | + let next; |
| 71 | + while (true) { |
| 72 | + const { version } = targetVersion; |
| 73 | + |
| 74 | + const result = await runVersion(version); |
| 75 | + if (result === RunResult.INVALID) { |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + next = bisector.continue(result === RunResult.SUCCESS); |
| 80 | + if (Array.isArray(next)) { |
| 81 | + break; |
| 82 | + } |
| 83 | + |
| 84 | + targetVersion = next; |
| 85 | + } |
| 86 | + |
| 87 | + const [good, bad] = next.map((v) => v.version); |
| 88 | + const resultGood = await runVersion(good); |
| 89 | + const resultBad = await runVersion(bad); |
| 90 | + if (resultGood === resultBad) { |
| 91 | + pushOutputLine( |
| 92 | + webContents, |
| 93 | + `${prefix} 'good' ${good} and 'bad' ${bad} both returned ${resultString[resultGood]}`, |
| 94 | + ); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + const msgs = [ |
| 99 | + `${prefix} complete`, |
| 100 | + `${prefix} ${resultString[RunResult.SUCCESS]} ${good}`, |
| 101 | + `${prefix} ${resultString[RunResult.FAILURE]} ${bad}`, |
| 102 | + `${prefix} Commits between versions:`, |
| 103 | + `https://github.com/electron/electron/compare/v${good}...v${bad}`, |
| 104 | + ]; |
| 105 | + msgs.forEach((msg) => pushOutputLine(webContents, msg)); |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Wire up the IPC handler so the renderer can trigger an autobisect. |
| 110 | + */ |
| 111 | +export function setupAutobisect(): void { |
| 112 | + ipcMainManager.on( |
| 113 | + IpcEvents.AUTOBISECT_FIDDLE, |
| 114 | + (event: IpcMainEvent, versions: Array<RunnableVersion>) => { |
| 115 | + autobisect(event.sender, versions).catch(console.error); |
| 116 | + }, |
| 117 | + ); |
| 118 | +} |
0 commit comments