From 9d8fcca360a3e3139e792a1843fbab8bf8b46747 Mon Sep 17 00:00:00 2001 From: Reece Stevens Date: Wed, 22 May 2024 07:15:40 -0500 Subject: [PATCH 1/6] Add test case for clock-out bug --- tests/plenary/clock/init_spec.lua | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/plenary/clock/init_spec.lua diff --git a/tests/plenary/clock/init_spec.lua b/tests/plenary/clock/init_spec.lua new file mode 100644 index 000000000..53fd863cb --- /dev/null +++ b/tests/plenary/clock/init_spec.lua @@ -0,0 +1,55 @@ +local OrgFiles = require('orgmode.files') +local OrgFile = require('orgmode.files.file') +local Files = require('orgmode.parser.files') +local org = require('orgmode') + +describe('Clock', function() + ---@return OrgFile + local load_file_sync = function(content, filename) + content = content or {} + filename = filename or vim.fn.tempname() .. '.org' + vim.fn.writefile(content, filename) + return OrgFile.load(filename):wait() + end + + it('should properly close out an existing clock when clocking in a new headline', function() + local file = load_file_sync({ + '* TODO Test 1', + ' :LOGBOOK:', + ' CLOCK: [2024-05-22 Wed 05:15]', + ' :END:', + '* TODO Test 2', + }) + + vim.cmd('edit ' .. file.filename) + + Files.file_loader = OrgFiles:new({ + paths = { file.filename }, + }) + local files = Files.loader() + files:add_to_paths(file.filename):wait() + + -- Establish baseline: Test 1 is clocked in + local clock = org.clock:new({ files = files }) + assert.are.same('Test 1', clock.clocked_headline:get_title()) + assert.is_true(clock.clocked_headline:is_clocked_in()) + + -- Move the test 2 header above test 1 and then clock test 2 in + vim.fn.cursor({ 5, 1 }) + vim.cmd('normal! dd') + vim.fn.cursor({ 1, 1 }) + vim.cmd('normal! P') + vim.fn.cursor({ 1, 1 }) + clock:org_clock_in():wait() + file:reload():wait() + + -- Test 2 is properly clocked in + assert.are.same('Test 2', clock.clocked_headline:get_title()) + assert.are.same('Test 2', file:get_headlines()[1]:get_title()) + assert.is_true(file:get_headlines()[1]:is_clocked_in()) + + -- Test 1 is properly clocked out + assert.are.same('Test 1', file:get_headlines()[2]:get_title()) + assert.is_false(file:get_headlines()[2]:is_clocked_in()) + end) +end) From 8600c5092c91d13a6eb1beef9c7017f4b2b82453 Mon Sep 17 00:00:00 2001 From: Reece Stevens Date: Wed, 22 May 2024 07:16:12 -0500 Subject: [PATCH 2/6] Force reload of clocked headline before clock operations Without this update, certain changes to the buffer would not be reflected in the `self.clocked_headline` attribute. This could cause changes such as the auto-clock-out of a previously clocked header to be inserted in the wrong location, corrupting the file. --- lua/orgmode/clock/init.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lua/orgmode/clock/init.lua b/lua/orgmode/clock/init.lua index 20020eb09..cf953f9c7 100644 --- a/lua/orgmode/clock/init.lua +++ b/lua/orgmode/clock/init.lua @@ -26,11 +26,20 @@ function Clock:init() end end +function Clock:update_clocked_headline() + local last_clocked_headline = self.files:get_clocked_headline() + if last_clocked_headline and last_clocked_headline:is_clocked_in() then + self.clocked_headline = last_clocked_headline + end +end + function Clock:has_clocked_headline() + self:update_clocked_headline() return self.clocked_headline ~= nil end function Clock:org_clock_in() + self:update_clocked_headline() local item = self.files:get_closest_headline() if item:is_clocked_in() then return utils.echo_info(string.format('Clock continues in "%s"', item:get_title())) @@ -53,6 +62,7 @@ function Clock:org_clock_in() end function Clock:org_clock_out() + self:update_clocked_headline() if not self.clocked_headline or not self.clocked_headline:is_clocked_in() then return end @@ -62,6 +72,7 @@ function Clock:org_clock_out() end function Clock:org_clock_cancel() + self:update_clocked_headline() if not self.clocked_headline or not self.clocked_headline:is_clocked_in() then return utils.echo_info('No active clock') end @@ -72,6 +83,7 @@ function Clock:org_clock_cancel() end function Clock:org_clock_goto() + self:update_clocked_headline() if not self.clocked_headline then return utils.echo_info('No active or recent clock task') end @@ -99,6 +111,7 @@ function Clock:org_set_effort() end function Clock:get_statusline() + self:update_clocked_headline() if not self.clocked_headline or not self.clocked_headline:is_clocked_in() then return '' end From a9e8084aa037f004fca6be8f11b38e3419ec0a2e Mon Sep 17 00:00:00 2001 From: Reece Stevens Date: Sat, 26 Oct 2024 13:25:11 -0500 Subject: [PATCH 3/6] Upgrade tests to use new file loading schema --- tests/plenary/clock/init_spec.lua | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/plenary/clock/init_spec.lua b/tests/plenary/clock/init_spec.lua index 53fd863cb..187fe54bb 100644 --- a/tests/plenary/clock/init_spec.lua +++ b/tests/plenary/clock/init_spec.lua @@ -1,6 +1,5 @@ local OrgFiles = require('orgmode.files') local OrgFile = require('orgmode.files.file') -local Files = require('orgmode.parser.files') local org = require('orgmode') describe('Clock', function() @@ -23,14 +22,12 @@ describe('Clock', function() vim.cmd('edit ' .. file.filename) - Files.file_loader = OrgFiles:new({ + orgmode.files = OrgFiles:new({ paths = { file.filename }, - }) - local files = Files.loader() - files:add_to_paths(file.filename):wait() + }):load_sync(true, 20000) -- Establish baseline: Test 1 is clocked in - local clock = org.clock:new({ files = files }) + local clock = org.clock:new({ files = orgmode.files }) assert.are.same('Test 1', clock.clocked_headline:get_title()) assert.is_true(clock.clocked_headline:is_clocked_in()) From 722b6578bf4d9e804b86d5b4464dfbddda619e5a Mon Sep 17 00:00:00 2001 From: Reece Stevens Date: Sat, 26 Oct 2024 13:28:45 -0500 Subject: [PATCH 4/6] Remove clocking update when refreshing statusline --- lua/orgmode/clock/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/orgmode/clock/init.lua b/lua/orgmode/clock/init.lua index cf953f9c7..19d5d6001 100644 --- a/lua/orgmode/clock/init.lua +++ b/lua/orgmode/clock/init.lua @@ -111,7 +111,6 @@ function Clock:org_set_effort() end function Clock:get_statusline() - self:update_clocked_headline() if not self.clocked_headline or not self.clocked_headline:is_clocked_in() then return '' end From 406ceb071623c02ee3385fa1f1f9f960f441a677 Mon Sep 17 00:00:00 2001 From: Reece Stevens Date: Mon, 4 Nov 2024 20:21:10 -0600 Subject: [PATCH 5/6] Move test to clock UI test file --- tests/plenary/clock/init_spec.lua | 52 ------------------------------- tests/plenary/ui/clock_spec.lua | 37 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 52 deletions(-) delete mode 100644 tests/plenary/clock/init_spec.lua diff --git a/tests/plenary/clock/init_spec.lua b/tests/plenary/clock/init_spec.lua deleted file mode 100644 index 187fe54bb..000000000 --- a/tests/plenary/clock/init_spec.lua +++ /dev/null @@ -1,52 +0,0 @@ -local OrgFiles = require('orgmode.files') -local OrgFile = require('orgmode.files.file') -local org = require('orgmode') - -describe('Clock', function() - ---@return OrgFile - local load_file_sync = function(content, filename) - content = content or {} - filename = filename or vim.fn.tempname() .. '.org' - vim.fn.writefile(content, filename) - return OrgFile.load(filename):wait() - end - - it('should properly close out an existing clock when clocking in a new headline', function() - local file = load_file_sync({ - '* TODO Test 1', - ' :LOGBOOK:', - ' CLOCK: [2024-05-22 Wed 05:15]', - ' :END:', - '* TODO Test 2', - }) - - vim.cmd('edit ' .. file.filename) - - orgmode.files = OrgFiles:new({ - paths = { file.filename }, - }):load_sync(true, 20000) - - -- Establish baseline: Test 1 is clocked in - local clock = org.clock:new({ files = orgmode.files }) - assert.are.same('Test 1', clock.clocked_headline:get_title()) - assert.is_true(clock.clocked_headline:is_clocked_in()) - - -- Move the test 2 header above test 1 and then clock test 2 in - vim.fn.cursor({ 5, 1 }) - vim.cmd('normal! dd') - vim.fn.cursor({ 1, 1 }) - vim.cmd('normal! P') - vim.fn.cursor({ 1, 1 }) - clock:org_clock_in():wait() - file:reload():wait() - - -- Test 2 is properly clocked in - assert.are.same('Test 2', clock.clocked_headline:get_title()) - assert.are.same('Test 2', file:get_headlines()[1]:get_title()) - assert.is_true(file:get_headlines()[1]:is_clocked_in()) - - -- Test 1 is properly clocked out - assert.are.same('Test 1', file:get_headlines()[2]:get_title()) - assert.is_false(file:get_headlines()[2]:is_clocked_in()) - end) -end) diff --git a/tests/plenary/ui/clock_spec.lua b/tests/plenary/ui/clock_spec.lua index 39f2d7fed..c413f908f 100644 --- a/tests/plenary/ui/clock_spec.lua +++ b/tests/plenary/ui/clock_spec.lua @@ -1,5 +1,7 @@ local helpers = require('tests.plenary.helpers') local Date = require('orgmode.objects.date') +local orgmode = require('orgmode') + describe('Clock', function() local files = {} @@ -149,4 +151,39 @@ describe('Clock', function() assert.are.same('', vim.fn.getline(6)) assert.are.same('', vim.fn.getline(7)) end) + + it('should properly clock in an entry if unsaved edits were made to the buffer', function() + local file = helpers.create_agenda_file({ + '* TODO Test 1', + ' :LOGBOOK:', + ' CLOCK: [2024-05-22 Wed 05:15]', + ' :END:', + '* TODO Test 2', + }) + + vim.cmd('edit ' .. file.filename) + + -- Establish baseline: Test 1 is clocked in + local clock = orgmode.clock + assert.are.same('Test 1', clock.clocked_headline:get_title()) + assert.is_true(clock.clocked_headline:is_clocked_in()) + + -- Move the test 2 header above test 1 and then clock test 2 in + vim.fn.cursor({ 5, 1 }) + vim.cmd([[norm dd]]) + vim.fn.cursor({ 1, 1 }) + vim.cmd([[norm P]]) + vim.fn.cursor({ 1, 1 }) + clock:org_clock_in():wait() + file:reload():wait() + + -- Test 2 is properly clocked in + assert.are.same('Test 2', clock.clocked_headline:get_title()) + assert.are.same('Test 2', file:get_headlines()[1]:get_title()) + assert.is_true(file:get_headlines()[1]:is_clocked_in()) + + -- Test 1 is properly clocked out + assert.are.same('Test 1', file:get_headlines()[2]:get_title()) + assert.is_false(file:get_headlines()[2]:is_clocked_in()) + end) end) From bd648ee2a176b3b95f946f229e2c9cf76f49590d Mon Sep 17 00:00:00 2001 From: Reece Stevens Date: Mon, 4 Nov 2024 20:23:08 -0600 Subject: [PATCH 6/6] Fix lint error --- tests/plenary/ui/clock_spec.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/plenary/ui/clock_spec.lua b/tests/plenary/ui/clock_spec.lua index c413f908f..6ee39ba92 100644 --- a/tests/plenary/ui/clock_spec.lua +++ b/tests/plenary/ui/clock_spec.lua @@ -2,7 +2,6 @@ local helpers = require('tests.plenary.helpers') local Date = require('orgmode.objects.date') local orgmode = require('orgmode') - describe('Clock', function() local files = {} after_each(function()