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
19 changes: 12 additions & 7 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-1543%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-1549%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 @@ -145,7 +145,7 @@ sw.js service worker
manifest.json PWA manifest
_headers response headers for the host
tests/ test suite (dev-only)
docs/ screenshots for this README (npm run screenshots)
docs/ README screenshots — all four are generated (npm run screenshots)
robots.txt · sitemap.xml indexing
```

Expand Down Expand Up @@ -455,6 +455,8 @@ Three things that are useful beside a calculation but are not part of one live
under **Tools** in the header — the quote builder and the two converters. On a
phone they are in the hamburger menu instead. Press `T` to open it.

<img src="docs/screenshot-tools.png" alt="The Tools menu open in the header, listing Quote builder, GP to Markup and Currency converter" width="620">

### GP % / markup % converter

Two ways of quoting the same profit, over different denominators — which is why
Expand All @@ -470,8 +472,9 @@ money. **Tools → GP ⇄ Markup** takes either and returns the other:
`markup = GP ÷ (100 − GP)` and `GP = markup ÷ (100 + markup)`. It shows the
result as money as well — *buy at ₹100.00, sell at ₹125.00* — because a
percentage on its own is easy to nod along to. **Use this calculation** seeds it
from whatever is on screen, and the two ends with no finite answer (100% GP,
−100% markup) say so rather than blanking.
from whatever is on screen; with nothing to pull in it stays put and says what
is missing, rather than disappearing. The two ends with no finite answer (100%
GP, −100% markup) say so rather than blanking.

### Currency converter

Expand All @@ -480,6 +483,8 @@ currencies, at the same rates the rest of the app uses. Type into either side
and the other follows; **⇄** reverses the pair, carrying the converted amount
back as the new input.

<img src="docs/screenshot-currency.png" alt="The currency converter, showing $250.00 converted to ₹20,833.33 at 1 USD = ₹83.33" width="520">

It is deliberately separate from the **Show** setting in Settings → Pricing.
That one restates the whole calculation in another currency; this is a scratch
pad, and leaves the calculation alone. Every rate is quoted per rupee, so a
Expand Down Expand Up @@ -794,7 +799,7 @@ Every number here is reproducible from the repo — none is hand-written.

| Metric | Value | Reproduce with |
|---|---|---|
| Tests | 1543 passing, 7 suites | `npm test` |
| Tests | 1549 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 @@ -831,7 +836,7 @@ commands after significant changes.

## Tests

1543 assertions across seven suites. They load the real `index.html`,
1549 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 @@ -842,7 +847,7 @@ npm test

| Suite | Assertions | Covers |
|---|---|---|
| `features` | 1087 | GST, incentives, quantity, landed costs, rounding, undo/redo of typed values and controls, quote maths, history |
| `features` | 1093 | 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 |
Expand Down
20 changes: 18 additions & 2 deletions assets/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1406,8 +1406,24 @@ function openConvert(){
o.classList.add('open');
document.body.style.overflow='hidden';
pushOverlay(o);
var use=el('cv-use');
if(use)use.style.display=(LAST_CP&&LAST_SP)?'':'none';
/* "Use this calculation" used to disappear when there was nothing to pull in.
From the outside that reads as a broken feature rather than an unavailable
one — the README describes a button that simply is not there. It stays put
and says why, the same way the exchange-rate override does. */
var use=el('cv-use'),have=!!(LAST_CP&&LAST_SP);
if(use){
use.style.display='';
use.disabled=!have;
use.title=have?'':'Enter MRP, cost and selling price on the calculator first';
}
var gpEl=el('cv-gp'),mkEl=el('cv-mk'),out=el('cv-out');
if(out&&gpEl&&mkEl&&gpEl.value===''&&mkEl.value===''){
out.textContent=have
? 'Type a percentage into either box — or pull in the calculation on screen.'
: 'Type a percentage into either box. Once MRP, cost and selling price are '+
'filled in on the calculator, you can pull that calculation straight in.';
out.className='cv-out';
}
setTimeout(function(){ var f=el('cv-gp'); if(f){f.focus();if(f.select)f.select()} },30);
}
/** Close it. */
Expand Down
Binary file added docs/screenshot-currency.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 added docs/screenshot-tools.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 tests/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tests

1543 assertions across seven suites. They load the real `index.html`,
1549 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,7 +34,7 @@ The runner exits non-zero if anything fails, so CI catches it.

| File | Assertions | Covers |
|---|---|---|
| `features.test.js` | 1087 | 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` | 1093 | 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 |
Expand Down Expand Up @@ -157,7 +157,7 @@ test disagrees with the app, check the formula before changing the code.
npm run screenshots
```

The two images the README embeds are generated, not captured by hand. They had
The four images the README embeds are generated, not captured by hand. They had
drifted twice — once when labels were renamed, once when a header button was
added — and nothing tied them to the app. `screenshots.js` drives the real page
through the same worked example the README's own table walks through, so the
Expand Down
30 changes: 28 additions & 2 deletions tests/features.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2797,10 +2797,36 @@ ok("and the markup matches the app's Markup %",
cvMk.value + ' vs ' + d.getElementById('s-mg').textContent);
w.closeConvert();
ok('closing hands the focus trap back', w._openOverlay === null);
// With nothing to pull in, the button used to disappear. From the outside that
// reads as a broken feature rather than an unavailable one — the README
// describes a button that simply is not there.
w.resetAll();
d.getElementById('cv-gp').value = ''; d.getElementById('cv-mk').value = '';
w.openConvert();
ok('with nothing calculated the offer is hidden',
d.getElementById('cv-use').style.display === 'none');
ok('with nothing calculated the offer stays put',
d.getElementById('cv-use').style.display !== 'none',
d.getElementById('cv-use').style.display);
ok('but cannot be pressed', d.getElementById('cv-use').disabled === true);
ok('and says what is missing',
/MRP, cost and selling price/.test(d.getElementById('cv-use').title),
d.getElementById('cv-use').title);
ok('with the same reason on screen, not only in a tooltip',
/MRP, cost and selling price/.test(d.getElementById('cv-out').textContent),
d.getElementById('cv-out').textContent);
w.closeConvert();

// And it comes back the moment there is something to offer
freshCalc(1000, 40, 25);
d.getElementById('cv-gp').value = ''; d.getElementById('cv-mk').value = '';
w.openConvert();
ok('a calculation re-enables it', d.getElementById('cv-use').disabled === false);
ok('and the empty state invites either route',
/pull in the calculation/.test(d.getElementById('cv-out').textContent),
d.getElementById('cv-out').textContent);
// Through the delegated handler, the way a click reaches it
w.ACT.convertUseCurrent();
ok('pressing it still seeds both boxes', cvGp.value === '20' && cvMk.value === '25',
cvGp.value + ' / ' + cvMk.value);
w.closeConvert();

ok('it is reachable from the Tools menu',
Expand Down
49 changes: 49 additions & 0 deletions tests/screenshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,53 @@ const SHOTS = [
{ file: 'screenshot-mobile.png', width: 390, height: 900, fullPage: false },
];

/**
* Two pictures for the Tools section: the menu open, and the currency converter.
* Separately, not composed — the converter is a modal with a blurred backdrop,
* so a menu shown behind it photographs as a smear.
*/
async function toolsShots(browser, origin) {
const page = await browser.newPage({
viewport: { width: 1100, height: 560 }, deviceScaleFactor: 2,
});
await page.addInitScript(() => { try { localStorage.setItem('ob-done', '1') } catch (e) {} });
await page.goto(origin + '/', { waitUntil: 'load' });
await page.waitForFunction(() => typeof window.calc === 'function');
await page.emulateMedia({ reducedMotion: 'reduce' });
await page.evaluate(workedExample);
await page.waitForTimeout(600);
await page.evaluate(tidyUp);

await page.evaluate(() => window.openTools());
await page.waitForTimeout(300);
await page.screenshot({ path: path.join(OUT, 'screenshot-tools.png'),
clip: { x: 340, y: 0, width: 760, height: 300 } });
console.log(' wrote docs/screenshot-tools.png');
await page.evaluate(() => window.closeTools());

// Fixed rates, so the picture does not change with the day's market
await page.evaluate(() => {
window.FX.rates = { INR: 1, USD: 0.012, EUR: 0.011, GBP: 0.0094 };
window.FX.fetched = window.nowMs();
});
await page.waitForFunction(() => typeof window._renderCcyConvImpl === 'function',
null, { timeout: 8000 });
await page.evaluate(() => {
window.openCcyConv();
document.getElementById('cc-from-amt').value = '250';
window.renderCcyConv('from');
});
// openCcyConv focuses and selects the first field on a 30ms timer, so the
// blur has to come after that or the figure photographs highlighted.
await page.waitForTimeout(400);
await page.evaluate(() => document.getElementById('cc-from-amt').blur());
await page.waitForTimeout(200);
await page.locator('#overlay-ccyconv .modal').screenshot({
path: path.join(OUT, 'screenshot-currency.png') });
console.log(' wrote docs/screenshot-currency.png');
await page.close();
}

(async () => {
let chromium;
try { ({ chromium } = require('playwright')); }
Expand Down Expand Up @@ -130,6 +177,8 @@ const SHOTS = [
await page.close();
}

await toolsShots(browser, origin);

await browser.close();
await new Promise(r => server.close(r));
})();
Loading