Files
joke-app/packages/games/src/bots/bots.test.ts
EJClaw 84d64b27f2 컴퓨터 난이도 1~60단계·방장 선택·판마다 변화·운, 공정성 확인에 표기, 업데이트 알림 팝업
- 난이도 1~60(쉬움 1~20·보통 21~40·어려움 41~60): 넣을 때 랜덤, 방장이 대기실에서 [랜덤][쉬움][보통][어려움] 선택(botLevel) → 범위 안 무작위
- 다음 판마다 -2~+2, 처음 단계 ±5·같은 범위 안(drift); 말투는 처음 단계(>30 존댓말)
- humanBest 확률을 단계별로 보간(BOT_PLAY_ANCHORS: 1·10·30·50·60)
- 운(-1~+1): 카드·주사위 게임에서 시작 전에 시드 후보 1~5개를 작업 스레드에서 끝까지 둬 보고 운에 맞는 것을 고름(luck.ts, luck-worker.ts). 해시 공개는 그대로
- 결과 화면 공정성 확인에 컴퓨터별 단계·운, 섞어 본 후보 수
- 업데이트 알림: 빌드 ID meta + /api/version + welcome.build → 상단 팝업 [지금 새로고침하기] [✕]
- 문서 03·04 §5.1·06 §12·07 §11.1·09 §9.1, 테스트(bot-level.room, luck, version, e2e bots·pwa)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 17:46:13 +09:00

63 lines
2.7 KiB
TypeScript

/** Every bot-enabled game: bots play only accepted actions, react in every window and never stall a game. */
import { describe, expect, test } from 'bun:test';
import { seedN, type AnyGameDefinition } from '@bg/engine';
import { GAMES } from '../index';
import { BOT_STRATEGIES, decideBotAction } from './index';
import { botPlayout } from './playout';
const isBot = (p: string) => p.startsWith('bot:');
const seats = (n: number, humans: number) => Array.from({ length: n }, (_, i) => (i < humans ? `human${i}` : `bot:${i}:x`));
function counts(def: AnyGameDefinition): number[] {
const range = def.playersFor?.(def.defaultOptions) ?? { min: def.minPlayers, max: def.maxPlayers };
const out = new Set([Math.max(2, range.min), Math.min(range.max, 4)]);
return [...out].filter((n) => !def.playerCountError?.(def.defaultOptions, n));
}
describe.each(Object.keys(BOT_STRATEGIES))('%s', (id) => {
const def = GAMES[id]!;
test('has a game definition with legalActions', () => {
expect(def).toBeDefined();
expect(typeof def.legalActions).toBe('function');
});
for (const n of counts(def)) {
test(`${n} players: one human who only times out, the rest bots — the game finishes`, () => {
for (let s = 1; s <= 3; s++) {
const r = botPlayout(def, { players: seats(n, 1), seed: seedN(s), isBot, maxSteps: 20_000 });
expect(r.finished).toBe(true);
expect(r.botTimeouts).toBe(0);
expect(r.maxBotMs).toBeLessThan(250);
}
});
}
test('bots only: a whole game finishes without timeouts', () => {
const n = counts(def).at(-1)!;
for (let s = 1; s <= 2; s++) {
const r = botPlayout(def, { players: seats(n, 0), seed: seedN(100 + s), isBot, maxSteps: 30_000 });
expect(r.finished).toBe(true);
expect(r.botTimeouts).toBe(0);
}
});
});
test('decideBotAction returns null when the bot has nothing to do', () => {
const def = GAMES.omok!;
const s = def.setup({ players: ['a', 'bot:1:x'], options: def.defaultOptions, rng: { next: () => 0.5, int: () => 0 } as never, now: 0 });
const waiting = def.activePlayers(s)[0]!;
const other = waiting === 'a' ? 'bot:1:x' : 'a';
expect(decideBotAction(def, s, other, { now: 0 })).toBeNull();
expect(decideBotAction(def, s, waiting, { now: 0 })).not.toBeNull();
});
describe('levels (docs/09 §9.1)', () => {
const BOARD = ['omok', 'chess', 'janggi', 'othello', 'baduk'];
test.each(BOARD)('%s: the weakest and strongest computers still finish a game against each other', (id) => {
const def = GAMES[id]!;
for (const level of [1, 60]) {
const r = botPlayout(def, { players: seats(2, 0), seed: seedN(7), isBot, maxSteps: 30_000, level });
expect(r.finished).toBe(true);
expect(r.botTimeouts).toBe(0);
}
});
});