import test from 'ava'; import jsdom from 'jsdom'; import sinon from 'sinon'; import { createTestMessageBroker } from './message-broker/test-message-broker'; import { initializeRuntime, interactionEvents } from './runtime'; const createTestWindow = () => { const dom = new jsdom.JSDOM('
', { runScripts: 'dangerously', }); return dom.window; }; test('ready', (t) => { const sendMessage = sinon.fake(); const broker = createTestMessageBroker(sendMessage); const runtime = initializeRuntime(broker, createTestWindow()); runtime.ready(); t.is(1, sendMessage.callCount); t.truthy(sendMessage.calledWith('ready')); // Test calling ready multiple times only sends a single message. runtime.ready(); t.is(1, sendMessage.callCount); }); test('complete', (t) => { const sendMessage = sinon.fake(); const broker = createTestMessageBroker(sendMessage); const runtime = initializeRuntime(broker, createTestWindow()); runtime.complete(); t.is(1, sendMessage.callCount); t.truthy(sendMessage.calledWith('complete')); }); test('complete with error', (t) => { const sendMessage = sinon.fake(); const broker = createTestMessageBroker(sendMessage); const runtime = initializeRuntime(broker, createTestWindow()); runtime.complete(new Error('test error')); t.is(1, sendMessage.callCount); t.truthy(sendMessage.calledWith('complete', 'test error')); // Test passing in a string as the error. runtime.complete('test error'); t.is(2, sendMessage.callCount); t.truthy(sendMessage.calledWith('complete', 'test error')); }); test('subscribe to play', (t) => { const sendMessage = sinon.fake(); const broker = createTestMessageBroker(sendMessage); const runtime = initializeRuntime(broker, createTestWindow()); const listener = sinon.fake(); const unsubscribe = runtime.subscribe('play', listener); // Test that the `runtime-subscribe` message was sent. t.is(1, sendMessage.callCount); t.truthy(sendMessage.calledWith('runtime-subscribe', 'play')); const anotherListener = sinon.fake(); const anotherUnsubscribe = runtime.subscribe('play', anotherListener); // Test that multiple subscribes only send a single `runtime-subscribe` message. t.is(1, sendMessage.callCount); // Test that the listeners were called. broker.runListeners('play'); t.is(1, listener.callCount); t.is(1, anotherListener.callCount); // Test that the `runtime-unsubscribe` message is sent when all subscribes are destroyed. unsubscribe(); anotherUnsubscribe(); t.truthy(sendMessage.calledWith('runtime-unsubscribe')); // Test that the listeners were not called. broker.runListeners('play'); t.is(1, listener.callCount); t.is(1, anotherListener.callCount); }); test('acquire interrupt lock', async (t) => { const sendMessage = sinon.fake(); const broker = createTestMessageBroker(sendMessage); const runtime = initializeRuntime(broker, createTestWindow()); // Acquire lock test. const acquireLockPromise = runtime.acquireInterruptLock(); // Verify the acquire request message was sent. const acquireCall = sendMessage.getCall(0); t.is(acquireCall.args[0], 'runtime-acquire-interrupt-lock-request'); // Get requestId from the message. const acquireRequestId = acquireCall.args[1].requestId; // Send a lock response message. broker.runListeners('runtime-acquire-interrupt-lock-response', { requestId: acquireRequestId, lock: { id: '123', expiresAt: 123 }, }); // Verify the promise was resolved with the lock. const lock = await acquireLockPromise; t.deepEqual(lock.id, '123'); t.deepEqual(lock.expiresAt, 123); // Renew lock. const renewLockPromise = lock.renew(); // Verify the renew request message was sent. const renewCall = sendMessage.getCall(1); t.is(renewCall.args[0], 'runtime-renew-interrupt-lock-request'); // Get requestId from the message. const renewRequestId = renewCall.args[1].requestId; // Send a renew lock response message. broker.runListeners('runtime-renew-interrupt-lock-response', { requestId: renewRequestId, lock: { id: '123', expiresAt: 456 }, }); // Verify lock was renewed. await renewLockPromise; t.deepEqual(lock.expiresAt, 456); // Release lock. const releaseLockPromise = lock.release(); // Verify the release request message was sent. const releaseCall = sendMessage.getCall(2); t.is(releaseCall.args[0], 'runtime-release-interrupt-lock-request'); // Get requestId from the message. const releaseRequestId = releaseCall.args[1].requestId; // Send a release lock response message. broker.runListeners('runtime-release-interrupt-lock-response', { requestId: releaseRequestId, lock: { id: '123', expiresAt: 0 }, }); // Verify lock was released. await releaseLockPromise; }); type IdleListenerTestCase = readonly [ string, (window: jsdom.DOMWindow) => any[], // eslint-disable-line @typescript-eslint/no-explicit-any { container: (window: jsdom.DOMWindow) => HTMLElement | Document; secondsUntilIdle: number; } ]; for (const [testName, argsFn, expected] of [ // runtime.addIdleListener(); [ 'defaults', () => [], { container: (window) => window.document, secondsUntilIdle: 30, }, ], // runtime.addIdleListener(5); [ 'set secondsUntilIdle', () => [5], { container: (window) => window.document, secondsUntilIdle: 5, }, ], // runtime.addIdleListener(container); [ 'set container', (window) => [window.document.querySelector('#container')], { container: (window) => window.document.querySelector('#container'), secondsUntilIdle: 30, }, ], // runtime.addIdleListener(container, 5); [ 'set container and secondsUntilIdle', (window) => [window.document.querySelector('#container'), 5], { container: (window) => window.document.querySelector('#container'), secondsUntilIdle: 5, }, ], // runtime.addIdleListener((isIdle) => { ... }) [ 'set idleCallback', () => [sinon.fake()], { container: (window) => window.document, secondsUntilIdle: 30, }, ], // runtime.addIdleListener(5, (isIdle) => { ... }) [ 'set secondsUntilIdle and idleCallback', () => [5, sinon.fake()], { container: (window) => window.document, secondsUntilIdle: 5, }, ], // runtime.addIdleListener(container, 5, (isIdle) => { ... }) [ 'set container, secondsUntilIdle, and idleCallback', (window) => [window.document.querySelector('#container'), 5, sinon.fake()], { container: (window) => window.document.querySelector('#container'), secondsUntilIdle: 5, }, ], ] as IdleListenerTestCase[]) { // Must be run serially because it modifies the global Date object. test.serial(`add idle listener: ${testName}`, async (t) => { const sendMessage = sinon.fake(); const broker = createTestMessageBroker(sendMessage); const window = createTestWindow(); const runtime = initializeRuntime(broker, window); const lockDuration = 60000; // 1 minute. const clock = sinon.useFakeTimers(Date.now()); const args = argsFn(window); const removeListener = runtime.addIdleListener(...args); let idleCallback: sinon.SinonSpy | undefined; if (typeof args[args.length - 1] === 'function') { idleCallback = args[args.length - 1] as sinon.SinonSpy; } if (idleCallback) { // Fast-forward time to ensure the initial idle state is set. await clock.tickAsync(expected.secondsUntilIdle * 1000); t.is(idleCallback.lastCall.args[0], true); } // Helper function to get the next call from the sendMessage spy. let callIndex = 0; const getCall = (fn: sinon.SinonSpy) => { const call = fn.getCall(callIndex); callIndex += 1; return call; }; // Ensure all interaction events acquire a lock. for (const eventType of interactionEvents) { // Trigger the event on the container. expected.container(window).dispatchEvent(new window.Event(eventType)); // Verify the acquire request message was sent. const acquireCall = getCall(sendMessage); t.is(acquireCall.args[0], 'runtime-acquire-interrupt-lock-request'); // Get requestId from the message. const acquireRequestId = acquireCall.args[1].requestId; // Send an acquire lock response message. broker.runListeners('runtime-acquire-interrupt-lock-response', { requestId: acquireRequestId, lock: { id: '123', expiresAt: Date.now() + lockDuration }, }); if (idleCallback) { // Ensure the idle state is set to false after an interaction event. await clock.tickAsync(1); t.is(idleCallback.lastCall.args[0], false); } // If the lock duration is greater than the expected idle time, ensure a renew request is sent. if (expected.secondsUntilIdle * 1000 > lockDuration) { // Fast-forward to just before the lock expires. await clock.tickAsync(lockDuration - 1000); // Verify the renew request message was sent. const renewCall = getCall(sendMessage); t.is(renewCall.args[0], 'runtime-renew-interrupt-lock-request'); // Bump the release call index since the renew call was sent. } // Fast-forward to after expected idle time, ensure a release request is sent. await clock.tickAsync(expected.secondsUntilIdle * 1000 + 1); // Verify the release request message was sent. const releaseCall = getCall(sendMessage); t.is(releaseCall.args[0], 'runtime-release-interrupt-lock-request'); // Get requestId from the message. const releaseRequestId = releaseCall.args[1].requestId; // Send a release lock response message. broker.runListeners('runtime-release-interrupt-lock-response', { requestId: releaseRequestId, lock: { id: '123', expiresAt: 0 }, }); await clock.tickAsync(1); if (idleCallback) { // Ensure the idle state is set to false after an interaction event. await clock.tickAsync(1); t.is(idleCallback.lastCall.args[0], true); } } // Remove listener. The lock is already released by this point. await removeListener(); // Test that listeners are removed by triggering another event. for (const eventType of interactionEvents) { expected.container(window).dispatchEvent(new window.Event(eventType)); } // Verify no additional messages were sent. t.is(callIndex, sendMessage.callCount); clock.restore(); }); } test.serial.only( 'remove idle listener releases lock before idle expiry', async (t) => { const sendMessage = sinon.fake(); const broker = createTestMessageBroker(sendMessage); const window = createTestWindow(); const runtime = initializeRuntime(broker, window); const lockDuration = 60000; // 1 minute. const clock = sinon.useFakeTimers(Date.now()); // Add idle listener with a 1 second idle time. const removeListener = runtime.addIdleListener(1); // Trigger an event on the container. window.document.dispatchEvent(new window.Event('click')); // Acquire lock. const acquireCall = sendMessage.getCall(0); t.is(acquireCall.args[0], 'runtime-acquire-interrupt-lock-request'); const acquireRequestId = acquireCall.args[1].requestId; // Send an acquire lock response message. broker.runListeners('runtime-acquire-interrupt-lock-response', { requestId: acquireRequestId, lock: { id: '123', expiresAt: Date.now() + lockDuration }, }); await clock.tickAsync(1); // Remove listener before idle expires. const removePromise = removeListener(); // Verify the release request message was sent. const releaseCall = sendMessage.getCall(1); t.is(releaseCall.args[0], 'runtime-release-interrupt-lock-request'); // Send a release lock response message. const releaseRequestId = releaseCall.args[1].requestId; broker.runListeners('runtime-release-interrupt-lock-response', { requestId: releaseRequestId, lock: { id: '123', expiresAt: 0 }, }); await removePromise; clock.restore(); } );