import { Upload } from '../../src/command/upload'
import { toMatchLinesInOrder } from '../custom-matcher'
import { describeCommand, invokeTestCli } from '../utility'
import { getStampOption } from '../utility/stamp'

expect.extend({
  toMatchLinesInOrder,
})

describeCommand(
  'Test Feed command',
  ({ consoleMessages, getLastMessage, hasMessageContaining }) => {
    it('should upload file, update feed and print it', async () => {
      // create identity
      await invokeTestCli(['identity', 'create', 'test', '--password', 'test'])
      // upload
      await invokeTestCli([
        'feed',
        'upload',
        `${__dirname}/../testpage/images/swarm.png`,
        '--identity',
        'test',
        '--topic-string',
        'test',
        '--password',
        'test',
        '--quiet',
        ...getStampOption(),
      ])
      // print with identity and password
      await invokeTestCli([
        'feed',
        'print',
        '--identity',
        'test',
        '--topic-string',
        'test',
        '--password',
        'test',
        '--quiet',
      ])
      expect(getLastMessage()).toMatch(/[a-z0-9]{64}/)
    })

    it('should print feed using address only', async () => {
      // create identity
      await invokeTestCli(['identity', 'create', 'test2', '--password', 'test'])
      const address = getLastMessage().split(' ')[1]
      // upload
      await invokeTestCli([
        'feed',
        'upload',
        `${__dirname}/../testpage/index.html`,
        '--identity',
        'test2',
        '--password',
        'test',
        ...getStampOption(),
      ])
      // print with address
      await invokeTestCli(['feed', 'print', '--address', address, '--quiet'])
      expect(getLastMessage()).toMatch(/[a-z0-9]{64}/)
    })

    it('should update feeds', async () => {
      await invokeTestCli(['identity', 'create', 'update-feed-test', '-P', '1234', '-v'])
      const uploadCommand = await invokeTestCli(['upload', 'README.md', ...getStampOption()])
      const upload = uploadCommand.runnable as Upload
      const hash = upload.result.getOrThrow()

      consoleMessages.length = 0
      await invokeTestCli([
        'feed',
        'update',
        '--topic-string',
        'test-topic',
        '-i',
        'update-feed-test',
        '-P',
        '1234',
        '-r',
        hash.toHex(),
        ...getStampOption(),
      ])
      expect(hasMessageContaining('Feed Manifest URL')).toBeTruthy()
      expect(hasMessageContaining('/bzz/')).toBeTruthy()
    })

    it.skip('should increment number of updates for sequence feeds', async () => {
      await invokeTestCli(['identity', 'create', 'd12617', '--password', 'test'])
      await invokeTestCli([
        'feed',
        'upload',
        'README.md',
        '--identity',
        'd12617',
        '--password',
        'test',
        ...getStampOption(),
      ])
      await invokeTestCli(['feed', 'print', '--identity', 'd12617', '--password', 'test'])
      expect(consoleMessages).toMatchLinesInOrder([['Number of Updates', '1']])
      await invokeTestCli([
        'feed',
        'upload',
        'CHANGELOG.md',
        '--identity',
        'd12617',
        '--password',
        'test',
        ...getStampOption(),
      ])
      await invokeTestCli(['feed', 'print', '--identity', 'd12617', '--password', 'test'])
      expect(consoleMessages).toMatchLinesInOrder([
        ['Number of Updates', '1'],
        ['Number of Updates', '2'],
      ])
    })
  },
  { configFileName: 'feed' },
)
