Describe gina#core#emitter
  Before all
    let Path = vital#gina#import('System.Filepath')
    let slit1 = Slit(tempname(), 1)
    let slit3 = Slit(tempname())
  End

  After all
    %bwipeout!
  End

  Before
    let tracker = {
          \ 'modified_called_count': 0,
          \ 'modified_delay_called_count': 0,
          \}

    function! tracker.modified(...) abort
      let self.modified_called_count += 1
    endfunction

    function! tracker.modified_delay(...) abort
      let self.modified_delay_called_count += 1
    endfunction

    call gina#core#emitter#subscribe(
          \ 'modified',
          \ tracker.modified, tracker,
          \)
    call gina#core#emitter#subscribe(
          \ 'modified:delay',
          \ tracker.modified_delay, tracker,
          \)
  End

  After
    call gina#core#emitter#unsubscribe(
          \ 'modified',
          \ tracker.modified, tracker,
          \)
    call gina#core#emitter#unsubscribe(
          \ 'modified:delay',
          \ tracker.modified_delay, tracker,
          \)
  End

  It emits 'modified' event a bit later by 'modified:delay' event
    Assert Equals(tracker.modified_called_count, 0)
    Assert Equals(tracker.modified_delay_called_count, 0)
    call gina#core#emitter#emit('modified:delay')
    Assert Equals(tracker.modified_called_count, 0)
    Assert Equals(tracker.modified_delay_called_count, 1)
    execute 'sleep' (g:gina#core#emitter#modified_delay * 10) . 'm'
    Assert Equals(tracker.modified_called_count, 1)
    Assert Equals(tracker.modified_delay_called_count, 1)
  End

  It squashes 'modified:delay' events emitted within 'modified_delay' milliseconds
    Assert Equals(tracker.modified_called_count, 0)
    Assert Equals(tracker.modified_delay_called_count, 0)
    call gina#core#emitter#emit('modified:delay')
    call gina#core#emitter#emit('modified:delay')
    call gina#core#emitter#emit('modified:delay')
    call gina#core#emitter#emit('modified:delay')
    call gina#core#emitter#emit('modified:delay')
    Assert Equals(tracker.modified_called_count, 0)
    Assert Equals(tracker.modified_delay_called_count, 5)
    execute 'sleep' (g:gina#core#emitter#modified_delay * 10) . 'm'
    Assert Equals(tracker.modified_called_count, 1)
    Assert Equals(tracker.modified_delay_called_count, 5)
  End

  It DOES NOT emits 'modified:delay' event by modifing a buffer NOT in a git repository
    execute 'edit' fnameescape(Path.join(slit3.worktree, 'foo.txt'))
    Assert Equals(tracker.modified_called_count, 0)
    Assert Equals(tracker.modified_delay_called_count, 0)
    execute "normal! ifoobar\<Esc>:w\<CR>"
    Assert Equals(tracker.modified_called_count, 0)
    Assert Equals(tracker.modified_delay_called_count, 0)
    execute 'sleep' (g:gina#core#emitter#modified_delay * 10) . 'm'
    Assert Equals(tracker.modified_called_count, 0)
    Assert Equals(tracker.modified_delay_called_count, 0)
  End
End
