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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Pricing Calculator

[![Tests](https://github.com/sterlingspares/calc/actions/workflows/tests.yml/badge.svg)](https://github.com/sterlingspares/calc/actions/workflows/tests.yml) ![Tests](https://img.shields.io/badge/tests-1479%20passing-brightgreen?style=flat-square) ![Coverage](https://img.shields.io/badge/coverage-80%25-green?style=flat-square) ![Lighthouse Performance](https://img.shields.io/badge/Lighthouse%20Perf-97-brightgreen?style=flat-square&logo=lighthouse) ![Lighthouse Accessibility](https://img.shields.io/badge/Lighthouse%20A11y-100-brightgreen?style=flat-square&logo=lighthouse) ![Lighthouse Best Practices](https://img.shields.io/badge/Best%20Practices-100-brightgreen?style=flat-square&logo=lighthouse) ![a11y](https://img.shields.io/badge/WCAG%202.1-AA-brightgreen?style=flat-square) ![PWA](https://img.shields.io/badge/PWA-offline--ready-brightgreen?style=flat-square&logo=pwa) ![License](https://img.shields.io/badge/license-MIT-blue?style=flat-square) ![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)
[![Tests](https://github.com/sterlingspares/calc/actions/workflows/tests.yml/badge.svg)](https://github.com/sterlingspares/calc/actions/workflows/tests.yml) ![Tests](https://img.shields.io/badge/tests-1489%20passing-brightgreen?style=flat-square) ![Coverage](https://img.shields.io/badge/coverage-80%25-green?style=flat-square) ![Lighthouse Performance](https://img.shields.io/badge/Lighthouse%20Perf-97-brightgreen?style=flat-square&logo=lighthouse) ![Lighthouse Accessibility](https://img.shields.io/badge/Lighthouse%20A11y-100-brightgreen?style=flat-square&logo=lighthouse) ![Lighthouse Best Practices](https://img.shields.io/badge/Best%20Practices-100-brightgreen?style=flat-square&logo=lighthouse) ![a11y](https://img.shields.io/badge/WCAG%202.1-AA-brightgreen?style=flat-square) ![PWA](https://img.shields.io/badge/PWA-offline--ready-brightgreen?style=flat-square&logo=pwa) ![License](https://img.shields.io/badge/license-MIT-blue?style=flat-square) ![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)

**Live app:** [calc.sterlingspares.com](https://calc.sterlingspares.com)

Expand Down Expand Up @@ -775,7 +775,7 @@ Every number here is reproducible from the repo — none is hand-written.

| Metric | Value | Reproduce with |
|---|---|---|
| Tests | 1479 passing, 7 suites | `npm test` |
| Tests | 1489 passing, 7 suites | `npm test` |
| Statement coverage | **82.7%** (app.js 86.3%, app-extra.js 72.3%) | `npm run coverage` |
| Lighthouse Performance | **97** | `npm i -D lighthouse && npm run lighthouse` |
| Lighthouse Accessibility | **100** | ” |
Expand Down Expand Up @@ -812,7 +812,7 @@ commands after significant changes.

## Tests

1479 assertions across seven suites. They load the real `index.html`,
1489 assertions across seven suites. They load the real `index.html`,
`assets/styles.css` and both script bundles, and drive the actual application
functions — no application code is mocked. **Node 22 or newer.**

Expand All @@ -823,13 +823,13 @@ npm test

| Suite | Assertions | Covers |
|---|---|---|
| `features` | 1041 | GST, incentives, quantity, landed costs, rounding, undo/redo of typed values and controls, quote maths, history |
| `features` | 1049 | GST, incentives, quantity, landed costs, rounding, undo/redo of typed values and controls, quote maths, history |
| `errors` | 33 | every failure path logs; a clean run stays silent; storage and payload recovery |
| `mobile` | 78 | modal layering, touch targets, sticky result bar, responsive quote layouts |
| `fab` | 50 | floating action button behaviour and z-index ordering |
| `modes` | 85 | solve modes, price input modes, what-if, comparison, Quick mode, wizard, share-link round trip, theming, auto-save |
| `a11y` | 104 | contrast ratios, structure, names, keyboard operability, focus trap, live regions, reduced motion, axe-core |
| `browser` | 88 | real Chromium over HTTP: asset loading, CSS cascade, `defer` timing, clicks, dialogs, control-bar spacing, mobile viewport, axe with layout |
| `browser` | 90 | real Chromium over HTTP: asset loading, CSS cascade, `defer` timing, clicks, dialogs, control-bar spacing, mobile viewport, axe with layout |

Individual suites: `npm run test:features`, `test:errors`, `test:mobile`,
`test:fab`, `test:modes`, `test:a11y`, `test:browser`. Full assertion output:
Expand Down
26 changes: 21 additions & 5 deletions assets/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2865,6 +2865,22 @@ function fillSpIncPanel(sp){
R('sp-inc-total-inr',inc>0?SINR(inc):'—');
R('sp-inc-eff-sp',SINR(eff));
}
/**
* Suffix for a discount field. Both discounts are percentages *off MRP* — the
* bare "%" left that to be inferred, and it is the one thing about the field a
* newcomer gets wrong.
* @param {'excl'|'incl'|'manual'} mode the card's input mode
* @param {number} gst whole-number GST rate for that mode
* @returns {string}
*/
function discSuffix(mode,gst){
/* The single normal space before the "+" is the only break point; everything
either side is joined with non-breaking spaces. In a narrow field the label
then wraps as "% on MRP" / "+ 18% GST" rather than leaving the "+" dangling
at the end of the first line. */
var base='%\u00A0on\u00A0MRP';
return (mode==='excl') ? base+' +\u00A0'+gst+'%\u00A0GST' : base;
}
/* ── Dynamic GST label updater ── */
function updateGSTLabels(){
var g = Math.round(G * 100); // main mode
Expand All @@ -2875,9 +2891,9 @@ function updateGSTLabels(){

// Main mode — disc field suffixes
var cpSuf=el('cpd-suf');
if(cpSuf) cpSuf.textContent = (CM==='excl') ? '% + '+g+'% GST' : '%';
if(cpSuf) cpSuf.textContent = discSuffix(CM,g);
var spSuf=el('spd-suf');
if(spSuf) spSuf.textContent = (SM==='excl') ? '% + '+g+'% GST' : '%';
if(spSuf) spSuf.textContent = discSuffix(SM,g);

// Manual entry sub-tabs
R('cpm-incl','CP incl '+g+'% GST'); R('cpm-excl','CP excl '+g+'% GST');
Expand Down Expand Up @@ -2907,17 +2923,17 @@ function updateGSTLabels(){

// Quick mode — disc field suffixes
var fcCpSuf=el('fc-cpd-suf');
if(fcCpSuf) fcCpSuf.textContent = (FC_CM==='excl') ? '% + '+fcg+'% GST' : '%';
if(fcCpSuf) fcCpSuf.textContent = discSuffix(FC_CM,fcg);
var fcSpSuf=el('fc-spd-suf');
if(fcSpSuf) fcSpSuf.textContent = (FC_SM==='excl') ? '% + '+fcg+'% GST' : '%';
if(fcSpSuf) fcSpSuf.textContent = discSuffix(FC_SM,fcg);

// Quick mode manual sub-tabs
R('fc-cpms-incl','CP incl '+fcg+'% GST'); R('fc-cpms-excl','CP excl '+fcg+'% GST');
R('fc-spms-incl','SP incl '+fcg+'% GST'); R('fc-spms-excl','SP excl '+fcg+'% GST');

// Wizard — disc field suffix
var wzSuf=el('wz-disc-suf');
if(wzSuf) wzSuf.textContent = (WZ_CM==='excl') ? '% + '+wzg+'% GST' : '%';
if(wzSuf) wzSuf.textContent = discSuffix(WZ_CM,wzg);

// Wizard labels (replaces inline textContent calls in wzSetT)
R('wz-ms-incl-lbl', wzLbl+' incl '+wzg+'% GST');
Expand Down
12 changes: 9 additions & 3 deletions assets/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,13 @@ body{font-family:var(--font-body);background:var(--bg);color:var(--text);min-hei
.field input{border:none;outline:none;background:transparent;padding:0 10px;height:38px;font-size:15px;font-family:var(--font-mono);font-weight:500;color:var(--text);width:100%;min-width:0}
.field-next{display:none;flex-shrink:0;width:30px;height:30px;border-radius:50%;border:none;background:var(--accent);color:var(--accent-fg);cursor:pointer;margin-right:4px;align-items:center;justify-content:center;transition:opacity .15s;opacity:0.7}
.field-next:hover{opacity:1}
.field .field-suf{padding:0 10px;font-size:12px;font-weight:500;color:var(--text3);border-left:1px solid var(--border);height:38px;display:flex;align-items:center;white-space:nowrap;background:var(--surface2);flex-shrink:0}
/* "% on MRP + 18% GST" on one line would take a third of the field. Wrapped to
two it still clears the 38px row, and it balances the two-line mode tabs
directly above. */
.field .field-suf{padding:0 10px;font-size:12px;font-weight:500;color:var(--text3);border-left:1px solid var(--border);height:38px;display:flex;align-items:center;text-align:right;line-height:1.2;max-width:86px;background:var(--surface2);flex-shrink:0}
/* The hint is not the data — at the input's own size it would not fit what is
left of a narrow field. */
.field input::placeholder{font-family:var(--font-body);font-size:12.5px}
@media(max-width:800px){.field-next{display:flex}}
.rows{display:flex;flex-direction:column}
.row{display:flex;justify-content:space-between;align-items:baseline;padding:6px 0;border-bottom:1px solid var(--border);gap:8px}
Expand Down Expand Up @@ -555,7 +561,7 @@ html[data-theme="dark"] .fc-dot.cur{background:var(--text)}
/* Set in fcMkField when the prefix is a word ("Discount") rather than a ₹ or
% glyph — at 20px heading weight it would crowd out the number. */
.fc-input-sym.word{font-family:var(--font-body);font-size:13px;font-weight:500;padding:0 12px}
.fc-input-suf{padding:0 12px;font-size:13px;font-weight:500;color:var(--text3);border-left:1px solid var(--border);height:56px;display:flex;align-items:center;white-space:nowrap;flex-shrink:0;font-family:var(--font-body)}
.fc-input-suf{padding:0 12px;font-size:13px;font-weight:500;color:var(--text3);border-left:1px solid var(--border);height:56px;display:flex;align-items:center;text-align:right;line-height:1.2;max-width:88px;flex-shrink:0;font-family:var(--font-body)}

.fc-mode-tabs{display:flex;gap:6px;flex-wrap:wrap}
.fc-tab{padding:6px 12px;border-radius:20px;border:1px solid var(--border-ctrl);background:transparent;font-size:11.5px;font-weight:500;color:var(--text2);cursor:pointer;font-family:var(--font-body);transition:all .15s}
Expand Down Expand Up @@ -597,7 +603,7 @@ html[data-theme="dark"] .fc-dot.cur{background:var(--text)}
.wz-field{display:flex;align-items:center;background:var(--surface2);border:1.5px solid var(--border-ctrl);border-radius:8px;overflow:hidden;flex:1;transition:border .15s}
.wz-field:focus-within{border-color:var(--accent)}
.wz-sym{padding:0 10px;font-size:13px;font-weight:600;color:var(--text3);border-right:1px solid var(--border);height:36px;display:flex;align-items:center;white-space:nowrap;background:var(--surface)}
.wz-suf{padding:0 10px;font-size:12px;font-weight:500;color:var(--text3);border-left:1px solid var(--border);height:36px;display:flex;align-items:center;white-space:nowrap;flex-shrink:0;background:var(--surface)}
.wz-suf{padding:0 10px;font-size:12px;font-weight:500;color:var(--text3);border-left:1px solid var(--border);height:36px;display:flex;align-items:center;text-align:right;line-height:1.2;max-width:86px;flex-shrink:0;background:var(--surface)}
.wz-input{border:none;outline:none;background:transparent;padding:0 10px;height:36px;font-size:15px;font-family:var(--font-mono);font-weight:500;color:var(--text);width:100%;min-width:0;user-select:text;-webkit-user-select:text}
.wz-inc-row{display:flex;align-items:center;gap:8px;padding:7px 0;border-bottom:1px solid var(--border)}
.wz-inc-row:last-child{border-bottom:none}
Expand Down
Binary file modified docs/screenshot-mobile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ <h1 class="header-brand">Price Calc</h1>
<div class="field" id="cpf-disc">
<span class="sym">Discount</span>
<input type="number" inputmode="decimal" id="cpd" placeholder="e.g. 30" min="0" max="100" step="0.01" data-input="a37" autocomplete="off" aria-label="Cost price discount percentage">
<span class="field-suf" id="cpd-suf">% + 18% GST</span>
<span class="field-suf" id="cpd-suf">%&nbsp;on&nbsp;MRP +&nbsp;18%&nbsp;GST</span>
<button class="field-next" data-click="a38" title="Next" aria-label="Go to the next field after cost price discount" tabindex="-1">
<svg width="12" height="12" viewBox="0 0 12 12" fill="none"><path d="M4 2l4 4-4 4" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
Expand Down Expand Up @@ -311,7 +311,7 @@ <h1 class="header-brand">Price Calc</h1>
<div class="field" id="spf-disc">
<span class="sym">Discount</span>
<input type="number" inputmode="decimal" id="spd" placeholder="e.g. 20" min="0" max="100" step="0.01" data-input="a37" autocomplete="off" aria-label="Selling price discount percentage">
<span class="field-suf" id="spd-suf">% + 18% GST</span>
<span class="field-suf" id="spd-suf">%&nbsp;on&nbsp;MRP +&nbsp;18%&nbsp;GST</span>
<button class="field-next" data-click="a47" title="Next" aria-label="Go to the next field after selling price discount" tabindex="-1">
<svg width="12" height="12" viewBox="0 0 12 12" fill="none"><path d="M4 2l4 4-4 4" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
Expand Down Expand Up @@ -545,7 +545,7 @@ <h2 class="panel-title" id="ttl-hist">History</h2>
<div class="wz-field">
<span class="wz-sym">Discount</span>
<input class="wz-input" type="number" inputmode="decimal" id="wz-disc" placeholder="e.g. 34" min="0" max="100" step="0.01" data-input="a80" autocomplete="off" aria-label="Wizard: discount percentage">
<span class="wz-suf" id="wz-disc-suf">% + 18% GST</span>
<span class="wz-suf" id="wz-disc-suf">%&nbsp;on&nbsp;MRP +&nbsp;18%&nbsp;GST</span>
</div>
</div>
<div id="wz-manual-wrap" style="display:none;flex-direction:column;gap:8px">
Expand Down
6 changes: 3 additions & 3 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tests

1479 assertions across seven suites. They load the real `index.html`,
1489 assertions across seven suites. They load the real `index.html`,
`assets/styles.css` and `assets/app.js` into
[jsdom](https://github.com/jsdom/jsdom) and drive the actual application
functions — no application code is mocked.
Expand Down Expand Up @@ -34,11 +34,11 @@ The runner exits non-zero if anything fails, so CI catches it.

| File | Assertions | Covers |
|---|---|---|
| `features.test.js` | 1041 | GST (presets, custom, decimal), incentive edit mode, add/delete/rename, %/₹ modes, quantity and order totals, rounding, undo/redo of typed values and every control, quote maths, history search/filter/tags, share-state round trips |
| `features.test.js` | 1049 | GST (presets, custom, decimal), incentive edit mode, add/delete/rename, %/₹ modes, quantity and order totals, rounding, undo/redo of typed values and every control, quote maths, history search/filter/tags, share-state round trips |
| `errors.test.js` | 33 | every failure path logs; a clean run logs nothing; storage-quota and corrupt-payload recovery; global handlers |
| `mobile.test.js` | 78 | modal layering vs the bottom nav, touch-target sizes, viewport zoom policy, type scale, sticky result bar states, quote table vs card layouts |
| `fab.test.js` | 50 | FAB visibility rules, open/close and dismissal, ARIA state, deferred dispatch, error containment, z-index ordering |
| `browser.test.js` | 88 | serves the repo over HTTP and drives real Chromium: asset loading, CSS cascade and media queries, `defer` timing, clicks, dialog focus, control-bar spacing, mobile viewport and z-index layering |
| `browser.test.js` | 90 | serves the repo over HTTP and drives real Chromium: asset loading, CSS cascade and media queries, `defer` timing, clicks, dialog focus, control-bar spacing, mobile viewport and z-index layering |
| `modes.test.js` | 85 | price maths, the three solve modes, input and profit modes, what-if scenarios, comparison, Quick mode, wizard, share-link URL round trip, summary text, theming, auto-save, floor limits |
| `a11y.test.js` | 104 | contrast ratios computed from the palette, document structure, accessible names, keyboard operability, dialog focus trap and restore, live regions, reduced motion, plus axe-core over every visible state |

Expand Down
32 changes: 26 additions & 6 deletions tests/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,18 +752,38 @@ async function launchChromium(chromium) {
await page.waitForTimeout(150);
const disc = await page.evaluate(() => {
const f = document.getElementById('cpf-disc');
const box = f.getBoundingClientRect(), inp = document.getElementById('cpd').getBoundingClientRect();
const inp = document.getElementById('cpd');
const suf = document.getElementById('cpd-suf');
// Width the hint needs, in the font actually rendering it
const ph = getComputedStyle(inp, '::placeholder'), box = getComputedStyle(inp);
const c = document.createElement('canvas').getContext('2d');
c.font = ph.fontWeight + ' ' + ph.fontSize + ' ' + ph.fontFamily;
return {
label: f.querySelector('.sym').textContent,
inputWidth: Math.round(inp.width),
suffix: suf.textContent.replace(/\u00a0/g, ' '),
// One client rect per rendered line — the element's own height is fixed,
// so dividing it by the line-height counts nothing.
lines: (() => { const r = document.createRange(); r.selectNodeContents(suf);
return r.getClientRects().length })(),
rowHeight: Math.round(f.getBoundingClientRect().height),
inputWidth: Math.round(inp.getBoundingClientRect().width),
hint: Math.ceil(c.measureText(inp.placeholder).width),
pad: Math.round(parseFloat(box.paddingLeft) + parseFloat(box.paddingRight)),
overflow: Math.round(f.scrollWidth - f.clientWidth),
bodyOverflow: document.body.scrollWidth - document.body.clientWidth,
boxWidth: Math.round(box.width)
bodyOverflow: document.body.scrollWidth - document.body.clientWidth
};
});
ok('the field says Discount, not Disc', disc.label === 'Discount', disc.label);
ok('the input still has room to type in at 320px', disc.inputWidth >= 40,
'input ' + disc.inputWidth + 'px of ' + disc.boxWidth + 'px');
// The discount is a percentage off MRP; the bare "%" left that to be inferred
ok('and the suffix says what the percentage is of',
disc.suffix === '% on MRP + 18% GST', disc.suffix);
// Two lines fit the 38px row; one would take a third of the field's width
ok('which wraps to two lines without growing the row',
disc.lines === 2 && disc.rowHeight <= 42,
disc.lines + ' lines, row ' + disc.rowHeight + 'px');
ok('the hint still fits what is left of the field at 320px',
disc.hint + disc.pad <= disc.inputWidth,
'hint ' + disc.hint + 'px + ' + disc.pad + 'px padding in ' + disc.inputWidth + 'px');
ok('and nothing overflows the field or the page',
disc.overflow <= 0 && disc.bodyOverflow <= 0,
'field ' + disc.overflow + 'px, page ' + disc.bodyOverflow + 'px');
Expand Down
37 changes: 37 additions & 0 deletions tests/features.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2935,6 +2935,43 @@ typeInto('hist-search', 'brake');
ok('nor is typing in the history search box', undoDepth() === 0, 'got ' + undoDepth());
w.setHistQuery('');

R.section('\n=== 29e. The discount fields say what the percentage is of ===');
// A bare "%" left it to be inferred that the discount comes off MRP, which is
// the one thing about the field a newcomer gets wrong.
const sufOf = id => d.getElementById(id).textContent.replace(/ /g, ' ');

freshCalc(1000, 40, 25);
ok('cost discount is a percentage on MRP, plus GST',
sufOf('cpd-suf') === '% on MRP + 18% GST', sufOf('cpd-suf'));
ok('and so is the sale discount',
sufOf('spd-suf') === '% on MRP + 18% GST', sufOf('spd-suf'));

w.setGST(5);
ok('the rate in the suffix follows the GST setting',
sufOf('cpd-suf') === '% on MRP + 5% GST', sufOf('cpd-suf'));
w.setGST(18);

// Nett Discount takes the price straight off MRP incl GST, so there is no
// separate GST to add — but it is still a percentage on MRP.
w.setCM('incl');
ok('Nett Discount drops the GST part but keeps the base',
sufOf('cpd-suf') === '% on MRP', sufOf('cpd-suf'));
ok('and the other card is unaffected',
sufOf('spd-suf') === '% on MRP + 18% GST', sufOf('spd-suf'));
w.setSM('incl');
ok('until it is switched too', sufOf('spd-suf') === '% on MRP');
w.setCM('excl'); w.setSM('excl');

ok('the wizard field says the same',
sufOf('wz-disc-suf').indexOf('% on MRP') === 0, sufOf('wz-disc-suf'));

// The single normal space before the "+" is the only place it may break, so a
// narrow field wraps to "% on MRP" / "+ 18% GST" rather than stranding the "+".
const rawSuf = d.getElementById('cpd-suf').textContent;
ok('only the break point is a normal space',
rawSuf.split(' ').length === 2 && rawSuf.split(' ')[1].charAt(0) === '+',
JSON.stringify(rawSuf));

R.section('\n=== 30a. The default view stays uncluttered ===');
// The main screen had grown to a control bar of 13, five derived rows per price
// card, and a 17-cell summary in which five cells repeated numbers shown in the
Expand Down
Loading