|
| 1 | +local renderer = require('opencode.ui.renderer') |
| 2 | +local config = require('opencode.config') |
| 3 | +local state = require('opencode.state') |
| 4 | +local helpers = require('tests.helpers') |
| 5 | +local ui = require('opencode.ui.ui') |
| 6 | + |
| 7 | +describe('hooks', function() |
| 8 | + before_each(function() |
| 9 | + helpers.replay_setup() |
| 10 | + config.hooks = { |
| 11 | + on_file_edited = nil, |
| 12 | + on_session_loaded = nil, |
| 13 | + } |
| 14 | + end) |
| 15 | + |
| 16 | + after_each(function() |
| 17 | + if state.windows then |
| 18 | + ui.close_windows(state.windows) |
| 19 | + end |
| 20 | + config.hooks = { |
| 21 | + on_file_edited = nil, |
| 22 | + on_session_loaded = nil, |
| 23 | + } |
| 24 | + end) |
| 25 | + |
| 26 | + describe('on_file_edited', function() |
| 27 | + it('should call hook when file is edited', function() |
| 28 | + local called = false |
| 29 | + local file_path = nil |
| 30 | + |
| 31 | + config.hooks.on_file_edited = function(file) |
| 32 | + called = true |
| 33 | + file_path = file |
| 34 | + end |
| 35 | + |
| 36 | + local test_event = { file = '/test/file.lua' } |
| 37 | + renderer.on_file_edited(test_event) |
| 38 | + |
| 39 | + assert.is_true(called) |
| 40 | + assert.are.equal('/test/file.lua', file_path) |
| 41 | + end) |
| 42 | + |
| 43 | + it('should not error when hook is nil', function() |
| 44 | + config.hooks.on_file_edited = nil |
| 45 | + |
| 46 | + local test_event = { file = '/test/file.lua' } |
| 47 | + assert.has_no.errors(function() |
| 48 | + renderer.on_file_edited(test_event) |
| 49 | + end) |
| 50 | + end) |
| 51 | + |
| 52 | + it('should not crash when hook throws error', function() |
| 53 | + config.hooks.on_file_edited = function() |
| 54 | + error('test error') |
| 55 | + end |
| 56 | + |
| 57 | + local test_event = { file = '/test/file.lua' } |
| 58 | + assert.has_no.errors(function() |
| 59 | + renderer.on_file_edited(test_event) |
| 60 | + end) |
| 61 | + end) |
| 62 | + end) |
| 63 | + |
| 64 | + describe('on_session_loaded', function() |
| 65 | + it('should call hook when session is loaded', function() |
| 66 | + local called = false |
| 67 | + local session_data = nil |
| 68 | + |
| 69 | + config.hooks.on_session_loaded = function(session) |
| 70 | + called = true |
| 71 | + session_data = session |
| 72 | + end |
| 73 | + |
| 74 | + local events = helpers.load_test_data('tests/data/simple-session.json') |
| 75 | + state.active_session = helpers.get_session_from_events(events, true) |
| 76 | + local loaded_session = helpers.load_session_from_events(events) |
| 77 | + |
| 78 | + renderer._render_full_session_data(loaded_session) |
| 79 | + |
| 80 | + assert.is_true(called) |
| 81 | + assert.are.same(state.active_session, session_data) |
| 82 | + end) |
| 83 | + |
| 84 | + it('should not error when hook is nil', function() |
| 85 | + config.hooks.on_session_loaded = nil |
| 86 | + |
| 87 | + local events = helpers.load_test_data('tests/data/simple-session.json') |
| 88 | + state.active_session = helpers.get_session_from_events(events, true) |
| 89 | + local loaded_session = helpers.load_session_from_events(events) |
| 90 | + |
| 91 | + assert.has_no.errors(function() |
| 92 | + renderer._render_full_session_data(loaded_session) |
| 93 | + end) |
| 94 | + end) |
| 95 | + |
| 96 | + it('should not crash when hook throws error', function() |
| 97 | + config.hooks.on_session_loaded = function() |
| 98 | + error('test error') |
| 99 | + end |
| 100 | + |
| 101 | + local events = helpers.load_test_data('tests/data/simple-session.json') |
| 102 | + state.active_session = helpers.get_session_from_events(events, true) |
| 103 | + local loaded_session = helpers.load_session_from_events(events) |
| 104 | + |
| 105 | + assert.has_no.errors(function() |
| 106 | + renderer._render_full_session_data(loaded_session) |
| 107 | + end) |
| 108 | + end) |
| 109 | + end) |
| 110 | +end) |
0 commit comments