import { wrapTask } from '../lib'
import { logToTaskOutput, teardownHelperMock, formatLogMessageOneLine } from '../lib/logging'

jest.mock('../lib/logging', () => {
  const teardownHelperMock = jest.fn()
  return {
    formatLogMessageOneLine: jest.fn((logMessage) => `formatted: ${logMessage.error.message}`),
    logToTaskOutput: jest.fn(() => {
      return teardownHelperMock
    }),
    teardownHelperMock
  }
})

afterEach(() => {
  logToTaskOutput.mockClear()
  teardownHelperMock.mockClear()
  formatLogMessageOneLine.mockClear()
})

test('wraps task, sets up listeners and allows modification of task context', () => {
  const ctx = {}

  const wrappedTask = wrapTask((taskCtx) => {
    taskCtx.done = true
    return Promise.resolve()
  })

  return wrappedTask(ctx)
    .then(() => {
      expect(ctx.done).toBe(true, 'context got modified by the task')
      expect(logToTaskOutput.mock.calls).toHaveLength(1, 'task listener was initialized')
      expect(teardownHelperMock.mock.calls).toHaveLength(1, 'task listener was teared down again')
      expect(formatLogMessageOneLine.mock.calls).toHaveLength(0, 'no error was formatted')
    })
})

test('wraps task and properly formats and throws error', async () => {
  expect.assertions(9)

  const ctx = {}

  const wrappedTask = wrapTask(() => {
    return Promise.reject(new Error('Task failed'))
  })

  let err

  await wrappedTask(ctx)
    .catch((error) => {
      err = error
    })

  expect(Object.keys(ctx)).toHaveLength(0, 'context got not modified by the task since it failed')
  expect(err.message).toBe('formatted: Task failed', 'error message contains formatted error message')
  expect(err.originalError.message).toBe('Task failed', 'original error message is still present')

  expect(logToTaskOutput.mock.calls).toHaveLength(1, 'task listener was initialized')
  expect(teardownHelperMock.mock.calls).toHaveLength(1, 'task listener was teared down again')
  expect(formatLogMessageOneLine.mock.calls).toHaveLength(1, 'error message was formatted')

  expect(formatLogMessageOneLine.mock.calls[0][0].ts).not.toHaveLength(0, 'log message contains a timestamp')
  expect(formatLogMessageOneLine.mock.calls[0][0].level).toBe('error', 'log message has level of error')
  expect(formatLogMessageOneLine.mock.calls[0][0].error.message).toBe('Task failed', 'log message error has original error message')
})

@@ -56,28 +56,8 @@
 put,
- teardownHelperMock,
  for
@@ -142,47 +142,8 @@
 %3E %7B%0A
-  const teardownHelperMock = jest.fn()%0A
   re
@@ -284,72 +284,13 @@
  =%3E 
-%7B%0A      return teardownHelperMock%0A    %7D),%0A    teardownHelperMock
+null)
 %0A  %7D
@@ -346,41 +346,8 @@
 r()%0A
-  teardownHelperMock.mockClear()%0A
   fo
@@ -461,16 +461,22 @@
 ontext',
+ async
  () =%3E %7B
@@ -601,22 +601,21 @@
   %7D)%0A%0A  
-return
+await
  wrapped
@@ -628,30 +628,9 @@
 tx)%0A
-    .then(() =%3E %7B%0A    
+%0A
   ex
@@ -657,50 +657,10 @@
 true
-, 'context got modified by the task')%0A    
+)%0A
   ex
@@ -710,146 +710,10 @@
 th(1
-, 'task listener was initialized')%0A      expect(teardownHelperMock.mock.calls).toHaveLength(1, 'task listener was teared down again')%0A    
+)%0A
   ex
@@ -771,41 +771,8 @@
 th(0
-, 'no error was formatted')%0A    %7D
 )%0A%7D)
@@ -868,9 +868,9 @@
 ons(
-9
+7
 )%0A%0A 
@@ -876,32 +876,69 @@
   const ctx = %7B%7D
+%0A  const errorMessage = 'Task failed'
 %0A%0A  const wrappe
@@ -963,21 +963,8 @@
 ) =%3E
- %7B%0A    return
  Pro
@@ -989,292 +989,90 @@
 ror(
-'Task failed'))%0A  %7D)%0A%0A  let err%0A%0A  await wrappedTask(ctx)%0A    .catch((error) =%3E %7B%0A      err = error%0A    %7D)%0A%0A  expect(Object.keys(ctx)).toHaveLength(0, 'context got not modified by the task since it failed')%0A  expect(err.message).toBe('formatted: Task failed', 'error message contains 
+errorMessage)))%0A%0A  await expect(wrappedTask(ctx)).rejects.toThrow(%7B%0A    message: %60
 form
@@ -1080,304 +1080,183 @@
 tted
- 
+: $%7B
 error
- m
+M
 essage
-')%0A  expect(err.originalError.message).toBe('Task failed', 'original
+%7D%60,%0A    originalError: %7B%0A      message:
  error
- m
+M
 essage
- is still present')%0A%0A  expect(logToTaskOutput.mock.calls).toHaveLength(1, 'task listener was initialized')%0A  expect(teardownHelperMock.mock.calls).toHaveLength(1, 'task listener was teared down again'
+%0A    %7D%0A  %7D)%0A%0A  expect(Object.keys(ctx)).toHaveLength(0)%0A  expect(logToTaskOutput.mock.calls).toHaveLength(1
 )%0A  
@@ -1316,39 +1316,8 @@
 th(1
-, 'error message was formatted'
 )%0A%0A 
@@ -1391,44 +1391,8 @@
 th(0
-, 'log message contains a timestamp'
 )%0A  
@@ -1456,42 +1456,8 @@
 Be('
-error', 'log message has level of 
 erro
@@ -1534,69 +1534,20 @@
 oBe(
-'Task failed', 'log message error has original error m
+errorM
 essage
-'
 )%0A%7D)

clean up listr tests