{"size": 2677, "ext": "js", "max_stars_repo_path": "setup-nuget-sources/test/nuget-sources.test.js", "max_stars_repo_name": "sensrtrx/actions", "max_stars_repo_stars_event_min_datetime": "2020-08-19T11:32:53.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-03T16:29:52.000Z", "max_issues_repo_path": "setup-nuget-sources/test/nuget-sources.test.js", "max_issues_repo_name": "sensrtrx/actions", "max_issues_repo_issues_event_min_datetime": "2019-11-28T07:06:47.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-22T09:33:00.000Z", "max_forks_repo_path": "setup-nuget-sources/test/nuget-sources.test.js", "max_forks_repo_name": "sensrtrx/actions", "max_forks_repo_forks_event_min_datetime": "2019-12-19T07:19:14.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-24T04:14:43.000Z"}
{"max_stars_count": 17, "max_issues_count": 321, "max_forks_count": 11, "avg_line_length": 37.1805555556, "max_line_length": 184, "alphanum_fraction": 0.6372805379}
const fs = require('fs');

const {
  generateRegexPattern,
  commentOutSourceUrl,
  parseNugetSourceJson,
} = require('../src/nuget-sources');

const nugetSorce = `[{
  "name": "nuget.org",
  "source": "https://api.nuget.org/v3/index.json"
},
{
  "name": "Extenda",
  "source": "https://repo.extendaretail.com/repository/nuget-group/",
  "username": "MyUser",
  "password": "MyPwd",
  "apikey": "MyKey"
}]`;

const nugetXml = `<?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <solution>
        <add key="disableSourceControlIntegration" value="true" />
      </solution>
      <packageSources>
          <add key="RS (Nexus)" value="https://repo.extendaretail.com/repository/nuget-group/" />
        <add key="test" value="test value" />
          <add key="nuget.org-proxy" value="https://repo.extendaretail.com/repository/nuget-group/" />
      </packageSources>
      <packageSourceCredentials />
      <activePackageSource>
        <add key="All" value="(Aggregate source)" />
      </activePackageSource>
      <packageSourceCredentials />
      <disabledPackageSources />
    </configuration>`;

describe('Setup Nuget Sources tests', () => {
  beforeAll(() => {
    fs.writeFileSync(`${__dirname}/NuGet-test.config`, nugetXml, { encoding: 'utf8', flag: 'w' });
  });

  test('Generate regex from url', () => {
    expect(generateRegexPattern('https://repo.extendaretail.com/repository/nuget-group/')).toEqual(/^\s*(.*"https:\/\/repo.extendaretail.com\/repository\/nuget-group\/\/?"\s*\/>)$/gm);
    expect(generateRegexPattern('https://repo.extendaretail.com/repository/nuget-group')).toEqual(/^\s*(.*"https:\/\/repo.extendaretail.com\/repository\/nuget-group\/?"\s*\/>)$/gm);
    expect(generateRegexPattern('https://api.nuget.org/v3/index.json')).toEqual(/^\s*(.*"https:\/\/api.nuget.org\/v3\/index.json\/?"\s*\/>)$/gm);
    expect(generateRegexPattern(null)).toEqual(null);
  });

  test('Comment out existing repo URLs', async () => {
    expect.assertions(2);
    return commentOutSourceUrl(`${__dirname}/NuGet-test.config`, /^\s*(.*"https:\/\/repo.extendaretail.com\/repository\/nuget-group\/\/?"\s*\/>)$/gm)
      .then((result) => {
        expect(result.length).toEqual(1);
        expect(result[0].hasChanged).toEqual(true);
      });
  });

  test('Parsing NuGet source JSON', () => {
    const result = parseNugetSourceJson(nugetSorce);

    expect(result[0].name).toEqual('nuget.org');
    expect(result[0].username).toEqual(undefined);

    expect(result[1].name).toEqual('Extenda');
    expect(result[1].username).toEqual('MyUser');
    expect(result[1].password).toEqual('MyPwd');
    expect(result[1].apikey).toEqual('MyKey');
  });
});
