-- SPDX-FileCopyrightText: © 2026 Vladimir Zorin -- SPDX-License-Identifier: OWL-1.0 or later local testimony = require("testimony") local std = require("std") local mneme = require("mneme") local testify = testimony.new("== mneme: MVCC origin tracking ==") local tmp_path = function() return "/tmp/mneme_mvcc_" .. std.nanoid() .. ".mneme" end local function setup() local path = tmp_path() local db = mneme.open(path, { sync = "none" }) local txn = db:txn_begin(false) local info = db:info() return db, txn, info.keyspace_root_pgno, path end local function teardown(db, path) db:close() std.fs.remove(path) end testify:that("freeing same-txn allocated page does not add to pending retire", function() local db, txn, _, path = setup() local pgno = txn:page_alloc() testimony.assert_not_nil(pgno) testimony.assert_not_nil(txn:page_free(pgno)) local meta = txn:meta() testimony.assert_not_nil(meta) testimony.assert_not_nil(txn:commit()) teardown(db, path) end) testify:that("same-txn overflow replacement commits cleanly", function() local db, txn, root, path = setup() local v1 = string.rep("A", 5000) local v2 = string.rep("B", 6000) root = txn:btree_put(root, "big", v1) testimony.assert_not_nil(root) root = txn:btree_put(root, "big", v2) testimony.assert_not_nil(root) local got = txn:btree_get(root, "big") testimony.assert_equal(v2, got) testimony.assert_not_nil(txn:commit()) local rtxn = db:txn_begin(true) local reread = rtxn:btree_get(root, "big") testimony.assert_equal(v2, reread) rtxn:abort() teardown(db, path) end) testify:that("branch split path with later branch split commits cleanly", function() local db, txn, root, path = setup() for i = 1, 1200 do local k = string.format("k:%06d", i) local v = string.rep(string.char(65 + (i % 26)), 40) root = txn:btree_put(root, k, v) testimony.assert_not_nil(root) end for i = 1, 1200 do local k = string.format("k:%06d", i) local got = txn:btree_get(root, k) testimony.assert_not_nil(got) end testimony.assert_not_nil(txn:commit()) teardown(db, path) end) testify:conclude()