<commit_before>TEST_MODE = true
require File.join(File.dirname(__FILE__), '..', 'lib', 'timetrap')
require 'spec'
require 'fakefs/safe'

module Timetrap::StubConfig
  def with_stubbed_config options
    options.each do |k, v|
      Timetrap::Config.stub(:[]).with(k).and_return v
    end
  end
end

describe Timetrap do
  def create_entry atts = {}
    Timetrap::Entry.create({
      :sheet => 'default',
      :start => Time.now,
      :end => Time.now,
      :note => 'note'}.merge(atts))
  end


  before :each do
    Timetrap::Entry.create_table!
    Timetrap::Meta.create_table!
    $stdout = StringIO.new
    $stdin = StringIO.new
  end

  describe 'CLI' do
    describe "COMMANDS" do
      def invoke command
        Timetrap::CLI.parse command
        Timetrap::CLI.invoke
      end

      describe 'archive' do
        before do
          3.times do |i|
            create_entry
          end
        end

        it "should put the entries in a hidden sheet" do
          $stdin.string = "yes\n"
          invoke 'archive'
          Timetrap::Entry.each do |e|
            e.sheet.should == '_default'
          end
        end

        it "should leave the running entry alone" do
          invoke "in"
          $stdin.string = "yes\n"
          invoke 'archive'
          Timetrap::Entry.order(:id).last.sheet.should == 'default'
        end
      end

      describe 'config' do
        it "should write a config file" do
          FakeFS do
            FileUtils.mkdir_p(ENV['HOME'])
            FileUtils.rm(ENV['HOME'] + '/.timetrap.yml')
            File.exist?(ENV['HOME'] + '/.timetrap.yml').should be_false
            invoke "configure"
            File.exist?(ENV['HOME'] + '/.timetrap.yml').should be_true
          end
        end

        it "should describe config file" do
          FakeFS do
            invoke "configure"
            $stdout.string.should == "Config file is at \"#{ENV['HOME']}/.timetrap.yml\"\n"
          end
        end
      end

      describe 'edit' do
        before do
          Timetrap.start "running entry", nil
        end

        it "should edit the description of the active period" do
          Timetrap.active_entry.note.should == 'running entry'
          invoke 'edit new description'
          Timetrap.active_entry.note.should == 'new description'
        end

        it "should allow you to move an entry to another sheet" do
          invoke 'edit --move blahblah'
          Timetrap.active_entry[:sheet].should == 'blahblah'
          invoke 'edit -m blahblahblah'
          Timetrap.active_entry[:sheet].should == 'blahblahblah'
        end

        it "should change the current sheet if the current entry's sheet is changed" do
          Timetrap.current_sheet.should_not == 'blahblahblah'
          invoke 'edit -m blahblahblah'
          Timetrap.active_entry[:sheet].should == 'blahblahblah'
          Timetrap.current_sheet.should == 'blahblahblah'
        end

        it "should change the current sheet if a non current entry's sheet is changed" do
          sheet = Timetrap.current_sheet
          id = Timetrap.active_entry[:id]
          invoke 'out'
          invoke "edit -m blahblahblah -i #{id}"
          Timetrap.current_sheet.should == sheet
          Timetrap::Entry[id][:sheet].should == 'blahblahblah'
        end

        it "should allow appending to the description of the active period" do
          Timetrap::Config.stub(:[]).with('append_notes_delimiter').and_return('//')
          Timetrap.active_entry.note.should == 'running entry'
          invoke 'edit --append new'
          Timetrap.active_entry.note.should == 'running entry//new'
          invoke 'edit -z more'
          Timetrap.active_entry.note.should == 'running entry//new//more'
        end<commit_msg>Use with_stubbed_config helper<commit_after>TEST_MODE = true
require File.join(File.dirname(__FILE__), '..', 'lib', 'timetrap')
require 'spec'
require 'fakefs/safe'

module Timetrap::StubConfig
  def with_stubbed_config options
    options.each do |k, v|
      Timetrap::Config.stub(:[]).with(k).and_return v
    end
  end
end

describe Timetrap do
  include Timetrap::StubConfig
  def create_entry atts = {}
    Timetrap::Entry.create({
      :sheet => 'default',
      :start => Time.now,
      :end => Time.now,
      :note => 'note'}.merge(atts))
  end


  before :each do
    Timetrap::Entry.create_table!
    Timetrap::Meta.create_table!
    $stdout = StringIO.new
    $stdin = StringIO.new
  end

  describe 'CLI' do
    describe "COMMANDS" do
      def invoke command
        Timetrap::CLI.parse command
        Timetrap::CLI.invoke
      end

      describe 'archive' do
        before do
          3.times do |i|
            create_entry
          end
        end

        it "should put the entries in a hidden sheet" do
          $stdin.string = "yes\n"
          invoke 'archive'
          Timetrap::Entry.each do |e|
            e.sheet.should == '_default'
          end
        end

        it "should leave the running entry alone" do
          invoke "in"
          $stdin.string = "yes\n"
          invoke 'archive'
          Timetrap::Entry.order(:id).last.sheet.should == 'default'
        end
      end

      describe 'config' do
        it "should write a config file" do
          FakeFS do
            FileUtils.mkdir_p(ENV['HOME'])
            FileUtils.rm(ENV['HOME'] + '/.timetrap.yml')
            File.exist?(ENV['HOME'] + '/.timetrap.yml').should be_false
            invoke "configure"
            File.exist?(ENV['HOME'] + '/.timetrap.yml').should be_true
          end
        end

        it "should describe config file" do
          FakeFS do
            invoke "configure"
            $stdout.string.should == "Config file is at \"#{ENV['HOME']}/.timetrap.yml\"\n"
          end
        end
      end

      describe 'edit' do
        before do
          Timetrap.start "running entry", nil
        end

        it "should edit the description of the active period" do
          Timetrap.active_entry.note.should == 'running entry'
          invoke 'edit new description'
          Timetrap.active_entry.note.should == 'new description'
        end

        it "should allow you to move an entry to another sheet" do
          invoke 'edit --move blahblah'
          Timetrap.active_entry[:sheet].should == 'blahblah'
          invoke 'edit -m blahblahblah'
          Timetrap.active_entry[:sheet].should == 'blahblahblah'
        end

        it "should change the current sheet if the current entry's sheet is changed" do
          Timetrap.current_sheet.should_not == 'blahblahblah'
          invoke 'edit -m blahblahblah'
          Timetrap.active_entry[:sheet].should == 'blahblahblah'
          Timetrap.current_sheet.should == 'blahblahblah'
        end

        it "should change the current sheet if a non current entry's sheet is changed" do
          sheet = Timetrap.current_sheet
          id = Timetrap.active_entry[:id]
          invoke 'out'
          invoke "edit -m blahblahblah -i #{id}"
          Timetrap.current_sheet.should == sheet
          Timetrap::Entry[id][:sheet].should == 'blahblahblah'
        end

        it "should allow appending to the description of the active period" do
          with_stubbed_config('append_notes_delimiter' => '//')
          Timetrap.active_entry.note.should == 'running entry'
          invoke 'edit --append new'
          Timetrap.active_entry.note.should == 'running entry//new'
          invoke 'edit -z more'
          Timetrap.active_entry.note.should == 'running entry//new//more'
        end
{'fraction_non_alphanumeric': 0.10593052436930392, 'fraction_numerical': 0.0002641658961828028, 'mean_word_length': 2.0191387559808613, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7352743', 'n_tokens_mistral': 2286, 'n_tokens_neox': 2247, 'n_words': 605}