dtf-site.html held commercial rules, the nesting engine, PDF analysis, the cart and every handler in a single inline script, 42% of the runtime code in one file, and the money logic lived in the middle of it. It is now nine files under local/static, cut at the section markers the original author left, so no function was split across a boundary: config, product modes, upload, sheet analysis, PDF, quality, packing, cart, flow. They load as classic scripts in the original order and share one global scope, so evaluation is exactly what it was; the extraction was checked byte-identical against the original before the tags replaced it. dtf-site.html is 1,394 lines of markup and style. With no inline script left anywhere, the policy no longer needs a hash allowlist: script-src is now 'self' alone, which is stronger than what it replaced and cannot drift as the page changes. Three things depended on the old shape and were updated rather than worked around. The pricing parity test read the ladder out of the HTML and now reads it from site-config.js, still proving the server agrees with what the customer is shown. The isolated artwork test served four hardcoded script paths and now serves any script that resolves inside local/static, so the next file added does not silently 404. The CSP assertion checked the whole policy for 'unsafe-inline' and now checks the script-src directive alone, since style-src legitimately carries it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
43 lines
2.3 KiB
Python
43 lines
2.3 KiB
Python
"""Run from repository root: python3 -m unittest local.test_pricing -v."""
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import unittest
|
|
from .pricing import price, TIERS
|
|
|
|
class PricingTests(unittest.TestCase):
|
|
def test_every_grade_against_actual_site_javascript(self):
|
|
# The ladders live with the rest of the Site's behaviour; this test exists
|
|
# to prove the server agrees with whatever the customer is shown.
|
|
source = Path('local/static/site-config.js').read_text()
|
|
tiers = source.split('const FAIXAS=')[1].split('\n};',1)[0]+'\n}'
|
|
calculator = source.split('const cobrar=')[1].split(';',1)[0]
|
|
js = f'const FAIXAS={tiers};const MINIMO_M=1;const cobrar={calculator};'
|
|
js += '''const results=[];for(const mode of Object.keys(FAIXAS))for(let grade=0;grade<=100;grade++)
|
|
for(const metres of [0.01,0.99,1,1.01,1.1,2.75,2.8,2.8000000000000003,19.99,60,12000]){
|
|
const unit=FAIXAS[mode].find(x=>grade>=x[0])[1];
|
|
results.push([mode,String(metres),grade,cobrar(metres),Math.round(unit*100),Math.round(cobrar(metres)*unit*100)]);
|
|
}process.stdout.write(JSON.stringify(results));'''
|
|
cases = json.loads(subprocess.check_output(['node','-e',js],text=True))
|
|
for mode,metres,grade,billed,unit,total in cases:
|
|
with self.subTest(mode=mode,metres=metres,grade=grade):
|
|
result=price(mode,metres,grade)
|
|
self.assertEqual(float(result['billed_metres']),billed)
|
|
self.assertEqual(result['unit_cents'],unit)
|
|
self.assertEqual(result['total_cents'],total)
|
|
|
|
def test_assembly_is_included_at_every_tier(self):
|
|
for grade in range(101):
|
|
self.assertEqual(price('avulsa','1',grade)['total_cents']-price('file','1',grade)['total_cents'],1000)
|
|
self.assertEqual(price('uv','1',grade)['total_cents']-price('uvfile','1',grade)['total_cents'],1400)
|
|
|
|
def test_invalid_inputs(self):
|
|
for value in ('0','-1','NaN','Infinity','12001'):
|
|
with self.assertRaises(ValueError):price('file',value,90)
|
|
for grade in (-1,101,True,89.5):
|
|
with self.assertRaises(ValueError):price('file','1',grade)
|
|
with self.assertRaises(ValueError):price('other','1',90)
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|