"""Execute the disclosed behavior contracts against an input module."""
import importlib.util,json,sys,tempfile,unittest
from decimal import Decimal
from pathlib import Path
from types import SimpleNamespace as Record
spec=importlib.util.spec_from_file_location('order_service',sys.argv[1]);service=importlib.util.module_from_spec(spec);spec.loader.exec_module(service)
class Contracts(unittest.TestCase):
 def test_stock_shortage_after_available_line(self):
  items=[Record(sku='available',quantity=1),Record(sku='missing',quantity=2)]
  self.assertFalse(service.can_fulfil(items,{'available':5,'missing':1}))
 def test_stock_all_available_and_empty(self):
  self.assertTrue(service.can_fulfil([Record(sku='a',quantity=2)],{'a':2}))
  self.assertTrue(service.can_fulfil([],{}))
 def test_malformed_order_raises(self):
  with tempfile.TemporaryDirectory() as directory:
   p=Path(directory)/'order.json';p.write_text('{')
   with self.assertRaises(json.JSONDecodeError):service.load_order(p)
 def test_missing_and_valid_orders(self):
  with tempfile.TemporaryDirectory() as directory:
   p=Path(directory)/'order.json'
   with self.assertRaises(FileNotFoundError):service.load_order(p)
   p.write_text('{"id": 4}')
   self.assertEqual(service.load_order(p),{'id':4})
 def test_release_adjacent_expired_entries(self):
  future=Record(sku='c',quantity=9,expires_at=20)
  reservations=[Record(sku='a',quantity=2,expires_at=1),Record(sku='b',quantity=3,expires_at=2),future]
  stock={};service.release_expired(stock,reservations,10)
  self.assertEqual(stock,{'a':2,'b':3});self.assertEqual(reservations,[future])
 def test_release_empty_and_unexpired(self):
  future=Record(sku='c',quantity=9,expires_at=20);reservations=[future];stock={}
  service.release_expired(stock,reservations,10)
  self.assertEqual(stock,{});self.assertEqual(reservations,[future])
 def test_subtotal_includes_final_line(self):
  items=[Record(price=Decimal('2.50'),quantity=2),Record(price=Decimal('7'),quantity=3)]
  self.assertEqual(service.subtotal(items),Decimal('26.00'))
 def test_subtotal_empty_and_single_line(self):
  self.assertEqual(service.subtotal([]),Decimal('0'))
  self.assertEqual(service.subtotal([Record(price=Decimal('7'),quantity=3)]),Decimal('21'))
if __name__=='__main__':
 unittest.main(argv=[sys.argv[0]],verbosity=2)
