test(web): specify team progress presentation
This commit is contained in:
1 parent
96bae0ce28
commit
81b951889f
4 files changed
+657
No files matched your search
@@ -0,0 +1,198 @@
|
||||
import type {
|
||||
BracketRoundView,
|
||||
SeriesId,
|
||||
SeriesProjection,
|
||||
TeamTournamentCodeView,
|
||||
} from "@lolympus/contracts";
|
||||
|
||||
export interface TeamSeriesGroup {
|
||||
series: SeriesProjection;
|
||||
codes: TeamTournamentCodeView[];
|
||||
}
|
||||
|
||||
export type TournamentCodeTone =
|
||||
| "pending"
|
||||
| "available"
|
||||
| "expired"
|
||||
| "used";
|
||||
|
||||
export interface TournamentCodePresentation {
|
||||
label: string;
|
||||
tone: TournamentCodeTone;
|
||||
canCopy: boolean;
|
||||
}
|
||||
|
||||
export type TournamentCodeCopyFeedback =
|
||||
| { kind: "success"; message: string }
|
||||
| { kind: "error"; message: string };
|
||||
|
||||
type ClipboardWriter = ((value: string) => Promise<void>) | undefined;
|
||||
|
||||
export function createTeamSeriesGroups(
|
||||
activeSeries: readonly SeriesProjection[],
|
||||
tournamentCodes: readonly TeamTournamentCodeView[],
|
||||
): TeamSeriesGroup[] {
|
||||
const codesBySeriesId = new Map<string, TeamTournamentCodeView[]>();
|
||||
for (const assignment of tournamentCodes) {
|
||||
const seriesId = String(assignment.seriesId);
|
||||
const existing = codesBySeriesId.get(seriesId) ?? [];
|
||||
existing.push(assignment);
|
||||
codesBySeriesId.set(seriesId, existing);
|
||||
}
|
||||
|
||||
return [...activeSeries]
|
||||
.sort(compareSeriesDisplayOrder)
|
||||
.map((series) => ({
|
||||
series,
|
||||
codes: [...(codesBySeriesId.get(String(series.seriesId)) ?? [])].sort(
|
||||
compareTournamentCodes,
|
||||
),
|
||||
}));
|
||||
}
|
||||
|
||||
export function getTeamSeriesDisplayLabel(
|
||||
series: SeriesProjection,
|
||||
rounds: readonly BracketRoundView[],
|
||||
): string {
|
||||
const round = rounds.find(({ seriesIds }) =>
|
||||
seriesIds.some((seriesId) => seriesId === series.seriesId),
|
||||
);
|
||||
const stage = round?.stage ?? series.stage.type;
|
||||
const roundNumber = round?.roundNumber ?? series.stage.roundNumber ?? 1;
|
||||
const baseLabel = isFinalRound(round, rounds)
|
||||
? stage === "upper"
|
||||
? "승자조 결승"
|
||||
: "패자조 결승"
|
||||
: stageLabel(stage, roundNumber);
|
||||
|
||||
if (!round || round.seriesIds.length <= 1) return baseLabel;
|
||||
const matchIndex = round.seriesIds.findIndex(
|
||||
(seriesId) => seriesId === series.seriesId,
|
||||
);
|
||||
return matchIndex >= 0
|
||||
? `${baseLabel} · ${matchIndex + 1}경기`
|
||||
: baseLabel;
|
||||
}
|
||||
|
||||
export function getTournamentCodePresentation(
|
||||
assignment: TeamTournamentCodeView,
|
||||
now = Date.now(),
|
||||
): TournamentCodePresentation {
|
||||
if (assignment.state === "used") {
|
||||
return { label: "사용 완료", tone: "used", canCopy: false };
|
||||
}
|
||||
if (assignment.state === "pending" || !assignment.code) {
|
||||
return { label: "발급 대기", tone: "pending", canCopy: false };
|
||||
}
|
||||
if (isExpired(assignment.expiresAt, now)) {
|
||||
return { label: "만료", tone: "expired", canCopy: false };
|
||||
}
|
||||
return { label: "사용 가능", tone: "available", canCopy: true };
|
||||
}
|
||||
|
||||
export function getTournamentCodeCopyLabel(
|
||||
assignment: TeamTournamentCodeView,
|
||||
seriesLabel: string,
|
||||
): string {
|
||||
return `${seriesLabel} ${assignment.ordinal}세트 토너먼트 코드 복사 (전체 ${assignment.displayNumber}번 게임)`;
|
||||
}
|
||||
|
||||
export async function copyTournamentCode(
|
||||
writeText: ClipboardWriter,
|
||||
code: string,
|
||||
gameLabel: string,
|
||||
): Promise<TournamentCodeCopyFeedback> {
|
||||
try {
|
||||
if (!writeText) throw new Error("Clipboard API unavailable");
|
||||
await writeText(code);
|
||||
return {
|
||||
kind: "success",
|
||||
message: `${gameLabel} 코드를 복사했습니다.`,
|
||||
};
|
||||
} catch {
|
||||
return {
|
||||
kind: "error",
|
||||
message: `${gameLabel} 코드를 복사하지 못했습니다. 코드를 직접 선택해 복사해 주세요.`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function sortCompletedTeamSeries(
|
||||
series: readonly Extract<SeriesProjection, { status: "completed" }>[],
|
||||
): Array<Extract<SeriesProjection, { status: "completed" }>> {
|
||||
return [...series].sort(
|
||||
(left, right) =>
|
||||
timestampValue(right.outcome.completedAt) -
|
||||
timestampValue(left.outcome.completedAt) ||
|
||||
right.stage.displayOrder - left.stage.displayOrder ||
|
||||
String(left.seriesId).localeCompare(String(right.seriesId)),
|
||||
);
|
||||
}
|
||||
|
||||
function compareSeriesDisplayOrder(
|
||||
left: SeriesProjection,
|
||||
right: SeriesProjection,
|
||||
): number {
|
||||
return (
|
||||
left.stage.displayOrder - right.stage.displayOrder ||
|
||||
String(left.seriesId).localeCompare(String(right.seriesId))
|
||||
);
|
||||
}
|
||||
|
||||
function compareTournamentCodes(
|
||||
left: TeamTournamentCodeView,
|
||||
right: TeamTournamentCodeView,
|
||||
): number {
|
||||
return (
|
||||
left.ordinal - right.ordinal ||
|
||||
left.displayNumber - right.displayNumber ||
|
||||
String(left.gameSlotId).localeCompare(String(right.gameSlotId))
|
||||
);
|
||||
}
|
||||
|
||||
function isFinalRound(
|
||||
round: BracketRoundView | undefined,
|
||||
rounds: readonly BracketRoundView[],
|
||||
): boolean {
|
||||
if (!round || (round.stage !== "upper" && round.stage !== "lower")) {
|
||||
return false;
|
||||
}
|
||||
const stageRoundNumbers = rounds
|
||||
.filter(({ stage }) => stage === round.stage)
|
||||
.map(({ roundNumber }) => roundNumber ?? 0);
|
||||
return (
|
||||
stageRoundNumbers.length > 0 &&
|
||||
(round.roundNumber ?? 0) === Math.max(...stageRoundNumbers)
|
||||
);
|
||||
}
|
||||
|
||||
function stageLabel(
|
||||
stage: SeriesProjection["stage"]["type"],
|
||||
roundNumber: number,
|
||||
): string {
|
||||
switch (stage) {
|
||||
case "upper":
|
||||
return `승자조 ${roundNumber}라운드`;
|
||||
case "lower":
|
||||
return `패자조 ${roundNumber}라운드`;
|
||||
case "grand-final":
|
||||
return "그랜드 파이널";
|
||||
case "reset-final":
|
||||
return "리셋 파이널";
|
||||
case "round-robin":
|
||||
return `${roundNumber}라운드`;
|
||||
case "pair":
|
||||
return "공식 대진";
|
||||
}
|
||||
}
|
||||
|
||||
function isExpired(expiresAt: string | null, now: number): boolean {
|
||||
if (!expiresAt) return false;
|
||||
const timestamp = Date.parse(expiresAt);
|
||||
return Number.isFinite(timestamp) && timestamp <= now;
|
||||
}
|
||||
|
||||
function timestampValue(value: string): number {
|
||||
const timestamp = Date.parse(value);
|
||||
return Number.isFinite(timestamp) ? timestamp : Number.NEGATIVE_INFINITY;
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import type {
|
||||
BracketRoundView,
|
||||
CompetitionFactId,
|
||||
GameSlotId,
|
||||
SeriesId,
|
||||
SeriesProjection,
|
||||
TeamId,
|
||||
TeamTournamentCodeView,
|
||||
} from "@lolympus/contracts";
|
||||
import {
|
||||
copyTournamentCode,
|
||||
createTeamSeriesGroups,
|
||||
getTeamSeriesDisplayLabel,
|
||||
getTournamentCodeCopyLabel,
|
||||
getTournamentCodePresentation,
|
||||
sortCompletedTeamSeries,
|
||||
} from "../src/pages/match-team-progress-view.ts";
|
||||
|
||||
const TEAM_A = "team-a" as TeamId;
|
||||
const TEAM_B = "team-b" as TeamId;
|
||||
|
||||
test("groups only matching tournament codes under each active Series", () => {
|
||||
const earlier = readySeries("series-earlier", 1);
|
||||
const later = readySeries("series-later", 2);
|
||||
const groups = createTeamSeriesGroups(
|
||||
[later, earlier],
|
||||
[
|
||||
tournamentCode("later-2", later.seriesId, 2, 20),
|
||||
tournamentCode("orphan", "series-orphan" as SeriesId, 1, 99),
|
||||
tournamentCode("earlier-1", earlier.seriesId, 1, 17),
|
||||
tournamentCode("later-1", later.seriesId, 1, 19),
|
||||
],
|
||||
);
|
||||
|
||||
assert.deepEqual(
|
||||
groups.map(({ series, codes }) => ({
|
||||
seriesId: series.seriesId,
|
||||
gameSlotIds: codes.map(({ gameSlotId }) => gameSlotId),
|
||||
})),
|
||||
[
|
||||
{ seriesId: earlier.seriesId, gameSlotIds: ["earlier-1"] },
|
||||
{ seriesId: later.seriesId, gameSlotIds: ["later-1", "later-2"] },
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
test("uses Korean round names while keeping global game numbers secondary", () => {
|
||||
const opening = readySeries("upper-opening-2", 1, "WB_R1_M2");
|
||||
const final = readySeries("upper-final", 3, "WB_FINAL");
|
||||
const rounds: BracketRoundView[] = [
|
||||
round("upper", 1, 1, ["upper-opening-1", opening.seriesId]),
|
||||
round("upper", 3, 3, [final.seriesId]),
|
||||
];
|
||||
|
||||
assert.equal(
|
||||
getTeamSeriesDisplayLabel(opening, rounds),
|
||||
"승자조 1라운드 · 2경기",
|
||||
);
|
||||
assert.equal(getTeamSeriesDisplayLabel(final, rounds), "승자조 결승");
|
||||
|
||||
const assignment = tournamentCode("final-1", final.seriesId, 1, 19);
|
||||
assert.equal(
|
||||
getTournamentCodeCopyLabel(assignment, "승자조 결승"),
|
||||
"승자조 결승 1세트 토너먼트 코드 복사 (전체 19번 게임)",
|
||||
);
|
||||
});
|
||||
|
||||
test("distinguishes pending, available, expired, and used code states", () => {
|
||||
const now = Date.parse("2026-07-13T12:00:00.000Z");
|
||||
const pending = tournamentCode("pending", "series-a" as SeriesId, 1, 1, {
|
||||
state: "pending",
|
||||
code: null,
|
||||
});
|
||||
const available = tournamentCode(
|
||||
"available",
|
||||
"series-a" as SeriesId,
|
||||
2,
|
||||
2,
|
||||
{ expiresAt: "2026-07-13T13:00:00.000Z" },
|
||||
);
|
||||
const expired = tournamentCode(
|
||||
"expired",
|
||||
"series-a" as SeriesId,
|
||||
3,
|
||||
3,
|
||||
{ expiresAt: "2026-07-13T11:00:00.000Z" },
|
||||
);
|
||||
const used = tournamentCode("used", "series-a" as SeriesId, 4, 4, {
|
||||
state: "used",
|
||||
expiresAt: "2026-07-13T11:00:00.000Z",
|
||||
});
|
||||
|
||||
assert.deepEqual(getTournamentCodePresentation(pending, now), {
|
||||
label: "발급 대기",
|
||||
tone: "pending",
|
||||
canCopy: false,
|
||||
});
|
||||
assert.deepEqual(getTournamentCodePresentation(available, now), {
|
||||
label: "사용 가능",
|
||||
tone: "available",
|
||||
canCopy: true,
|
||||
});
|
||||
assert.deepEqual(getTournamentCodePresentation(expired, now), {
|
||||
label: "만료",
|
||||
tone: "expired",
|
||||
canCopy: false,
|
||||
});
|
||||
assert.deepEqual(getTournamentCodePresentation(used, now), {
|
||||
label: "사용 완료",
|
||||
tone: "used",
|
||||
canCopy: false,
|
||||
});
|
||||
});
|
||||
|
||||
test("returns explicit game-specific clipboard success and failure feedback", async () => {
|
||||
const writes: string[] = [];
|
||||
const success = await copyTournamentCode(
|
||||
async (value) => {
|
||||
writes.push(value);
|
||||
},
|
||||
"KR-CODE-19",
|
||||
"승자조 결승 1세트",
|
||||
);
|
||||
const failure = await copyTournamentCode(
|
||||
async () => {
|
||||
throw new Error("clipboard denied");
|
||||
},
|
||||
"KR-CODE-20",
|
||||
"승자조 결승 2세트",
|
||||
);
|
||||
|
||||
assert.deepEqual(writes, ["KR-CODE-19"]);
|
||||
assert.deepEqual(success, {
|
||||
kind: "success",
|
||||
message: "승자조 결승 1세트 코드를 복사했습니다.",
|
||||
});
|
||||
assert.deepEqual(failure, {
|
||||
kind: "error",
|
||||
message:
|
||||
"승자조 결승 2세트 코드를 복사하지 못했습니다. 코드를 직접 선택해 복사해 주세요.",
|
||||
});
|
||||
});
|
||||
|
||||
test("sorts completed Series by most recent decision time", () => {
|
||||
const older = completedSeries("older", "2026-07-12T10:00:00.000Z", 1);
|
||||
const newer = completedSeries("newer", "2026-07-13T10:00:00.000Z", 2);
|
||||
|
||||
assert.deepEqual(
|
||||
sortCompletedTeamSeries([older, newer]).map(({ seriesId }) => seriesId),
|
||||
[newer.seriesId, older.seriesId],
|
||||
);
|
||||
});
|
||||
|
||||
function readySeries(
|
||||
id: string,
|
||||
displayOrder: number,
|
||||
code = id.toUpperCase(),
|
||||
): SeriesProjection {
|
||||
return {
|
||||
seriesId: id as SeriesId,
|
||||
code,
|
||||
stage: { type: "upper", roundNumber: displayOrder, displayOrder },
|
||||
rule: { type: "first-to", winsRequired: 2, maxGames: 3 },
|
||||
status: "ready",
|
||||
entries: [
|
||||
{ slot: "A", teamId: TEAM_A, gameWins: 0 },
|
||||
{ slot: "B", teamId: TEAM_B, gameWins: 0 },
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
function completedSeries(
|
||||
id: string,
|
||||
completedAt: string,
|
||||
displayOrder: number,
|
||||
): Extract<SeriesProjection, { status: "completed" }> {
|
||||
return {
|
||||
...readySeries(id, displayOrder),
|
||||
status: "completed",
|
||||
entries: [
|
||||
{ slot: "A", teamId: TEAM_A, gameWins: 0 },
|
||||
{ slot: "B", teamId: TEAM_B, gameWins: 0 },
|
||||
],
|
||||
outcome: {
|
||||
type: "walkover",
|
||||
winnerTeamId: TEAM_A,
|
||||
loserTeamId: TEAM_B,
|
||||
decisionFactId: `${id}-fact` as CompetitionFactId,
|
||||
completedAt,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function tournamentCode(
|
||||
id: string,
|
||||
seriesId: SeriesId,
|
||||
ordinal: number,
|
||||
displayNumber: number,
|
||||
overrides: Partial<TeamTournamentCodeView> = {},
|
||||
): TeamTournamentCodeView {
|
||||
return {
|
||||
gameSlotId: id as GameSlotId,
|
||||
seriesId,
|
||||
ordinal,
|
||||
displayNumber,
|
||||
state: "reserved",
|
||||
code: `KR-${id}`,
|
||||
expiresAt: null,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function round(
|
||||
stage: BracketRoundView["stage"],
|
||||
roundNumber: number,
|
||||
displayOrder: number,
|
||||
seriesIds: Array<string | SeriesId>,
|
||||
): BracketRoundView {
|
||||
return {
|
||||
stage,
|
||||
roundNumber,
|
||||
displayOrder,
|
||||
label: `${stage}-${roundNumber}`,
|
||||
seriesIds: seriesIds.map((id) => id as SeriesId),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user