<commit_before>import { expect } from 'chai'
import sinon from 'sinon'
import { shallow } from 'enzyme'
import proxyquire from 'proxyquire'
import React from 'react'
import DisabledPlugin from '../js/rendererjs/disabledplugin.js'

let running = true
const mock = {
	'react-dom': {
		render: sinon.spy(),
	},
	'sia.js': {
		ifRunning: (is, not) => {
			if (running) {
				is()
			} else {
				not()
			}
		},
		start: sinon.spy(),
	},
	'electron': {
		remote: {
			getCurrentWindow: sinon.spy(),
			getGlobal: (name) => {
				if (name === 'config') {
					return {
						siad: {
							path: 'testpath/siad',
							datadir: 'testpath/datadir',
							detached: false,
						}
					}
				}
			},
			dialog: {
				showOpenDialog: sinon.spy(),
				showSaveDialog: sinon.spy(),
				showMessageBox: sinon.spy(),
				showErrorBox: sinon.spy(),
			}
		}
	}
}

proxyquire('../js/rendererjs/pluginapi.js', mock)

describe('plugin API', () => {
	it('creates a SiaAPI window object', () => {
		expect(window.SiaAPI).to.exist
	})
	it('does not mount disabled plugin component if siad is running', function(done) {
		running = true
		this.timeout(10000)
		setTimeout(() => {
			expect(mock['react-dom'].render.called).to.be.false
			done()
		}, 2000)
	})
	it('mounts disabled plugin if siad is not running', function(done) {
		running = false
		this.timeout(10000)
		setTimeout(() => {
			expect(mock['react-dom'].render.called).to.be.true
			expect(mock['react-dom'].render.calledWith(<DisabledPlugin startSiad={mock['sia.js'].start} />, document.body)).to.be.true
			done()
		}, 2000)
	})
	describe('DisabledPlugin component', () => {<commit_msg>use a poll instead of a timeout to make async plugin api testing faster<commit_after>import { expect } from 'chai'
import sinon from 'sinon'
import { shallow } from 'enzyme'
import proxyquire from 'proxyquire'
import React from 'react'
import DisabledPlugin from '../js/rendererjs/disabledplugin.js'

let running = true
const mock = {
	'react-dom': {
		render: sinon.spy(),
	},
	'sia.js': {
		ifRunning: (is, not) => {
			if (running) {
				is()
			} else {
				not()
			}
		},
		start: sinon.spy(),
	},
	'electron': {
		remote: {
			getCurrentWindow: sinon.spy(),
			getGlobal: (name) => {
				if (name === 'config') {
					return {
						siad: {
							path: 'testpath/siad',
							datadir: 'testpath/datadir',
							detached: false,
						}
					}
				}
			},
			dialog: {
				showOpenDialog: sinon.spy(),
				showSaveDialog: sinon.spy(),
				showMessageBox: sinon.spy(),
				showErrorBox: sinon.spy(),
			}
		}
	}
}

proxyquire('../js/rendererjs/pluginapi.js', mock)

describe('plugin API', () => {
	it('creates a SiaAPI window object', () => {
		expect(window.SiaAPI).to.exist
	})
	it('does not mount disabled plugin component if siad is running', function(done) {
		running = true
		this.timeout(10000)
		const poll = setInterval(() => {
			if (mock['react-dom'].render.called === false) {
				clearInterval(poll)
				done()
			}
		}, 50)
	})
	it('mounts disabled plugin if siad is not running', function(done) {
		running = false
		this.timeout(10000)
		const poll = setInterval(() => {
			if (mock['react-dom'].render.called && 
				  mock['react-dom'].render.calledWith(<DisabledPlugin startSiad={mock['sia.js'].start} />, document.body)) {
				clearInterval(poll)
				done()
			}
		}, 50)
	})
	describe('DisabledPlugin component', () => {
{'fraction_non_alphanumeric': 0.1707533234859675, 'fraction_numerical': 0.009453471196454948, 'mean_word_length': 3.1699507389162562, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5773003', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1181, 'n_words': 258}