|
| 1 | +import json |
1 | 2 | import os |
2 | 3 |
|
3 | 4 | import pytest |
| 5 | +from pytest_embedded_wokwi.wokwi import Wokwi |
4 | 6 |
|
5 | 7 | wokwi_token_required = pytest.mark.skipif( |
6 | 8 | not os.getenv('WOKWI_CLI_TOKEN', None), |
@@ -54,3 +56,115 @@ def test_pexpect_by_wokwi(dut): |
54 | 56 | ) |
55 | 57 |
|
56 | 58 | result.assert_outcomes(passed=1) |
| 59 | + |
| 60 | + |
| 61 | +class TestApplySerialInterfaceOverride: |
| 62 | + """Unit tests for Wokwi._apply_serial_interface_override (no token needed).""" |
| 63 | + |
| 64 | + def _write_diagram(self, tmp_path, diagram: dict) -> str: |
| 65 | + path = os.path.join(str(tmp_path), 'diagram.json') |
| 66 | + with open(path, 'w') as f: |
| 67 | + json.dump(diagram, f) |
| 68 | + return path |
| 69 | + |
| 70 | + def test_adds_serial_interface_and_removes_serial_monitor(self, tmp_path): |
| 71 | + diagram = { |
| 72 | + 'version': 1, |
| 73 | + 'parts': [{'type': 'board-esp32-devkit-c-v4', 'id': 'esp'}], |
| 74 | + 'connections': [ |
| 75 | + ['esp:TX', '$serialMonitor:RX', ''], |
| 76 | + ['esp:RX', '$serialMonitor:TX', ''], |
| 77 | + ], |
| 78 | + } |
| 79 | + src = self._write_diagram(tmp_path, diagram) |
| 80 | + result_path = Wokwi._apply_serial_interface_override(src) |
| 81 | + |
| 82 | + try: |
| 83 | + with open(result_path) as f: |
| 84 | + result = json.load(f) |
| 85 | + |
| 86 | + assert result['parts'][0]['attrs']['serialInterface'] == 'USB_SERIAL_JTAG' |
| 87 | + assert result['connections'] == [] |
| 88 | + finally: |
| 89 | + os.unlink(result_path) |
| 90 | + |
| 91 | + def test_preserves_non_serial_monitor_connections(self, tmp_path): |
| 92 | + diagram = { |
| 93 | + 'version': 1, |
| 94 | + 'parts': [{'type': 'board-esp32-s3-devkitc-1', 'id': 'esp32', 'attrs': {}}], |
| 95 | + 'connections': [ |
| 96 | + ['esp32:RX', '$serialMonitor:TX', '', []], |
| 97 | + ['esp32:TX', '$serialMonitor:RX', '', []], |
| 98 | + ['btn1:1.l', 'esp32:14', 'blue', ['h-38.4', 'v105.78']], |
| 99 | + ['esp32:4', 'led1:A', 'green', ['h0']], |
| 100 | + ], |
| 101 | + } |
| 102 | + src = self._write_diagram(tmp_path, diagram) |
| 103 | + result_path = Wokwi._apply_serial_interface_override(src) |
| 104 | + |
| 105 | + try: |
| 106 | + with open(result_path) as f: |
| 107 | + result = json.load(f) |
| 108 | + |
| 109 | + assert len(result['connections']) == 2 |
| 110 | + assert result['connections'][0] == ['btn1:1.l', 'esp32:14', 'blue', ['h-38.4', 'v105.78']] |
| 111 | + assert result['connections'][1] == ['esp32:4', 'led1:A', 'green', ['h0']] |
| 112 | + finally: |
| 113 | + os.unlink(result_path) |
| 114 | + |
| 115 | + def test_does_not_mutate_original_file(self, tmp_path): |
| 116 | + diagram = { |
| 117 | + 'version': 1, |
| 118 | + 'parts': [{'type': 'board-esp32-devkit-c-v4', 'id': 'esp'}], |
| 119 | + 'connections': [ |
| 120 | + ['esp:TX', '$serialMonitor:RX', ''], |
| 121 | + ['esp:RX', '$serialMonitor:TX', ''], |
| 122 | + ], |
| 123 | + } |
| 124 | + src = self._write_diagram(tmp_path, diagram) |
| 125 | + result_path = Wokwi._apply_serial_interface_override(src) |
| 126 | + |
| 127 | + try: |
| 128 | + with open(src) as f: |
| 129 | + original = json.load(f) |
| 130 | + |
| 131 | + assert 'serialInterface' not in original['parts'][0].get('attrs', {}) |
| 132 | + assert len(original['connections']) == 2 |
| 133 | + assert result_path != src |
| 134 | + finally: |
| 135 | + os.unlink(result_path) |
| 136 | + |
| 137 | + def test_adds_attrs_when_missing(self, tmp_path): |
| 138 | + diagram = { |
| 139 | + 'version': 1, |
| 140 | + 'parts': [{'type': 'board-esp32-p4-function-ev', 'id': 'esp'}], |
| 141 | + 'connections': [], |
| 142 | + } |
| 143 | + src = self._write_diagram(tmp_path, diagram) |
| 144 | + result_path = Wokwi._apply_serial_interface_override(src) |
| 145 | + |
| 146 | + try: |
| 147 | + with open(result_path) as f: |
| 148 | + result = json.load(f) |
| 149 | + |
| 150 | + assert result['parts'][0]['attrs'] == {'serialInterface': 'USB_SERIAL_JTAG'} |
| 151 | + assert result['connections'] == [] |
| 152 | + finally: |
| 153 | + os.unlink(result_path) |
| 154 | + |
| 155 | + def test_overrides_existing_serial_interface(self, tmp_path): |
| 156 | + diagram = { |
| 157 | + 'version': 1, |
| 158 | + 'parts': [{'type': 'board-esp32-devkit-c-v4', 'id': 'esp', 'attrs': {'serialInterface': 'UART'}}], |
| 159 | + 'connections': [], |
| 160 | + } |
| 161 | + src = self._write_diagram(tmp_path, diagram) |
| 162 | + result_path = Wokwi._apply_serial_interface_override(src) |
| 163 | + |
| 164 | + try: |
| 165 | + with open(result_path) as f: |
| 166 | + result = json.load(f) |
| 167 | + |
| 168 | + assert result['parts'][0]['attrs']['serialInterface'] == 'USB_SERIAL_JTAG' |
| 169 | + finally: |
| 170 | + os.unlink(result_path) |
0 commit comments