|
| 1 | +RSpec.describe Unleash::StreamingEventProcessor do |
| 2 | + let(:engine) { YggdrasilEngine.new } |
| 3 | + let(:processor) { Unleash::StreamingEventProcessor.new(engine) } |
| 4 | + let(:backup_file) { Unleash.configuration.backup_file } |
| 5 | + |
| 6 | + before do |
| 7 | + Unleash.configure do |config| |
| 8 | + config.url = 'http://test-url/' |
| 9 | + config.app_name = 'test-app' |
| 10 | + end |
| 11 | + Unleash.logger = Unleash.configuration.logger |
| 12 | + end |
| 13 | + |
| 14 | + after do |
| 15 | + File.delete(backup_file) if File.exist?(backup_file) |
| 16 | + end |
| 17 | + |
| 18 | + class TestEvent |
| 19 | + attr_reader :type, :data |
| 20 | + |
| 21 | + def initialize(type, data) |
| 22 | + @type = type |
| 23 | + @data = data |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + def feature_event(name, enabled = true) |
| 28 | + { |
| 29 | + "events": [{ |
| 30 | + "type": "feature-updated", |
| 31 | + "eventId": 1, |
| 32 | + "feature": { |
| 33 | + "name": name, |
| 34 | + "enabled": enabled, |
| 35 | + "strategies": [{ "name": "default" }] |
| 36 | + } |
| 37 | + }] |
| 38 | + }.to_json |
| 39 | + end |
| 40 | + |
| 41 | + def backup_contains_feature?(name) |
| 42 | + return false unless File.exist?(backup_file) |
| 43 | + |
| 44 | + parsed = JSON.parse(File.read(backup_file)) |
| 45 | + feature_names = parsed['features'].map { |f| f['name'] } |
| 46 | + feature_names.include?(name) |
| 47 | + end |
| 48 | + |
| 49 | + describe '#process_event' do |
| 50 | + it 'processes valid events and saves full engine state' do |
| 51 | + event = TestEvent.new('unleash-updated', feature_event('test-feature')) |
| 52 | + processor.process_event(event) |
| 53 | + |
| 54 | + expect(engine.enabled?('test-feature', {})).to eq(true) |
| 55 | + expect(backup_contains_feature?('test-feature')).to eq(true) |
| 56 | + end |
| 57 | + |
| 58 | + it 'ignores unknown event types' do |
| 59 | + event = TestEvent.new('unknown-event', feature_event('test-feature')) |
| 60 | + processor.process_event(event) |
| 61 | + |
| 62 | + expect(File.exist?(backup_file)).to eq(false) |
| 63 | + expect(engine.enabled?('test-feature', {})).to be_falsy |
| 64 | + end |
| 65 | + |
| 66 | + it 'saves full engine state, not partial event data' do |
| 67 | + processor.process_event(TestEvent.new('unleash-updated', feature_event('first-feature', true))) |
| 68 | + processor.process_event(TestEvent.new('unleash-updated', feature_event('second-feature', false))) |
| 69 | + |
| 70 | + expect(backup_contains_feature?('first-feature')).to eq(true) |
| 71 | + expect(backup_contains_feature?('second-feature')).to eq(true) |
| 72 | + end |
| 73 | + |
| 74 | + it 'handles invalid JSON gracefully without creating backup' do |
| 75 | + event = TestEvent.new('unleash-updated', 'invalid json') |
| 76 | + |
| 77 | + expect { processor.process_event(event) }.not_to raise_error |
| 78 | + expect(File.exist?(backup_file)).to eq(false) |
| 79 | + end |
| 80 | + end |
| 81 | +end |
0 commit comments