Files
OpenFrontIO/tests/ArchivedRecordSchema.test.ts
T
RyanandGitHub 774d98ddad bugfix: trim archived map names (#4632)
## Description:

fixed a bug where "Deglaciated Antarctica" had a trailing space in an
earlier version of the game and caused visual issues:
<img width="1045" height="323" alt="image"
src="https://github.com/user-attachments/assets/11628e19-523e-43f9-b3dc-15a5ee1bcd8a"
/>
and prevented stats from loading:
<img width="1070" height="529" alt="image"
src="https://github.com/user-attachments/assets/590a7c2b-7dc5-4d8d-816f-469731d42fd4"
/>
 

now works:
<img width="969" height="387" alt="image"
src="https://github.com/user-attachments/assets/35d4ac89-5f97-4534-adbc-d0ce824ea4d8"
/>
<img width="1013" height="787" alt="image"
src="https://github.com/user-attachments/assets/44d6c20a-caf6-417a-b7fe-08f0765e1b16"
/>


## 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

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

w.o.n
2026-07-17 15:09:39 -07:00

110 lines
3.4 KiB
TypeScript

import {
AnalyticsRecordSchema,
ArchivedAnalyticsRecordSchema,
} from "../src/core/Schemas";
// A record as an old build would have written it: no `nations` in the config,
// no `clanTag` on players, a username that fails today's tighter regex, and a
// scalar `conquests` stat (it became an array later). The `version` literal
// never changed across those schema changes, so only the lenient archived
// schema can read it back.
function oldRecord() {
return {
version: "v0.0.2",
gitCommit: "0123456789abcdef0123456789abcdef01234567",
subdomain: "eu1",
domain: "openfront.io",
info: {
gameID: "abCD1234",
lobbyCreatedAt: 1700000000000,
start: 1700000001000,
end: 1700000002000,
duration: 1000,
num_turns: 100,
lobbyFillTime: 5000,
winner: ["player", "abCD1234"],
config: {
// no `nations` — predates configurable nation count
gameMap: "Africa",
difficulty: "Medium",
donateGold: true,
donateTroops: true,
gameType: "Public",
gameMode: "Free For All",
gameMapSize: "Normal",
bots: 400,
infiniteGold: false,
infiniteTroops: false,
instantBuild: false,
randomSpawn: false,
},
players: [
{
clientID: "abCD1234",
username: "[BR] køva!", // fails today's UsernameSchema regex
// no `clanTag` — predates clan tags
persistentID: null,
stats: {
conquests: "3", // scalar, pre-array
attacks: ["100"],
},
},
],
},
};
}
describe("ArchivedAnalyticsRecordSchema", () => {
test("strict schema rejects old record (why the archived one exists)", () => {
expect(AnalyticsRecordSchema.safeParse(oldRecord()).success).toBe(false);
});
test("parses old record", () => {
const result = ArchivedAnalyticsRecordSchema.safeParse(oldRecord());
expect(result.success).toBe(true);
if (!result.success) return;
const player = result.data.info.players[0];
expect(player.username).toBe("[BR] køva!");
expect(player.clanTag).toBeNull();
expect(player.stats?.conquests).toEqual([3n]);
expect(result.data.info.config.nations).toBe("default");
});
test("parses current-format record unchanged", () => {
const base = oldRecord();
const record = {
...base,
info: {
...base.info,
config: { ...base.info.config, nations: "disabled" },
players: [
{
...base.info.players[0],
username: "NormalName",
clanTag: "ABC",
stats: { conquests: ["1", "2", "0"] },
},
],
},
};
const result = ArchivedAnalyticsRecordSchema.safeParse(record);
expect(result.success).toBe(true);
if (!result.success) return;
expect(result.data.info.config.nations).toBe("disabled");
expect(result.data.info.players[0].clanTag).toBe("ABC");
expect(result.data.info.players[0].stats?.conquests).toEqual([1n, 2n, 0n]);
});
test("normalizes accidental whitespace around an archived map name", () => {
const record = oldRecord();
record.info.config.gameMap = "Deglaciated Antarctica ";
const result = ArchivedAnalyticsRecordSchema.safeParse(record);
expect(result.success).toBe(true);
if (!result.success) return;
expect(result.data.info.config.gameMap).toBe("Deglaciated Antarctica");
});
});