Skip to content

Commit

Permalink
fix: bug with boost dice. add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jziggas committed Dec 5, 2024
1 parent beb9b0e commit f4d0d48
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ export function parseDiceNotation(input: string): DicePool {
break;
// b/boo = Blue/Boost
case "b":
pool.setBackDice = count;
pool.boostDice = count;
break;
case "boo":
pool.setBackDice = count;
pool.boostDice = count;
break;
// r/c = Red/ Challenge
case "r":
Expand Down Expand Up @@ -115,7 +115,7 @@ export function formatResult(result: any): string {
return parts.join(", ") || "No effects";
}

const main = () => {
export const main = () => {
const input = process.argv.slice(2).join(" ");
if (!input) {
console.log(`Usage: > swrpg-dice 2y 1g 2p 1r
Expand Down
200 changes: 197 additions & 3 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
// tests/cli.test.ts
import { formatResult, parseDiceNotation } from "../src/cli";
import { formatResult, main, parseDiceNotation } from "../src/cli";
import { DicePool } from "../src/types";

jest.mock("../src/dice", () => ({
roll: jest.fn(),
roll: jest.fn(() => ({
summary: {
successes: 1,
failures: 0,
advantages: 0,
threats: 0,
triumphs: 0,
despair: 0,
},
results: [],
})),
}));

describe("CLI", () => {
Expand Down Expand Up @@ -92,6 +102,30 @@ describe("CLI", () => {
challengeDice: 0,
});
});
test("handles invalid dice color", () => {
const input = "2x"; // Invalid color code
const result = parseDiceNotation(input);
expect(result).toEqual({
boostDice: 0,
abilityDice: 0,
proficiencyDice: 0,
setBackDice: 0,
difficultyDice: 0,
challengeDice: 0,
});
});
test("handles invalid number format", () => {
const input = "abc"; // Invalid number
const result = parseDiceNotation(input);
expect(result).toEqual({
boostDice: 0,
abilityDice: 0,
proficiencyDice: 0,
setBackDice: 0,
difficultyDice: 0,
challengeDice: 0,
});
});
});

describe("formatResult", () => {
Expand Down Expand Up @@ -126,5 +160,165 @@ describe("CLI", () => {
const formatted = formatResult(result);
expect(formatted).toBe("No effects");
});

test("formats successes and failures correctly", () => {
const result = {
summary: {
successes: 2,
failures: 1,
advantages: 0,
threats: 0,
triumphs: 0,
despair: 0,
},
};
expect(formatResult(result)).toBe("2 Success(es), 1 Failure(s)");
});
test("formats advantages and threats correctly", () => {
const result = {
summary: {
successes: 0,
failures: 0,
advantages: 3,
threats: 1,
triumphs: 0,
despair: 0,
},
};
expect(formatResult(result)).toBe("3 Advantage(s), 1 Threat(s)");
});
test("formats triumphs and despairs correctly", () => {
const result = {
summary: {
successes: 1,
failures: 0,
advantages: 0,
threats: 0,
triumphs: 2,
despair: 1,
},
};
expect(formatResult(result)).toBe(
"1 Success(es), 2 Triumph(s), 1 Despair(s)",
);
});
test("handles failure with threats", () => {
const result = {
summary: {
successes: 0,
failures: 2,
advantages: 0,
threats: 3,
triumphs: 0,
despair: 0,
},
};
expect(formatResult(result)).toBe("2 Failure(s), 3 Threat(s)");
});
});
});

describe("CLI Functions", () => {
describe("parseDiceNotation", () => {
test("parses valid dice notation", () => {
const input = "2y 1g 2p 1r";
const expected: DicePool = {
proficiencyDice: 2,
abilityDice: 1,
boostDice: 0,
challengeDice: 1,
difficultyDice: 2,
setBackDice: 0,
};
expect(parseDiceNotation(input)).toEqual(expected);
});
test("parses alternative dice notation", () => {
const input = "1pro 1a 1boo 1c 1diff 1sb";
const expected: DicePool = {
proficiencyDice: 1,
abilityDice: 1,
boostDice: 1,
challengeDice: 1,
difficultyDice: 1,
setBackDice: 1,
};
expect(parseDiceNotation(input)).toEqual(expected);
});
test("handles empty input", () => {
const input = "";
const expected: DicePool = {
proficiencyDice: 0,
abilityDice: 0,
boostDice: 0,
challengeDice: 0,
difficultyDice: 0,
setBackDice: 0,
};
expect(parseDiceNotation(input)).toEqual(expected);
});
});
});

describe("Error handling", () => {
test("handles invalid dice notation", () => {
expect(parseDiceNotation("invalid")).toEqual({
boostDice: 0,
abilityDice: 0,
proficiencyDice: 0,
setBackDice: 0,
difficultyDice: 0,
challengeDice: 0,
});
});
test("handles malformed dice numbers", () => {
expect(parseDiceNotation("ayg")).toEqual({
boostDice: 0,
abilityDice: 0,
proficiencyDice: 0,
setBackDice: 0,
difficultyDice: 0,
challengeDice: 0,
});
});
});

describe("CLI main function", () => {
// Mock console.log
const mockConsoleLog = jest
.spyOn(console, "log")
.mockImplementation(() => {});
// Mock process.exit
const mockExit = jest.spyOn(process, "exit").mockImplementation((number) => {
throw new Error("process.exit: " + number);
});
// Store original argv
const originalArgv = process.argv;

beforeEach(() => {
process.argv = ["node", "script.js"]; // Reset to default
});

afterEach(() => {
mockConsoleLog.mockClear();
mockExit.mockClear();
});

afterAll(() => {
process.argv = originalArgv;
mockConsoleLog.mockRestore();
mockExit.mockRestore();
});

test("shows usage when no arguments provided", () => {
expect(() => main()).toThrow("process.exit: 1");
expect(mockConsoleLog).toHaveBeenCalledWith(
expect.stringContaining("Usage:"),
);
});

test("processes valid dice notation", () => {
process.argv = ["node", "script.js", "1y", "2g"];
expect(() => main()).not.toThrow();
expect(mockConsoleLog).toHaveBeenCalled();
});
});
25 changes: 25 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
roll,
createSkillCheck,
createCombatCheck,
createOpposedCheck,
createDifficultyPool,
DicePool,
} from "../src/index";

describe("Index exports", () => {
test("roll function is exported and working", () => {
const pool: DicePool = { abilityDice: 1 };
const result = roll(pool);
expect(result).toBeDefined();
expect(result.results).toBeDefined();
expect(result.summary).toBeDefined();
});

test("pool creation functions are exported and working", () => {
expect(createSkillCheck(1, 1)).toBeDefined();
expect(createCombatCheck(1, 1, 1)).toBeDefined();
expect(createOpposedCheck(1, 1, 1, 1)).toBeDefined();
expect(createDifficultyPool(1, 1)).toBeDefined();
});
});

0 comments on commit f4d0d48

Please sign in to comment.