|
| 1 | +import fs from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 5 | + |
| 6 | +const plugin = require('./patch-expo-background-task'); |
| 7 | + |
| 8 | +const { |
| 9 | + APPLIED_MARKER, |
| 10 | + WORK_INFO_MARKER, |
| 11 | + applyRunningWorkerGuard, |
| 12 | + getSchedulerCandidates, |
| 13 | + patchSchedulerSource, |
| 14 | +} = plugin.__testables; |
| 15 | + |
| 16 | +// The real vendored file. It may already carry the patch from an earlier prebuild, so the |
| 17 | +// anchor-level assertions run against a pristine fixture and this one checks the end state. |
| 18 | +const vendoredSchedulerPath = [process.cwd(), path.join(process.cwd(), 'apps', 'mobile')] |
| 19 | + .flatMap((root) => getSchedulerCandidates(root)) |
| 20 | + .find((candidate) => fs.existsSync(candidate)); |
| 21 | + |
| 22 | +const readVendoredScheduler = () => { |
| 23 | + if (!vendoredSchedulerPath) { |
| 24 | + throw new Error('expo-background-task is not installed - cannot verify the patch.'); |
| 25 | + } |
| 26 | + return fs.readFileSync(vendoredSchedulerPath, 'utf8'); |
| 27 | +}; |
| 28 | + |
| 29 | +// The two upstream regions the patch anchors on, verbatim from expo-background-task 1.0.10. |
| 30 | +const PRISTINE_SOURCE = `object BackgroundTaskScheduler { |
| 31 | + private suspend fun scheduleWorker(context: Context, appScopeKey: String, cancelExisting: Boolean = true, overriddenIntervalMinutes: Long = intervalMinutes): Boolean { |
| 32 | + if (numberOfRegisteredTasksOfThisType == 0) { |
| 33 | + return false |
| 34 | + } |
| 35 | +
|
| 36 | + // Stop the current worker (if any) |
| 37 | + if (cancelExisting) { |
| 38 | + stopWorker(context) |
| 39 | + } |
| 40 | +
|
| 41 | + return true |
| 42 | + } |
| 43 | +
|
| 44 | + private suspend fun getWorkerInfo(context: Context): WorkInfo? { |
| 45 | + val workManager = WorkManager.getInstance(context) |
| 46 | +
|
| 47 | + return try { |
| 48 | + val workInfos = workManager.getWorkInfosForUniqueWork(WORKER_IDENTIFIER).await() |
| 49 | + return workInfos.firstOrNull() |
| 50 | + } catch (e: Exception) { |
| 51 | + return null |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | +`; |
| 56 | + |
| 57 | +const tempRoots = []; |
| 58 | + |
| 59 | +const makeProjectRoot = (source) => { |
| 60 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'bgtask-patch-')); |
| 61 | + tempRoots.push(root); |
| 62 | + const target = getSchedulerCandidates(root)[0]; |
| 63 | + fs.mkdirSync(path.dirname(target), { recursive: true }); |
| 64 | + fs.writeFileSync(target, source); |
| 65 | + return { root, target }; |
| 66 | +}; |
| 67 | + |
| 68 | +afterEach(() => { |
| 69 | + while (tempRoots.length) { |
| 70 | + fs.rmSync(tempRoots.pop(), { force: true, recursive: true }); |
| 71 | + } |
| 72 | +}); |
| 73 | + |
| 74 | +describe('patch-expo-background-task plugin', () => { |
| 75 | + it('returns early on a running worker before scheduleWorker cancels it', () => { |
| 76 | + const patched = applyRunningWorkerGuard(PRISTINE_SOURCE); |
| 77 | + |
| 78 | + expect(patched).toContain( |
| 79 | + 'if (cancelExisting && getWorkerInfo(context)?.state == WorkInfo.State.RUNNING)' |
| 80 | + ); |
| 81 | + expect(patched).toContain(APPLIED_MARKER); |
| 82 | + // The guard must sit above the cancel, otherwise the running worker still dies. |
| 83 | + expect(patched.indexOf(APPLIED_MARKER)).toBeLessThan( |
| 84 | + patched.indexOf('// Stop the current worker (if any)') |
| 85 | + ); |
| 86 | + expect(patched).toContain(' return true\n }'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('makes a RUNNING work info win the unique-work lookup', () => { |
| 90 | + const patched = applyRunningWorkerGuard(PRISTINE_SOURCE); |
| 91 | + |
| 92 | + // An APPEND chain holds several work infos; firstOrNull() can return an ENQUEUED sibling |
| 93 | + // while another is RUNNING, and cancelUniqueWork would then cancel the running one too. |
| 94 | + expect(patched).toContain(`return ${WORK_INFO_MARKER} ?: workInfos.firstOrNull()`); |
| 95 | + expect(patched).not.toMatch(/return workInfos\.firstOrNull\(\)\n/); |
| 96 | + }); |
| 97 | + |
| 98 | + it('leaves the cancel-and-re-enqueue path intact for a worker that is not running', () => { |
| 99 | + const patched = applyRunningWorkerGuard(PRISTINE_SOURCE); |
| 100 | + |
| 101 | + expect(patched).toContain(' // Stop the current worker (if any)\n' |
| 102 | + + ' if (cancelExisting) {\n' |
| 103 | + + ' stopWorker(context)\n' |
| 104 | + + ' }'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('is idempotent', () => { |
| 108 | + const once = applyRunningWorkerGuard(PRISTINE_SOURCE); |
| 109 | + |
| 110 | + expect(applyRunningWorkerGuard(once)).toBe(once); |
| 111 | + }); |
| 112 | + |
| 113 | + it('still finds both anchors in the vendored expo-background-task source', () => { |
| 114 | + // Fails loudly if an upstream bump moves either anchor, instead of shipping an inert patch. |
| 115 | + const patched = applyRunningWorkerGuard(readVendoredScheduler()); |
| 116 | + |
| 117 | + expect(patched).toContain(APPLIED_MARKER); |
| 118 | + expect(patched).toContain(WORK_INFO_MARKER); |
| 119 | + }); |
| 120 | + |
| 121 | + it('writes both edits through patchSchedulerSource and does not rewrite them', () => { |
| 122 | + const { root, target } = makeProjectRoot(PRISTINE_SOURCE); |
| 123 | + |
| 124 | + expect(patchSchedulerSource(root)).toBe(true); |
| 125 | + const afterFirst = fs.readFileSync(target, 'utf8'); |
| 126 | + expect(afterFirst).toContain(APPLIED_MARKER); |
| 127 | + expect(afterFirst).toContain(WORK_INFO_MARKER); |
| 128 | + |
| 129 | + expect(patchSchedulerSource(root)).toBe(true); |
| 130 | + expect(fs.readFileSync(target, 'utf8')).toBe(afterFirst); |
| 131 | + }); |
| 132 | + |
| 133 | + it('throws when an anchor is gone so an inert patch cannot ship silently', () => { |
| 134 | + const { root } = makeProjectRoot('object BackgroundTaskScheduler {\n}\n'); |
| 135 | + |
| 136 | + expect(() => patchSchedulerSource(root)).toThrow(/did not apply/); |
| 137 | + }); |
| 138 | + |
| 139 | + it('throws when only one of the two anchors survives upstream', () => { |
| 140 | + const halfSource = PRISTINE_SOURCE.replace(' return workInfos.firstOrNull()', ' return null'); |
| 141 | + const { root } = makeProjectRoot(halfSource); |
| 142 | + |
| 143 | + expect(() => patchSchedulerSource(root)).toThrow(/did not apply/); |
| 144 | + }); |
| 145 | +}); |
0 commit comments