File: ai_logging_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (97 lines) | stat: -rw-r--r-- 3,301 bytes parent folder | download
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
# frozen_string_literal: true

require 'fast_spec_helper'
require 'gitlab/dangerfiles/spec_helper'
require_relative '../../../tooling/danger/ai_logging'
require_relative '../../../tooling/danger/project_helper'

RSpec.describe Tooling::Danger::AiLogging, feature_category: :service_ping do
  include_context "with dangerfile"

  subject(:ai_logging) { fake_danger.new(helper: fake_helper) }

  let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) }
  let(:fake_project_helper) { instance_double(Tooling::Danger::ProjectHelper) }
  let(:modified_files) { ['app/services/llm/ai_service.rb'] }

  before do
    stub_const('Diff', Struct.new(:patch))
    stub_const('Git', Struct.new(:modified_files, :file_content) do
      def diff_for_file(_file)
        Diff.new(file_content)
      end
    end)

    allow(fake_helper).to receive(:git).and_return(
      Git.new(modified_files, file_content)
    )
    allow(fake_helper).to receive(:stable_branch?).and_return(false)
    allow(fake_helper).to receive(:markdown_list).and_return("app/services/llm/ai_service.rb")
  end

  describe '#check_ai_logging' do
    subject(:check_ai_logging) { ai_logging.check_ai_logging }

    context 'when there are no AI logging issues' do
      let(:modified_files) { ['app/models/user.rb'] }
      let(:file_content) { 'def some_method; end' }

      it 'does not warn' do
        expect(ai_logging).not_to receive(:warn)
        check_ai_logging
      end
    end

    context 'when there are no AI logging issues but we are using same method name' do
      let(:modified_files) { ['app/models/user.rb'] }
      let(:file_content) { 'log_error(message: "test")' }

      it 'does not warn' do
        expect(ai_logging).not_to receive(:warn)
        check_ai_logging
      end
    end

    context 'when there are AI logging issues' do
      let(:modified_files) { ['app/services/duo/ai_service.rb'] }
      let(:file_content) { 'log_info(message: "Some AI log")' }

      it 'warns about non-compliant AI logging' do
        expect(ai_logging).to receive(:warn).with(Tooling::Danger::AiLogging::AI_LOGGING_WARNING)
        expect(ai_logging).to receive(:markdown).with(
          a_string_including(Tooling::Danger::AiLogging::AI_LOGGING_FILES_MESSAGE)
        )
        check_ai_logging
      end

      context 'when file content is multi-lined' do
        let(:file_content) do
          <<~TEXT
            log_info(
              message: "Some AI log"
            )
          TEXT
        end

        it 'warns about non-compliant AI logging' do
          expect(ai_logging).to receive(:warn).with(Tooling::Danger::AiLogging::AI_LOGGING_WARNING)
          expect(ai_logging).to receive(:markdown).with(
            a_string_including(Tooling::Danger::AiLogging::AI_LOGGING_FILES_MESSAGE)
          )
          check_ai_logging
        end
      end

      context 'when there is appropriate AI logging with log_conditional_info' do
        let(:modified_files) { ['app/services/llm/ai_service.rb'] }
        let(:file_content) { 'log_conditional_info(test_user, message: "Some AI log with log_conditional_info")' }

        it 'warns about non-compliant AI logging' do
          expect(ai_logging).not_to receive(:warn)

          check_ai_logging
        end
      end
    end
  end
end