Files
OpenFrontIO/tests/client/graphics/layers/PlayerPanelKick.test.ts
Ryan 1049b7e7dc Clan System Part 1 (#3276)
## Description:

Properly split out clantags and usernames, a clantag should not be part
of a username.

<img width="285" height="286" alt="image"
src="https://github.com/user-attachments/assets/8ac56e82-b12c-4fc0-9774-e445252a6e61"
/>

https://api.openfront.dev/game/ojkqZFb2


<img width="296" height="596" alt="image"
src="https://github.com/user-attachments/assets/85152f80-c111-4f87-b85b-8516c9c6137b"
/>


https://api.openfront.dev/game/MF32BkVc


requires;
https://github.com/openfrontio/infra/pull/264

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

w.o.n
2026-03-17 15:55:47 -07:00

175 lines
5.6 KiB
TypeScript

vi.mock("lit", () => ({
html: (strings: TemplateStringsArray, ...values: unknown[]) => ({
strings,
values,
}),
LitElement: class extends EventTarget {
requestUpdate() {}
},
}));
vi.mock("lit/decorators.js", () => ({
customElement: () => (clazz: unknown) => clazz,
state: () => () => {},
property: () => () => {},
query: () => () => {},
}));
vi.mock("../../../../src/client/Utils", () => ({
translateText: vi.fn((key: string) => key),
renderDuration: vi.fn(),
renderNumber: vi.fn(),
renderTroops: vi.fn(),
}));
vi.mock("../../../../src/client/components/ui/ActionButton", () => ({
actionButton: vi.fn((props: unknown) => props),
}));
import { actionButton } from "../../../../src/client/components/ui/ActionButton";
import { PlayerModerationModal } from "../../../../src/client/graphics/layers/PlayerModerationModal";
import { PlayerPanel } from "../../../../src/client/graphics/layers/PlayerPanel";
import { SendKickPlayerIntentEvent } from "../../../../src/client/Transport";
import { PlayerType } from "../../../../src/core/game/Game";
import { PlayerView } from "../../../../src/core/game/GameView";
describe("PlayerPanel - kick player moderation", () => {
let panel: PlayerPanel;
const originalConfirm = globalThis.confirm;
beforeEach(() => {
panel = new PlayerPanel();
(panel as any).requestUpdate = vi.fn();
(panel as any).isVisible = true;
});
afterEach(() => {
vi.clearAllMocks();
globalThis.confirm = originalConfirm;
});
test("renders moderation action only when allowed or already kicked", () => {
const my = { isLobbyCreator: () => true } as unknown as PlayerView;
const other = {
id: () => 2,
name: () => "Other",
displayName: () => "[TAG] Other",
type: () => PlayerType.Human,
clientID: () => "client-2",
} as unknown as PlayerView;
(actionButton as unknown as ReturnType<typeof vi.fn>).mockClear();
(panel as any).renderModeration(my, other);
expect(actionButton).toHaveBeenCalledTimes(1);
expect(
(actionButton as unknown as ReturnType<typeof vi.fn>).mock.calls[0][0],
).toMatchObject({
label: "player_panel.moderation",
title: "player_panel.moderation",
type: "red",
});
(actionButton as unknown as ReturnType<typeof vi.fn>).mockClear();
(panel as any).kickedPlayerIDs.add("2");
(panel as any).renderModeration(my, other);
expect(actionButton).toHaveBeenCalledTimes(1);
const notCreator = { isLobbyCreator: () => false } as unknown as PlayerView;
(actionButton as unknown as ReturnType<typeof vi.fn>).mockClear();
(panel as any).kickedPlayerIDs.clear();
(panel as any).renderModeration(notCreator, other);
expect(actionButton).not.toHaveBeenCalled();
});
test("opens moderation modal and hides after a kick", () => {
const other = {
id: () => 2,
name: () => "Other",
displayName: () => "[TAG] Other",
type: () => PlayerType.Human,
clientID: () => "client-2",
} as unknown as PlayerView;
(panel as any).openModeration({ stopPropagation: vi.fn() }, other);
expect((panel as any).moderationTarget).toBe(other);
expect((panel as any).suppressNextHide).toBe(true);
(panel as any).handleModerationKicked(
new CustomEvent("kicked", { detail: { playerId: "2" } }),
);
expect((panel as any).kickedPlayerIDs.has("2")).toBe(true);
expect((panel as any).moderationTarget).toBe(null);
expect((panel as any).isVisible).toBe(false);
});
});
describe("PlayerModerationModal - kick confirmation", () => {
const originalConfirm = globalThis.confirm;
afterEach(() => {
vi.clearAllMocks();
globalThis.confirm = originalConfirm;
});
test("emits SendKickPlayerIntentEvent and dispatches kicked when confirmed", () => {
(globalThis as any).confirm = vi.fn(() => true);
const modal = new PlayerModerationModal();
const eventBus = { emit: vi.fn() };
const my = { isLobbyCreator: () => true } as unknown as PlayerView;
const other = {
id: () => 2,
name: () => "Other",
displayName: () => "[TAG] Other",
type: () => PlayerType.Human,
clientID: () => "client-2",
} as unknown as PlayerView;
modal.eventBus = eventBus as any;
modal.myPlayer = my;
modal.target = other;
const kickedListener = vi.fn();
modal.addEventListener("kicked", kickedListener as any);
(modal as any).handleKickClick({ stopPropagation: vi.fn() });
expect(eventBus.emit).toHaveBeenCalledTimes(1);
const event = eventBus.emit.mock.calls[0][0] as SendKickPlayerIntentEvent;
expect(event).toBeInstanceOf(SendKickPlayerIntentEvent);
expect(event.target).toBe("client-2");
expect(kickedListener).toHaveBeenCalledTimes(1);
const kickedEvent = kickedListener.mock.calls[0][0] as CustomEvent;
expect(kickedEvent.detail).toEqual({ playerId: "2" });
});
test("does not emit when confirmation is cancelled", () => {
(globalThis as any).confirm = vi.fn(() => false);
const modal = new PlayerModerationModal();
const eventBus = { emit: vi.fn() };
const my = { isLobbyCreator: () => true } as unknown as PlayerView;
const other = {
id: () => 2,
name: () => "Other",
displayName: () => "[TAG] Other",
type: () => PlayerType.Human,
clientID: () => "client-2",
} as unknown as PlayerView;
modal.eventBus = eventBus as any;
modal.myPlayer = my;
modal.target = other;
const kickedListener = vi.fn();
modal.addEventListener("kicked", kickedListener as any);
(modal as any).handleKickClick({ stopPropagation: vi.fn() });
expect(eventBus.emit).not.toHaveBeenCalled();
expect(kickedListener).not.toHaveBeenCalled();
});
});