2016-09-13 03:02:48 +00:00
|
|
|
describe("ReadHistory module", function()
|
|
|
|
local DocSettings
|
|
|
|
local DataStorage
|
2017-07-19 12:56:17 +00:00
|
|
|
local joinPath
|
2016-09-13 03:02:48 +00:00
|
|
|
local mkdir
|
|
|
|
local realpath
|
2017-07-19 12:56:17 +00:00
|
|
|
local reload
|
2020-12-15 20:10:36 +00:00
|
|
|
local lfs
|
|
|
|
local now = 61
|
2016-09-13 03:02:48 +00:00
|
|
|
|
|
|
|
local function file(name)
|
|
|
|
return joinPath(DataStorage:getDataDir(), name)
|
|
|
|
end
|
|
|
|
|
2017-07-19 12:56:17 +00:00
|
|
|
local function test_data_dir()
|
|
|
|
return joinPath(DataStorage:getDataDir(), "testdata")
|
|
|
|
end
|
|
|
|
|
2016-09-13 03:02:48 +00:00
|
|
|
local function test_file(name)
|
2017-07-19 12:56:17 +00:00
|
|
|
return joinPath(test_data_dir(), name)
|
2016-09-13 03:02:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function legacy_history_file(name)
|
|
|
|
return DocSettings:getHistoryPath(realpath(test_file(name)))
|
|
|
|
end
|
|
|
|
|
2017-08-08 20:35:40 +00:00
|
|
|
local function rm(filename)
|
|
|
|
os.remove(filename)
|
2016-09-13 03:02:48 +00:00
|
|
|
end
|
|
|
|
|
2017-07-19 12:56:17 +00:00
|
|
|
local function mv(source, target)
|
|
|
|
os.rename(source, target)
|
|
|
|
end
|
|
|
|
|
2017-08-08 20:35:40 +00:00
|
|
|
local function touch(filename)
|
2020-12-15 20:10:36 +00:00
|
|
|
-- Create file if need be
|
2017-08-08 20:35:40 +00:00
|
|
|
local f = io.open(filename, "w")
|
2016-09-13 03:02:48 +00:00
|
|
|
f:close()
|
2020-12-15 20:10:36 +00:00
|
|
|
-- Increment by 61s every time we're called
|
|
|
|
now = now + 61
|
|
|
|
lfs.touch(filename, now, now)
|
2016-09-13 03:02:48 +00:00
|
|
|
end
|
|
|
|
|
2017-07-19 12:56:17 +00:00
|
|
|
local function assert_item_is(h, i, name, fileRemoved)
|
2020-08-24 23:47:04 +00:00
|
|
|
assert.is.same(name, h.hist[i].text)
|
|
|
|
assert.is.same(joinPath(realpath(test_data_dir()), name), h.hist[i].file)
|
2017-07-19 12:56:17 +00:00
|
|
|
if fileRemoved then
|
|
|
|
assert.is_nil(realpath(test_file(name)))
|
|
|
|
else
|
2020-08-24 23:47:04 +00:00
|
|
|
assert.is.same(realpath(test_file(name)), h.hist[i].file)
|
2017-07-19 12:56:17 +00:00
|
|
|
end
|
2016-09-13 03:02:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
setup(function()
|
|
|
|
require("commonrequire")
|
|
|
|
DocSettings = require("docsettings")
|
|
|
|
DataStorage = require("datastorage")
|
2017-07-19 12:56:17 +00:00
|
|
|
joinPath = require("ffi/util").joinPath
|
2016-09-13 03:02:48 +00:00
|
|
|
mkdir = require("libs/libkoreader-lfs").mkdir
|
|
|
|
realpath = require("ffi/util").realpath
|
2017-07-19 12:56:17 +00:00
|
|
|
reload = function() return package.reload("readhistory") end
|
2020-12-15 20:10:36 +00:00
|
|
|
lfs = require("libs/libkoreader-lfs")
|
2016-09-13 03:02:48 +00:00
|
|
|
|
|
|
|
mkdir(joinPath(DataStorage:getDataDir(), "testdata"))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should read empty history.lua", function()
|
|
|
|
rm(file("history.lua"))
|
|
|
|
local h = reload()
|
|
|
|
assert.is.same(#h.hist, 0)
|
|
|
|
touch(file("history.lua"))
|
|
|
|
h = reload()
|
|
|
|
assert.is.same(#h.hist, 0)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should read non-empty history.lua", function()
|
|
|
|
rm(file("history.lua"))
|
|
|
|
local h = reload()
|
|
|
|
touch(test_file("a"))
|
2020-12-15 20:10:36 +00:00
|
|
|
now = now + 61
|
|
|
|
h:addItem(test_file("a"), now)
|
2016-09-13 03:02:48 +00:00
|
|
|
h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
assert.is.same(1, #h.hist)
|
2016-09-13 03:02:48 +00:00
|
|
|
assert_item_is(h, 1, "a")
|
|
|
|
rm(test_file("a"))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should order legacy and history.lua", function()
|
|
|
|
rm(file("history.lua"))
|
|
|
|
touch(test_file("a"))
|
|
|
|
touch(test_file("b"))
|
|
|
|
local h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
now = now + 61
|
|
|
|
h:addItem(test_file("a"), now)
|
2023-03-02 06:39:09 +00:00
|
|
|
mkdir(DataStorage:getHistoryDir())
|
2016-09-13 03:02:48 +00:00
|
|
|
touch(legacy_history_file("b"))
|
|
|
|
h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
assert.is.same(2, #h.hist)
|
2016-09-13 03:02:48 +00:00
|
|
|
assert_item_is(h, 1, "b")
|
|
|
|
assert_item_is(h, 2, "a")
|
|
|
|
rm(legacy_history_file("b"))
|
|
|
|
rm(test_file("a"))
|
|
|
|
rm(test_file("b"))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should read legacy history folder", function()
|
|
|
|
rm(file("history.lua"))
|
|
|
|
touch(test_file("a"))
|
|
|
|
touch(test_file("b"))
|
|
|
|
touch(test_file("c"))
|
|
|
|
touch(test_file("d"))
|
|
|
|
touch(test_file("e"))
|
|
|
|
touch(test_file("f"))
|
|
|
|
local h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
now = now + 61
|
|
|
|
h:addItem(test_file("f"), now)
|
2023-03-02 06:39:09 +00:00
|
|
|
mkdir(DataStorage:getHistoryDir())
|
2016-09-13 03:02:48 +00:00
|
|
|
touch(legacy_history_file("c"))
|
|
|
|
touch(legacy_history_file("b"))
|
2020-12-15 20:10:36 +00:00
|
|
|
now = now + 61
|
|
|
|
h:addItem(test_file("d"), now)
|
2016-09-13 03:02:48 +00:00
|
|
|
touch(legacy_history_file("a"))
|
2020-12-15 20:10:36 +00:00
|
|
|
now = now + 61
|
|
|
|
h:addItem(test_file("e"), now)
|
2016-09-13 03:02:48 +00:00
|
|
|
h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
assert.is.same(6, #h.hist)
|
2016-09-13 03:02:48 +00:00
|
|
|
assert_item_is(h, 1, "e")
|
|
|
|
assert_item_is(h, 2, "a")
|
|
|
|
assert_item_is(h, 3, "d")
|
|
|
|
assert_item_is(h, 4, "b")
|
|
|
|
assert_item_is(h, 5, "c")
|
|
|
|
assert_item_is(h, 6, "f")
|
|
|
|
|
|
|
|
rm(legacy_history_file("c"))
|
|
|
|
rm(legacy_history_file("b"))
|
|
|
|
rm(legacy_history_file("a"))
|
|
|
|
rm(test_file("a"))
|
|
|
|
rm(test_file("b"))
|
|
|
|
rm(test_file("c"))
|
|
|
|
rm(test_file("d"))
|
|
|
|
rm(test_file("e"))
|
|
|
|
rm(test_file("f"))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should add item", function()
|
|
|
|
rm(file("history.lua"))
|
|
|
|
touch(test_file("a"))
|
|
|
|
local h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
now = now + 61
|
|
|
|
h:addItem(test_file("a"), now)
|
|
|
|
assert.is.same(1, #h.hist)
|
2016-09-13 03:02:48 +00:00
|
|
|
assert_item_is(h, 1, "a")
|
|
|
|
rm(test_file("a"))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should be able to remove the first item", function()
|
|
|
|
rm(file("history.lua"))
|
|
|
|
touch(test_file("a"))
|
|
|
|
touch(test_file("b"))
|
|
|
|
touch(test_file("c"))
|
|
|
|
local h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
-- NOTE: Identical timestamps to neuter sorting by mtime, instead alphabetical order kicks in (c.f., ReadHistory:_sort)
|
|
|
|
-- This goes for basically the rest of the tests.
|
|
|
|
now = now + 61
|
|
|
|
h:addItem(test_file("a"), now)
|
|
|
|
h:addItem(test_file("b"), now)
|
|
|
|
h:addItem(test_file("c"), now)
|
2016-09-13 03:02:48 +00:00
|
|
|
h:removeItem(h.hist[1])
|
|
|
|
assert_item_is(h, 1, "b")
|
|
|
|
assert_item_is(h, 2, "c")
|
|
|
|
h:removeItem(h.hist[1])
|
|
|
|
assert_item_is(h, 1, "c")
|
|
|
|
rm(test_file("a"))
|
|
|
|
rm(test_file("b"))
|
|
|
|
rm(test_file("c"))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should be able to remove an item in the middle", function()
|
|
|
|
rm(file("history.lua"))
|
|
|
|
touch(test_file("a"))
|
|
|
|
touch(test_file("b"))
|
|
|
|
touch(test_file("c"))
|
|
|
|
local h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
now = now + 61
|
|
|
|
h:addItem(test_file("a"), now)
|
|
|
|
h:addItem(test_file("b"), now)
|
|
|
|
h:addItem(test_file("c"), now)
|
2016-09-13 03:02:48 +00:00
|
|
|
h:removeItem(h.hist[2])
|
|
|
|
assert_item_is(h, 1, "a")
|
|
|
|
assert_item_is(h, 2, "c")
|
|
|
|
rm(test_file("a"))
|
|
|
|
rm(test_file("b"))
|
|
|
|
rm(test_file("c"))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should be able to remove the last item", function()
|
|
|
|
rm(file("history.lua"))
|
|
|
|
touch(test_file("a"))
|
|
|
|
touch(test_file("b"))
|
|
|
|
touch(test_file("c"))
|
|
|
|
local h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
now = now + 61
|
|
|
|
h:addItem(test_file("a"), now)
|
|
|
|
h:addItem(test_file("b"), now)
|
|
|
|
h:addItem(test_file("c"), now)
|
2016-09-13 03:02:48 +00:00
|
|
|
h:removeItem(h.hist[3])
|
|
|
|
assert_item_is(h, 1, "a")
|
|
|
|
assert_item_is(h, 2, "b")
|
|
|
|
h:removeItem(h.hist[2])
|
|
|
|
assert_item_is(h, 1, "a")
|
|
|
|
rm(test_file("a"))
|
|
|
|
rm(test_file("b"))
|
|
|
|
rm(test_file("c"))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should be able to remove two items", function()
|
|
|
|
rm(file("history.lua"))
|
|
|
|
touch(test_file("a"))
|
|
|
|
touch(test_file("b"))
|
|
|
|
touch(test_file("c"))
|
|
|
|
touch(test_file("d"))
|
|
|
|
local h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
now = now + 61
|
|
|
|
h:addItem(test_file("a"), now)
|
|
|
|
h:addItem(test_file("b"), now)
|
|
|
|
h:addItem(test_file("c"), now)
|
|
|
|
h:addItem(test_file("d"), now)
|
2016-09-13 03:02:48 +00:00
|
|
|
h:removeItem(h.hist[3]) -- remove c
|
|
|
|
h:removeItem(h.hist[2]) -- remove b
|
|
|
|
assert_item_is(h, 1, "a")
|
|
|
|
assert_item_is(h, 2, "d")
|
|
|
|
rm(test_file("a"))
|
|
|
|
rm(test_file("b"))
|
|
|
|
rm(test_file("c"))
|
|
|
|
rm(test_file("d"))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should be able to remove three items", function()
|
|
|
|
rm(file("history.lua"))
|
|
|
|
touch(test_file("a"))
|
|
|
|
touch(test_file("b"))
|
|
|
|
touch(test_file("c"))
|
|
|
|
touch(test_file("d"))
|
|
|
|
touch(test_file("e"))
|
|
|
|
local h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
now = now + 61
|
|
|
|
h:addItem(test_file("a"), now)
|
|
|
|
h:addItem(test_file("b"), now)
|
|
|
|
h:addItem(test_file("c"), now)
|
|
|
|
h:addItem(test_file("d"), now)
|
|
|
|
h:addItem(test_file("e"), now)
|
2016-09-13 03:02:48 +00:00
|
|
|
h:removeItem(h.hist[2]) -- remove b
|
|
|
|
h:removeItem(h.hist[2]) -- remove c
|
|
|
|
h:removeItem(h.hist[3]) -- remove e
|
|
|
|
assert_item_is(h, 1, "a")
|
|
|
|
assert_item_is(h, 2, "d")
|
|
|
|
rm(test_file("a"))
|
|
|
|
rm(test_file("b"))
|
|
|
|
rm(test_file("c"))
|
|
|
|
rm(test_file("d"))
|
|
|
|
rm(test_file("e"))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should reduce the total count", function()
|
|
|
|
local function to_file(i)
|
|
|
|
return test_file(string.format("%04d", i))
|
|
|
|
end
|
|
|
|
rm(file("history.lua"))
|
|
|
|
local h = reload()
|
|
|
|
for i = 1000, 1, -1 do
|
|
|
|
touch(to_file(i))
|
|
|
|
h:addItem(to_file(i))
|
|
|
|
end
|
|
|
|
|
|
|
|
for i = 1, 500 do -- at most 500 items are stored
|
|
|
|
assert_item_is(h, i, string.format("%04d", i))
|
|
|
|
end
|
2016-11-24 02:48:00 +00:00
|
|
|
|
2016-09-13 03:02:48 +00:00
|
|
|
for i = 1, 1000 do
|
|
|
|
rm(to_file(i))
|
|
|
|
end
|
|
|
|
end)
|
2017-07-19 12:56:17 +00:00
|
|
|
|
|
|
|
it("should reload the history file if it updated", function()
|
|
|
|
-- Prepare a history.lua file with two items a and b.
|
|
|
|
rm(file("history.lua"))
|
|
|
|
touch(test_file("a"))
|
|
|
|
touch(test_file("b"))
|
|
|
|
local h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
now = now + 61
|
|
|
|
h:addItem(test_file("a"), now)
|
|
|
|
h:addItem(test_file("b"), now)
|
2017-07-19 12:56:17 +00:00
|
|
|
mv(file("history.lua"), file("history.backup"))
|
|
|
|
|
|
|
|
h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
assert.is.same(0, #h.hist)
|
2017-07-19 12:56:17 +00:00
|
|
|
mv(file("history.backup"), file("history.lua"))
|
|
|
|
h:reload()
|
|
|
|
|
2020-12-15 20:10:36 +00:00
|
|
|
assert.is.same(2, #h.hist)
|
2017-07-19 12:56:17 +00:00
|
|
|
assert_item_is(h, 1, "a")
|
|
|
|
assert_item_is(h, 2, "b")
|
|
|
|
|
|
|
|
rm(test_file("a"))
|
|
|
|
rm(test_file("b"))
|
|
|
|
end)
|
|
|
|
|
|
|
|
local function testAutoRemoveDeletedItems()
|
|
|
|
-- Prepare a history.lua file with two items a and b.
|
|
|
|
rm(file("history.lua"))
|
|
|
|
touch(test_file("a"))
|
|
|
|
touch(test_file("b"))
|
|
|
|
local h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
now = now + 61
|
|
|
|
h:addItem(test_file("a"), now)
|
|
|
|
h:addItem(test_file("b"), now)
|
2017-07-19 12:56:17 +00:00
|
|
|
|
|
|
|
rm(test_file("a"))
|
|
|
|
|
|
|
|
h:reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
assert.is.same(1, #h.hist)
|
2017-07-19 12:56:17 +00:00
|
|
|
assert_item_is(h, 1, "b")
|
|
|
|
|
|
|
|
rm(test_file("b"))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function testDoNotAutoRemoveDeletedItems()
|
|
|
|
-- Prepare a history.lua file with two items a and b.
|
|
|
|
rm(file("history.lua"))
|
|
|
|
touch(test_file("a"))
|
|
|
|
touch(test_file("b"))
|
|
|
|
local h = reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
now = now + 61
|
|
|
|
h:addItem(test_file("a"), now)
|
|
|
|
h:addItem(test_file("b"), now)
|
2017-07-19 12:56:17 +00:00
|
|
|
|
|
|
|
rm(test_file("a"))
|
|
|
|
|
|
|
|
h:reload()
|
2020-12-15 20:10:36 +00:00
|
|
|
assert.is.same(2, #h.hist)
|
2017-07-19 12:56:17 +00:00
|
|
|
assert_item_is(h, 1, "a", true)
|
|
|
|
assert_item_is(h, 2, "b")
|
|
|
|
|
|
|
|
rm(test_file("b"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it("should automatically remove deleted items from history if setting has been set",
|
|
|
|
function()
|
2021-03-06 21:44:18 +00:00
|
|
|
G_reader_settings:saveSetting("autoremove_deleted_items_from_history", true)
|
2017-07-19 12:56:17 +00:00
|
|
|
testAutoRemoveDeletedItems()
|
|
|
|
G_reader_settings:delSetting("autoremove_deleted_items_from_history")
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should not automatically remove deleted items from history if setting has not been set",
|
|
|
|
function()
|
|
|
|
G_reader_settings:delSetting("autoremove_deleted_items_from_history")
|
|
|
|
testDoNotAutoRemoveDeletedItems()
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("should not automatically remove deleted items from history if setting has been set to false",
|
|
|
|
function()
|
2021-03-06 21:44:18 +00:00
|
|
|
G_reader_settings:saveSetting("autoremove_deleted_items_from_history", false)
|
2017-07-19 12:56:17 +00:00
|
|
|
testDoNotAutoRemoveDeletedItems()
|
|
|
|
G_reader_settings:delSetting("autoremove_deleted_items_from_history")
|
|
|
|
end)
|
2016-09-13 03:02:48 +00:00
|
|
|
end)
|