rewrite: TS monorepo — pi SDK stateless agents, Postgres+pg-boss, SvelteKit themed UI; legacy moved to legacy/
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { GameState, renderTurnContext, DEFAULT_FLAGS, rateGame, DEFAULT_RATING, classifyMove, humanTranscript } from "../src/index.ts";
|
||||
|
||||
const DEMO = "e4 e5 Nf3 Nc6 Bc4 Bc5 c3 Nf6 d3 d6 O-O a6 a4 O-O h3 Ba7 Re1 h6 Nbd2 Re8 Nf1 Be6 Bxe6 Rxe6 Ng3 d5 exd5 Qxd5 Be3 Rd8 Qc2 Bxe3 Rxe3 Qd7 Rae1 Rde8 b4 Nd5 R3e2 Nf4 Re4 Nxh3+ gxh3".split(" ");
|
||||
|
||||
function play(moves: string[]) {
|
||||
const g = new GameState();
|
||||
for (const m of moves) {
|
||||
const r = g.tryMove(m);
|
||||
if (!r.ok) throw new Error(`${m}: ${r.message}`);
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
describe("GameState", () => {
|
||||
it("replays the demo game and renders context", () => {
|
||||
const g = play(DEMO);
|
||||
expect(g.fen()).toBe("4r1k1/1ppq1pp1/p1n1r2p/4p3/PP2R3/2PP1NNP/2Q2P2/4R1K1 b - - 0 22");
|
||||
const ctx = renderTurnContext({
|
||||
state: g,
|
||||
me: { side: "black" },
|
||||
opponent: { name: "GPT-6.1 Sol" },
|
||||
opponentPhrase: "Любопытная идея.",
|
||||
drawOffered: false,
|
||||
noteFromSelf: "атака по g-линии",
|
||||
flags: { ...DEFAULT_FLAGS, notes: true },
|
||||
});
|
||||
expect(ctx).toContain("22. gxh3 — пешка g2→h3, взял коня");
|
||||
expect(ctx).toContain("атака по g-линии");
|
||||
expect(ctx).toContain("у белых +2");
|
||||
expect(ctx).not.toContain("рокировка"); // nothing castle-able for black here
|
||||
});
|
||||
|
||||
it("rejects illegal moves with an explanation", () => {
|
||||
const g = play(DEMO);
|
||||
const r = g.tryMove("Qxh3");
|
||||
expect(r.ok).toBe(false);
|
||||
if (!r.ok) expect(r.message).toMatch(/ферзь/);
|
||||
const r2 = g.tryMove("d7h3");
|
||||
expect(r2.ok).toBe(false);
|
||||
expect(g.tryMove("Rg6").ok).toBe(true);
|
||||
});
|
||||
|
||||
it("accepts sloppy input", () => {
|
||||
const g = new GameState();
|
||||
expect(g.tryMove("e2e4").ok).toBe(true);
|
||||
expect(g.tryMove("e7-e5").ok).toBe(true);
|
||||
expect(g.tryMove("Ng1f3").ok || g.tryMove("Nf3").ok).toBe(true);
|
||||
});
|
||||
|
||||
it("castling notes only when the position looks castle-able", () => {
|
||||
const g = play(["e4", "e5", "Nf3", "Nc6", "Bc4", "Bc5"]);
|
||||
expect(g.castlingNotes("white")).toHaveLength(1);
|
||||
expect(g.castlingNotes("white")[0]).toMatch(/правами разрешена/);
|
||||
// king walks and comes back
|
||||
const h = play(["e4", "e5", "Nf3", "Nc6", "Bc4", "Bc5", "Ke2", "Nf6", "Ke1", "Ng8"]);
|
||||
expect(h.castlingNotes("white")[0]).toMatch(/НЕВОЗМОЖНА/);
|
||||
// KB*R -> nothing
|
||||
const s = new GameState();
|
||||
expect(s.castlingNotes("white")).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("detects repetitions and the moves that would draw", () => {
|
||||
const g = play(["Nf3", "Nf6", "Ng1", "Ng8", "Nf3", "Nf6", "Ng1"]);
|
||||
const info = g.repetitionInfo();
|
||||
expect(info.anyRepeated).toBe(true);
|
||||
expect(info.drawingMoves).toContain("Ng8");
|
||||
expect(g.tryMove("Ng8").ok).toBe(true);
|
||||
expect(g.outcome()?.termination).toBe("threefold_repetition");
|
||||
});
|
||||
|
||||
it("en passant note", () => {
|
||||
const g = play(["e4", "a6", "e5", "d5"]);
|
||||
expect(g.enPassantNote()).toMatch(/exd6/);
|
||||
});
|
||||
|
||||
it("fifty-move reminder", () => {
|
||||
const g = new GameState("8/8/8/4k3/8/8/4K3/7R w - - 85 120");
|
||||
expect(g.fiftyMoveNote(40)).toMatch(/42 из 50/);
|
||||
});
|
||||
|
||||
it("cheating relocations", () => {
|
||||
const g = new GameState();
|
||||
const r = g.applyIllegal("d1d7");
|
||||
expect(r.ok).toBe(true);
|
||||
expect(g.turn).toBe("black");
|
||||
const r2 = g.applyIllegal("e8e2");
|
||||
expect(r2.ok).toBe(true);
|
||||
expect(g.applyIllegal("d7e8").ok).toBe(true);
|
||||
expect(g.outcome()).toBeNull();
|
||||
const k = new GameState("4k3/8/8/8/8/8/8/3QK3 w - - 0 1");
|
||||
k.applyIllegal("d1e8");
|
||||
expect(k.outcome()?.termination).toBe("king_captured");
|
||||
});
|
||||
|
||||
it("transcript", () => {
|
||||
const g = play(["e4", "e5"]);
|
||||
const t = humanTranscript({ white: "A", black: "B", plies: g.plies.map((p) => ({ ...p, phrase: "привет" })) });
|
||||
expect(t).toContain("1. e4");
|
||||
expect(t).toContain("A: «привет»");
|
||||
});
|
||||
});
|
||||
|
||||
describe("rating & analysis", () => {
|
||||
it("glicko moves ratings in the right direction", () => {
|
||||
const r = rateGame(DEFAULT_RATING, DEFAULT_RATING, "1-0");
|
||||
expect(r.white.r).toBeGreaterThan(1500);
|
||||
expect(r.black.r).toBeLessThan(1500);
|
||||
});
|
||||
it("classifies a blunder", () => {
|
||||
const c = classifyMove({ side: "white", before: { cp: 50, mate: null, bestUci: "e2e4", pv: [], depth: 18 }, after: { cp: -400, mate: null, bestUci: null, pv: [], depth: 18 }, uci: "a2a3", legalMoveCount: 20 });
|
||||
expect(c.cls).toBe("blunder");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user