import { Eltro as t, assert, stub } from 'eltro'
import Util from '../../core/util.mjs'
import fs from 'fs/promises'
import GitProvider from '../../core/providers/git.mjs'

t.after(function() {
  return fs.rm('./test/providers/file.7z')
    .catch(function() { })
})

t.timeout(5000).describe('#getLatestVersion()', function() {
  t.test('should return latest version in a valid repository', async function() {
    let provider = new GitProvider({ url: 'https://git.nfp.is/api/v1/repos/thething/sc-helloworld/releases' })
    let version = await provider.getLatestVersion()
    assert.ok(version)
    assert.ok(version.version)
    assert.ok(version.description)
    assert.ok(version.link)
    assert.match(version.link, /\/download\//)
  })

  t.test('should fail if link does not return json repository object', async function() {
    let err = await assert.isRejected(new GitProvider({ url: 'http://git.nfp.is/api/v1/repos/thething/ProgramQueuer' }).getLatestVersion())
    assert.match(err.message, /valid/i)
    assert.match(err.message, /repository/i)
    err = await assert.isRejected(new GitProvider({ url: 'http://git.nfp.is/api/v1/orgs/nfp/repos' }).getLatestVersion())
    assert.match(err.message, /service-core/i)
    assert.match(err.message, /release/i)
  })

  t.test('should fail if no active release repository with assets', async function() {
    let err = await assert.isRejected(new GitProvider({ url: 'https://git.nfp.is/api/v1/repos/thething/eltro/releases' }).getLatestVersion())
    assert.match(err.message, /service-core/i)
    assert.match(err.message, /release/i)
  })

  t.test('should fail on private repositories', async function() {
    let err = await assert.isRejected(new GitProvider({ url: 'https://git.nfp.is/api/v1/repos/TheThing/privateexample/releases' }).getLatestVersion())
    assert.match(err.message, /fail/i)
    assert.match(err.message, /404/i)
    assert.match(err.message, /release/i)
  })

  t.test('should otherwise succeed', function() {
    return new GitProvider({ url: 'https://git.nfp.is/api/v1/repos/TheThing/sc-helloworld/releases' })
      .getLatestVersion()
  })

  let test = t
  if (!process.env.gittesttoken) {
    console.log('Skipping "git.test.integration: #getLatestVersion() should succeed on private repo with token"')
    test = test.skip()
  }
  test.test('should succeed on private repo with token', function() {
    return new GitProvider({
      token: process.env.gittesttoken.trim(),
      url: 'https://git.nfp.is/api/v1/repos/TheThing/privateexample/releases',
    }).getLatestVersion()
  })
})

t.timeout(5000).describe('#downloadVersion()', function() {
  const util = new Util(import.meta.url)

  let test = t
  if (!process.env.gittesttoken) {
    console.log('Skipping "git.test.integration: #downloadVersion() should successfully download release"')
    test = test.skip()
  }
  test.test('should successfully download release', async function() {
    let provider = new GitProvider({
      token: process.env.gittesttoken.trim(),
      url: 'https://git.nfp.is/api/v1/repos/TheThing/privateexample/releases',
    })
    await provider.checkConfig()
    let version = await provider.getLatestVersion()
    assert.ok(version.version)
    assert.ok(version.filename)
    assert.ok(version.link)

    let path = util.getPathFromRoot('./file.7z')
    await provider.downloadVersion(version, path)

    let stat = await fs.stat(path)
    assert.ok(stat.size > 0)
    stat = await fs.stat('./test/providers/file.7z')
    assert.ok(stat.size > 0)

    let output = ''
    await util.runCommand(util.get7zipExecutable(), ['l', 'file.7z'], util.getPathFromRoot('./'), function(chunk) {
      output += chunk + '\n'
    })
    assert.ok(output.indexOf('file1.txt'))
    assert.ok(output.indexOf('file2.txt'))
  })
})
