"""Line harness for fleetmesh_core/storage/negentropy.py, speaking hoytech's conformance protocol.
The negentropy project drives every implementation it tests through one stdin
protocol: `item,<created>,<id>` to load, `seal`, then `initiate` or
`msg,<hex>`, answering with `have,<id>`, `need,<id>`, `msg,<hex>` or `done`.
Its own harnesses are test/js/harness.js and test/cpp/harness.cpp; this is the
same program for ours, so the reference's driver can put the two
implementations on opposite ends of a reconciliation without either knowing.
python3 -m fleetmesh_core.conformance.harness < commands
Only the client is told anything by the protocol, so `have` and `need` are
printed by whichever side received `initiate`.
"""
from __future__ import annotations
import sys
from typing import Optional
from ..storage import negentropy
[docs]def main() -> None:
items: list[tuple[int, str]] = []
ne: Optional[negentropy.Negentropy] = None
for line in sys.stdin:
line = line.strip()
if not line:
continue
parts = line.split(',')
cmd = parts[0]
match cmd:
case 'item':
items.append((int(parts[1]), parts[2].strip()))
case 'seal':
pass # fleetmesh_core/storage/negentropy.py sorts at construction
case 'initiate':
ne = negentropy.Negentropy(items, is_client=True)
print('msg,%s' % ne.initiate().hex())
case 'msg':
if ne is None:
ne = negentropy.Negentropy(items, is_client=False)
reply, have, need = ne.reconcile(bytes.fromhex(parts[1]))
for i in have:
print('have,%s' % i)
for i in need:
print('need,%s' % i)
if reply is None:
# A server always answers. Nothing to say is the empty
# message, which is the version byte alone - the reference
# does the same.
print('done' if ne.is_client else 'msg,%02x' % negentropy.VERSION)
else:
print('msg,%s' % reply.hex())
case _:
raise SystemExit('unknown cmd: %s' % cmd)
sys.stdout.flush()
if __name__ == '__main__':
main()