Add cart set/update/remove with bulk patch + fix cart clear (#7)#8
Open
axelpaul wants to merge 2 commits into
Open
Add cart set/update/remove with bulk patch + fix cart clear (#7)#8axelpaul wants to merge 2 commits into
axelpaul wants to merge 2 commits into
Conversation
Exposes safe per-line cart edits so agents can correct individual items
without rebuilding the whole cart. Adds:
- cart set '<json>' replace whole cart (array or {sku:qty} map)
- cart update <sku|id> --quantity N single-line update; 0 removes
- cart update '<json-patch>' bulk patch: listed SKUs set/removed,
untouched SKUs preserved, new SKUs added
- cart remove <sku|id> shorthand for --quantity 0
- --dry-run on set/update/remove/clear prints the planned cart without
hitting the API
Substitution is preserved from existing lines unless overridden in the
patch. Single-line update refuses negatives, ambiguous matches, and
unknown identifiers with clear errors.
POST /checkout/lines/ with {lines: [], replace: true} is silently a
no-op against the live Krónan API — it returns the unchanged checkout
instead of clearing it. The CLI was printing "Cart cleared." regardless,
because it never inspected the response.
Work around the API by detecting the empty-replace case inside
replaceCheckoutLines: fetch the current lines and resend them with
quantity: 0, which the backend honours. This transparently fixes:
- cart clear
- cart update <only-sku> --quantity 0
- cart remove <only-sku>
- cart set '[]'
- cart update '{"<sku>":0,...}' patches that zero out every line
Also harden cartClearCommand to throw if the API response still contains
lines, so silent regressions surface instead of being reported as success.
Verified live: add → clear leaves cart empty; clear-when-empty is a no-op;
bulk patch that zeroes the last line also empties the cart.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The CLI previously only supported
cart,cart add, andcart clear. There was no safe way to correct a single bad item — agents had to nuke the whole cart and rebuild. And as reported in #7,cart clearitself was actually broken.This PR adds:
cart set '<json>'— replace the whole cart with JSON lines (accepts either an array of{sku,quantity,substitution?}or an object map{sku:qty})cart update <sku-or-line-id> --quantity N— single-line update;--quantity 0removescart update '<json-patch>'— bulk patch: listed SKUs are set to the given qty (0 removes), unlisted lines are untouched, new SKUs are added when qty > 0cart remove <sku-or-line-id>— shorthand for--quantity 0--dry-runonset,update,remove,clear— prints the planned cart and skips the API callAnd fixes:
cart clear(closes "kronan cart clear" ekki að virka #7) —POST /checkout/lines/with{lines: [], replace: true}is silently a no-op against the live Krónan API; the CLI was printing "Cart cleared." without checking the response. Worked around by detecting the empty-replace case insidereplaceCheckoutLinesand resending the existing lines withquantity: 0, which the backend honours.cartClearCommandnow also throws if the response still contains lines, so silent regressions surface.The workaround applies transparently to every code path that produces an empty cart (
cart clear,cart set '[]',cart update/removeof the last remaining line, bulk patches that zero out the cart).Implementation notes
substitution: true.POST /checkout/lines/withreplace: true(the documented default).Test plan
bun run check(lint + typecheck + 27 tests) passesparseCartLinesJsoncover both shapes, negatives, non-integers, missing SKU, non-number values, scalar JSONhelpand bad invocations error cleanlycart add→cart clear→ cart actually empty (was the bug in "kronan cart clear" ekki að virka #7)cart clearon an already-empty cart → no-op, prints successcart update <sku> --quantity 3→ quantity changes, other lines preservedcart update <line-id> --quantity 0→ line removedcart update '{"<sku>":2,"<other>":0,"NEW":1}'→ update + remove + add in one call, untouched lines preservedcart update '{"<only-sku>":0}'→ cart actually emptied (workaround kicked in)No cart line matched "..."Closes #7.