mirror of
https://github.com/rickbarrette/stopwatch.git
synced 2026-11-17 11:50:36 -05:00
Timer safety: length limit, server-side saving, stop on close
- Maximum timer length setting (default 10h): a timer that runs past it is stopped at the limit and its entry's comment is flagged, so a forgotten timer can't add a night or weekend of hours - StopwatchTimerTickJob saves a running timer's time every minute on the server (no browser needed) and stops it at the limit on time; open tabs keep saving too - Timer updates lock the user's preference row and re-read the timer, so simultaneous saves (several tabs, the job) never add time twice - Closing an issue stops timers running on it before anything else (e.g. billing) uses its time; deleting a time entry only stops the timer running on that entry - Issue timers require a visible issue; starting again moves the timer instead of raising "timer is already running" - Tests updated and extended (limit, locking, tick job, close, delete, visibility, issue context menu); CI runs on Redmine 7.0 - Upstream's German notes translated Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
15 files changed
+366
-36
No files matched your search
@@ -14,8 +14,7 @@ jobs:
|
||||
strategy:
|
||||
matrix:
|
||||
redmine:
|
||||
- '6.0'
|
||||
- '6.1'
|
||||
- '7.0'
|
||||
ruby:
|
||||
- '3.3'
|
||||
database:
|
||||
|
||||
@@ -28,6 +28,17 @@ Rick Barrette (2026):
|
||||
- **Colour-coded Start/Stop:** an optional setting ("Color-code Start/Stop") shows the issue Start
|
||||
link in green and Stop in red, with tinted buttons on mobile (March 2026; colours adjusted for
|
||||
readable contrast in October 2026).
|
||||
- **Timer safety (October 2026):**
|
||||
- *Maximum timer length* setting (default 10 hours): a forgotten timer is stopped at the limit and
|
||||
its time entry's comment is flagged, instead of adding a night or weekend of hours.
|
||||
- Running timers are saved every minute on the server (a background job), so issues show the time
|
||||
live even with no browser open; open tabs keep saving too. Saves lock the timer, so several tabs
|
||||
can't add the same time twice.
|
||||
- Closing an issue stops any timer running on it first, so the logged time is final (e.g. before
|
||||
billing). Deleting a time entry only stops the timer if it runs on that entry.
|
||||
- Issue timers require an issue the user can see; starting twice moves the timer instead of
|
||||
erroring.
|
||||
- Tests run against Redmine 7.0.
|
||||
|
||||
## Installation
|
||||
|
||||
|
||||
@@ -41,8 +41,11 @@ class StopwatchIssueTimersController < StopwatchController
|
||||
User.current.allowed_to?(:log_time, @project) or deny_access
|
||||
end
|
||||
|
||||
# Only issues the user can see (log_time on the project alone isn't enough)
|
||||
def find_issue
|
||||
@issue = Issue.find params[:issue_id]
|
||||
@issue = Issue.visible.find_by(id: params[:issue_id])
|
||||
return render_404 unless @issue
|
||||
|
||||
@project = @issue.project
|
||||
end
|
||||
end
|
||||
@@ -1,12 +1,12 @@
|
||||
# weiter
|
||||
# Ideas carried over from upstream (translated from the original German notes):
|
||||
#
|
||||
# - update title / menu item after starting new entry
|
||||
# - update time entry row after context menu stop (reflect saved time)
|
||||
# - 0:60 anstelle 1:00? Redmine-bug?
|
||||
# - update title / menu item after starting a new entry
|
||||
# - update the time entry row after stopping from the context menu (show the saved time)
|
||||
# - 0:60 instead of 1:00? Redmine bug?
|
||||
#
|
||||
# - remember last activity, preselect that in 'new' form
|
||||
# - same for project, unless we are in a project context
|
||||
# - focus first field that needs an action, depending on above
|
||||
# - remember the last activity and preselect it in the 'new' form
|
||||
# - same for the project, unless we are in a project context
|
||||
# - focus the first field that needs input, depending on the above
|
||||
#
|
||||
class StopwatchTimersController < StopwatchController
|
||||
|
||||
@@ -86,6 +86,8 @@ class StopwatchTimersController < StopwatchController
|
||||
|
||||
private
|
||||
|
||||
# Saves the elapsed time into the running entry (also done every minute on the server by
|
||||
# StopwatchTimerTickJob). Safe from several tabs at once: Timer#update locks the timer record.
|
||||
def find_timer
|
||||
@timer = Stopwatch::Timer.new User.current
|
||||
@timer.update
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# Saves a running timer's time into its time entry every minute on the server, so the issue shows the
|
||||
# hours live even when nobody has Redmine open, and a forgotten timer is stopped at the maximum timer
|
||||
# length on time. Each start gets its own chain of jobs (identified by a token); a chain ends when the
|
||||
# timer stops or is restarted. Pending jobs are lost if Redmine restarts with the default in-process
|
||||
# job runner; the next start, or an open tab's once-a-minute check, keeps the time up to date meanwhile.
|
||||
class StopwatchTimerTickJob < ActiveJob::Base
|
||||
queue_as :default
|
||||
|
||||
INTERVAL = 1.minute
|
||||
|
||||
# Schedules the next tick, if the job backend supports delayed jobs (the :inline backend doesn't)
|
||||
def self.schedule(user_id, token)
|
||||
set(wait: INTERVAL).perform_later(user_id, token)
|
||||
rescue NotImplementedError => e
|
||||
Rails.logger.info "[Stopwatch] Server-side timer saving unavailable with this job backend: #{e.message}"
|
||||
end
|
||||
|
||||
def perform(user_id, token)
|
||||
user = User.find_by(id: user_id)
|
||||
return unless user
|
||||
|
||||
timer = Stopwatch::Timer.new(user)
|
||||
return unless timer.running? && timer.tick_token == token
|
||||
|
||||
timer.update
|
||||
self.class.schedule(user_id, token) if timer.running? && timer.tick_token == token
|
||||
rescue StandardError => e
|
||||
Rails.logger.error "[Stopwatch] Timer tick failed for user ##{user_id}: #{e.class}: #{e.message}"
|
||||
end
|
||||
end
|
||||
@@ -3,6 +3,12 @@
|
||||
<%= select_tag 'settings[default_activity]', options_from_collection_for_select( [['always_ask', t('.label_always_ask')], ['system', t('.label_system')]] + TimeEntryActivity.system.active.to_a.pluck(:id, :name), :first, :last, Stopwatch.settings['default_activity'] ) %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<label for="settings_max_timer_hours"><%= t '.label_max_timer_hours' %></label>
|
||||
<%= text_field_tag 'settings[max_timer_hours]', Stopwatch.settings['max_timer_hours'] || '10', size: 4, id: 'settings_max_timer_hours' %>
|
||||
<em class="info"><%= t '.text_max_timer_hours' %></em>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<label for="settings_highlighted_links"><%= t '.label_highlighted_links' %> </label>
|
||||
<%= hidden_field_tag 'settings[highlighted_links]', '0', id: nil %>
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
en:
|
||||
label_stopwatch_start: Start tracking
|
||||
label_stopwatch_stop: Stop tracking
|
||||
text_stopwatch_timer_capped: "[Timer stopped automatically at the %{hours} hour limit - please check these hours]"
|
||||
stopwatch:
|
||||
settings:
|
||||
label_always_ask: 'Always ask'
|
||||
label_default_activity: 'Default activity for "Start tracking"'
|
||||
label_highlighted_links: "Color-code Start/Stop"
|
||||
label_max_timer_hours: "Maximum timer length (hours)"
|
||||
text_max_timer_hours: "A timer running longer than this is stopped at the limit and its entry is flagged in the comment. Leave blank or 0 for no limit."
|
||||
label_system: 'Use system default'
|
||||
stopwatch_timers:
|
||||
entries_list:
|
||||
|
||||
@@ -9,6 +9,7 @@ Redmine::Plugin.register :stopwatch do
|
||||
settings default: {
|
||||
'default_activity' => 'always_ask',
|
||||
'highlighted_links' => '0',
|
||||
'max_timer_hours' => '10',
|
||||
}, partial: 'stopwatch/settings'
|
||||
|
||||
menu :account_menu, :stopwatch,
|
||||
|
||||
@@ -4,6 +4,7 @@ module Stopwatch
|
||||
def self.setup
|
||||
Stopwatch::IssuesControllerPatch.apply
|
||||
::TimeEntry.prepend Stopwatch::TimeEntryPatch
|
||||
::Issue.prepend Stopwatch::IssuePatch
|
||||
::User.prepend Stopwatch::UserPatch
|
||||
Stopwatch::Hooks # just load it
|
||||
end
|
||||
@@ -41,6 +42,15 @@ module Stopwatch
|
||||
end
|
||||
end
|
||||
|
||||
# Longest a timer may run before it is stopped automatically (hours), or nil for no limit.
|
||||
# Protects against a forgotten timer adding a night or weekend to a time entry.
|
||||
def self.max_timer_hours
|
||||
value = settings['max_timer_hours']
|
||||
return 10.0 if value.nil? # setting never saved: use the default
|
||||
hours = value.to_s.strip.tr(',', '.').to_f
|
||||
hours.positive? ? hours : nil
|
||||
end
|
||||
|
||||
# Colour-code the issue Start/Stop links (green to start, red to stop)
|
||||
def self.highlighted_links
|
||||
settings['highlighted_links'].to_s == '1'
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
module Stopwatch
|
||||
# When an issue is closed, stop any timer still running on it, so the time logged on the issue is
|
||||
# final before anything uses it (e.g. billing that runs when an issue closes). Otherwise the timer
|
||||
# would keep adding hours to an entry that was already billed.
|
||||
module IssuePatch
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
prepended do
|
||||
after_save :stop_timers_on_close, if: -> { saved_change_to_status_id? && closed? }
|
||||
end
|
||||
|
||||
def stop_timers_on_close
|
||||
user_ids = time_entries.distinct.pluck(:user_id)
|
||||
User.where(id: user_ids).find_each do |user|
|
||||
timer = Stopwatch::Timer.new(user)
|
||||
next unless timer.running? && timer.time_entry&.issue_id == id
|
||||
|
||||
timer.stop
|
||||
Rails.logger.info "[Stopwatch] Stopped user ##{user.id}'s timer because issue ##{id} was closed"
|
||||
end
|
||||
rescue StandardError => e
|
||||
# Never block closing the issue
|
||||
Rails.logger.error "[Stopwatch] Could not stop timers for issue ##{id}: #{e.class}: #{e.message}"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -6,9 +6,10 @@ module Stopwatch
|
||||
before_destroy :stop_timer
|
||||
end
|
||||
|
||||
# Stop the user's timer only if it is running on this entry (deleting another entry used to stop it)
|
||||
def stop_timer
|
||||
t = Stopwatch::Timer.new(user)
|
||||
t.update(stop: true) if t.running?
|
||||
t.update(stop: true) if t.running? && t.time_entry_id == id
|
||||
end
|
||||
end
|
||||
end
|
||||
+88
-14
@@ -1,8 +1,13 @@
|
||||
require 'json'
|
||||
require 'securerandom'
|
||||
|
||||
module Stopwatch
|
||||
# A user's running timer. Stored as JSON in the user's preferences:
|
||||
# started_at - when time was last saved into the time entry (nil when stopped)
|
||||
# time_entry_id - the time entry the timer adds to
|
||||
# session_hours - hours already added since the timer was started, for the maximum timer length
|
||||
# tick_token - identifies this start's chain of server-side saves (StopwatchTimerTickJob)
|
||||
class Timer
|
||||
|
||||
class HourFormatter
|
||||
include ApplicationHelper
|
||||
|
||||
@@ -19,7 +24,6 @@ module Stopwatch
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
attr_reader :user
|
||||
|
||||
def initialize(user)
|
||||
@@ -30,29 +34,52 @@ module Stopwatch
|
||||
data[:started_at].present?
|
||||
end
|
||||
|
||||
# Starts the timer on time_entry. If a timer is already running (e.g. after a double click), its
|
||||
# time is saved and the timer moves to the new entry, instead of failing.
|
||||
def start(time_entry = nil)
|
||||
fail "timer is already running" if running?
|
||||
data[:started_at] = Time.now.to_i
|
||||
data[:time_entry_id] = time_entry.id if time_entry
|
||||
save
|
||||
token = SecureRandom.hex(8)
|
||||
locked do
|
||||
apply_elapsed_time(stop: true) if running?
|
||||
data[:started_at] = Time.now.to_i
|
||||
data[:session_hours] = 0.0
|
||||
data[:time_entry_id] = time_entry&.id
|
||||
data[:tick_token] = token
|
||||
save
|
||||
end
|
||||
# Keep saving the time on the server every minute while it runs
|
||||
StopwatchTimerTickJob.schedule(user.id, token)
|
||||
end
|
||||
|
||||
def stop
|
||||
update stop: true
|
||||
end
|
||||
|
||||
# saves currently logged time to the time entry and resets the started_at
|
||||
# timestamp to either nil (stop: true) or current time.
|
||||
# Saves the time elapsed since the last save into the time entry, then stops the timer
|
||||
# (stop: true) or keeps it running from now. The timer record is locked meanwhile, so two requests
|
||||
# at the same moment (e.g. two open tabs) can't add the same time twice. Once a timer reaches the
|
||||
# maximum timer length it is stopped at the limit and the entry's comment says so.
|
||||
def update(stop: false)
|
||||
if hours = runtime_hours and entry = self.time_entry
|
||||
time_entry.update_column :hours, entry.read_attribute(:hours) + hours
|
||||
locked do
|
||||
if running?
|
||||
apply_elapsed_time(stop: stop)
|
||||
save
|
||||
end
|
||||
end
|
||||
data[:started_at] = stop || !running? ? nil : Time.now.to_i
|
||||
save
|
||||
end
|
||||
|
||||
def tick_token
|
||||
data[:tick_token]
|
||||
end
|
||||
|
||||
# True when the running timer has reached the maximum timer length and should be stopped
|
||||
def over_limit?
|
||||
max = Stopwatch.max_timer_hours
|
||||
running? && max.present? && data[:session_hours].to_f + (runtime_hours || 0) >= max
|
||||
end
|
||||
|
||||
# Hours to display: what's saved on the entry plus the running time (never beyond the limit)
|
||||
def time_spent
|
||||
h = runtime_hours || 0
|
||||
h = unsaved_hours
|
||||
if e = time_entry
|
||||
h += e.hours
|
||||
end
|
||||
@@ -89,12 +116,59 @@ module Stopwatch
|
||||
@data ||= (t = user.pref[:current_timer]) ? JSON.parse(t).symbolize_keys : {}
|
||||
end
|
||||
|
||||
# Runs the block with the user's preference row locked, after re-reading the saved timer
|
||||
def locked
|
||||
pref = user.pref
|
||||
pref.save! if pref.new_record?
|
||||
pref.with_lock do
|
||||
@data = nil
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
# Running time not yet saved into the entry, limited by the maximum timer length
|
||||
def unsaved_hours
|
||||
hours = runtime_hours || 0
|
||||
max = Stopwatch.max_timer_hours
|
||||
max ? [hours, [max - data[:session_hours].to_f, 0].max].min : hours
|
||||
end
|
||||
|
||||
def apply_elapsed_time(stop:)
|
||||
hours = unsaved_hours
|
||||
capped = over_limit?
|
||||
entry = time_entry
|
||||
|
||||
if entry && (hours.positive? || capped)
|
||||
attributes = {}
|
||||
attributes[:hours] = entry.read_attribute(:hours).to_f + hours if hours.positive?
|
||||
attributes[:comments] = capped_comment(entry) if capped
|
||||
entry.update_columns(attributes) if attributes.any?
|
||||
end
|
||||
|
||||
if stop || capped
|
||||
Rails.logger.info "[Stopwatch] Timer for user ##{user.id} stopped at the #{Stopwatch.max_timer_hours}h limit" if capped
|
||||
data[:started_at] = nil
|
||||
data[:session_hours] = 0.0
|
||||
else
|
||||
data[:started_at] = Time.now.to_i
|
||||
data[:session_hours] = data[:session_hours].to_f + hours
|
||||
end
|
||||
end
|
||||
|
||||
# Notes on the entry that the timer hit the limit, so someone can correct the hours
|
||||
def capped_comment(entry)
|
||||
note = I18n.t(:text_stopwatch_timer_capped, hours: Stopwatch.max_timer_hours.to_s.sub(/\.0\z/, ''))
|
||||
comments = entry.comments.to_s
|
||||
return comments if comments.include?(note)
|
||||
|
||||
[comments.presence, note].compact.join(' ').truncate(1024)
|
||||
end
|
||||
|
||||
def runtime_hours
|
||||
if start = data[:started_at]
|
||||
runtime = Time.now.to_i - start
|
||||
return runtime.to_f / 1.hour
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -73,6 +73,19 @@ class TicketTimerTest < Redmine::IntegrationTest
|
||||
assert_not_running
|
||||
end
|
||||
|
||||
test "should not start a timer on an issue the user cannot see" do
|
||||
log_user 'jsmith', 'jsmith'
|
||||
post "/issues/999999/timer/start", xhr: true
|
||||
assert_response 404
|
||||
end
|
||||
|
||||
test "should offer start tracking in the issue context menu" do
|
||||
log_user 'jsmith', 'jsmith'
|
||||
get "/issues/context_menu", params: { ids: [@issue.id] }, xhr: true
|
||||
assert_response :success
|
||||
assert_select "a.stopwatch_issue_timer", text: /start tracking/i
|
||||
end
|
||||
|
||||
test "should ask by default" do
|
||||
log_user 'jsmith', 'jsmith'
|
||||
TimeEntry.delete_all
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
require File.expand_path('../../test_helper', __FILE__)
|
||||
|
||||
class IssueCloseTest < ActiveSupport::TestCase
|
||||
fixtures :projects, :users, :user_preferences, :roles, :members, :member_roles,
|
||||
:trackers, :projects_trackers, :enabled_modules, :issue_statuses, :issues,
|
||||
:enumerations, :time_entries
|
||||
|
||||
setup do
|
||||
@user = User.find 2
|
||||
@issue = Issue.find 1
|
||||
@time_entry = TimeEntry.create!(project: @issue.project, issue: @issue, user: @user, author: @user,
|
||||
spent_on: Date.today, hours: 1, activity: TimeEntryActivity.first)
|
||||
@timer = Stopwatch::Timer.new(@user)
|
||||
@timer.start @time_entry
|
||||
data = @timer.send(:data)
|
||||
data[:started_at] = 1.hour.ago.to_i
|
||||
@timer.save
|
||||
end
|
||||
|
||||
test "closing an issue stops a timer running on it and saves the time" do
|
||||
@issue.status = IssueStatus.where(is_closed: true).first
|
||||
@issue.save!
|
||||
|
||||
refute Stopwatch::Timer.new(User.find(@user.id)).running?
|
||||
assert_in_delta 2, @time_entry.reload.hours, 0.01
|
||||
end
|
||||
|
||||
test "other changes to the issue leave the timer running" do
|
||||
@issue.subject = 'Renamed'
|
||||
@issue.save!
|
||||
|
||||
assert Stopwatch::Timer.new(User.find(@user.id)).running?
|
||||
end
|
||||
|
||||
test "deleting another time entry leaves the timer running" do
|
||||
other = TimeEntry.create!(project: @issue.project, issue: @issue, user: @user, author: @user,
|
||||
spent_on: Date.today, hours: 1, activity: TimeEntryActivity.first)
|
||||
other.destroy
|
||||
|
||||
assert Stopwatch::Timer.new(User.find(@user.id)).running?
|
||||
end
|
||||
end
|
||||
+120
-11
@@ -1,53 +1,68 @@
|
||||
require File.expand_path('../../test_helper', __FILE__)
|
||||
|
||||
class TimerTest < ActiveSupport::TestCase
|
||||
include ActiveJob::TestHelper
|
||||
|
||||
fixtures :users, :user_preferences, :time_entries
|
||||
|
||||
setup do
|
||||
@user = User.find 1
|
||||
@timer = Stopwatch::Timer.new @user
|
||||
@data = @timer.send :data
|
||||
@time_entry = TimeEntry.last
|
||||
@time_entry.update_column :comments, 'work'
|
||||
Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('max_timer_hours' => '10')
|
||||
end
|
||||
|
||||
# The timer re-reads its saved state under a lock, so always look at the current data
|
||||
def data
|
||||
@timer.send :data
|
||||
end
|
||||
|
||||
# Pretend the timer was last saved `ago` seconds ago
|
||||
def started(ago)
|
||||
data[:started_at] = ago.ago.to_i
|
||||
@timer.save
|
||||
end
|
||||
|
||||
test "should know wether its running" do
|
||||
refute @timer.running?
|
||||
@data[:started_at] = 5.minutes.ago
|
||||
data[:started_at] = 5.minutes.ago
|
||||
assert @timer.running?
|
||||
end
|
||||
|
||||
test "should start timer" do
|
||||
assert_nil @data[:started_at]
|
||||
assert_nil data[:started_at]
|
||||
@timer.start @time_entry
|
||||
assert @data[:started_at].present?
|
||||
assert_equal @time_entry.id, @data[:time_entry_id]
|
||||
assert data[:started_at].present?
|
||||
assert_equal @time_entry.id, data[:time_entry_id]
|
||||
end
|
||||
|
||||
test "should stop timer and update time entry" do
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
@data[:started_at] = 1.hour.ago.to_i
|
||||
started 1.hour
|
||||
@timer.stop
|
||||
|
||||
@time_entry.reload
|
||||
assert_equal hours + 1, @time_entry.hours
|
||||
refute @timer.running?
|
||||
end
|
||||
|
||||
test "should update time entry" do
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
@data[:started_at] = 1.hour.ago.to_i
|
||||
started 1.hour
|
||||
@timer.update
|
||||
|
||||
@time_entry.reload
|
||||
assert_equal hours + 1, @time_entry.hours
|
||||
end
|
||||
assert @timer.running?
|
||||
end
|
||||
|
||||
test "should save and restore" do
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
@data[:started_at] = 1.hour.ago.to_i
|
||||
@timer.save
|
||||
started 1.hour
|
||||
|
||||
timer = Stopwatch::Timer.new User.find(@user.id)
|
||||
assert timer.running?
|
||||
@@ -57,6 +72,100 @@ class TimerTest < ActiveSupport::TestCase
|
||||
assert_equal hours + 1, @time_entry.hours
|
||||
end
|
||||
|
||||
test "should not add the same time twice when updated twice" do
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
started 1.hour
|
||||
|
||||
# e.g. two open tabs checking the timer at the same moment
|
||||
other_tab = Stopwatch::Timer.new User.find(@user.id)
|
||||
@timer.update
|
||||
other_tab.update
|
||||
|
||||
@time_entry.reload
|
||||
assert_in_delta hours + 1, @time_entry.hours, 0.01
|
||||
end
|
||||
|
||||
test "should stop a forgotten timer at the limit and flag the entry" do
|
||||
Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('max_timer_hours' => '2')
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
started 60.hours # left running over a weekend
|
||||
@timer.stop
|
||||
|
||||
@time_entry.reload
|
||||
assert_equal hours + 2, @time_entry.hours
|
||||
assert_includes @time_entry.comments, 'work'
|
||||
assert_includes @time_entry.comments, '2 hour limit'
|
||||
refute @timer.running?
|
||||
end
|
||||
|
||||
test "should count earlier saves toward the limit" do
|
||||
Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('max_timer_hours' => '2')
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
started 90.minutes
|
||||
@timer.update
|
||||
assert @timer.running?
|
||||
|
||||
started 1.hour
|
||||
@timer.update
|
||||
|
||||
@time_entry.reload
|
||||
assert_in_delta hours + 2, @time_entry.hours, 0.01
|
||||
refute @timer.running?
|
||||
end
|
||||
|
||||
test "should not limit timers when the limit is blank" do
|
||||
Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('max_timer_hours' => '')
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
started 30.hours
|
||||
@timer.stop
|
||||
|
||||
@time_entry.reload
|
||||
assert_in_delta hours + 30, @time_entry.hours, 0.01
|
||||
end
|
||||
|
||||
test "starting again moves the timer instead of failing" do
|
||||
other_entry = TimeEntry.where.not(id: @time_entry.id).first
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
started 1.hour
|
||||
|
||||
@timer.start other_entry # e.g. a double click, or starting another entry
|
||||
|
||||
assert_equal other_entry.id, data[:time_entry_id]
|
||||
assert_in_delta hours + 1, @time_entry.reload.hours, 0.01
|
||||
end
|
||||
|
||||
test "should schedule server-side saves while running" do
|
||||
assert_enqueued_with(job: StopwatchTimerTickJob) do
|
||||
@timer.start @time_entry
|
||||
end
|
||||
end
|
||||
|
||||
test "tick job saves the time and schedules the next tick" do
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
started 1.hour
|
||||
token = data[:tick_token]
|
||||
|
||||
clear_enqueued_jobs
|
||||
assert_enqueued_with(job: StopwatchTimerTickJob, args: [@user.id, token]) do
|
||||
StopwatchTimerTickJob.perform_now(@user.id, token)
|
||||
end
|
||||
assert_in_delta hours + 1, @time_entry.reload.hours, 0.01
|
||||
end
|
||||
|
||||
test "tick job from an earlier start does nothing" do
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
started 1.hour
|
||||
|
||||
clear_enqueued_jobs
|
||||
StopwatchTimerTickJob.perform_now(@user.id, 'old-token')
|
||||
assert_no_enqueued_jobs
|
||||
assert_equal hours, @time_entry.reload.hours
|
||||
end
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user