From b1306685bee086385f494170b59da857c867a881 Mon Sep 17 00:00:00 2001 From: marginsystems <45528597+marginsystems@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:07:06 +0700 Subject: [PATCH] Shopping cart library with tests --- .github/workflows/test.yml | 14 ++++++++++++++ README.md | 5 ++++- package.json | 9 +++++++++ src/cart.js | 14 ++++++++++++++ src/cart.test.js | 15 +++++++++++++++ 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/test.yml create mode 100644 package.json create mode 100644 src/cart.js create mode 100644 src/cart.test.js diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..9a7175b --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,14 @@ +name: test +on: + pull_request: + push: + branches: [main] +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 + - run: npm test diff --git a/README.md b/README.md index c004396..13a4b78 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # sample-app -Sample repository for trying Mergestorm code review, stacks and the merge queue + +A small shopping-cart library used to demonstrate Mergestorm: AI code review on pull requests, auto-fixes, stacked PRs and the merge queue. + +Prices are integer cents. Run the tests with `npm test`. diff --git a/package.json b/package.json new file mode 100644 index 0000000..fe37842 --- /dev/null +++ b/package.json @@ -0,0 +1,9 @@ +{ + "name": "sample-app", + "version": "1.0.0", + "private": true, + "type": "module", + "scripts": { + "test": "node --test" + } +} diff --git a/src/cart.js b/src/cart.js new file mode 100644 index 0000000..e6b56b0 --- /dev/null +++ b/src/cart.js @@ -0,0 +1,14 @@ +export function lineTotal(item) { + return item.price * item.quantity; +} + +export function subtotal(items) { + return items.reduce((sum, item) => sum + lineTotal(item), 0); +} + +export function applyDiscount(amount, percent) { + if (percent < 0 || percent > 100) { + throw new RangeError("percent must be between 0 and 100"); + } + return Math.round(amount * (100 - percent)) / 100; +} diff --git a/src/cart.test.js b/src/cart.test.js new file mode 100644 index 0000000..a73b72e --- /dev/null +++ b/src/cart.test.js @@ -0,0 +1,15 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { applyDiscount, lineTotal, subtotal } from "./cart.js"; + +test("lineTotal multiplies price by quantity", () => { + assert.equal(lineTotal({ price: 250, quantity: 3 }), 750); +}); + +test("subtotal adds every line", () => { + assert.equal(subtotal([{ price: 100, quantity: 2 }, { price: 50, quantity: 1 }]), 250); +}); + +test("applyDiscount takes a percentage off", () => { + assert.equal(applyDiscount(1000, 10), 900); +});