'use strict' const assert = require('assert') const fs = require('fs') const path = require('path') const vm = require('vm') const source = fs.readFileSync(path.join(path.resolve(__dirname, '..'), 'src', 'runtime-channel.js'), 'utf8') class FakeChannel { constructor(name) { this.name = name; this.closed = false; FakeChannel.last = this } postMessage(value) { this.value = value } close() { this.closed = true } } const sandbox = { BroadcastChannel: FakeChannel } vm.runInNewContext(source, sandbox) const channel = sandbox.IWBRuntimeChannel.create('test') assert.strictEqual(channel.supported, true) let value = null const off = channel.add((next) => { value = next }) FakeChannel.last.onmessage({ data: { ok: true } }) assert.strictEqual(value.ok, true) assert.strictEqual(channel.post({ hello: 'world' }), true) off(); channel.close(); channel.close() assert.strictEqual(FakeChannel.last.closed, true) assert.strictEqual(channel.post({ nope: true }), false) assert.strictEqual(channel.snapshot().closed, true) const unsupported = sandbox.IWBRuntimeChannel.create('none', { BroadcastChannel: null }) assert.strictEqual(unsupported.supported, false) assert.strictEqual(unsupported.post({}), false) console.log('runtime channel checks passed')