0ad/binaries/data/mods/public/simulation/components/tests/test_Builder.js
Dunedan 93ce94655d
Use @stylistic/brace-style for eslint
Up to now `eslint-plugin-brace-rules` was used to enforce a common brace
style for JavaScript code. This plugin was however updated the last time
over 9 years ago and will be incompatible with ESLint v10, as that
[removes `context.getSourceCode()`][1], the plugin relies on.

To keep the eslint config working with ESLint v10, this replaces
`eslint-plugin-brace-rules` with the [`@stylistic/brace-style`][2] rule
from `@stylistic/eslint-plugin`, a package we already use.

While `@stylistic/brace-style` doesn't offer an option to format braces
in exactly the same way as before, the "allman" style seems to be the
one closest to the existing code.

[1]: https://eslint.org/blog/2025/11/eslint-v10.0.0-alpha.0-released/#removed-deprecated-rule-context-members
[2]: https://eslint.style/rules/brace-style
2026-01-12 21:33:52 +01:00

196 lines
5.5 KiB
JavaScript

Engine.LoadHelperScript("Player.js");
Engine.LoadComponentScript("interfaces/Builder.js");
Engine.LoadComponentScript("interfaces/Cost.js");
Engine.LoadComponentScript("interfaces/Diplomacy.js");
Engine.LoadComponentScript("interfaces/Foundation.js");
Engine.LoadComponentScript("interfaces/Health.js");
Engine.LoadComponentScript("interfaces/Repairable.js");
Engine.LoadComponentScript("interfaces/Timer.js");
Engine.LoadComponentScript("interfaces/UnitAI.js");
Engine.LoadComponentScript("Builder.js");
Engine.LoadComponentScript("Health.js");
Engine.LoadComponentScript("Repairable.js");
Engine.LoadComponentScript("Timer.js");
const builderId = 6;
const target = 7;
const playerId = 1;
const playerEntityID = 2;
AddMock(SYSTEM_ENTITY, IID_ObstructionManager, {
"IsInTargetRange": () => true
});
AddMock(SYSTEM_ENTITY, IID_TemplateManager, {
"TemplateExists": () => true
});
Engine.RegisterGlobal("ApplyValueModificationsToEntity", (prop, oVal, ent) => oVal);
function testEntitiesList()
{
const cmpBuilder = ConstructComponent(builderId, "Builder", {
"Rate": "1.0",
"Entities": { "_string": "structures/{civ}/barracks structures/{civ}/civil_centre structures/{native}/house" }
});
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), []);
AddMock(SYSTEM_ENTITY, IID_PlayerManager, {
"GetPlayerByID": id => playerEntityID
});
AddMock(playerEntityID, IID_Player, {
"GetDisabledTemplates": () => ({}),
"GetPlayerID": () => playerId
});
AddMock(playerEntityID, IID_Identity, {
"GetCiv": () => "iber",
});
AddMock(builderId, IID_Ownership, {
"GetOwner": () => playerId
});
AddMock(builderId, IID_Identity, {
"GetCiv": () => "iber"
});
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/iber/barracks", "structures/iber/civil_centre", "structures/iber/house"]);
AddMock(SYSTEM_ENTITY, IID_TemplateManager, {
"TemplateExists": name => name == "structures/iber/civil_centre"
});
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/iber/civil_centre"]);
AddMock(SYSTEM_ENTITY, IID_TemplateManager, {
"TemplateExists": () => true
});
AddMock(playerEntityID, IID_Player, {
"GetDisabledTemplates": () => ({ "structures/athen/barracks": true }),
"GetPlayerID": () => playerId
});
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/iber/barracks", "structures/iber/civil_centre", "structures/iber/house"]);
AddMock(playerEntityID, IID_Player, {
"GetDisabledTemplates": () => ({ "structures/iber/barracks": true }),
"GetPlayerID": () => playerId
});
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/iber/civil_centre", "structures/iber/house"]);
AddMock(playerEntityID, IID_Player, {
"GetDisabledTemplates": () => ({ "structures/athen/barracks": true }),
"GetPlayerID": () => playerId
});
AddMock(playerEntityID, IID_Identity, {
"GetCiv": () => "athen",
});
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/athen/civil_centre", "structures/iber/house"]);
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetRange(), { "max": 2, "min": 0 });
AddMock(builderId, IID_Obstruction, {
"GetSize": () => 1
});
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetRange(), { "max": 3, "min": 0 });
}
testEntitiesList();
function testBuildingFoundation()
{
const cmpBuilder = ConstructComponent(builderId, "Builder", {
"Rate": "1.0",
"Entities": { "_string": "" }
});
AddMock(playerEntityID, IID_Diplomacy, {
"IsAlly": (p) => p == playerId
});
AddMock(target, IID_Ownership, {
"GetOwner": () => playerId
});
let increased = false;
AddMock(target, IID_Foundation, {
"Build": (entity, amount) =>
{
increased = true;
TS_ASSERT_EQUALS(amount, 1);
},
"AddBuilder": () => {}
});
const cmpTimer = ConstructComponent(SYSTEM_ENTITY, "Timer");
TS_ASSERT(cmpBuilder.StartRepairing(target));
cmpTimer.OnUpdate({ "turnLength": 1 });
TS_ASSERT(increased);
increased = false;
cmpTimer.OnUpdate({ "turnLength": 2 });
TS_ASSERT(increased);
}
testBuildingFoundation();
function testRepairing()
{
AddMock(playerEntityID, IID_Diplomacy, {
"IsAlly": (p) => p == playerId
});
const cmpBuilder = ConstructComponent(builderId, "Builder", {
"Rate": "1.0",
"Entities": { "_string": "" }
});
AddMock(target, IID_Ownership, {
"GetOwner": () => playerId
});
AddMock(target, IID_Cost, {
"GetBuildTime": () => 100
});
const cmpTargetHealth = ConstructComponent(target, "Health", {
"Max": 100,
"RegenRate": 0,
"IdleRegenRate": 0,
"DeathType": "vanish",
"Unhealable": false
});
cmpTargetHealth.SetHitpoints(50);
DeleteMock(target, IID_Foundation);
const cmpTargetRepairable = ConstructComponent(target, "Repairable", {
"RepairTimeRatio": 1,
});
const cmpTimer = ConstructComponent(SYSTEM_ENTITY, "Timer");
TS_ASSERT(cmpTargetRepairable.IsRepairable());
TS_ASSERT(cmpBuilder.StartRepairing(target));
cmpTimer.OnUpdate({ "turnLength": 1 });
TS_ASSERT_EQUALS(cmpTargetHealth.GetHitpoints(), 51);
cmpTimer.OnUpdate({ "turnLength": 1 });
TS_ASSERT_EQUALS(cmpTargetHealth.GetHitpoints(), 52);
cmpTargetRepairable.SetRepairability(false);
cmpTimer.OnUpdate({ "turnLength": 1 });
TS_ASSERT_EQUALS(cmpTargetHealth.GetHitpoints(), 52);
cmpTargetRepairable.SetRepairability(true);
// Check that we indeed stopped - shouldn't restart on its own.
cmpTimer.OnUpdate({ "turnLength": 1 });
TS_ASSERT_EQUALS(cmpTargetHealth.GetHitpoints(), 52);
TS_ASSERT(cmpBuilder.StartRepairing(target));
cmpTimer.OnUpdate({ "turnLength": 1 });
TS_ASSERT_EQUALS(cmpTargetHealth.GetHitpoints(), 53);
}
testRepairing();