summaryrefslogtreecommitdiffstats
path: root/test/command_test.rb
blob: a2b4afff8ab71bc618e4b9c5719ce573c558aa9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require 'test_helper'
class CommandTest < HCl::TestCase
  include HCl::Commands
  include HCl::Utility

  def setup
    super
    @settings = {}
  end

  # the current_time utility method needs to be deterministic
  def current_time
    'high noon'
  end

  # stub settings helpers
  def write_settings; end
  def read_settings
    @settings
  end

  def test_log_failure
    HCl::DayEntry.expects(:with_timer).returns(stub)
    assert_raises(HCl::CommandError) { log "stuff" }
  end

  def test_tasks
    FileUtils.rm_rf ENV['HCL_DIR']+"/cache"
    register_uri(:get, '/daily', {:day_entries=>[], :projects=> [{
      :name=>"Harvest Command Line", :code=>"hcl", :id=>"123", :client=>"zenhob",
      :tasks=> [{:name=>"Development", :id=>"456"} ]
    }]})
    result = tasks
    assert_equal "123 456\tzenhob - [hcl] Harvest Command Line - Development", result
  end

  def test_show
    HCl::DayEntry.expects(:daily).returns([HCl::DayEntry.new({
      hours:'2.06',
      notes: 'hi world',
      project: 'App'
    })])
    result = show
    assert_equal \
      "\t2:03\tApp: hi world\n\t-------------\n\t2:03\ttotal (as of high noon)\n",
      result
  end

  def test_aliases
    HCl::Task.expects(:all).returns([HCl::Task.new(
      id:123,
      name: 'Dev',
      project: HCl::Project.new(id:456, name:'App', client:'Bob', code:'b')
    )])
    result = send :alias, *%w[ hcl 456 123 ]
    assert_equal '456 123', @settings['task.hcl']

    result = aliases
    assert_equal ["@hcl"], result

    result = unalias 'hcl'
    assert !@settings.key?('task.hcl'), 'hcl alias is no longer defined'
  end

  def test_start
    task = HCl::Task.new(
      id:123,
      name: 'Dev',
      project: HCl::Project.new(id:456, name:'App', client:'Bob', code:'b')
    )
    HCl::Task.expects(:find).with('456','123').returns(task)
    task.expects(:start).with(http, starting_time:nil, note:'do stuff')
    start *%w[ 456 123 do stuff ]
  end

  def test_stop
    entry = stub
    register_uri(:get, '/daily', {day_entries:[{id:123,notes:'',hours:1,client:nil,project:nil,timer_started_at:DateTime.now}]})
    register_uri(:post, '/daily/update/123', {day_entry:{notes:'all done'}})
    register_uri(:get, '/daily/timer/123')
    stop 'all done'
  end

  def test_stop_yesterday
    entry = stub
    yesterday = (DateTime.now - 1).strftime("%3j")
    register_uri(:get, '/daily', {day_entries:[]})
    register_uri(:get, "/daily/#{yesterday}/2014", {day_entries:[{id:321,notes:'',hours:1,client:nil,project:nil,timer_started_at:DateTime.now}]})
    register_uri(:post, '/daily/update/321', {day_entry:{notes:'all done next day'}})
    register_uri(:get, '/daily/timer/321')
    stop 'all done next day'
  end

  def test_resume
    entry = stub
    HCl::DayEntry.expects(:last).returns(entry)
    entry.expects(:toggle)
    resume
  end

  def test_resume_with_task_alias
    entry = stub
    expects(:get_task_ids).with('mytask',[]).returns(%w[ 456 789 ])
    HCl::DayEntry.expects(:last_by_task).with(http, '456', '789').returns(entry)
    entry.expects(:toggle).with(http)
    resume 'mytask'
  end

  def test_cancel
    entry = stub
    HCl::DayEntry.expects(:with_timer).with(http).returns(entry)
    entry.expects(:cancel).with(http).returns(true)
    expects(:ask).returns('y')
    cancel
  end

  def test_cancel_no
    entry = stub
    HCl::DayEntry.expects(:with_timer).with(http).returns(entry)
    expects(:ask).returns('n')
    cancel
  end

  def test_note
    entry = stub
    HCl::DayEntry.expects(:with_timer).returns(entry)
    entry.expects(:append_note).with(http, 'hi world')
    note 'hi world'
  end

  def test_note_display
    entry = stub(notes:"your face")
    HCl::DayEntry.expects(:with_timer).returns(entry)
    assert_equal "your face", note
  end

end