-- SPDX-FileCopyrightText: © 2022—2026 Vladimir Zorin -- SPDX-License-Identifier: LicenseRef-OWL-1.0-or-later -- Licensed under OWL v1.0+. See LICENSE. local testimony = require("testimony") local cache = require("lilgit.cache") local testify = testimony.new("== lilgit.cache ==") testify:that("get/set roundtrip, miss returns nil", function() local c = cache.new(4) testimony.assert_nil(c:get("a")) c:set("a", "va") testimony.assert_equal("va", c:get("a")) c:set("a", "va2") testimony.assert_equal("va2", c:get("a")) end) testify:that("entries survive one rotation via the stale generation", function() local c = cache.new(2) c:set("a", "va") c:set("b", "vb") -- Cap reached: a and b are now stale, fresh is empty. testimony.assert_equal("va", c:get("a")) testimony.assert_equal("vb", c:get("b")) end) testify:that("cold entries evict after two rotations", function() local c = cache.new(2) c:set("a", "va") c:set("b", "vb") -- First rotation done; fill the fresh generation without touching a. c:set("c", "vc") c:set("d", "vd") -- Second rotation: a and b are gone, c and d are stale. testimony.assert_nil(c:get("a")) testimony.assert_nil(c:get("b")) testimony.assert_equal("vc", c:get("c")) testimony.assert_equal("vd", c:get("d")) end) testify:that("a promoted hit outlives the generation it was born in", function() local c = cache.new(2) c:set("a", "va") c:set("b", "vb") -- Promote a into the fresh generation. testimony.assert_equal("va", c:get("a")) -- Fill and rotate again: b (never touched) dies, a survives. c:set("c", "vc") testimony.assert_nil(c:get("b")) testimony.assert_equal("va", c:get("a")) end) testify:that("live entries never exceed twice the cap", function() local c = cache.new(3) for i = 1, 100 do c:set("k" .. i, i) end local live = 0 for _ in pairs(c.__state.fresh) do live = live + 1 end for _ in pairs(c.__state.stale) do live = live + 1 end testimony.assert_true(live <= 6, "live entries: " .. live) end) testify:conclude()