mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-23 20:25:37 +00:00
## Summary Client side of openfrontio/infra#436 — every player-facing GET now returns the player's **account username** next to their `publicId`, and this PR renders it everywhere, with a blue verified check for premium/indefinite bare-name holders. ### Schemas (all additive, `.nullable().optional()` — no deploy-order constraint) - `FriendEntry`, `PlayerProfile`: `username` - `ClanMember`, `ClanJoinRequest`: `username`; `ClanBan`: `username` + `bannedByUsername` - `RankedLeaderboardEntry`: `accountUsername` (the existing session `username` field is untouched) ### `<player-name>` element One component owns player-identity rendering on account surfaces: - displays `username ?? publicId` - click-to-copy copies the **username when set**, the publicId otherwise; `copyText` overrides (profile modal copies the share URL) - `onNameClick` replaces copying with an action (clan member rows + leaderboard rows open the player's profile); `nameClass` overrides the chip styling so leaderboard names keep their original bold look - appends the verified check (same mark as the username-row toggle) when `isVerifiedUsername(username)` Used by: friends list + requests, all clan member/request/ban rows (bans badge both the banned player and the issuing officer), ranked leaderboard rows + sticky self row (showing `accountUsername ?? public_id` — the session name is deliberately dropped ahead of its API removal), profile modal header. ### Verified inference No API flag needed: account-username bases can never contain dots, so a dotless display name **is** the bare-name claim (premium/indefinite). `TEMPORARY####` server renames are excluded, matching the play-toggle eligibility rule. The leaderboard derives the badge from `accountUsername` only — session names are free-form and would false-positive. Also: clan member/request/ban search filters now match usernames, not just publicIds, and sending a friend request refetches the request lists instead of inserting the raw input (which may be a username, not a publicId). ## Test plan - [x] `npm test` — full suite green (2087 + 176 server) - [x] New schema tests: each field's set / null / absent states; `accountUsername` coexists with session `username` - [x] `isVerifiedUsername` unit tests (bare / suffixed / null / TEMPORARY) - [x] tsc, ESLint, Prettier clean 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
451 lines
13 KiB
TypeScript
451 lines
13 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
ClanBanSchema,
|
|
ClanGameFilterSchema,
|
|
ClanGamePlayerSchema,
|
|
ClanGameResultSchema,
|
|
ClanGameSchema,
|
|
ClanGamesResponseSchema,
|
|
ClanInfoSchema,
|
|
ClanJoinRequestSchema,
|
|
ClanMemberSchema,
|
|
} from "../../../src/core/ClanApiSchemas";
|
|
|
|
describe("ClanInfoSchema", () => {
|
|
const base = {
|
|
name: "Test Clan",
|
|
tag: "TEST",
|
|
description: "A clan",
|
|
isOpen: true,
|
|
};
|
|
|
|
it("accepts valid data with ISO datetime createdAt", () => {
|
|
const result = ClanInfoSchema.safeParse({
|
|
...base,
|
|
createdAt: "2024-01-15T12:00:00.000Z",
|
|
memberCount: 5,
|
|
});
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it("rejects non-ISO strings for createdAt", () => {
|
|
const result = ClanInfoSchema.safeParse({
|
|
...base,
|
|
createdAt: "January 15, 2024",
|
|
});
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it("accepts data without optional createdAt", () => {
|
|
const result = ClanInfoSchema.safeParse(base);
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it("accepts data without optional memberCount", () => {
|
|
const result = ClanInfoSchema.safeParse({
|
|
...base,
|
|
createdAt: "2024-01-15T12:00:00.000Z",
|
|
});
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it("accepts data with neither createdAt nor memberCount", () => {
|
|
const result = ClanInfoSchema.safeParse(base);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.data.createdAt).toBeUndefined();
|
|
expect(result.data.memberCount).toBeUndefined();
|
|
}
|
|
});
|
|
|
|
it("accepts a string discordUrl", () => {
|
|
const result = ClanInfoSchema.safeParse({
|
|
...base,
|
|
discordUrl: "https://discord.gg/abc123",
|
|
});
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it("accepts a null discordUrl (link unset)", () => {
|
|
const result = ClanInfoSchema.safeParse({ ...base, discordUrl: null });
|
|
expect(result.success).toBe(true);
|
|
if (result.success) expect(result.data.discordUrl).toBeNull();
|
|
});
|
|
|
|
it("accepts data without discordUrl (omitted by browse results)", () => {
|
|
const result = ClanInfoSchema.safeParse(base);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) expect(result.data.discordUrl).toBeUndefined();
|
|
});
|
|
|
|
it("rejects a discordUrl longer than 255 characters", () => {
|
|
const result = ClanInfoSchema.safeParse({
|
|
...base,
|
|
discordUrl: "https://discord.gg/" + "a".repeat(255),
|
|
});
|
|
expect(result.success).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("ClanMemberSchema", () => {
|
|
it("accepts a valid member with ISO datetime joinedAt", () => {
|
|
const result = ClanMemberSchema.safeParse({
|
|
role: "member",
|
|
joinedAt: "2024-03-01T09:30:00.000Z",
|
|
publicId: "abc123",
|
|
});
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it("rejects a plain string for joinedAt", () => {
|
|
const result = ClanMemberSchema.safeParse({
|
|
role: "member",
|
|
joinedAt: "last Tuesday",
|
|
publicId: "abc123",
|
|
});
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it("rejects null publicId", () => {
|
|
const result = ClanMemberSchema.safeParse({
|
|
role: "leader",
|
|
joinedAt: "2024-03-01T09:30:00.000Z",
|
|
publicId: null,
|
|
});
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it("accepts stats with total/ffa/team/ranked/1v1 win-loss breakdown", () => {
|
|
const result = ClanMemberSchema.safeParse({
|
|
role: "member",
|
|
joinedAt: "2024-03-01T09:30:00.000Z",
|
|
publicId: "abc123",
|
|
stats: {
|
|
total: { wins: 8, losses: 8 },
|
|
ffa: { wins: 2, losses: 4 },
|
|
team: { wins: 5, losses: 1 },
|
|
hvn: { wins: 0, losses: 0 },
|
|
duos: { wins: 1, losses: 0 },
|
|
trios: { wins: 2, losses: 0 },
|
|
quads: { wins: 2, losses: 1 },
|
|
"2": { wins: 1, losses: 0 },
|
|
"3": { wins: 2, losses: 0 },
|
|
"4": { wins: 2, losses: 1 },
|
|
"5": { wins: 0, losses: 0 },
|
|
"6": { wins: 0, losses: 0 },
|
|
"7": { wins: 0, losses: 0 },
|
|
ranked: { wins: 1, losses: 3 },
|
|
"1v1": { wins: 1, losses: 3 },
|
|
},
|
|
});
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it("treats stats as optional for backwards compatibility", () => {
|
|
const result = ClanMemberSchema.safeParse({
|
|
role: "member",
|
|
joinedAt: "2024-03-01T09:30:00.000Z",
|
|
publicId: "abc123",
|
|
});
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.data.stats).toBeUndefined();
|
|
}
|
|
});
|
|
|
|
it("accepts an account username, null, or absence (older API)", () => {
|
|
const base = {
|
|
role: "member",
|
|
joinedAt: "2024-03-01T09:30:00.000Z",
|
|
publicId: "abc123",
|
|
};
|
|
const named = ClanMemberSchema.safeParse({
|
|
...base,
|
|
username: "bob.4821",
|
|
});
|
|
expect(named.success).toBe(true);
|
|
if (named.success) {
|
|
expect(named.data.username).toBe("bob.4821");
|
|
}
|
|
expect(
|
|
ClanMemberSchema.safeParse({ ...base, username: null }).success,
|
|
).toBe(true);
|
|
expect(ClanMemberSchema.safeParse(base).success).toBe(true);
|
|
});
|
|
|
|
it("rejects stats missing a bucket", () => {
|
|
const result = ClanMemberSchema.safeParse({
|
|
role: "member",
|
|
joinedAt: "2024-03-01T09:30:00.000Z",
|
|
publicId: "abc123",
|
|
stats: {
|
|
ffa: { wins: 1, losses: 1 },
|
|
team: { wins: 1, losses: 1 },
|
|
},
|
|
});
|
|
expect(result.success).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("ClanJoinRequestSchema", () => {
|
|
it("accepts a valid join request with ISO datetime createdAt", () => {
|
|
const result = ClanJoinRequestSchema.safeParse({
|
|
publicId: "player-xyz",
|
|
createdAt: "2024-06-10T08:00:00.000Z",
|
|
});
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it("rejects a plain string for createdAt", () => {
|
|
const result = ClanJoinRequestSchema.safeParse({
|
|
publicId: "player-xyz",
|
|
createdAt: "2024-06-10",
|
|
});
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it("accepts the requester's account username, null, or absence", () => {
|
|
const base = {
|
|
publicId: "player-xyz",
|
|
createdAt: "2024-06-10T08:00:00.000Z",
|
|
};
|
|
expect(
|
|
ClanJoinRequestSchema.safeParse({ ...base, username: "bob.4821" })
|
|
.success,
|
|
).toBe(true);
|
|
expect(
|
|
ClanJoinRequestSchema.safeParse({ ...base, username: null }).success,
|
|
).toBe(true);
|
|
expect(ClanJoinRequestSchema.safeParse(base).success).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("ClanBanSchema", () => {
|
|
const validBan = {
|
|
publicId: "player-1",
|
|
bannedBy: "officer-1",
|
|
reason: "spamming",
|
|
createdAt: "2024-06-01T00:00:00.000Z",
|
|
};
|
|
|
|
it("accepts a valid ban with reason", () => {
|
|
const result = ClanBanSchema.safeParse(validBan);
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it("accepts a ban with null reason", () => {
|
|
const result = ClanBanSchema.safeParse({ ...validBan, reason: null });
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.data.reason).toBeNull();
|
|
}
|
|
});
|
|
|
|
it("rejects a ban with missing reason field", () => {
|
|
const result = ClanBanSchema.safeParse({
|
|
publicId: validBan.publicId,
|
|
bannedBy: validBan.bannedBy,
|
|
createdAt: validBan.createdAt,
|
|
});
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it("rejects a non-ISO string for createdAt", () => {
|
|
const result = ClanBanSchema.safeParse({
|
|
...validBan,
|
|
createdAt: "June 1 2024",
|
|
});
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it("rejects null bannedBy", () => {
|
|
const result = ClanBanSchema.safeParse({ ...validBan, bannedBy: null });
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it("accepts account usernames for both the banned player and the officer", () => {
|
|
const result = ClanBanSchema.safeParse({
|
|
...validBan,
|
|
username: null,
|
|
bannedByUsername: "bigboss",
|
|
});
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.data.username).toBeNull();
|
|
expect(result.data.bannedByUsername).toBe("bigboss");
|
|
}
|
|
});
|
|
|
|
it("accepts a ban without the username fields (older API)", () => {
|
|
expect(ClanBanSchema.safeParse(validBan).success).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("ClanGameResultSchema", () => {
|
|
it.each(["victory", "defeat", "incomplete"])("accepts %s", (value) => {
|
|
expect(ClanGameResultSchema.safeParse(value).success).toBe(true);
|
|
});
|
|
|
|
it("rejects an unknown result value", () => {
|
|
expect(ClanGameResultSchema.safeParse("win").success).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("ClanGameFilterSchema", () => {
|
|
it.each(["ffa", "team", "hvn", "ranked"])("accepts %s", (value) => {
|
|
expect(ClanGameFilterSchema.safeParse(value).success).toBe(true);
|
|
});
|
|
|
|
it("rejects an unknown filter value", () => {
|
|
expect(ClanGameFilterSchema.safeParse("all").success).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("ClanGamePlayerSchema", () => {
|
|
const validPlayer = {
|
|
publicId: "p1",
|
|
username: "alice",
|
|
won: true,
|
|
};
|
|
|
|
it("accepts a valid player", () => {
|
|
expect(ClanGamePlayerSchema.safeParse(validPlayer).success).toBe(true);
|
|
});
|
|
|
|
it("rejects when won is not a boolean", () => {
|
|
expect(
|
|
ClanGamePlayerSchema.safeParse({ ...validPlayer, won: "true" }).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("rejects when required fields are missing", () => {
|
|
expect(ClanGamePlayerSchema.safeParse({ publicId: "p1" }).success).toBe(
|
|
false,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("ClanGameSchema", () => {
|
|
const validGame = {
|
|
gameId: "g1",
|
|
start: "2024-06-01T00:00:00.000Z",
|
|
durationSeconds: 1234,
|
|
map: "World",
|
|
mode: "Team",
|
|
playerTeams: "Duos",
|
|
rankedType: "1v1",
|
|
result: "victory" as const,
|
|
totalPlayers: 8,
|
|
clanPlayers: [{ publicId: "p1", username: "alice", won: true }],
|
|
};
|
|
|
|
it("accepts a fully-populated game", () => {
|
|
expect(ClanGameSchema.safeParse(validGame).success).toBe(true);
|
|
});
|
|
|
|
it("normalizes accidental whitespace around archived map names", () => {
|
|
const result = ClanGameSchema.safeParse({
|
|
...validGame,
|
|
map: "Deglaciated Antarctica ",
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
if (!result.success) return;
|
|
expect(result.data.map).toBe("Deglaciated Antarctica");
|
|
});
|
|
|
|
it("accepts playerTeams: null (FFA / non-team games)", () => {
|
|
const result = ClanGameSchema.safeParse({
|
|
...validGame,
|
|
playerTeams: null,
|
|
});
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it("accepts totalPlayers: null (historical rows)", () => {
|
|
const result = ClanGameSchema.safeParse({
|
|
...validGame,
|
|
totalPlayers: null,
|
|
});
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it("accepts a row with map/mode/rankedType/result omitted", () => {
|
|
const minimal = {
|
|
gameId: validGame.gameId,
|
|
start: validGame.start,
|
|
durationSeconds: validGame.durationSeconds,
|
|
playerTeams: validGame.playerTeams,
|
|
totalPlayers: validGame.totalPlayers,
|
|
clanPlayers: validGame.clanPlayers,
|
|
};
|
|
expect(ClanGameSchema.safeParse(minimal).success).toBe(true);
|
|
});
|
|
|
|
it("rejects a non-ISO start", () => {
|
|
expect(
|
|
ClanGameSchema.safeParse({ ...validGame, start: "June 1 2024" }).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("rejects a negative durationSeconds", () => {
|
|
expect(
|
|
ClanGameSchema.safeParse({ ...validGame, durationSeconds: -1 }).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("rejects a negative totalPlayers", () => {
|
|
expect(
|
|
ClanGameSchema.safeParse({ ...validGame, totalPlayers: -1 }).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("rejects an unknown result value", () => {
|
|
expect(
|
|
ClanGameSchema.safeParse({ ...validGame, result: "win" }).success,
|
|
).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("ClanGamesResponseSchema", () => {
|
|
const validGame = {
|
|
gameId: "g1",
|
|
start: "2024-06-01T00:00:00.000Z",
|
|
durationSeconds: 1234,
|
|
clanPlayers: [{ publicId: "p1", username: "alice", won: true }],
|
|
};
|
|
|
|
it("accepts a non-empty page with an opaque cursor", () => {
|
|
// The cursor is contractually opaque (see ClanGamesResponseSchema
|
|
// comment) — use a non-date token to make that explicit.
|
|
const result = ClanGamesResponseSchema.safeParse({
|
|
results: [validGame],
|
|
nextCursor: "opaque-cursor-abc123",
|
|
});
|
|
expect(result.success).toBe(true);
|
|
if (result.success)
|
|
expect(result.data.nextCursor).toBe("opaque-cursor-abc123");
|
|
});
|
|
|
|
it("accepts an empty page with a null cursor", () => {
|
|
const result = ClanGamesResponseSchema.safeParse({
|
|
results: [],
|
|
nextCursor: null,
|
|
});
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it("rejects when nextCursor is missing (must be string or null)", () => {
|
|
const result = ClanGamesResponseSchema.safeParse({ results: [] });
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it("rejects when results is not an array", () => {
|
|
const result = ClanGamesResponseSchema.safeParse({
|
|
results: "not-an-array",
|
|
nextCursor: null,
|
|
});
|
|
expect(result.success).toBe(false);
|
|
});
|
|
});
|