From b5bc0389d9097f29245fd55440be3417fd83a26b Mon Sep 17 00:00:00 2001 From: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 28 Oct 2026 22:38:40 -0400 Subject: [PATCH] test(platform): pin platform()'s no-platform throw and scope fall-back #607 landed platform() with two arms no suite reaches: the throw when nothing installed a platform (src/platform/index.ts:51, 0 hits in both the unit and the bundle coverage runs at 8878c9d) and the fall-back from an installed scope that has no platform for the current work to the process-wide one. Each case imports src/platform fresh so the empty slot and the installed scope never leak into the platform setup.ts gives every other test. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- __tests__/platform/platformGuards.test.ts | 57 +++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 __tests__/platform/platformGuards.test.ts diff --git a/__tests__/platform/platformGuards.test.ts b/__tests__/platform/platformGuards.test.ts new file mode 100644 index 00000000..7e63fd07 --- /dev/null +++ b/__tests__/platform/platformGuards.test.ts @@ -0,0 +1,57 @@ +import type { PlatformScope } from '../../src/platform' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +// The two arms of platform() that runState.test.ts never reaches: the throw +// when nothing has installed a platform, and the fall-back to the process-wide +// value when an installed scope has no platform for the current work (an +// adapter's AsyncLocalStorage outside any run). Each case imports src/platform +// fresh so that neither the empty slot nor the installed scope leaks into the +// platform setup.ts gives every other test. + +beforeEach(() => vi.resetModules()) +afterEach(() => vi.resetModules()) + +async function freshPlatformModule() { + return import('../../src/platform') +} + +describe('platform()', () => { + it('throws, naming both installers, when no platform has been installed', async () => { + const { platform } = await freshPlatformModule() + + expect(() => platform()).toThrow(new Error('no platform installed: the entry point must call setPlatform() or runWithPlatform() before dispatching an event')) + }) + + it('falls back to the process-wide platform when the installed scope has none for the current work', async () => { + const { actionsPlatform } = await import('../../src/adapters/actions') + const { installPlatformScope, platform, setPlatform } = await freshPlatformModule() + const processWide = actionsPlatform() + const scoped = actionsPlatform() + let inRun = false + const scope: PlatformScope = { + get: () => (inRun ? scoped : undefined), + run: (_value, fn) => { + inRun = true + try { + return fn() + } + finally { + inRun = false + } + }, + } + installPlatformScope(scope) + setPlatform(processWide) + + expect(platform()).toBe(processWide) + expect(scope.run(scoped, () => platform())).toBe(scoped) + expect(platform()).toBe(processWide) + }) + + it('still throws under an installed scope when neither it nor the process has a platform', async () => { + const { installPlatformScope, platform } = await freshPlatformModule() + installPlatformScope({ get: () => undefined, run: (_value, fn) => fn() }) + + expect(() => platform()).toThrow('no platform installed') + }) +})