Files
OpenFrontIO/tests/MapManifestFlags.test.ts
Aotumuri 0cc58a8f5a fix: add validation for unknown flags in manifest.json (#3044)
Resolves #3041

## Description:

- Add a test to ensure an error is thrown when manifest.json specifies a
non-existent flag.
- Fix the underlying issue by removing the invalid flag specification
(see error below).

```
resources/maps/straitofgibraltar/manifest.json -> nations[0].flag "Rif" does not exist in resources/flags
resources/maps/straitofgibraltar/manifest.json -> nations[5].flag "Shilha" does not exist in resources/flags
resources/maps/straitofgibraltar/manifest.json -> nations[6].flag "Andalusia" does not exist in resources/flags
resources/maps/italia/manifest.json -> nations[0].flag "custom:Kingdom of the Two Sicilies" does not exist in resources/flags
resources/maps/italia/manifest.json -> nations[3].flag "custom:Tuscany" does not exist in resources/flags
resources/maps/italia/manifest.json -> nations[5].flag "custom:Modena" does not exist in resources/flags
resources/maps/italia/manifest.json -> nations[6].flag "custom:Parma" does not exist in resources/flags
resources/maps/italia/manifest.json -> nations[8].flag "custom:Kingdom of Sardinia" does not exist in resources/flags
resources/maps/italia/manifest.json -> nations[11].flag "custom:Ottoman Empire2" does not exist in resources/flags
resources/maps/britannia/manifest.json -> nations[19].flag "gb-nir" does not exist in resources/flags
resources/maps/montreal/manifest.json -> nations[0].flag "quebec" does not exist in resources/flags
resources/maps/montreal/manifest.json -> nations[1].flag "quebec" does not exist in resources/flags
resources/maps/montreal/manifest.json -> nations[2].flag "quebec" does not exist in resources/flags
resources/maps/montreal/manifest.json -> nations[4].flag "quebec" does not exist in resources/flags
resources/maps/montreal/manifest.json -> nations[5].flag "quebec" does not exist in resources/flags
resources/maps/montreal/manifest.json -> nations[6].flag "quebec" does not exist in resources/flags
resources/maps/montreal/manifest.json -> nations[7].flag "quebec" does not exist in resources/flags
resources/maps/montreal/manifest.json -> nations[8].flag "quebec" does not exist in resources/flags
resources/maps/montreal/manifest.json -> nations[9].flag "quebec" does not exist in resources/flags
resources/maps/montreal/manifest.json -> nations[10].flag "quebec" does not exist in resources/flags
resources/maps/montreal/manifest.json -> nations[11].flag "quebec" does not exist in resources/flags
```

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

aotumuri
2026-01-27 15:54:01 -08:00

62 lines
1.7 KiB
TypeScript

import fs from "fs";
import { globSync } from "glob";
import path from "path";
type Nation = {
flag?: string;
};
type Manifest = {
nations?: Nation[];
};
describe("Map manifests: nation flags exist", () => {
test("All nations' flags reference existing SVG files", () => {
const manifestPaths = globSync("resources/maps/**/manifest.json");
expect(manifestPaths.length).toBeGreaterThan(0);
const flagDir = path.join(__dirname, "../resources/flags");
const errors: string[] = [];
for (const manifestPath of manifestPaths) {
try {
const raw = fs.readFileSync(manifestPath, "utf8");
const manifest = JSON.parse(raw) as Manifest;
(manifest.nations ?? []).forEach((nation, idx) => {
const flag = nation?.flag;
if (flag === undefined || flag === null) return;
if (typeof flag !== "string") {
errors.push(
`${manifestPath} -> nations[${idx}].flag is not a string`,
);
return;
}
if (flag.trim().length === 0) return;
if (flag.startsWith("!")) return;
const svgFile = flag.endsWith(".svg") ? flag : `${flag}.svg`;
const flagPath = path.join(flagDir, svgFile);
if (!fs.existsSync(flagPath)) {
errors.push(
`${manifestPath} -> nations[${idx}].flag "${flag}" does not exist in resources/flags`,
);
}
});
} catch (err) {
errors.push(
`Failed to parse ${manifestPath}: ${(err as Error).message}`,
);
}
}
if (errors.length > 0) {
throw new Error(
"Map manifest flag file violations:\n" + errors.join("\n"),
);
}
});
});