Files
tetrig/server/test/rules.test.js
tkrmagid 8fcef8e720 서버: Cloudflare Workers 멀티플레이 백엔드 (Phase 4~6 서버 측)
- 게스트/이메일/Google 계정, 게스트 기록 이전, 닉네임 정책
- GameRoomDO: 커스텀 1:1·FFA, 가비지 중계·배율·장기전, 타겟팅, KO, 재접속 유예, 리매치 투표
- MatchmakerDO + Glicko-2 + TR/18랭크 + 리그 결과 기록
- 리더보드·솔로 기록·시즌/RD 감쇠 Cron
- 단위 테스트 17개 + 종단 테스트 스크립트

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 09:27:15 +09:00

84 lines
3.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test } from "node:test";
import assert from "node:assert/strict";
import { attackFor, overtimeMultiplier, trFor, rankFor, leagueFt, randomRoomCode, isRoomCode, randomGuestName, looksLikeGuestName, sanitizeRules } from "../src/rules.js";
import { glickoUpdate } from "../src/glicko.js";
import { findPairs, compatible } from "../src/matchmaking.js";
test("가비지 전송 테이블 (§6)", () => {
assert.equal(attackFor({ lines: 1 }), 0);
assert.equal(attackFor({ lines: 2 }), 1);
assert.equal(attackFor({ lines: 3 }), 2);
assert.equal(attackFor({ lines: 4, difficult: true, b2b: 0 }), 4);
assert.equal(attackFor({ lines: 4, difficult: true, b2b: 1 }), 5); // B2B +1
assert.equal(attackFor({ lines: 1, tspin: "mini" }), 1);
assert.equal(attackFor({ lines: 0, tspin: "mini" }), 0);
assert.equal(attackFor({ lines: 2, tspin: "tspin", difficult: true, b2b: 0 }), 4);
assert.equal(attackFor({ lines: 3, tspin: "tspin", difficult: true, b2b: 0 }), 6);
assert.equal(attackFor({ lines: 1, combo: 1 }), 1); // 콤보 2번째 → +1
assert.equal(attackFor({ lines: 1, combo: 20 }), 5); // 이후 5 고정
assert.equal(attackFor({ lines: 4, allClear: true }), 9);
});
test("장기전 배율", () => {
assert.equal(overtimeMultiplier(89_999), 1);
assert.equal(overtimeMultiplier(90_000), 1.25);
assert.equal(overtimeMultiplier(120_000), 1.5);
assert.equal(overtimeMultiplier(200_000, false), 1);
});
test("TR·랭크·FT", () => {
assert.ok(trFor(1500, 350) < 1000);
assert.ok(trFor(2500, 60) > 24000);
assert.equal(rankFor(0), "D");
assert.equal(rankFor(7800), "A+");
assert.equal(rankFor(9199), "A+");
assert.equal(rankFor(9200), "S-");
assert.equal(rankFor(25000), "X+");
assert.equal(leagueFt(7000, 9199), 3);
assert.equal(leagueFt(1000, 9200), 5);
});
test("방 코드·게스트명·규칙", () => {
for (let i = 0; i < 200; i++) {
const c = randomRoomCode();
assert.ok(isRoomCode(c), c);
assert.ok(!/[I1O0]/.test(c));
}
const n = randomGuestName();
assert.match(n, /\d{3}$/);
assert.ok(looksLikeGuestName(n));
assert.ok(!looksLikeGuestName("홍길동"));
assert.deepEqual(sanitizeRules({ garbageMul: 9, ft: 7, sameBag: true }), { garbageMul: 2, ft: 0, sameBag: true, overtime: true });
});
test("Glicko-2: Glickman 논문 예제", () => {
const p = glickoUpdate({ r: 1500, rd: 200, vol: 0.06 }, [
{ r: 1400, rd: 30, score: 1 },
{ r: 1550, rd: 100, score: 0 },
{ r: 1700, rd: 300, score: 0 },
]);
assert.ok(Math.abs(p.r - 1464.06) < 0.05, String(p.r));
assert.ok(Math.abs(p.rd - 151.52) < 0.05, String(p.rd));
assert.ok(Math.abs(p.vol - 0.05999) < 0.0001, String(p.vol));
});
test("Glicko-2: 승자는 오르고 패자는 내려간다, RD 하한 58", () => {
const w = glickoUpdate({ r: 1500, rd: 350, vol: 0.06 }, [{ r: 1500, rd: 350, score: 1 }]);
const l = glickoUpdate({ r: 1500, rd: 350, vol: 0.06 }, [{ r: 1500, rd: 350, score: 0 }]);
assert.ok(w.r > 1500 && l.r < 1500);
const tight = glickoUpdate({ r: 1500, rd: 58, vol: 0.06 }, [{ r: 1500, rd: 58, score: 1 }]);
assert.ok(tight.rd >= 58);
});
test("매치메이킹: R±2RD 겹침, 대기 시간에 따라 확장", () => {
const now = 1_000_000;
const a = { uid: "a", r: 1500, rd: 50, joinedAt: now };
const b = { uid: "b", r: 1750, rd: 50, joinedAt: now };
assert.equal(compatible(a, b, now), false); // 250 > 100+100
assert.equal(compatible({ ...a, joinedAt: now - 60_000 }, { ...b, joinedAt: now - 60_000 }, now), true); // ×1.44
const c = { uid: "c", r: 1520, rd: 50, joinedAt: now };
const pairs = findPairs([b, a, c], now);
assert.equal(pairs.length, 1);
assert.deepEqual(pairs[0].map((x) => x.uid).sort(), ["a", "c"]);
});