Skip to content

Commit

Permalink
Merge pull request #14 from modos189/fix-external
Browse files Browse the repository at this point in the history
Bug fix when re-adding an external plugin
  • Loading branch information
modos189 authored Dec 3, 2022
2 parents c470298 + ddaf926 commit e521eda
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 110 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lib-iitc-manager",
"version": "1.3.4",
"version": "1.3.5",
"description": "Library for managing IITC plugins",
"main": "src/index.js",
"type": "module",
Expand Down
5 changes: 4 additions & 1 deletion src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ export class Manager extends Worker {
let plugins_local = local[this.channel + '_plugins_local'];
let plugins_user = local[this.channel + '_plugins_user'];

if (!isSet(categories)) categories = {};
if (!isSet(plugins_flat)) plugins_flat = {};
if (!isSet(plugins_local)) plugins_local = {};
if (!isSet(plugins_user)) plugins_user = {};

Expand All @@ -226,13 +228,14 @@ export class Manager extends Worker {

if (plugin_uid === null) throw new Error('The plugin has an incorrect ==UserScript== header');

const is_user_plugins = plugins_user[plugin_uid] !== undefined;
plugins_user[plugin_uid] = Object.assign(meta, {
uid: plugin_uid,
status: 'on',
code: code,
});

if (plugin_uid in plugins_flat) {
if (plugin_uid in plugins_flat && !is_user_plugins) {
if (plugin_uid in plugins_local && plugins_flat[plugin_uid]['status'] !== 'off') {
plugins_local[plugin_uid]['status'] = 'off';
}
Expand Down
64 changes: 64 additions & 0 deletions test/manager.0.base.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3

import { describe, it, before } from 'mocha';
import { Manager } from '../src/manager.js';
import storage from '../test/storage.js';
import { expect } from 'chai';

describe('manage.js base integration tests', function () {
let manager = null;
before(function () {
const params = {
storage: storage,
channel: 'beta',
network_host: {
release: 'http://127.0.0.1:31606/release',
beta: 'http://127.0.0.1:31606/beta',
custom: 'http://127.0.0.1/',
},
inject_user_script: function callBack(data) {
expect(data).to.include('// ==UserScript==');
},
progressbar: function callBack(is_show) {
expect(is_show).to.be.oneOf([true, false]);
},
is_daemon: false,
};
manager = new Manager(params);
});

describe('run', function () {
it('Should not return an error', async function () {
const run = await manager.run();
expect(run).to.be.undefined;
});
});

describe('Check channel', function () {
it('Should return beta', async function () {
const channel = await storage.get(['channel']).then((data) => data.channel);
expect(channel).to.equal('beta');
});
});

describe('setChannel', function () {
it('Should not return an error', async function () {
const channel = await manager.setChannel('release');
expect(channel).to.be.undefined;
});
});

describe('setUpdateCheckInterval', function () {
it('Should not return an error', async function () {
const fn = await manager.setUpdateCheckInterval(24 * 60 * 60, 'release');
expect(fn).to.be.undefined;
});
});

describe('inject', function () {
it('Should not return an error', async function () {
const inject = await manager.inject();
expect(inject).to.be.undefined;
});
});
});
88 changes: 88 additions & 0 deletions test/manager.1.build-in.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3

import { describe, it, before } from 'mocha';
import { Manager } from '../src/manager.js';
import storage from '../test/storage.js';
import { expect } from 'chai';

describe('manage.js build-in plugins integration tests', function () {
let manager = null;
before(function () {
const params = {
storage: storage,
channel: 'release',
network_host: {
release: 'http://127.0.0.1:31606/release',
beta: 'http://127.0.0.1:31606/beta',
custom: 'http://127.0.0.1/',
},
inject_user_script: function callBack(data) {
expect(data).to.include('// ==UserScript==');
},
progressbar: function callBack(is_show) {
expect(is_show).to.be.oneOf([true, false]);
},
is_daemon: false,
};
manager = new Manager(params);
});

const first_plugin_uid = 'Available AP statistics+https://github.com/IITC-CE/ingress-intel-total-conversion';
const second_plugin_uid = 'Bing maps+https://github.com/IITC-CE/ingress-intel-total-conversion';

describe('run', function () {
it('Should not return an error', async function () {
const run = await manager.run();
expect(run).to.be.undefined;
});
});

describe('Manage build-in plugins', function () {
it('Enable first plugin', async function () {
const run = await manager.managePlugin(first_plugin_uid, 'on');
expect(run).to.be.undefined;

const db_data = await storage.get(['release_plugins_flat', 'release_plugins_local']);
expect(db_data['release_plugins_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid);
expect(db_data['release_plugins_local'], 'release_plugins_local').to.have.all.keys(first_plugin_uid);

expect(db_data['release_plugins_local'][first_plugin_uid]['status'], 'release_plugins_local: ' + first_plugin_uid).to.equal('on');
expect(db_data['release_plugins_flat'][first_plugin_uid]['status'], 'release_plugins_flat: ' + first_plugin_uid).to.equal('on');
expect(db_data['release_plugins_flat'][second_plugin_uid]['status'], 'release_plugins_flat: ' + second_plugin_uid).to.equal('off');
});

it('Enable second plugin', async function () {
const run = await manager.managePlugin(second_plugin_uid, 'on');
expect(run).to.be.undefined;

const db_data = await storage.get(['release_plugins_flat', 'release_plugins_local']);
expect(db_data['release_plugins_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid);
expect(db_data['release_plugins_local'], 'release_plugins_local').to.have.all.keys(first_plugin_uid, second_plugin_uid);

expect(db_data['release_plugins_local'][first_plugin_uid]['status'], 'release_plugins_local: ' + first_plugin_uid).to.equal('on');

expect(db_data['release_plugins_local'][second_plugin_uid]['status'], 'release_plugins_local: ' + second_plugin_uid).to.equal('on');

expect(db_data['release_plugins_flat'][first_plugin_uid]['status'], 'release_plugins_flat: ' + first_plugin_uid).to.equal('on');

expect(db_data['release_plugins_flat'][second_plugin_uid]['status'], 'release_plugins_flat: ' + second_plugin_uid).to.equal('on');
});

it('Disable plugin', async function () {
const run = await manager.managePlugin(first_plugin_uid, 'off');
expect(run).to.be.undefined;

const db_data = await storage.get(['release_plugins_flat', 'release_plugins_local']);
expect(db_data['release_plugins_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid);
expect(db_data['release_plugins_local'], 'release_plugins_local').to.have.all.keys(first_plugin_uid, second_plugin_uid);

expect(db_data['release_plugins_local'][first_plugin_uid]['status'], 'release_plugins_local: ' + first_plugin_uid).to.equal('off');

expect(db_data['release_plugins_local'][second_plugin_uid]['status'], 'release_plugins_local: ' + second_plugin_uid).to.equal('on');

expect(db_data['release_plugins_flat'][first_plugin_uid]['status'], 'release_plugins_flat: ' + first_plugin_uid).to.equal('off');

expect(db_data['release_plugins_flat'][second_plugin_uid]['status'], 'release_plugins_flat: ' + second_plugin_uid).to.equal('on');
});
});
});
155 changes: 47 additions & 108 deletions test/manager.spec.js → test/manager.2.external.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ const expectThrowsAsync = async (method, errorMessage) => {
}
};

describe('manage.js integration tests', function () {
describe('manage.js external plugins integration tests', function () {
let manager = null;
before(function () {
const params = {
storage: storage,
channel: 'beta',
channel: 'release',
network_host: {
release: 'http://127.0.0.1:31606/release',
beta: 'http://127.0.0.1:31606/beta',
Expand Down Expand Up @@ -51,83 +51,6 @@ describe('manage.js integration tests', function () {
});
});

describe('Check channel', function () {
it('Should return beta', async function () {
const channel = await storage.get(['channel']).then((data) => data.channel);
expect(channel).to.equal('beta');
});
});

describe('setChannel', function () {
it('Should not return an error', async function () {
const channel = await manager.setChannel('release');
expect(channel).to.be.undefined;
});
});

describe('setUpdateCheckInterval', function () {
it('Should not return an error', async function () {
const fn = await manager.setUpdateCheckInterval(24 * 60 * 60, 'release');
expect(fn).to.be.undefined;
});
});

describe('inject', function () {
it('Should not return an error', async function () {
const inject = await manager.inject();
expect(inject).to.be.undefined;
});
});

describe('Manage build-in plugins', function () {
it('Enable first plugin', async function () {
const run = await manager.managePlugin(first_plugin_uid, 'on');
expect(run).to.be.undefined;

const db_data = await storage.get(['release_plugins_flat', 'release_plugins_local']);
expect(db_data['release_plugins_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid);
expect(db_data['release_plugins_local'], 'release_plugins_local').to.have.all.keys(first_plugin_uid);

expect(db_data['release_plugins_local'][first_plugin_uid]['status'], 'release_plugins_local: ' + first_plugin_uid).to.equal('on');
expect(db_data['release_plugins_flat'][first_plugin_uid]['status'], 'release_plugins_flat: ' + first_plugin_uid).to.equal('on');
expect(db_data['release_plugins_flat'][second_plugin_uid]['status'], 'release_plugins_flat: ' + second_plugin_uid).to.equal('off');
});

it('Enable second plugin', async function () {
const run = await manager.managePlugin(second_plugin_uid, 'on');
expect(run).to.be.undefined;

const db_data = await storage.get(['release_plugins_flat', 'release_plugins_local']);
expect(db_data['release_plugins_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid);
expect(db_data['release_plugins_local'], 'release_plugins_local').to.have.all.keys(first_plugin_uid, second_plugin_uid);

expect(db_data['release_plugins_local'][first_plugin_uid]['status'], 'release_plugins_local: ' + first_plugin_uid).to.equal('on');

expect(db_data['release_plugins_local'][second_plugin_uid]['status'], 'release_plugins_local: ' + second_plugin_uid).to.equal('on');

expect(db_data['release_plugins_flat'][first_plugin_uid]['status'], 'release_plugins_flat: ' + first_plugin_uid).to.equal('on');

expect(db_data['release_plugins_flat'][second_plugin_uid]['status'], 'release_plugins_flat: ' + second_plugin_uid).to.equal('on');
});

it('Disable plugin', async function () {
const run = await manager.managePlugin(first_plugin_uid, 'off');
expect(run).to.be.undefined;

const db_data = await storage.get(['release_plugins_flat', 'release_plugins_local']);
expect(db_data['release_plugins_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid);
expect(db_data['release_plugins_local'], 'release_plugins_local').to.have.all.keys(first_plugin_uid, second_plugin_uid);

expect(db_data['release_plugins_local'][first_plugin_uid]['status'], 'release_plugins_local: ' + first_plugin_uid).to.equal('off');

expect(db_data['release_plugins_local'][second_plugin_uid]['status'], 'release_plugins_local: ' + second_plugin_uid).to.equal('on');

expect(db_data['release_plugins_flat'][first_plugin_uid]['status'], 'release_plugins_flat: ' + first_plugin_uid).to.equal('off');

expect(db_data['release_plugins_flat'][second_plugin_uid]['status'], 'release_plugins_flat: ' + second_plugin_uid).to.equal('on');
});
});

describe('Manage external plugins', function () {
const external_1_uid = 'Bookmarks for maps and portals+https://github.com/IITC-CE/ingress-intel-total-conversion';
const external_2_uid = 'Bookmarks2 for maps and portals+https://github.com/IITC-CE/ingress-intel-total-conversion';
Expand Down Expand Up @@ -283,6 +206,51 @@ describe('manage.js integration tests', function () {
});
});

describe('Re-adding an external plugin', function () {
const external_1_uid = 'Bookmarks for maps and portals+https://github.com/IITC-CE/ingress-intel-total-conversion';

it('Double adding an external plugin', async function () {
const scripts = [
{
meta: {
id: 'bookmarks1',
namespace: 'https://github.com/IITC-CE/ingress-intel-total-conversion',
name: 'Bookmarks for maps and portals',
category: 'Controls',
},
code: external_code,
},
{
meta: {
id: 'bookmarks1',
namespace: 'https://github.com/IITC-CE/ingress-intel-total-conversion',
name: 'Bookmarks for maps and portals',
category: 'Controls',
},
code: external_code,
},
];
const run = await manager.addUserScripts(scripts);
expect(run).to.be.undefined;
});

it('Check external plugin', async function () {
const db_data = await storage.get(['release_plugins_flat', 'release_plugins_user']);
expect(db_data['release_plugins_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid, external_1_uid);
expect(db_data['release_plugins_flat'][external_1_uid]['override']).to.be.undefined;
expect(db_data['release_plugins_user'], 'release_plugins_user').to.have.all.keys(external_1_uid);
});

it('Remove the external plugin', async function () {
const run = await manager.managePlugin(external_1_uid, 'delete');
expect(run).to.be.undefined;

const db_data = await storage.get(['release_plugins_flat', 'release_plugins_user']);
expect(db_data['release_plugins_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid);
expect(db_data['release_plugins_user'], 'release_plugins_user').to.be.empty;
});
});

describe('Manage external plugins that replace built-in plugins', function () {
const external_1_uid = 'Available AP statistics+https://github.com/IITC-CE/ingress-intel-total-conversion';

Expand Down Expand Up @@ -338,33 +306,4 @@ describe('manage.js integration tests', function () {
expect(db_data['release_plugins_flat'][external_1_uid]['override'], "release_plugins_flat['override']: " + external_1_uid).to.not.be.true;
});
});

describe('Custom repository', function () {
const custom_repo = 'http://127.0.0.1:31606/custom';

it('Setting the URL of the custom repository', async function () {
const run = await manager.setCustomChannelUrl(custom_repo);
expect(run).to.be.undefined;
});

it('Check the URL of the custom repository', async function () {
const network_host = await storage.get(['network_host']).then((data) => data.network_host);
expect(network_host.custom).to.equal(custom_repo);
});

it('Switching to a custom channel', async function () {
const channel = await manager.setChannel('custom');
expect(channel).to.be.undefined;
});

it('Check the IITC version', async function () {
const script = await storage.get(['custom_iitc_code']).then((data) => data['custom_iitc_code']);
expect(script).to.include('@version 0.99.0');
});

it('Switching back to the Release channel', async function () {
const channel = await manager.setChannel('release');
expect(channel).to.be.undefined;
});
});
});
Loading

0 comments on commit e521eda

Please sign in to comment.