"""Chunk, content-hash, and a stale-base conflict. Not Google Drive and not Dropbox.

Chunks are 4 bytes so the printout fits. Drive's documented chunk multiple is 256 KiB.
"""

import hashlib


def digest(data: bytes) -> str:
    return hashlib.sha256(data).hexdigest()[:12]


def chunks(data: bytes, size: int = 4) -> list[bytes]:
    return [data[i : i + size] for i in range(0, len(data), size)]


def main() -> None:
    a = b"aaaabbbbcccc"
    b = b"xxxxbbbbcccc"
    store: dict[str, bytes] = {}
    def put(blob: bytes) -> list[str]:
        ids = []
        for piece in chunks(blob):
            key = digest(piece)
            store.setdefault(key, piece)
            ids.append(key)
        return ids
    print(f"file_a={put(a)}")
    print(f"file_b={put(b)}")
    print(f"unique_chunks={len(store)}")
    base = digest(a)
    left = digest(b"aaaabbbbdddd")
    right = digest(b"aaaabbbbeeee")
    print(f"base={base} left={left} right={right}")
    print(f"stale_base_conflict={left != base and right != base and left != right}")


if __name__ == "__main__":
    main()
