|
| 1 | +local notify = require('CopilotChat.notify') |
| 2 | + |
| 3 | +describe('CopilotChat.notify', function() |
| 4 | + before_each(function() |
| 5 | + -- Clear all listeners before each test |
| 6 | + notify.clear() |
| 7 | + end) |
| 8 | + |
| 9 | + describe('publish and listen', function() |
| 10 | + it('calls listener when event is published', function() |
| 11 | + local called = false |
| 12 | + local received_data = nil |
| 13 | + |
| 14 | + notify.listen('test_event', function(data) |
| 15 | + called = true |
| 16 | + received_data = data |
| 17 | + end) |
| 18 | + |
| 19 | + notify.publish('test_event', 'test_data') |
| 20 | + |
| 21 | + assert.is_true(called) |
| 22 | + assert.equals('test_data', received_data) |
| 23 | + end) |
| 24 | + |
| 25 | + it('supports multiple listeners for same event', function() |
| 26 | + local count = 0 |
| 27 | + |
| 28 | + notify.listen('test_event', function(data) |
| 29 | + count = count + 1 |
| 30 | + end) |
| 31 | + notify.listen('test_event', function(data) |
| 32 | + count = count + 10 |
| 33 | + end) |
| 34 | + |
| 35 | + notify.publish('test_event', 'data') |
| 36 | + |
| 37 | + assert.equals(11, count) |
| 38 | + end) |
| 39 | + |
| 40 | + it('does not call listeners for different events', function() |
| 41 | + local called = false |
| 42 | + |
| 43 | + notify.listen('event_a', function(data) |
| 44 | + called = true |
| 45 | + end) |
| 46 | + |
| 47 | + notify.publish('event_b', 'data') |
| 48 | + |
| 49 | + assert.is_false(called) |
| 50 | + end) |
| 51 | + |
| 52 | + it('passes correct data to listeners', function() |
| 53 | + local received = nil |
| 54 | + |
| 55 | + notify.listen('test_event', function(data) |
| 56 | + received = data |
| 57 | + end) |
| 58 | + |
| 59 | + notify.publish('test_event', { foo = 'bar', num = 123 }) |
| 60 | + |
| 61 | + assert.are.same({ foo = 'bar', num = 123 }, received) |
| 62 | + end) |
| 63 | + |
| 64 | + it('handles nil and empty data', function() |
| 65 | + local received = 'not_called' |
| 66 | + |
| 67 | + notify.listen('test_event', function(data) |
| 68 | + received = data |
| 69 | + end) |
| 70 | + |
| 71 | + notify.publish('test_event', nil) |
| 72 | + assert.is_nil(received) |
| 73 | + |
| 74 | + notify.publish('test_event', '') |
| 75 | + assert.equals('', received) |
| 76 | + end) |
| 77 | + |
| 78 | + it('handles publishing to events with no listeners', function() |
| 79 | + -- Should not error |
| 80 | + assert.has_no.errors(function() |
| 81 | + notify.publish('nonexistent_event', 'data') |
| 82 | + end) |
| 83 | + end) |
| 84 | + end) |
| 85 | + |
| 86 | + describe('clear', function() |
| 87 | + it('removes all listeners', function() |
| 88 | + local called = false |
| 89 | + |
| 90 | + notify.listen('test_event', function(data) |
| 91 | + called = true |
| 92 | + end) |
| 93 | + |
| 94 | + notify.clear() |
| 95 | + notify.publish('test_event', 'data') |
| 96 | + |
| 97 | + assert.is_false(called) |
| 98 | + end) |
| 99 | + |
| 100 | + it('allows adding new listeners after clear', function() |
| 101 | + local called = false |
| 102 | + |
| 103 | + notify.listen('test_event', function(data) |
| 104 | + called = true |
| 105 | + end) |
| 106 | + notify.clear() |
| 107 | + |
| 108 | + notify.listen('test_event', function(data) |
| 109 | + called = true |
| 110 | + end) |
| 111 | + notify.publish('test_event', 'data') |
| 112 | + |
| 113 | + assert.is_true(called) |
| 114 | + end) |
| 115 | + end) |
| 116 | + |
| 117 | + describe('constants', function() |
| 118 | + it('defines STATUS constant', function() |
| 119 | + assert.equals('status', notify.STATUS) |
| 120 | + end) |
| 121 | + |
| 122 | + it('defines MESSAGE constant', function() |
| 123 | + assert.equals('message', notify.MESSAGE) |
| 124 | + end) |
| 125 | + end) |
| 126 | +end) |
0 commit comments