|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const vm = require('vm'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | + |
| 7 | +const EFS_SRC = fs.readFileSync( |
| 8 | + path.join(__dirname, '..', 'actions', 'EventFlowSystem.js'), |
| 9 | + 'utf8' |
| 10 | +); |
| 11 | + |
| 12 | +function loadEventFlowSystem(windowOverrides = {}, globals = {}) { |
| 13 | + const sandbox = vm.createContext({ |
| 14 | + window: { |
| 15 | + location: { search: '' }, |
| 16 | + sendMessageToTabs: null, |
| 17 | + sendTargetP2P: null, |
| 18 | + sendToDestinations: null, |
| 19 | + fetchWithTimeout: null, |
| 20 | + sanitizeRelay: null, |
| 21 | + checkExactDuplicateAlreadyRelayed: null, |
| 22 | + messageStore: {}, |
| 23 | + handleMessageStore: null, |
| 24 | + ...windowOverrides, |
| 25 | + }, |
| 26 | + console, |
| 27 | + setTimeout, |
| 28 | + clearTimeout, |
| 29 | + setInterval, |
| 30 | + clearInterval, |
| 31 | + Promise, |
| 32 | + IDBKeyRange: {}, |
| 33 | + indexedDB: { open: () => ({}) }, |
| 34 | + ...globals, |
| 35 | + }); |
| 36 | + |
| 37 | + vm.runInContext(EFS_SRC + '\nwindow.EventFlowSystem = EventFlowSystem;', sandbox); |
| 38 | + return sandbox.window.EventFlowSystem; |
| 39 | +} |
| 40 | + |
| 41 | +let passed = 0; |
| 42 | +let failed = 0; |
| 43 | + |
| 44 | +function assert(condition, label) { |
| 45 | + if (condition) { |
| 46 | + console.log(` PASS ${label}`); |
| 47 | + passed++; |
| 48 | + } else { |
| 49 | + console.error(` FAIL ${label}`); |
| 50 | + failed++; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +async function runTests() { |
| 55 | + console.log('\n[1] playTenorGiphy preserves explicit duration 0'); |
| 56 | + { |
| 57 | + let sentPayload = null; |
| 58 | + const EFS = loadEventFlowSystem(); |
| 59 | + const sys = new EFS({ |
| 60 | + sendTargetP2P: (payload) => { |
| 61 | + sentPayload = payload; |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + await sys.executeAction( |
| 66 | + { |
| 67 | + id: 'media1', |
| 68 | + actionType: 'playTenorGiphy', |
| 69 | + config: { |
| 70 | + mediaUrl: 'https://giphy.com/embed/X9izlczKyCpmCSZu0l', |
| 71 | + mediaType: 'iframe', |
| 72 | + duration: 0, |
| 73 | + }, |
| 74 | + }, |
| 75 | + { textonly: true } |
| 76 | + ); |
| 77 | + |
| 78 | + assert(sentPayload?.overlayNinja?.duration === 0, 'duration 0 is sent as 0 (manual close)'); |
| 79 | + } |
| 80 | + |
| 81 | + console.log('\n[2] playTenorGiphy keeps default duration when not configured'); |
| 82 | + { |
| 83 | + let sentPayload = null; |
| 84 | + const EFS = loadEventFlowSystem(); |
| 85 | + const sys = new EFS({ |
| 86 | + sendTargetP2P: (payload) => { |
| 87 | + sentPayload = payload; |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + await sys.executeAction( |
| 92 | + { |
| 93 | + id: 'media2', |
| 94 | + actionType: 'playTenorGiphy', |
| 95 | + config: { |
| 96 | + mediaUrl: 'https://giphy.com/embed/X9izlczKyCpmCSZu0l', |
| 97 | + mediaType: 'iframe', |
| 98 | + }, |
| 99 | + }, |
| 100 | + { textonly: true } |
| 101 | + ); |
| 102 | + |
| 103 | + assert(sentPayload?.overlayNinja?.duration === 10000, 'undefined duration falls back to 10000ms'); |
| 104 | + } |
| 105 | + |
| 106 | + console.log(`\n${'-'.repeat(50)}`); |
| 107 | + console.log(`Results: ${passed} passed, ${failed} failed`); |
| 108 | + if (failed > 0) { |
| 109 | + process.exit(1); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +runTests().catch((err) => { |
| 114 | + console.error('Unexpected error:', err); |
| 115 | + process.exit(1); |
| 116 | +}); |
0 commit comments