Mock¶
Simulate an OSECHI detector without real hardware for testing and development.
MockDevice provides a drop-in replacement for PortDevice, while MockGenerator creates simulated detection events with configurable timing and data sources.
Architecture¶
Class Diagram¶
classDiagram
class DeviceProtocol {
<<interface>>
+send_command(command: str) bool
+receive_response(field_type: str) DeviceResponse
+query(command: str) DeviceResponse
+close() None
+__enter__() Self
+__exit__() None
}
class MockDevice {
-port: str
-is_open: bool
-generator: MockGenerator | None
-events: list[dict]
-event_index: int
-events_path: Path
+send_command(command: str) bool
+receive_response(field_type: str) DeviceResponse
+query(command: str) DeviceResponse
+close() None
-_get_events() dict | None
-_get_response(command: str) dict
}
class MockGenerator {
-events: list[dict]
-index: int
-jitter: float
-speed: float
-seed: int | None
+from_file(path: str) MockGenerator
+from_random(count: int, seed: int | None) MockGenerator
+set_jitter(jitter: float) MockGenerator
+set_speed(speed: float) MockGenerator
+get_next_event() dict | None
+reset() None
}
DeviceProtocol <|.. MockDevice: implements
MockDevice --> MockGenerator: uses (optional)
Event Priority System¶
When MockDevice.receive_response(field_type="event") is called:
flowchart TD
A["receive_response(field_type='event')"] --> B{MockGenerator<br/>provided?}
B -->|Yes| C["Return event from<br/>MockGenerator"]
B -->|No| D{Loaded events<br/>available?}
D -->|Yes| E["Return event from<br/>data/events/events.jsonl"]
D -->|No| F{Command<br/>response cached?}
F -->|Yes| G["Return command<br/>response"]
F -->|No| H["Return default<br/>STATUS response"]
C --> I["type = 'mock'<br/>received_us = timestamp"]
E --> I
G --> I
H --> I
MockDevice¶
kazunoko.mock.MockDevice
¶
Mock OSECHI detector for testing without real hardware
Loads device responses from data/responses/ directory. Automatically loads detection events from data/events/events.jsonl. Supports advanced event simulation via MockGenerator with speed/jitter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generator
|
MockGenerator | None
|
Optional MockGenerator for dynamic event simulation with speed/jitter control. If set, takes priority over loaded events. |
None
|
Event Priority
- MockGenerator (if provided) - dynamic events with speed/jitter
- data/events/events.jsonl - static events for testing
- Command responses - if neither above available
Example
from kazunoko import MockDevice, MockGenerator, Command
# Simple mock device (no code changes needed for testing)
device = MockDevice() # Auto-loads events from data/events/
event = device.receive_response(field_type="event")
# Mock device with command responses
response = device.query("GET_STATUS")
device.close()
# Mock device with speed-controlled events (for demos/simulations)
gen = MockGenerator.from_random(count=100, seed=42)
gen.set_speed(2.0).set_jitter(0.1)
with MockDevice(generator=gen) as device:
cmd = Command(device)
status = cmd.status()
event = cmd.read()
# Mock device with custom events from file
gen = MockGenerator.from_file("detector_data.jsonl")
device = MockDevice(generator=gen)
event = device.receive_response(field_type="event")
Source code in src/kazunoko/mock.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 | |
__enter__()
¶
Context manager entry
Source code in src/kazunoko/mock.py
511 512 513 | |
__exit__(exc_type, exc_val, exc_tb)
¶
Context manager exit
Source code in src/kazunoko/mock.py
515 516 517 | |
__init__(generator=None)
¶
Initialize mock device with data/responses from package
Source code in src/kazunoko/mock.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
close()
¶
Close mock device connection
Source code in src/kazunoko/mock.py
507 508 509 | |
query(command)
¶
Send command and receive response in one operation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
Text command to send |
required |
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
Simulated DeviceResponse object |
Source code in src/kazunoko/mock.py
493 494 495 496 497 498 499 500 501 502 503 504 505 | |
receive_response(field_type)
¶
Receive response from mock device
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_type
|
str
|
Expected response type ("response" or "event") |
required |
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
Simulated DeviceResponse object with received_us timestamp |
DeviceResponse
|
(in microseconds) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If device is not open |
Source code in src/kazunoko/mock.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | |
send_command(command)
¶
Send command to mock device (no-op, just validates)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
Text command to send |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if command is valid |
Raises:
| Type | Description |
|---|---|
ValueError
|
If device is not open |
Source code in src/kazunoko/mock.py
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
MockGenerator¶
kazunoko.mock.MockGenerator
¶
Generator for simulated detection events
Supports multiple data sources (file, random) with configurable speed and jitter to simulate realistic detector behavior.
Source code in src/kazunoko/mock.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
__init__()
¶
Initialize generator
Source code in src/kazunoko/mock.py
139 140 141 142 143 144 145 146 | |
from_file(path)
classmethod
¶
Load detection events from JSONL file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to JSONL file (one event per line) |
required |
Returns:
| Type | Description |
|---|---|
MockGenerator
|
MockGenerator instance with loaded events |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file does not exist |
JSONDecodeError
|
If JSONL format is invalid |
Source code in src/kazunoko/mock.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
from_random(count=1000, seed=None)
classmethod
¶
Load detection events from events.jsonl with optional shuffling
Loads events from events.jsonl file and optionally shuffles them based on seed. If count is larger than available events, cycles through events repeatedly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of events to return (cycles through file if needed) |
1000
|
seed
|
int | None
|
Random seed for shuffling events (None = no shuffle) |
None
|
Returns:
| Type | Description |
|---|---|
MockGenerator
|
MockGenerator instance with events loaded from events.jsonl |
Source code in src/kazunoko/mock.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
get_next_event()
¶
Get next event with speed and jitter applied
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
Next event dict with type set to "mock" and detected_us preserved from events.jsonl, |
dict[str, Any] | None
|
or None if no more events |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If used with from_random() without seed |
Source code in src/kazunoko/mock.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
reset()
¶
Reset generator to first event
Source code in src/kazunoko/mock.py
338 339 340 341 | |
set_jitter(jitter=0.0)
¶
Set time variation between events
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
jitter
|
float
|
Random time variation in seconds (0.0-inf) |
0.0
|
Returns:
| Type | Description |
|---|---|
MockGenerator
|
Self for method chaining |
Source code in src/kazunoko/mock.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
set_speed(speed=1.0)
¶
Set event generation speed multiplier
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
speed
|
float
|
Speed multiplier (0.01-100x) 1.0 = normal speed, 2.0 = 2x faster, 0.5 = half speed |
1.0
|
Returns:
| Type | Description |
|---|---|
MockGenerator
|
Self for method chaining |
Raises:
| Type | Description |
|---|---|
ValueError
|
If speed is outside valid range |
Source code in src/kazunoko/mock.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |