# Copyright (c) reifydb.com 2025
# This file is licensed under the AGPL-3.0-or-later

# This file includes and modifies code from the toydb project (https://github.com/erikgrinaker/toydb),
# originally licensed under the Apache License, Version 2.0.
# Original copyright:
#   Copyright (c) 2024 Erik Grinaker
#
# The original Apache License can be found at:
#   http://www.apache.org/licenses/LICENSE-2.0

# Tests that the key encoding is resistant to key/version overlap.
# For example, a naïve concatenation of keys and versions would
# produce incorrect ordering in this case:
#
# 00|00 00 00 00 00 00 00 01
# 00 00 00 00 00 00 00 00 02|00 00 00 00 00 00 00 02
# 00|00 00 00 00 00 00 00 03

t1: begin
t1: set "\x00"="\x01" [ops]
t1: commit
---
t1: engine set mvcc:TxWrite(1, "\x00") → "" ["\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\xff\x00\x00" → ""]
t1: engine set mvcc:Version("\x00", 1) → "\x01" ["\x04\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" → "\x01\x01\x01"]

t2: begin
t2: set "\x00"="\x02" [ops]
t2: set "\x00\x00\x00\x00\x00\x00\x00\x00\x02"="\x02" [ops]
t2: commit
---
t2: engine set mvcc:TxWrite(2, "\x00") → "" ["\x03\x00\x00\x00\x00\x00\x00\x00\x02\x00\xff\x00\x00" → ""]
t2: engine set mvcc:Version("\x00", 2) → "\x02" ["\x04\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02" → "\x01\x01\x02"]
t2: engine set mvcc:TxWrite(2, "\x00\x00\x00\x00\x00\x00\x00\x00\x02") → "" ["\x03\x00\x00\x00\x00\x00\x00\x00\x02\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x02\x00\x00" → ""]
t2: engine set mvcc:Version("\x00\x00\x00\x00\x00\x00\x00\x00\x02", 2) → "\x02" ["\x04\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02" → "\x01\x01\x02"]

t3: begin
t3: set "\x00"="\x03" [ops]
t3: commit
---
t3: engine set mvcc:TxWrite(3, "\x00") → "" ["\x03\x00\x00\x00\x00\x00\x00\x00\x03\x00\xff\x00\x00" → ""]
t3: engine set mvcc:Version("\x00", 3) → "\x03" ["\x04\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03" → "\x01\x01\x03"]

t4: begin readonly
t4: scan
---
t4: "\x00" → "\x03"
t4: "\x00\x00\x00\x00\x00\x00\x00\x00\x02" → "\x02"
