Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions client/src/viewmodel/__tests__/costLabel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,28 @@ describe("formatAbilityCost", () => {
})).toBe("{1} or Pay 2 life");
});
});

describe("additionalCostChoices — Choice with a malformed (short) engine payload", () => {
const manaCost = {
type: "Mana",
cost: { type: "Cost", shards: [], generic: 2 },
};

it("labels both options for a well-formed two-cost Choice", () => {
const cost = { type: "Choice", data: [manaCost, manaCost] } as AdditionalCost;
const { options } = additionalCostChoices(cost);
expect(options.find((o) => o.id === "pay")!.label).toBeTruthy();
expect(options.find((o) => o.id === "decline")!.label).toBeTruthy();
});

it("does not throw when a Choice payload is missing its second cost", () => {
// The tuple type promises two costs, but `data` is deserialized from the
// engine — a short array must degrade to a fallback label, not crash.
const cost = { type: "Choice", data: [manaCost] } as unknown as AdditionalCost;
expect(() => additionalCostChoices(cost)).not.toThrow();
const decline = additionalCostChoices(cost).options.find(
(o) => o.id === "decline",
)!;
expect(decline.label).toBe("Decline");
});
});
12 changes: 9 additions & 3 deletions client/src/viewmodel/costLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,20 @@ export function additionalCostChoices(
{ id: "decline", label: "Cancel" },
],
};
case "Choice":
case "Choice": {
// `data` is typed as a 2-tuple, but it is deserialized from the engine, so
// a malformed/short payload leaves an element `undefined`. formatAbilityCost
// switches on `cost.type`, so passing `undefined` throws — guard each option
// (mirroring the `Kicker` branch's `first ? … : …` defense).
const [pay, decline] = cost.data;
return {
title: "Choose additional cost",
options: [
{ id: "pay", label: formatAbilityCost(cost.data[0]) },
{ id: "decline", label: formatAbilityCost(cost.data[1]) },
{ id: "pay", label: pay ? formatAbilityCost(pay) : "Pay" },
{ id: "decline", label: decline ? formatAbilityCost(decline) : "Decline" },
],
};
}
}
}

Expand Down
Loading