From 21a811c56b0466d1194ffaaf6f8aeac313f84268 Mon Sep 17 00:00:00 2001 From: "Benjamin P. Jones" Date: Fri, 10 Nov 2023 21:09:25 -0800 Subject: [PATCH] Add ScoreEstimator tests and raise threshold Coverage for the file is 91% and global coverage is now 61%. --- jest.config.ts | 2 +- src/__tests__/ScoreEstimator.test.ts | 71 ++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/jest.config.ts b/jest.config.ts index babeffe1..d68c4e62 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -49,7 +49,7 @@ export default { // An object that configures minimum threshold enforcement for coverage results coverageThreshold: { "global": { - "lines": 55 + "lines": 60 } }, diff --git a/src/__tests__/ScoreEstimator.test.ts b/src/__tests__/ScoreEstimator.test.ts index 20d4493f..b06d3b6a 100644 --- a/src/__tests__/ScoreEstimator.test.ts +++ b/src/__tests__/ScoreEstimator.test.ts @@ -1,4 +1,5 @@ import { GoEngine } from "../GoEngine"; +import { makeMatrix } from "../GoMath"; import { ScoreEstimator, adjust_estimate, @@ -323,4 +324,74 @@ describe("ScoreEstimator", () => { expect(se.getProbablyDead()).toBe("babbcacb"); // expect(se.getProbablyDead()).toBe("cacb"); }); + + test("Falls back to local scorer if remote scorer is not set", async () => { + set_remote_scorer(undefined as any); + const mock_local_scorer = jest.fn(); + mock_local_scorer.mockReturnValue([ + [1, 1, -1, -1], + [1, 1, -1, -1], + ]); + set_local_scorer(mock_local_scorer); + + const se = new ScoreEstimator(undefined, engine, 10, 0.5, true); + await se.when_ready; + + expect(mock_local_scorer).toBeCalled(); + expect(se.ownership).toEqual([ + [1, 0, 0, -1], + [1, 0, 0, -1], + ]); + }); + + test("remote scorers do not need to set score", async () => { + const engine = new GoEngine({ komi: 3.5, width: 4, height: 2, rules: "chinese" }); + engine.place(1, 0); + engine.place(2, 0); + engine.place(1, 1); + engine.place(2, 1); + + set_remote_scorer(async () => ({ + ownership: OWNERSHIP, + })); + + const se = new ScoreEstimator(undefined, engine, 10, 0.5, true); + await se.when_ready; + + expect(se.ownership).toEqual(OWNERSHIP); + expect(se.winner).toBe("Black"); + // I'm not actually sure this is the "right" behavior when the + // remote scorer doesn't return a score. I would think it would + // derive the score from the ownership map. Instead, it assumes + // missing score means zero, and compensates for a komi of 7.5.. + // - bpj + expect(se.amount).toBe(4); + }); + + test("local scorer with stones removed", async () => { + set_local_scorer(estimateScoreVoronoi); + const se = new ScoreEstimator(undefined, engine, 10, 0.5, false); + await se.when_ready; + + se.handleClick(1, 0, false); + se.handleClick(2, 0, false); + expect(se.removal).toEqual([ + [0, 1, 1, 0], + [0, 1, 1, 0], + ]); + + expect(se.ownership).toEqual(makeMatrix(4, 2)); + }); + + test("modkey", async () => { + set_local_scorer(estimateScoreVoronoi); + const se = new ScoreEstimator(undefined, engine, 10, 0.5, false); + await se.when_ready; + + se.handleClick(1, 0, true); + expect(se.removal).toEqual([ + [0, 1, 0, 0], + [0, 0, 0, 0], + ]); + }); });