Files
dragonroll/tests/character-sheet-portrait-column.test.js
Gitea OpenCode Agent 8418b5c254
Some checks failed
Test / test (push) Failing after 2s
Fix issue #64: More modifications to the character sheet
2026-10-15 15:01:35 +00:00

168 lines
9.5 KiB
JavaScript

// Tests for Syndria98/dragonroll#64, the second round of modifications to the character sheet:
// * the tab rail hangs off the border of the window instead of standing inside the sheet body,
// * the armor class, the speed, the proficiency bonus and the initiative bonus stand under the
// portrait, together with the hit points and the hit dice, both drawn as bars.
//
// The window is a `.vue` file and the repository root installs no dependencies for it, so its
// template and styles are read out of the source the way tests/character-sheet-tabs.test.js reads
// them. The plain functions the sheet and the route work out their numbers with are lifted out of
// those sources and called, the way tests/map-viewer.test.js runs a service it cannot import.
//
// Run with `npm test` from the repository root, or `node --test tests/`.
const { test } = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const read = (...parts) => fs.readFileSync(path.join(__dirname, "..", ...parts), "utf8");
const sheetSource = read("frontend", "app", "components", "windows", "CharacterSheetWindow.vue");
const script = /<script setup>([\s\S]*?)<\/script>/.exec(sheetSource)?.[1] ?? "";
const template = /<template>([\s\S]*)<\/template>/.exec(sheetSource)?.[1] ?? "";
const actorModelSource = read("backend", "src", "models", "Actor.js");
const actorRouteSource = read("backend", "src", "routes", "actor.js");
// `fromSource` hands back one top-level declaration as a value. Imports and refs are module scope
// the test does not have, so the few names a declaration reads are passed in as arguments. The
// declaration runs in this realm, so the objects it builds compare with the ones the test builds.
function fromSource(source, pattern, locals = {}) {
const declaration = pattern.exec(source)?.[0];
assert.ok(declaration, `expected ${pattern} in the source`);
const name = /(?:const|function)\s+(\w+)/.exec(declaration)[1];
return new Function(...Object.keys(locals), `${declaration}\n;return ${name};`)(...Object.values(locals));
}
const abilityStats = fromSource(script, /const abilityStats = \[[\s\S]*?\];/);
const normalizeActor = fromSource(script, /function normalizeActor\(actor\) \{[\s\S]*?\n\}/, { abilityStats });
const barPercent = fromSource(script, /function barPercent\(current, max\) \{[\s\S]*?\n\}/);
const barPool = fromSource(actorRouteSource, /function barPool\(existing, value\) \{[\s\S]*?\n\}/);
// The column that opens the abilities tab: the portrait with everything under it.
function portraitColumn() {
const start = template.indexOf('<div class="character-column">');
const end = template.indexOf('<section class="sheet-panel abilities-panel">');
assert.ok(start !== -1 && end > start, "expected a character column ahead of the ability scores");
return template.slice(start, end);
}
test("the tab rail hangs off the border of the window, outside the sheet body", () => {
const rail = /\.sheet-tabs\s*\{[^}]*\}/.exec(sheetSource)?.[0] ?? "";
assert.match(rail, /position:\s*absolute/, "the rail is mounted on the window frame");
assert.match(rail, /left:\s*100%/, "it starts at the right border and reaches outwards");
assert.ok(
template.indexOf('<nav class="sheet-tabs"') < template.indexOf('<div class="sheet-body">'),
"the rail is a child of the window, ahead of the body it hangs beside"
);
const body = /\.sheet-body\s*\{[^}]*\}/.exec(sheetSource)?.[0] ?? "";
assert.match(body, /overflow:\s*auto/, "the sheet body scrolls on its own, the rail stays put");
});
test("the armor class, the speed and both bonuses stand under the portrait", () => {
const column = portraitColumn();
assert.ok(column.includes('class="sheet-portrait"'), "the portrait opens the column");
assert.ok(column.includes('class="sheet-panel vitals-panel"'), "the readouts follow the image");
assert.ok(
column.indexOf('class="sheet-portrait"') < column.indexOf("vitals-panel"),
"the readouts stand under the image, not above it"
);
for (const key of ["armor-class", "speed", "proficiency-bonus", "initiative"]) {
assert.ok(
column.includes(`campaigns.character-sheet.${key}`),
`the ${key} belongs under the image`
);
}
// Gone from where they were: the combat panel is nothing but proficiencies left over, and the
// ability scores panel keeps the six scores only.
assert.doesNotMatch(template, /combat-panel/, "the combat panel went with the combat stats");
const abilities = template
.slice(template.indexOf('<section class="sheet-panel abilities-panel">'))
.split("</section>")[0];
assert.doesNotMatch(abilities, /proficiency-bonus/, "the proficiency bonus moved down the column");
assert.match(abilities, /campaigns\.character-sheet\.ability-scores/);
});
test("the hit points and the hit dice stand as bars under the portrait", () => {
const column = portraitColumn();
assert.match(column, /class="vital-bar hit-points-bar"/);
assert.match(column, /class="vital-bar hit-dice-bar"/);
for (const pool of ["hitPoints", "hitDice"]) {
assert.match(column, new RegExp(`v-model\\.number="form\\.${pool}\\.current"`), `${pool} is editable`);
assert.match(column, new RegExp(`v-model\\.number="form\\.${pool}\\.max"`), `${pool} has a maximum to set`);
assert.match(
column,
new RegExp(`\\{\\{ form\\.${pool}\\.current \\}\\} / \\{\\{ form\\.${pool}\\.max \\}\\}`),
`${pool} reads current of max outside of editing`
);
}
// Each bar fills what its own maximum allows, and each is painted its own colour.
assert.match(column, /:style="\{ width: `\$\{hitPointsPercent\}%` \}"/);
assert.match(column, /:style="\{ width: `\$\{hitDicePercent\}%` \}"/);
assert.match(sheetSource, /\.hit-points-bar \.vital-bar-fill\s*\{[^}]*var\(--color-red\)/);
assert.match(sheetSource, /\.hit-dice-bar \.vital-bar-fill\s*\{[^}]*var\(--color-link\)/);
});
test("a bar fills what its maximum allows, no more and no less", () => {
assert.equal(barPercent(5, 10), 50);
assert.equal(barPercent(12, 10), 100, "healed past the maximum does not overflow the track");
assert.equal(barPercent(-3, 10), 0, "nothing left stands empty");
assert.equal(barPercent(4, 0), 0, "a maximum of nothing is no bar either");
assert.equal(barPercent("", 10), 0, "an input cleared mid-edit is read as nothing");
});
test("the initiative bonus is the modifier of dexterity", () => {
assert.match(
script,
/const initiativeBonus = computed\(\(\) => abilityModifier\(form\.value\.abilityScores\.dex\)\)/,
"D&D counts initiative as the dexterity modifier while the character carries no extra bonus"
);
const abilityModifier = fromSource(script, /function abilityModifier\(score\) \{[\s\S]*?\n\}/);
assert.equal(abilityModifier(16), "+3");
assert.equal(abilityModifier(10), "+0", "the sheet writes a flat modifier with its sign, as the ability cards do");
assert.equal(abilityModifier(9), "-1");
});
test("an actor carries its pools, and one without them starts on its level", () => {
assert.deepEqual(normalizeActor({ level: 3 }).hitPoints, { current: 10, max: 10 });
assert.deepEqual(normalizeActor({ level: 3 }).hitDice, { current: 3, max: 3 }, "one die per level");
const wounded = normalizeActor({
level: 5,
hitPoints: { current: 4, max: 44 },
hitDice: { current: 2, max: 5 }
});
assert.deepEqual(wounded.hitPoints, { current: 4, max: 44 });
assert.deepEqual(wounded.hitDice, { current: 2, max: 5 });
});
test("the Actor model stores both pools as current of a maximum", () => {
for (const pool of ["hitPoints", "hitDice"]) {
assert.match(
actorModelSource,
new RegExp(`${pool}:\\s*\\{\\s*current:\\s*\\{\\s*type:\\s*Number[^}]*\\},\\s*max:\\s*\\{\\s*type:\\s*Number[^}]*\\}`),
`${pool} holds a current and a maximum`
);
}
});
test("the actor update takes both pools and never lets one pass its maximum", () => {
assert.match(actorRouteSource, /const \{[^}]*hitPoints[^}]*hitDice[^}]*\} = req\.body;/, "the update reads them off the body");
assert.match(actorRouteSource, /if \(hitPoints !== undefined\) actor\.hitPoints = barPool\(actor\.hitPoints, hitPoints\)/);
assert.match(actorRouteSource, /if \(hitDice !== undefined\) actor\.hitDice = barPool\(actor\.hitDice, hitDice\)/);
assert.deepEqual(barPool({ current: 5, max: 20 }, { current: 8, max: 30 }), { current: 8, max: 30 });
assert.deepEqual(barPool({ current: 5, max: 20 }, { current: 40 }), { current: 20, max: 20 }, "a current over its maximum stops at the maximum");
assert.deepEqual(barPool({ current: 5, max: 20 }, { max: 4 }), { current: 4, max: 4 }, "a maximum lowered below the current pulls it down");
assert.deepEqual(barPool({ current: 5, max: 20 }, { current: 12 }), { current: 12, max: 20 }, "half a pair keeps the half it did not send");
assert.deepEqual(barPool({ current: 5, max: 20 }, { current: -2, max: 20 }), { current: 0, max: 20 });
});
test("every locale names the readouts that moved under the portrait", () => {
for (const locale of ["en", "es", "ca"]) {
const sheet = JSON.parse(read("frontend", "i18n", "locales", locale, "campaigns.json"))["character-sheet"];
for (const key of ["initiative", "hit-points", "max-hit-points", "hit-dice", "max-hit-dice"]) {
assert.ok(sheet[key], `${locale} is missing campaigns.character-sheet.${key}`);
}
}
});