"""Run the Node lab and contrast Python's standard JSON integer decoding.

Needs Python 3 and a Node runtime with JSON.parse reviver context.source
(the published run uses Node 24). No third-party modules or network calls.
Run: python3 experiment.py with experiment.mjs in the same directory.
"""
import json
import os
import platform
import subprocess
from pathlib import Path

root = Path(__file__).resolve().parent
completed = subprocess.run([os.environ.get('NODE_BINARY', 'node'),
                            str(root / 'experiment.mjs')],
                           check=True, text=True, capture_output=True)
result = json.loads(completed.stdout)
python_rows = []
for row in result['rows']:
    text = '{"id":' + row['id'] + '}'
    value = json.loads(text)['id']
    assert type(value) is int
    assert str(value) == row['id']
    python_rows.append(dict(id=row['id'], decoded=str(value), type='int'))
assert result['rowCount'] == 8
assert result['changedNumberRows'] == 4
assert result['exactStringRows'] == 8
result.update(pythonVersion=platform.python_version(), pythonRows=python_rows,
              pythonExactRows=len(python_rows))
(root / 'results.json').write_text(json.dumps(result, indent=2) + '\n')
print(json.dumps({key: result[key] for key in ['nodeVersion', 'rowCount',
      'changedNumberRows', 'exactStringRows', 'pythonExactRows']}))
