Skip to content

Commit

Permalink
Merge pull request #19 from modos189/fix-del
Browse files Browse the repository at this point in the history
Fixed a bug when deleting a custom plugin that overwrites a built-in plugin
  • Loading branch information
modos189 authored Dec 25, 2022
2 parents 75f6562 + 9e3e21e commit feb6609
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 13 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.4.0",
"version": "1.4.2",
"description": "Library for managing IITC plugins",
"main": "src/index.js",
"type": "module",
Expand Down
6 changes: 5 additions & 1 deletion src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ export class Manager extends Worker {
}
if (action === 'delete') {
if (plugins_flat[uid]['override']) {
plugins_flat[uid] = { ...plugins_local[uid] };
if (plugins_local[uid] !== undefined) {
plugins_flat[uid] = { ...plugins_local[uid] };
}
plugins_flat[uid]['user'] = false;
plugins_flat[uid]['override'] = false;
plugins_flat[uid]['status'] = 'off';
} else {
delete plugins_flat[uid];
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/release/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
"description": "Displays the per-team AP gains available in the current view.",
"namespace": "https://github.com/IITC-CE/ingress-intel-total-conversion",
"version": "0.4.2"
},
{
"filename": "missions.user.js",
"id": "missions",
"name": "Missions",
"author": "jonatkins",
"description": "View missions. Marking progress on waypoints/missions basis. Showing mission paths on the map.",
"namespace": "https://github.com/IITC-CE/ingress-intel-total-conversion",
"version": "0.3.0"
}
]
},
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/release/plugins/missions.meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ==UserScript==
// @author jonatkins
// @name IITC plugin: Missions
// @category Info
// @version 0.3.0
// @description View missions. Marking progress on waypoints/missions basis. Showing mission paths on the map.
// @id missions
// @namespace https://github.com/IITC-CE/ingress-intel-total-conversion
// @updateURL https://iitc.app/build/release/plugins/missions.meta.js
// @downloadURL https://iitc.app/build/release/plugins/missions.user.js
// @match https://intel.ingress.com/*
// @grant none
// ==/UserScript==
13 changes: 13 additions & 0 deletions test/fixtures/release/plugins/missions.user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ==UserScript==
// @author jonatkins
// @name IITC plugin: Missions
// @category Info
// @version 0.3.0
// @description View missions. Marking progress on waypoints/missions basis. Showing mission paths on the map.
// @id missions
// @namespace https://github.com/IITC-CE/ingress-intel-total-conversion
// @updateURL https://iitc.app/build/release/plugins/missions.meta.js
// @downloadURL https://iitc.app/build/release/plugins/missions.user.js
// @match https://intel.ingress.com/*
// @grant none
// ==/UserScript==
7 changes: 4 additions & 3 deletions test/manager.1.build-in.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('manage.js build-in plugins integration tests', function () {

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';
const third_plugin_uid = 'Missions+https://github.com/IITC-CE/ingress-intel-total-conversion';

describe('run', function () {
it('Should not return an error', async function () {
Expand All @@ -43,7 +44,7 @@ describe('manage.js build-in plugins integration tests', function () {
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_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid, third_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');
Expand All @@ -56,7 +57,7 @@ describe('manage.js build-in plugins integration tests', function () {
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_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid, third_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');
Expand All @@ -73,7 +74,7 @@ describe('manage.js build-in plugins integration tests', function () {
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_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid, third_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');
Expand Down
102 changes: 94 additions & 8 deletions test/manager.2.external.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('manage.js external plugins integration tests', function () {

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';
const third_plugin_uid = 'Missions+https://github.com/IITC-CE/ingress-intel-total-conversion';
const external_code = '// ==UserScript==\nreturn false;';

describe('run', function () {
Expand Down Expand Up @@ -83,7 +84,12 @@ describe('manage.js external plugins integration tests', function () {
expect(run).to.deep.equal(installed);

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'], 'release_plugins_flat').to.have.all.keys(
first_plugin_uid,
second_plugin_uid,
third_plugin_uid,
external_1_uid
);
expect(db_data['release_plugins_user'], 'release_plugins_user').to.have.all.keys(external_1_uid);

expect(db_data['release_plugins_user'][external_1_uid]['status'], "release_plugins_user['status']: " + external_1_uid).to.equal('on');
Expand Down Expand Up @@ -123,6 +129,7 @@ describe('manage.js external plugins integration tests', function () {
expect(db_data['release_plugins_flat'], 'release_plugins_flat').to.have.all.keys(
first_plugin_uid,
second_plugin_uid,
third_plugin_uid,
external_1_uid,
external_2_uid
);
Expand Down Expand Up @@ -154,6 +161,7 @@ describe('manage.js external plugins integration tests', function () {
expect(db_data['release_plugins_flat'], 'release_plugins_flat').to.have.all.keys(
first_plugin_uid,
second_plugin_uid,
third_plugin_uid,
external_1_uid,
external_2_uid
);
Expand Down Expand Up @@ -183,6 +191,7 @@ describe('manage.js external plugins integration tests', function () {
expect(db_data['release_plugins_flat'], 'release_plugins_flat').to.have.all.keys(
first_plugin_uid,
second_plugin_uid,
third_plugin_uid,
external_1_uid,
external_2_uid
);
Expand All @@ -201,6 +210,7 @@ describe('manage.js external plugins integration tests', function () {
expect(db_data['release_plugins_flat'], 'release_plugins_flat').to.have.all.keys(
first_plugin_uid,
second_plugin_uid,
third_plugin_uid,
external_1_uid,
external_2_uid
);
Expand All @@ -216,7 +226,12 @@ describe('manage.js external plugins integration tests', function () {
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, external_1_uid);
expect(db_data['release_plugins_flat'], 'release_plugins_flat').to.have.all.keys(
first_plugin_uid,
second_plugin_uid,
third_plugin_uid,
external_1_uid
);
expect(db_data['release_plugins_user'], 'release_plugins_user').to.have.all.keys(external_1_uid);
});

Expand All @@ -225,7 +240,7 @@ describe('manage.js external plugins integration tests', function () {
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_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid, third_plugin_uid);
expect(db_data['release_plugins_user'], 'release_plugins_user').to.be.empty;
});
});
Expand Down Expand Up @@ -272,7 +287,12 @@ describe('manage.js external plugins integration tests', function () {

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'], 'release_plugins_flat').to.have.all.keys(
first_plugin_uid,
second_plugin_uid,
third_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);
});
Expand All @@ -282,7 +302,7 @@ describe('manage.js external plugins integration tests', function () {
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_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid, third_plugin_uid);
expect(db_data['release_plugins_user'], 'release_plugins_user').to.be.empty;
});
});
Expand Down Expand Up @@ -321,7 +341,7 @@ describe('manage.js external plugins integration tests', function () {
expect(run).to.deep.equal(installed);

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_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid, third_plugin_uid);
expect(db_data['release_plugins_user'], 'release_plugins_user').to.have.all.keys(external_1_uid);

expect(db_data['release_plugins_user'][external_1_uid]['code'], "release_plugins_user['code']: " + external_1_uid).to.equal(external_code);
Expand All @@ -346,7 +366,7 @@ describe('manage.js external plugins integration tests', function () {
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_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid, third_plugin_uid);
expect(db_data['release_plugins_user'], 'release_plugins_user').to.have.all.keys(external_1_uid);

expect(db_data['release_plugins_flat'][external_1_uid]['status'], "release_plugins_flat['status']: " + external_1_uid).to.equal('off');
Expand All @@ -359,7 +379,7 @@ describe('manage.js external plugins integration tests', function () {
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_flat'], 'release_plugins_flat').to.have.all.keys(first_plugin_uid, second_plugin_uid, third_plugin_uid);
expect(db_data['release_plugins_user'], 'release_plugins_user').to.be.empty;

expect(db_data['release_plugins_flat'][external_1_uid]['status'], "release_plugins_flat['status']: " + external_1_uid).to.equal('off');
Expand All @@ -369,4 +389,70 @@ describe('manage.js external plugins 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('Adding and removing an external plugin that overwrites a built-in plugin', function () {
const external_3_uid = 'Missions+https://github.com/IITC-CE/ingress-intel-total-conversion';
const external_3_plugin = {
meta: {
namespace: 'https://github.com/IITC-CE/ingress-intel-total-conversion',
name: 'Missions',
},
code: external_code,
};

it('Add external plugin and replace built-in plugin', async function () {
const installed = {
'Missions+https://github.com/IITC-CE/ingress-intel-total-conversion': {
uid: external_3_uid,
id: 'missions',
author: 'jonatkins',
description: 'View missions. Marking progress on waypoints/missions basis. Showing mission paths on the map.',
filename: 'missions.user.js',
namespace: 'https://github.com/IITC-CE/ingress-intel-total-conversion',
name: 'Missions',
category: 'Info',
status: 'on',
override: true,
user: true,
version: '0.3.0',
code: external_code,
},
};
const run = await manager.addUserScripts([external_3_plugin]);
expect(run).to.deep.equal(installed);

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, third_plugin_uid);
expect(db_data['release_plugins_user'], 'release_plugins_user').to.have.all.keys(external_3_uid);

expect(db_data['release_plugins_user'][external_3_uid]['code'], "release_plugins_user['code']: " + external_3_uid).to.equal(external_code);
expect(db_data['release_plugins_flat'][external_3_uid]['code'], "release_plugins_flat['code']: " + external_3_uid).to.equal(external_code);
expect(db_data['release_plugins_flat'][external_3_uid]['override'], "release_plugins_flat['override']: " + external_3_uid).to.be.true;
});

it('Remove external plugin and replace it with built-in plugin', async function () {
const run = await manager.managePlugin(external_3_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, third_plugin_uid);
expect(db_data['release_plugins_user'], 'release_plugins_user').to.be.empty;

expect(db_data['release_plugins_flat'][external_3_uid]['status'], "release_plugins_flat['status']: " + external_3_uid).to.equal('off');
expect(db_data['release_plugins_flat'][external_3_uid]['override'], "release_plugins_flat['override']: " + external_3_uid).to.be.false;
});

it('Enable and disable build-in plugin', async function () {
const run1 = await manager.managePlugin(external_3_uid, 'on');
expect(run1).to.be.undefined;

const run2 = await manager.managePlugin(external_3_uid, 'off');
expect(run2).to.be.undefined;

const db_data = await storage.get(['release_plugins_flat']);
expect(db_data['release_plugins_flat'][external_3_uid]['status'], "release_plugins_flat['status']: " + external_3_uid).to.equal('off');
expect(db_data['release_plugins_flat'][external_3_uid]['code'], "release_plugins_flat['code']: " + external_3_uid).to.have.lengthOf(596);
expect(db_data['release_plugins_flat'][external_3_uid]['override'], "release_plugins_flat['override']: " + external_3_uid).to.be.false;
});
});
});

0 comments on commit feb6609

Please sign in to comment.