diff --git a/__tests__/scope.spec.ts b/__tests__/scope.spec.ts index e9783da..58153b9 100644 --- a/__tests__/scope.spec.ts +++ b/__tests__/scope.spec.ts @@ -89,6 +89,20 @@ describe('electing an implementation', () => { expect(loaded).toEqual(['2.2.0']) }) + it('announces the winner', async () => { + const debug = vi.spyOn(logger, 'debug') + registerImplementation({ version: '2.1.0', load: async () => {} }) + registerImplementation({ version: '2.2.0', load: async () => {} }) + + await loadImplementation() + + expect(debug).toHaveBeenCalledWith( + '🗳️ The votes are in: 2.2.0 is elected viewer of this page (2 running: 2.1.0, 2.2.0)', + { versions: ['2.1.0', '2.2.0'] }, + ) + debug.mockRestore() + }) + it('loads once however many callers ask', async () => { const load = vi.fn().mockResolvedValue(undefined) registerImplementation({ version: '2.0.0', load }) diff --git a/lib/scope.ts b/lib/scope.ts index 063ece8..2111621 100644 --- a/lib/scope.ts +++ b/lib/scope.ts @@ -162,6 +162,8 @@ export function loadImplementation(): Promise { if (best === undefined) { throw new Error('No viewer implementation is available on this page') } + const versions = scope.candidates.map((candidate) => candidate.version) + logger.debug(`🗳️ The votes are in: ${best.version} is elected viewer of this page (${versions.length} running: ${versions.join(', ')})`, { versions }) return best.load() })().catch((error) => { // A load that failed is not the answer for the rest of the page's