2025-02-11 15:04:12 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
require 'prometheus_exporter/client'
|
|
|
|
|
|
|
|
|
|
RSpec.describe Maps::TileUsage::Track do
|
|
|
|
|
describe '#call' do
|
2025-02-13 15:04:29 -05:00
|
|
|
subject(:track) { described_class.new(user_id, tile_count).call }
|
2025-02-11 15:04:12 -05:00
|
|
|
|
2025-02-13 15:04:29 -05:00
|
|
|
let(:user_id) { 1 }
|
2025-02-11 15:04:12 -05:00
|
|
|
let(:tile_count) { 5 }
|
|
|
|
|
let(:prometheus_client) { instance_double(PrometheusExporter::Client) }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
allow(PrometheusExporter::Client).to receive(:default).and_return(prometheus_client)
|
2025-02-13 15:04:29 -05:00
|
|
|
allow(prometheus_client).to receive(:send_json)
|
|
|
|
|
allow(DawarichSettings).to receive(:prometheus_exporter_enabled?).and_return(true)
|
2025-02-11 15:04:12 -05:00
|
|
|
end
|
|
|
|
|
|
2025-02-13 15:04:29 -05:00
|
|
|
it 'tracks tile usage in prometheus' do
|
2025-02-11 15:04:12 -05:00
|
|
|
expect(prometheus_client).to receive(:send_json).with(
|
|
|
|
|
{
|
|
|
|
|
type: 'counter',
|
2025-02-11 15:17:33 -05:00
|
|
|
name: 'dawarich_map_tiles_usage',
|
2025-02-11 15:04:12 -05:00
|
|
|
value: tile_count
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
track
|
|
|
|
|
end
|
2025-02-13 15:04:29 -05:00
|
|
|
|
|
|
|
|
it 'tracks tile usage in cache' do
|
|
|
|
|
expect(Rails.cache).to receive(:write).with(
|
|
|
|
|
"dawarich_map_tiles_usage:#{user_id}:#{Time.zone.today}",
|
|
|
|
|
tile_count,
|
|
|
|
|
expires_in: 7.days
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
track
|
|
|
|
|
end
|
2025-02-11 15:04:12 -05:00
|
|
|
end
|
|
|
|
|
end
|