Skip to content
Merged
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
20 changes: 20 additions & 0 deletions lib/deck/io/__tests__/intake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,26 @@ describe("intakeDecklist — replace mode", () => {
expect(result.updated).toBe(0);
});

it("aborts without wiping the deck when the paste fails to parse (issue #87)", async () => {
// Guard short-circuits before the deck is read or mutated, so no
// deckCard.findMany / applyChanges mocks are queued here on purpose.
const result = await intakeDecklist({
deckId: "deck-1",
userId: "user-1",
// Detected as JSON (leading "{") but not valid JSON → zero parsed cards.
text: "{ not valid json",
mode: "replace",
});

expect(mockApplyChanges).not.toHaveBeenCalled();
expect(mockDeckCardFindMany).not.toHaveBeenCalled();
expect(result.applied).toBe(0);
expect(result.removed).toBe(0);
expect(result.warnings).toContain(
"Decklist looks like JSON but could not be parsed",
);
});

it("re-throws non-InvariantViolation errors from applyChanges", async () => {
mockCardFindMany.mockResolvedValueOnce([
{ id: 1, name: "Lightning Bolt" },
Expand Down
9 changes: 9 additions & 0 deletions lib/deck/io/intake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ export async function intakeDecklist(input: IntakeInput): Promise<IntakeResult>
const warnings = [...resolved.warnings];
const unmatchedNames = resolved.unmatched.map((c) => c.name);

// Replace mode diffs the parsed list against the entire deck, so a parse
// that yielded nothing would delete every card. A parse failure produces
// zero parsed cards *with* warnings — distinct from a legitimately empty
// paste (zero cards, no warnings), which may intentionally clear the deck.
// Abort with a warning-only result rather than silently wiping the deck.
if (mode === "replace" && parsed.cards.length === 0 && warnings.length > 0) {
return { applied: 0, added: 0, removed: 0, updated: 0, unmatchedNames, warnings };
}

const changes =
mode === "append" ? asAdds(resolved) : await buildReplaceChanges(deckId, resolved);

Expand Down
Loading