Skip to content

Commit

Permalink
Add tests to validate XWS
Browse files Browse the repository at this point in the history
This will check all Quick Builds to make sure they use XWS ids that exist
This will also make sure there are no duplicate pilot or upgrade XWS ids
  • Loading branch information
guidokessels committed Feb 17, 2019
1 parent 03fd9d1 commit e491b11
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ cache:
yarn: true
script:
- yarn run validate:json
- yarn run validate:schema
- yarn run validate:tests
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"license": "MIT",
"scripts": {
"validate:json": "jsonlint-cli data/**/*.json",
"validate:schema": "jest tests/**-schema.test.js",
"validate:schema:watch": "jest --watch tests/**-schema.test.js",
"validate:tests": "jest tests/**.test.js",
"validate:tests:watch": "jest --watch tests/**.test.js",
"format": "prettier --loglevel warn --write \"data/**/*.json\"",
"changelog": "git log --pretty=format:'- %s (%h)' --no-merges",
"ffg2xws": "node scripts/ffg2xws.js && prettier --write data/ffg-xws.json",
Expand Down
106 changes: 106 additions & 0 deletions tests/helpers/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const manifest = require("../../data/manifest.json");

const loadedData = {};
const mapSlotXWSToName = {
astromech: "Astromech",
cannon: "Cannon",
configuration: "Configuration",
crew: "Crew",
device: "Device",
"force-power": "Force Power",
gunner: "Gunner",
illicit: "Illicit",
missile: "Missile",
modification: "Modification",
sensor: "Sensor",
tacticalrelay: "Tactical Relay",
talent: "Talent",
tech: "Tech",
title: "Title",
torpedo: "Torpedo",
turret: "Turret"
};

const loadPilotsAndShips = () => {
const allPilots = [];
const allShips = [];

manifest.pilots.forEach(({ ships }) => {
ships.forEach(filename => {
const { pilots = [], ship } = require(`../../${filename}`);
allPilots.push(...pilots);
allShips.push(ship);
});
});

return { ships: allShips, pilots: allPilots };
};

const getPilots = () => {
if (!loadedData.pilots) {
const { pilots } = loadPilotsAndShips();
loadedData.pilots = pilots;
}
return loadedData.pilots;
};

const getPilotXWSIds = () => {
const pilots = getPilots();
return pilots.map(p => p.xws).filter(Boolean);
};

const validatePilotXWSId = id => {
const ids = getPilotXWSIds();
if (ids.indexOf(id) === -1) {
throw new Error(`Pilot xws id "${id}" does not exist`);
}
};

const loadUpgrades = () => {
const allUpgrades = [];

manifest.upgrades.forEach(filename => {
const upgrades = require(`../../${filename}`);
allUpgrades.push(...upgrades);
});

return { upgrades: allUpgrades };
};

const getUpgrades = () => {
if (!loadedData.upgrades) {
const { upgrades } = loadUpgrades();
loadedData.upgrades = upgrades;
}
return loadedData.upgrades;
};

const getUpgradesXWSIds = () => {
const upgrades = getUpgrades();
return upgrades.map(u => u.xws).filter(Boolean);
};

const getUpgradesXWSIdsForSlot = slotName => {
const slotId = mapSlotXWSToName[slotName] || slotName;
const upgrades = getUpgrades();
return upgrades
.filter(u => u.sides.some(s => s.type === slotId))
.map(u => u.xws)
.filter(Boolean);
};

const validateUpgradeXWSIdForSlot = (id, slotName) => {
const ids = getUpgradesXWSIdsForSlot(slotName);
if (ids.indexOf(id) === -1) {
throw new Error(
`Upgrade xws id "${id}" does not exist for slot "${slotName}"`
);
}
};

module.exports = {
getPilotXWSIds,
validatePilotXWSId,
getUpgradesXWSIds,
validateUpgradeXWSIdForSlot
};
28 changes: 0 additions & 28 deletions tests/quick-builds-schema.test.js

This file was deleted.

55 changes: 55 additions & 0 deletions tests/quick-builds.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const path = require("path");
const {
validatePilotXWSId,
validateUpgradeXWSIdForSlot
} = require("./helpers/data");
const { matchers } = require("jest-json-schema");
expect.extend(matchers);

const { "quick-builds": quickBuildFiles } = require("../data/manifest.json");

const quickBuildsSchema = require("./schemas/quick-build.schema.json");

describe("Quick Builds", () => {
quickBuildFiles.forEach(file => {
const contents = require(`../${file}`);

test("has a top-level `quick-builds` property", () => {
expect(contents).toHaveProperty("quick-builds");
});

// `data/quick-builds/resistance.json` -> `resistance`
const faction = path.basename(file, path.extname(file));

describe(`${faction}`, () => {
const { "quick-builds": quickBuilds } = contents;

quickBuilds.forEach((qb, i) => {
// Example: `#64 - threat 2 - deathrain`
const testName = [
`#${i}`,
`threat ${qb.threat}`,
qb.pilots.map(d => d.id).join(", ")
].join(" - ");

describe(testName, () => {
test("matches schema", () => {
expect(qb).toMatchSchema(quickBuildsSchema);
});

test("uses valid XWS ids", () => {
qb.pilots.forEach(pilot => {
const { id: pilotId, upgrades = {} } = pilot;
validatePilotXWSId(pilotId);
Object.entries(upgrades).forEach(([slot, ids]) => {
ids.forEach(upgradeId =>
validateUpgradeXWSIdForSlot(upgradeId, slot)
);
});
});
});
});
});
});
});
});
20 changes: 20 additions & 0 deletions tests/xws.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { getPilotXWSIds, getUpgradesXWSIds } = require("./helpers/data");

const checkForDuplicates = ids => {
ids.reduce((acc, id) => {
if (acc.indexOf(id) > -1) {
throw new Error(`Duplicate XWS id: "${id}"`);
}
acc.push(id);
return acc;
}, []);
};

describe("XWS", () => {
test("no duplicate pilot XWS ids", () => {
checkForDuplicates(getPilotXWSIds());
});
test("no duplicate upgrade XWS ids", () => {
checkForDuplicates(getUpgradesXWSIds());
});
});

0 comments on commit e491b11

Please sign in to comment.