Files
dragonroll/tests/theme-color-vars.test.js
Gitea OpenCode Agent 37e6e44612
All checks were successful
Test / test (push) Successful in 2s
Fix issue #71: Nuxt error with colors
2026-10-15 16:44:08 +00:00

73 lines
4.6 KiB
JavaScript

// Tests for Syndria98/dragonroll#71: `pnpm dev` warned, on every frontend build,
// "You probably don't mean to use the color value red in interpolation here" from `theme-vars()`
// in colors.scss. `red`, `green` and `gray` are also Sass colour keywords, so as unquoted map keys
// they were parsed as colour *values* and interpolating one into `--color-#{$name}` warned (and can
// emit an invalid name). Quoting those keys keeps them strings; the emitted CSS is unchanged, so
// `var(--color-red)` and friends still resolve.
//
// `sass` is not installed for `npm test`, so this reads colors.scss the way the other tests read
// sources: the check is that no map key is left bare when it spells a CSS colour keyword.
//
// 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 colorsSource = read("frontend", "app", "assets", "css", "colors.scss");
// The CSS named colours. Sass parses any of these (in any case) as a colour value, so a theme or
// accent map key spelled like one of them has to be quoted to stay a string.
const colorKeywords = new Set(
[
"aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige", "bisque", "black",
"blanchedalmond", "blue", "blueviolet", "brown", "burlywood", "cadetblue", "chartreuse",
"chocolate", "coral", "cornflowerblue", "cornsilk", "crimson", "cyan", "darkblue", "darkcyan",
"darkgoldenrod", "darkgray", "darkgreen", "darkkhaki", "darkmagenta", "darkolivegreen",
"darkorange", "darkorchid", "darkred", "darksalmon", "darkseagreen", "darkslateblue",
"darkslategray", "darkturquoise", "darkviolet", "deeppink", "deepskyblue", "dimgray",
"dodgerblue", "firebrick", "floralwhite", "forestgreen", "fuchsia", "gainsboro", "ghostwhite",
"gold", "goldenrod", "gray", "green", "greenyellow", "honeydew", "hotpink", "indianred",
"indigo", "ivory", "khaki", "lavender", "lavenderblush", "lawngreen", "lemonchiffon",
"lightblue", "lightcoral", "lightcyan", "lightgoldenrodyellow", "lightgray", "lightgreen",
"lightpink", "lightsalmon", "lightseagreen", "lightskyblue", "lightslategray",
"lightsteelblue", "lightyellow", "lime", "limegreen", "linen", "magenta", "maroon",
"mediumaquamarine", "mediumblue", "mediumorchid", "mediumpurple", "mediumseagreen",
"mediumslateblue", "mediumspringgreen", "mediumturquoise", "mediumvioletred", "midnightblue",
"mintcream", "mistyrose", "moccasin", "navajowhite", "navy", "oldlace", "olive", "olivedrab",
"orange", "orangered", "orchid", "palegoldenrod", "palegreen", "paleturquoise",
"palevioletred", "papayawhip", "peachpuff", "peru", "pink", "plum", "powderblue", "purple",
"rebeccapurple", "red", "rosybrown", "royalblue", "saddlebrown", "salmon", "sandybrown",
"seagreen", "seashell", "sienna", "silver", "skyblue", "slateblue", "slategray", "snow",
"springgreen", "steelblue", "tan", "teal", "thistle", "tomato", "transparent", "turquoise",
"violet", "wheat", "white", "whitesmoke", "yellow", "yellowgreen",
]
);
// The keys of the `$themes` and `$accents` maps and of each theme inside them: a line starting with
// `key:` or `"key":`. The custom properties of the mixins (`--color-#{$name}: #{$value};`) do not
// match, `#` is not part of a key.
const mapKeys = [...colorsSource.matchAll(/^\s*("[^"]+"|[A-Za-z][A-Za-z0-9_-]*)\s*:/gm)].map((m) => m[1]);
test("the theme and accent maps are read as maps of keys", () => {
// sanity check on the extraction above before it is trusted to find a bare colour keyword
for (const key of ["dark", "light", "katlum", "solus", "silang", "background", "text"]) {
assert.ok(mapKeys.includes(key), `expected to find the map key '${key}'`);
}
});
test("no map key is a bare CSS colour keyword", () => {
const bare = mapKeys.filter((key) => !key.startsWith('"') && colorKeywords.has(key.toLowerCase()));
assert.deepEqual(bare, [], `quote these colors.scss map keys so Sass keeps them strings: ${bare.join(", ")}`);
});
test("the colour-named theme keys are quoted in both maps", () => {
for (const [key, value] of [["red", "#e06c75"], ["green", "#98c379"], ["gray", "#cccccc"]]) {
const declarations = colorsSource.match(new RegExp(`"${key}":\\s*${value}\\b`, "g")) || [];
assert.equal(declarations.length, 2, `expected quoted "${key}": ${value} in both the dark and the light map`);
}
});