<commit_before>require 'rails_helper'

describe Lights::StatusManager do
  describe '::forced_on?' do
    include FakeFS::SpecHelpers

    before do
      Dir.mkdir File.join('', 'tmp')
    end

    context 'when the force on trigger file is present' do
      it 'is true' do
        File.open(Lights::StatusManager::TRIGGER, 'w') { |file| file.write '{P}~~~ kroci' }
        expect(Lights::StatusManager.forced_on?).to be_truthy
      end
    end

    context 'when the force on trigger file is not present' do
      it 'is false' do
        expect(Lights::StatusManager.forced_on?).to be_falsy
      end
    end
  end

  describe '::auto!' do
    include FakeFS::SpecHelpers

    before do
      Dir.mkdir File.join('', 'tmp')
      File.open(Lights::StatusManager::TRIGGER, 'w') { |file| file.write '{P}~~~ kroci' }
    end

    it 'deletes the trigger file' do
      expect(File.exist? Lights::StatusManager::TRIGGER).to be_truthy

      Lights::StatusManager.auto!

      expect(File.exist? Lights::StatusManager::TRIGGER).to be_falsy
    end

    it 'returns true' do
      expect(Lights::StatusManager.auto!).to be_truthy
    end
  end

  describe '::force_on!' do
    include FakeFS::SpecHelpers

    before do
      Dir.mkdir File.join('', 'tmp')<commit_msg>Add specs for lights controller notification<commit_after>require 'rails_helper'
require 'socket'

describe Lights::StatusManager do
  describe '::notify_controller!' do
    it 'sends a ping to the controller socket' do
      socket = instance_double('UNIXSocket')
      expect(socket).to receive(:send).and_return 1
      expect(UNIXSocket).to receive(:open).with(Lights::StatusManager::LIGHTS_DAEMON_SOCKET).and_yield socket

      Lights::StatusManager.notify_controller!
    end
  end

  describe '::forced_on?' do
    include FakeFS::SpecHelpers

    before do
      Dir.mkdir File.join('', 'tmp')
    end

    context 'when the force on trigger file is present' do
      it 'is true' do
        File.open(Lights::StatusManager::TRIGGER, 'w') { |file| file.write '{P}~~~ kroci' }
        expect(Lights::StatusManager.forced_on?).to be_truthy
      end
    end

    context 'when the force on trigger file is not present' do
      it 'is false' do
        expect(Lights::StatusManager.forced_on?).to be_falsy
      end
    end
  end

  describe '::auto!' do
    include FakeFS::SpecHelpers

    before do
      Dir.mkdir File.join('', 'tmp')
      File.open(Lights::StatusManager::TRIGGER, 'w') { |file| file.write '{P}~~~ kroci' }
      allow(Lights::StatusManager).to receive(:notify_controller!).and_return true
    end

    it 'pings the controller that there is a state change' do
      expect(Lights::StatusManager).to receive(:notify_controller!).and_return true

      Lights::StatusManager.auto!
    end

    it 'deletes the trigger file' do
      expect(File.exist? Lights::StatusManager::TRIGGER).to be_truthy

      Lights::StatusManager.auto!

      expect(File.exist? Lights::StatusManager::TRIGGER).to be_falsy
    end

    it 'returns true' do
      expect(Lights::StatusManager.auto!).to be_truthy
    end
  end

  describe '::force_on!' do
    include FakeFS::SpecHelpers

    before do
      Dir.mkdir File.join('', 'tmp')
      allow(Lights::StatusManager).to receive(:notify_controller!).and_return true
    end

    it 'pings the controller that there is a state change' do
      expect(Lights::StatusManager).to receive(:notify_controller!).and_return true
      Lights::StatusManager.force_on!
{'fraction_non_alphanumeric': 0.11439220183486239, 'fraction_numerical': 0.00028669724770642203, 'mean_word_length': 3.3127317676143386, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '811774', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1071, 'n_words': 285}