import {
  jest,
  describe,
  it,
  expect,
  beforeAll,
  beforeEach,
  afterEach,
  afterAll
} from '@jest/globals';
import fs from 'fs';
import cp from 'child_process';

import goJsonData from './data/golang-dl.json' with {type: 'json'};
import matchers from '../matchers.json' with {type: 'json'};
import goTestManifest from './data/versions-manifest.json' with {type: 'json'};

import type {IGoVersion} from '../src/installer.js';
import type {IToolRelease} from '@actions/tool-cache';

const httpClientGetJson = jest.fn();

const realCore = await import('@actions/core');

jest.unstable_mockModule('@actions/core', () => ({
  ...realCore,
  getInput: jest.fn(),
  getBooleanInput: jest.fn(),
  info: jest.fn(),
  debug: jest.fn(),
  exportVariable: jest.fn()
}));

const realOs = (await import('os')).default;
const osPlatformMock = jest.fn();
const osArchMock = jest.fn();
const osExports = {
  ...realOs,
  platform: osPlatformMock,
  arch: osArchMock
};
jest.unstable_mockModule('os', () => ({...osExports, default: osExports}));

const realPath = (await import('path')).default;
const pathJoinMock = jest.fn();
const pathExports = {
  ...realPath,
  join: pathJoinMock
};
jest.unstable_mockModule('path', () => ({
  ...pathExports,
  default: pathExports
}));

jest.unstable_mockModule('@actions/io', () => ({
  which: jest.fn(),
  mkdirP: jest.fn(),
  rmRF: jest.fn(),
  mv: jest.fn(),
  cp: jest.fn()
}));

const realTc = await import('@actions/tool-cache');

jest.unstable_mockModule('@actions/tool-cache', () => ({
  ...realTc,
  find: jest.fn(),
  downloadTool: jest.fn(),
  extractTar: jest.fn(),
  extractZip: jest.fn(),
  cacheDir: jest.fn(),
  getManifestFromRepo: jest.fn()
}));

const realHttp = await import('@actions/http-client');

jest.unstable_mockModule('@actions/http-client', () => ({
  ...realHttp,
  HttpClient: jest.fn().mockImplementation(() => ({
    getJson: httpClientGetJson
  }))
}));

jest.unstable_mockModule('../src/go-version-fetch.js', () => ({
  getVersionsDist: jest.fn()
}));

const core = await import('@actions/core');
const io = await import('@actions/io');
const tc = await import('@actions/tool-cache');
const vf = await import('../src/go-version-fetch.js');
const main = await import('../src/main.js');
const im = await import('../src/installer.js');
const osm = (await import('os')).default;
const path = (await import('path')).default;
const matcherPattern = matchers.problemMatcher[0].pattern[0];
const matcherRegExp = new RegExp(matcherPattern.regexp);
const win32Join = path.win32.join;
const posixJoin = path.posix.join;

jest.setTimeout(10000);

describe('setup-go', () => {
  let inputs = {} as any;
  let os = {} as any;

  let inSpy: jest.Mock<typeof core.getInput>;
  let getBooleanInputSpy: jest.Mock<typeof core.getBooleanInput>;
  let exportVarSpy: jest.Mock<typeof core.exportVariable>;
  let findSpy: jest.Mock;
  let cnSpy: jest.SpiedFunction<typeof process.stdout.write>;
  let logSpy: jest.Mock;
  let getSpy: jest.Mock;
  let platSpy: jest.Mock;
  let archSpy: jest.Mock;
  let joinSpy: jest.Mock<typeof path.join>;
  let dlSpy: jest.Mock;
  let extractTarSpy: jest.Mock;
  let extractZipSpy: jest.Mock;
  let cacheSpy: jest.Mock;
  let dbgSpy: jest.Mock;
  let whichSpy: jest.Mock;
  let existsSpy: jest.SpiedFunction<typeof fs.existsSync>;
  let readFileSpy: jest.SpiedFunction<typeof fs.readFileSync>;
  let mkdirpSpy: jest.Mock;
  let mkdirSpy: jest.SpiedFunction<typeof fs.mkdir>;
  let symlinkSpy: jest.SpiedFunction<typeof fs.symlinkSync>;
  let execSpy: jest.SpiedFunction<typeof cp.execSync>;
  let execFileSpy: jest.SpiedFunction<typeof cp.execFileSync>;
  let getManifestSpy: jest.Mock;
  let httpmGetJsonSpy: jest.Mock;

  beforeAll(async () => {
    process.env['GITHUB_ENV'] = ''; // Stub out Environment file functionality so we can verify it writes to standard out (toolkit is backwards compatible)
  }, 100000);

  beforeEach(() => {
    process.env['GITHUB_PATH'] = ''; // Stub out ENV file functionality so we can verify it writes to standard out

    // @actions/core
    inputs = {};
    inSpy = core.getInput as jest.Mock<typeof core.getInput>;
    inSpy.mockImplementation(name => inputs[name]);
    getBooleanInputSpy = core.getBooleanInput as jest.Mock<
      typeof core.getBooleanInput
    >;
    getBooleanInputSpy.mockImplementation(name => inputs[name]);
    exportVarSpy = core.exportVariable as jest.Mock<typeof core.exportVariable>;

    // node
    os = {};
    platSpy = osm.platform as jest.Mock;
    platSpy.mockImplementation(() => os['platform']);
    archSpy = osm.arch as jest.Mock;
    archSpy.mockImplementation(() => os['arch']);
    execSpy = jest.spyOn(cp, 'execSync');
    execFileSpy = jest.spyOn(cp, 'execFileSync');
    execFileSpy.mockImplementation(() => {
      throw new Error('ENOENT');
    });

    // switch path join behaviour based on set os.platform
    joinSpy = path.join as jest.Mock<typeof path.join>;
    joinSpy.mockImplementation((...paths: string[]): string => {
      if (os['platform'] == 'win32') {
        return win32Join(...paths);
      }

      return posixJoin(...paths);
    });

    // @actions/tool-cache
    findSpy = tc.find as jest.Mock;
    dlSpy = tc.downloadTool as jest.Mock;
    extractTarSpy = tc.extractTar as jest.Mock;
    extractZipSpy = tc.extractZip as jest.Mock;
    cacheSpy = tc.cacheDir as jest.Mock;
    getSpy = vf.getVersionsDist as jest.Mock;
    getManifestSpy = tc.getManifestFromRepo as jest.Mock;

    // httm
    httpmGetJsonSpy = httpClientGetJson;

    // io
    whichSpy = io.which as jest.Mock;
    existsSpy = jest.spyOn(fs, 'existsSync');
    readFileSpy = jest.spyOn(fs, 'readFileSync');
    mkdirpSpy = io.mkdirP as jest.Mock;

    // fs
    mkdirSpy = jest.spyOn(fs, 'mkdir');
    symlinkSpy = jest.spyOn(fs, 'symlinkSync');
    symlinkSpy.mockImplementation(() => {});

    // gets
    getManifestSpy.mockImplementation(() => goTestManifest as IToolRelease[]);

    // writes
    cnSpy = jest.spyOn(process.stdout, 'write');
    logSpy = core.info as jest.Mock;
    dbgSpy = core.debug as jest.Mock;
    getSpy.mockImplementation(() => goJsonData as IGoVersion[] | null);
    cnSpy.mockImplementation(() => true);
    logSpy.mockImplementation(() => {});
    dbgSpy.mockImplementation(() => {});
  });

  afterEach(() => {
    // clear out env vars set during 'run'
    delete process.env[im.GOTOOLCHAIN_ENV_VAR];
    delete process.env['GO_DOWNLOAD_BASE_URL'];

    // reset the exit code that core.setFailed sets on the error-path tests so
    // that a passing run does not leak a non-zero process exit code
    process.exitCode = 0;

    //jest.resetAllMocks();
    jest.clearAllMocks();
    //jest.restoreAllMocks();
  });

  afterAll(async () => {
    jest.restoreAllMocks();
  }, 100000);

  it('can extract the major.minor.patch version from a given Go version string', async () => {
    const goVersionOutput = 'go version go1.16.6 darwin/amd64';
    expect(main.parseGoVersion(goVersionOutput)).toBe('1.16.6');
  });

  it('can find 1.9.7 from manifest on osx', async () => {
    os.platform = 'darwin';
    os.arch = 'x64';

    const match = await im.getInfoFromManifest('1.9.7', true, 'mocktoken');
    expect(match).toBeDefined();
    expect(match!.resolvedVersion).toBe('1.9.7');
    expect(match!.type).toBe('manifest');
    expect(match!.downloadUrl).toBe(
      'https://github.com/actions/go-versions/releases/download/1.9.7/go-1.9.7-darwin-x64.tar.gz'
    );
  });

  it('should return manifest from repo', async () => {
    const manifest = await im.getManifest(undefined);
    expect(manifest).toEqual(goTestManifest);
  });

  it('should return manifest from raw URL if repo fetch fails', async () => {
    (getManifestSpy as jest.Mock<any>).mockRejectedValue(
      new Error('Fetch failed')
    );
    (httpmGetJsonSpy as jest.Mock<any>).mockResolvedValue({
      result: goTestManifest
    });
    const manifest = await im.getManifest(undefined);
    expect(httpmGetJsonSpy).toHaveBeenCalled();
    expect(manifest).toEqual(goTestManifest);
  });

  it('can find 1.9 from manifest on linux', async () => {
    os.platform = 'linux';
    os.arch = 'x64';

    const match = await im.getInfoFromManifest('1.9.7', true, 'mocktoken');
    expect(match).toBeDefined();
    expect(match!.resolvedVersion).toBe('1.9.7');
    expect(match!.type).toBe('manifest');
    expect(match!.downloadUrl).toBe(
      'https://github.com/actions/go-versions/releases/download/1.9.7/go-1.9.7-linux-x64.tar.gz'
    );
  });

  it('can find 1.9 from manifest on windows', async () => {
    os.platform = 'win32';
    os.arch = 'x64';

    const match = await im.getInfoFromManifest('1.9.7', true, 'mocktoken');
    expect(match).toBeDefined();
    expect(match!.resolvedVersion).toBe('1.9.7');
    expect(match!.type).toBe('manifest');
    expect(match!.downloadUrl).toBe(
      'https://github.com/actions/go-versions/releases/download/1.9.7/go-1.9.7-win32-x64.zip'
    );
  });

  it('finds stable match for exact dot zero version', async () => {
    os.platform = 'darwin';
    os.arch = 'x64';

    // spec: 1.13.0 => 1.13
    const match: IGoVersion | undefined = await im.findMatch('1.13.0');
    expect(match).toBeDefined();
    const version: string = match ? match.version : '';
    expect(version).toBe('go1.13');
    const fileName = match ? match.files[0].filename : '';
    expect(fileName).toBe('go1.13.darwin-amd64.tar.gz');
  });

  it('finds latest patch version for minor version spec', async () => {
    os.platform = 'linux';
    os.arch = 'x64';

    // spec: 1.13 => 1.13.7 (latest)
    const match: IGoVersion | undefined = await im.findMatch('1.13');
    expect(match).toBeDefined();
    const version: string = match ? match.version : '';
    expect(version).toBe('go1.13.7');
    const fileName = match ? match.files[0].filename : '';
    expect(fileName).toBe('go1.13.7.linux-amd64.tar.gz');
  });

  it('finds latest patch version for caret version spec', async () => {
    os.platform = 'linux';
    os.arch = 'x64';

    // spec: ^1.13.6 => 1.13.7
    const match: IGoVersion | undefined = await im.findMatch('^1.13.6');
    expect(match).toBeDefined();
    const version: string = match ? match.version : '';
    expect(version).toBe('go1.13.7');
    const fileName = match ? match.files[0].filename : '';
    expect(fileName).toBe('go1.13.7.linux-amd64.tar.gz');
  });

  it('finds latest version for major version spec', async () => {
    os.platform = 'win32';
    os.arch = 'x32';

    // spec: 1 => 1.13.7 (latest)
    const match: IGoVersion | undefined = await im.findMatch('1');
    expect(match).toBeDefined();
    const version: string = match ? match.version : '';
    expect(version).toBe('go1.13.7');
    const fileName = match ? match.files[0].filename : '';
    expect(fileName).toBe('go1.13.7.windows-386.zip');
  });

  it('finds unstable pre-release version', async () => {
    os.platform = 'linux';
    os.arch = 'x64';

    // spec: 1.14, stable=false => go1.14rc1
    const match: IGoVersion | undefined = await im.findMatch('1.14.0-rc.1');
    expect(match).toBeDefined();
    const version: string = match ? match.version : '';
    expect(version).toBe('go1.14rc1');
    const fileName = match ? match.files[0].filename : '';
    expect(fileName).toBe('go1.14rc1.linux-amd64.tar.gz');
  });

  it('evaluates to stable with input as true', async () => {
    inputs['go-version'] = '1.13.0';
    inputs.stable = 'true';

    const toolPath = path.normalize('/cache/go/1.13.0/x64');
    findSpy.mockImplementation(() => toolPath);
    await main.run();

    expect(logSpy).toHaveBeenCalledWith(`Setup go version spec 1.13.0`);
  });

  it('evaluates to stable with no input', async () => {
    inputs['go-version'] = '1.13.0';

    inSpy.mockImplementation(name => inputs[name]);

    const toolPath = path.normalize('/cache/go/1.13.0/x64');
    findSpy.mockImplementation(() => toolPath);
    await main.run();

    expect(logSpy).toHaveBeenCalledWith(`Setup go version spec 1.13.0`);
  });

  it('does not export GOROOT for Go versions >=1.9', async () => {
    inputs['go-version'] = '1.13.0';
    inSpy.mockImplementation(name => inputs[name]);

    const toolPath = path.normalize('/cache/go/1.13.0/x64');
    findSpy.mockImplementation(() => toolPath);

    const vars: {[key: string]: string} = {};
    exportVarSpy.mockImplementation((name: string, val: string) => {
      vars[name] = val;
    });

    await main.run();
    expect(vars).not.toHaveProperty('GOROOT');
  });

  it('exports GOROOT for Go versions <1.9', async () => {
    inputs['go-version'] = '1.8';
    inSpy.mockImplementation(name => inputs[name]);

    const toolPath = path.normalize('/cache/go/1.8.0/x64');
    findSpy.mockImplementation(() => toolPath);

    const vars: {[key: string]: string} = {};
    exportVarSpy.mockImplementation((name: string, val: string) => {
      vars[name] = val;
    });

    await main.run();
    expect(vars).toHaveProperty('GOROOT', toolPath);
  });

  it('finds a version of go already in the cache', async () => {
    inputs['go-version'] = '1.13.0';

    const toolPath = path.normalize('/cache/go/1.13.0/x64');
    findSpy.mockImplementation(() => toolPath);
    await main.run();

    expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
  });

  it('finds a version in the cache and adds it to the path', async () => {
    inputs['go-version'] = '1.13.0';
    const toolPath = path.normalize('/cache/go/1.13.0/x64');
    findSpy.mockImplementation(() => toolPath);
    await main.run();

    const expPath = path.join(toolPath, 'bin');
    expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
  });

  it('handles unhandled error and reports error', async () => {
    const errMsg = 'unhandled error message';
    inputs['go-version'] = '1.13.0';

    findSpy.mockImplementation(() => {
      throw new Error(errMsg);
    });
    await main.run();
    expect(cnSpy).toHaveBeenCalledWith('::error::' + errMsg + osm.EOL);
  });

  it('downloads a version not in the cache', async () => {
    os.platform = 'linux';
    os.arch = 'x64';

    inputs['go-version'] = '1.13.1';

    findSpy.mockImplementation(() => '');
    dlSpy.mockImplementation(() => '/some/temp/path');
    const toolPath = path.normalize('/cache/go/1.13.0/x64');
    extractTarSpy.mockImplementation(() => '/some/other/temp/path');
    cacheSpy.mockImplementation(() => toolPath);
    await main.run();

    const expPath = path.join(toolPath, 'bin');

    expect(dlSpy).toHaveBeenCalled();
    expect(extractTarSpy).toHaveBeenCalled();
    expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
  });

  it('downloads a version not in the cache (windows)', async () => {
    os.platform = 'win32';
    os.arch = 'x64';

    inputs['go-version'] = '1.13.1';
    process.env['RUNNER_TEMP'] = 'C:\\temp\\';

    findSpy.mockImplementation(() => '');
    dlSpy.mockImplementation(() => 'C:\\temp\\some\\path');
    extractZipSpy.mockImplementation(() => 'C:\\temp\\some\\other\\path');

    const toolPath = path.normalize('C:\\cache\\go\\1.13.0\\x64');
    cacheSpy.mockImplementation(() => toolPath);

    await main.run();

    const expPath = path.win32.join(toolPath, 'bin');
    expect(dlSpy).toHaveBeenCalledWith(
      'https://go.dev/dl/go1.13.1.windows-amd64.zip',
      'C:\\temp\\go1.13.1.windows-amd64.zip',
      undefined
    );
    expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
  });

  it('does not find a version that does not exist', async () => {
    os.platform = 'linux';
    os.arch = 'x64';

    inputs['go-version'] = '9.99.9';

    findSpy.mockImplementation(() => '');
    await main.run();

    expect(cnSpy).toHaveBeenCalledWith(
      `::error::Unable to find Go version '9.99.9' for platform linux and architecture x64.${osm.EOL}`
    );
  });

  it('downloads a version from a manifest match', async () => {
    os.platform = 'linux';
    os.arch = 'x64';

    const versionSpec = '1.12.16';

    inputs['go-version'] = versionSpec;
    inputs['token'] = 'faketoken';

    const expectedUrl =
      'https://github.com/actions/go-versions/releases/download/1.12.16-20200616.20/go-1.12.16-linux-x64.tar.gz';

    // ... but not in the local cache
    findSpy.mockImplementation(() => '');

    dlSpy.mockImplementation(async () => '/some/temp/path');
    const toolPath = path.normalize('/cache/go/1.12.16/x64');
    extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
    cacheSpy.mockImplementation(async () => toolPath);

    await main.run();

    const expPath = path.join(toolPath, 'bin');

    expect(dlSpy).toHaveBeenCalled();
    expect(extractTarSpy).toHaveBeenCalled();
    expect(logSpy).not.toHaveBeenCalledWith(
      'Not found in manifest.  Falling back to download directly from Go'
    );
    expect(logSpy).toHaveBeenCalledWith(
      `Acquiring 1.12.16 from ${expectedUrl}`
    );

    expect(logSpy).toHaveBeenCalledWith(`Added go to the path`);
    expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
  });

  it('downloads a major and minor from a manifest match', async () => {
    os.platform = 'linux';
    os.arch = 'x64';

    const versionSpec = '1.12';

    inputs['go-version'] = versionSpec;
    inputs['token'] = 'faketoken';

    const expectedUrl =
      'https://github.com/actions/go-versions/releases/download/1.12.17-20200616.21/go-1.12.17-linux-x64.tar.gz';

    // ... but not in the local cache
    findSpy.mockImplementation(() => '');

    dlSpy.mockImplementation(async () => '/some/temp/path');
    const toolPath = path.normalize('/cache/go/1.12.17/x64');
    extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
    cacheSpy.mockImplementation(async () => toolPath);

    await main.run();

    const expPath = path.join(toolPath, 'bin');

    expect(dlSpy).toHaveBeenCalled();
    expect(extractTarSpy).toHaveBeenCalled();
    expect(logSpy).not.toHaveBeenCalledWith(
      'Not found in manifest.  Falling back to download directly from Go'
    );
    expect(logSpy).toHaveBeenCalledWith(
      `Acquiring 1.12.17 from ${expectedUrl}`
    );

    expect(logSpy).toHaveBeenCalledWith(`Added go to the path`);
    expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
  });

  it('falls back to a version from go dist', async () => {
    os.platform = 'linux';
    os.arch = 'x64';

    const versionSpec = '1.12.14';

    inputs['go-version'] = versionSpec;
    inputs['token'] = 'faketoken';

    // ... but not in the local cache
    findSpy.mockImplementation(() => '');

    dlSpy.mockImplementation(async () => '/some/temp/path');
    const toolPath = path.normalize('/cache/go/1.12.14/x64');
    extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
    cacheSpy.mockImplementation(async () => toolPath);

    await main.run();

    const expPath = path.join(toolPath, 'bin');
    expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.12.14');
    expect(findSpy).toHaveBeenCalled();
    expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.12.14...');
    expect(dlSpy).toHaveBeenCalled();
    expect(logSpy).toHaveBeenCalledWith('matching 1.12.14...');
    expect(extractTarSpy).toHaveBeenCalled();
    expect(logSpy).toHaveBeenCalledWith(
      'Not found in manifest.  Falling back to download directly from Go'
    );
    expect(logSpy).toHaveBeenCalledWith(`Install from dist`);
    expect(logSpy).toHaveBeenCalledWith(`Added go to the path`);
    expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
  });

  it('reports a failed download', async () => {
    const errMsg = 'unhandled download message';
    os.platform = 'linux';
    os.arch = 'x64';

    inputs['go-version'] = '1.13.1';

    findSpy.mockImplementation(() => '');
    dlSpy.mockImplementation(() => {
      throw new Error(errMsg);
    });
    await main.run();

    expect(cnSpy).toHaveBeenCalledWith(
      `::error::Failed to download version 1.13.1: Error: ${errMsg}${osm.EOL}`
    );
  });

  it('does not add BIN if go is not in path', async () => {
    whichSpy.mockImplementation(async () => {
      return '';
    });
    const added = await main.addBinToPath();
    expect(added).toBeFalsy();
  });

  it('adds bin if dir not exists', async () => {
    whichSpy.mockImplementation(async () => {
      return '/usr/local/go/bin/go';
    });

    execSpy.mockImplementation(() => {
      return '/Users/testuser/go';
    });

    mkdirpSpy.mockImplementation(async () => {});
    existsSpy.mockImplementation(() => {
      return false;
    });

    const added = await main.addBinToPath();
    expect(added).toBeTruthy();
  });

  interface Annotation {
    file: string;
    line: number;
    column: number;
    message: string;
  }

  //
  // problem matcher regex pattern tests

  function testMatch(line: string): Annotation {
    const annotation = <Annotation>{};

    const match = matcherRegExp.exec(line);
    if (match) {
      annotation.line = parseInt(match[matcherPattern.line], 10);
      annotation.column = parseInt(match[matcherPattern.column], 10);
      annotation.file = match[matcherPattern.file].trim();
      annotation.message = match[matcherPattern.message].trim();
    }

    return annotation;
  }

  it('matches on relative unix path', async () => {
    const line = './main.go:13:2: undefined: fmt.Printl';
    const annotation = testMatch(line);
    expect(annotation).toBeDefined();
    expect(annotation.line).toBe(13);
    expect(annotation.column).toBe(2);
    expect(annotation.file).toBe('./main.go');
    expect(annotation.message).toBe('undefined: fmt.Printl');
  });

  it('matches on unix path up the tree', async () => {
    const line = '../main.go:13:2: undefined: fmt.Printl';
    const annotation = testMatch(line);
    expect(annotation).toBeDefined();
    expect(annotation.line).toBe(13);
    expect(annotation.column).toBe(2);
    expect(annotation.file).toBe('../main.go');
    expect(annotation.message).toBe('undefined: fmt.Printl');
  });

  it('matches on unix path down the tree', async () => {
    const line = 'foo/main.go:13:2: undefined: fmt.Printl';
    const annotation = testMatch(line);
    expect(annotation).toBeDefined();
    expect(annotation.line).toBe(13);
    expect(annotation.column).toBe(2);
    expect(annotation.file).toBe('foo/main.go');
    expect(annotation.message).toBe('undefined: fmt.Printl');
  });

  it('matches on rooted unix path', async () => {
    const line = '/assert.go:4:1: missing return at end of function';
    const annotation = testMatch(line);
    expect(annotation).toBeDefined();
    expect(annotation.line).toBe(4);
    expect(annotation.column).toBe(1);
    expect(annotation.file).toBe('/assert.go');
    expect(annotation.message).toBe('missing return at end of function');
  });

  it('matches on unix path with spaces', async () => {
    const line = '   ./assert.go:5:2: missing return at end of function   ';
    const annotation = testMatch(line);
    expect(annotation).toBeDefined();
    expect(annotation.line).toBe(5);
    expect(annotation.column).toBe(2);
    expect(annotation.file).toBe('./assert.go');
    expect(annotation.message).toBe('missing return at end of function');
  });

  it('matches on unix path with tabs', async () => {
    const line = '\t./assert.go:5:2: missing return at end of function   ';
    const annotation = testMatch(line);
    expect(annotation).toBeDefined();
    expect(annotation.line).toBe(5);
    expect(annotation.column).toBe(2);
    expect(annotation.file).toBe('./assert.go');
    expect(annotation.message).toBe('missing return at end of function');
  });

  it('matches on relative windows path', async () => {
    const line = '.\\main.go:13:2: undefined: fmt.Printl';
    const annotation = testMatch(line);
    expect(annotation).toBeDefined();
    expect(annotation.line).toBe(13);
    expect(annotation.column).toBe(2);
    expect(annotation.file).toBe('.\\main.go');
    expect(annotation.message).toBe('undefined: fmt.Printl');
  });

  it('matches on windows path up the tree', async () => {
    const line = '..\\main.go:13:2: undefined: fmt.Printl';
    const annotation = testMatch(line);
    expect(annotation).toBeDefined();
    expect(annotation.line).toBe(13);
    expect(annotation.column).toBe(2);
    expect(annotation.file).toBe('..\\main.go');
    expect(annotation.message).toBe('undefined: fmt.Printl');
  });

  // 1.13.1 => 1.13.1
  // 1.13 => 1.13.0
  // 1.10beta1 => 1.10.0-beta.1, 1.10rc1 => 1.10.0-rc.1
  // 1.8.5beta1 => 1.8.5-beta.1, 1.8.5rc1 => 1.8.5-rc.1
  it('converts prerelease versions', async () => {
    expect(im.makeSemver('1.10beta1')).toBe('1.10.0-beta.1');
    expect(im.makeSemver('1.10rc1')).toBe('1.10.0-rc.1');
  });

  it('converts dot zero versions', async () => {
    expect(im.makeSemver('1.13')).toBe('1.13.0');
  });

  it('does not convert exact versions', async () => {
    expect(im.makeSemver('1.13.1')).toBe('1.13.1');
  });

  describe('check-latest flag', () => {
    it("use local version and don't check manifest if check-latest is not specified", async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      inputs['go-version'] = '1.16';
      inputs['check-latest'] = false;

      const toolPath = path.normalize('/cache/go/1.16.1/x64');
      findSpy.mockReturnValue(toolPath);
      await main.run();

      expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
      expect(logSpy).not.toHaveBeenCalledWith(
        'Attempting to resolve the latest version from the manifest...'
      );
    });

    it('check latest version and resolve it from local cache', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      inputs['go-version'] = '1.16';
      inputs['check-latest'] = true;

      const toolPath = path.normalize('/cache/go/1.16.1/x64');
      findSpy.mockReturnValue(toolPath);
      dlSpy.mockImplementation(async () => '/some/temp/path');
      extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
      cacheSpy.mockImplementation(async () => toolPath);

      await main.run();

      expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.16');
      expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
    });

    it('check latest version and install it from manifest', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      const versionSpec = '1.17';
      const patchVersion = '1.17.6';
      inputs['go-version'] = versionSpec;
      inputs['stable'] = 'true';
      inputs['check-latest'] = true;

      findSpy.mockImplementation(() => '');
      dlSpy.mockImplementation(async () => '/some/temp/path');
      const toolPath = path.normalize('/cache/go/1.17.6/x64');
      extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
      cacheSpy.mockImplementation(async () => toolPath);

      await main.run();

      expect(logSpy).toHaveBeenCalledWith(
        `Setup go version spec ${versionSpec}`
      );
      expect(logSpy).toHaveBeenCalledWith(
        'Attempting to resolve the latest version from the manifest...'
      );
      expect(logSpy).toHaveBeenCalledWith(`Resolved as '${patchVersion}'`);
      expect(logSpy).toHaveBeenCalledWith(
        `Attempting to download ${patchVersion}...`
      );
      expect(logSpy).toHaveBeenCalledWith('Extracting Go...');
      expect(logSpy).toHaveBeenCalledWith('Adding to the cache ...');
      expect(logSpy).toHaveBeenCalledWith('Added go to the path');
      expect(logSpy).toHaveBeenCalledWith(
        `Successfully set up Go version ${versionSpec}`
      );
    });

    it('fallback to dist if version is not found in manifest', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      const versionSpec = '1.13';

      inputs['go-version'] = versionSpec;
      inputs['check-latest'] = true;
      inputs['always-auth'] = false;
      inputs['token'] = 'faketoken';

      // ... but not in the local cache
      findSpy.mockImplementation(() => '');

      dlSpy.mockImplementation(async () => '/some/temp/path');
      const toolPath = path.normalize('/cache/go/1.13.7/x64');
      extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
      cacheSpy.mockImplementation(async () => toolPath);

      await main.run();

      const expPath = path.join(toolPath, 'bin');

      expect(dlSpy).toHaveBeenCalled();
      expect(extractTarSpy).toHaveBeenCalled();
      expect(logSpy).toHaveBeenCalledWith(
        'Attempting to resolve the latest version from the manifest...'
      );
      expect(logSpy).toHaveBeenCalledWith(
        `Failed to resolve version ${versionSpec} from manifest`
      );
      expect(logSpy).toHaveBeenCalledWith(
        `Attempting to download ${versionSpec}...`
      );
      expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
    });

    it('fallback to dist if manifest is not available', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      const versionSpec = '1.13';

      process.env['GITHUB_PATH'] = '';

      inputs['go-version'] = versionSpec;
      inputs['check-latest'] = true;
      inputs['always-auth'] = false;
      inputs['token'] = 'faketoken';

      // ... but not in the local cache
      findSpy.mockImplementation(() => '');
      getManifestSpy.mockImplementation(() => {
        throw new Error('Unable to download manifest');
      });
      (httpmGetJsonSpy as jest.Mock<any>).mockRejectedValue(
        new Error('Unable to download manifest from raw URL')
      );

      dlSpy.mockImplementation(async () => '/some/temp/path');
      const toolPath = path.normalize('/cache/go/1.13.7/x64');
      extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
      cacheSpy.mockImplementation(async () => toolPath);

      await main.run();

      const expPath = path.join(toolPath, 'bin');

      expect(logSpy).toHaveBeenCalledWith(
        `Failed to resolve version ${versionSpec} from manifest`
      );
      expect(dlSpy).toHaveBeenCalled();
      expect(extractTarSpy).toHaveBeenCalled();
      expect(logSpy).toHaveBeenCalledWith(
        'Attempting to resolve the latest version from the manifest...'
      );
      expect(logSpy).toHaveBeenCalledWith(
        'Unable to resolve a version from the manifest...'
      );
      expect(logSpy).toHaveBeenCalledWith(
        `Failed to resolve version ${versionSpec} from manifest`
      );
      expect(logSpy).toHaveBeenCalledWith(
        `Attempting to download ${versionSpec}...`
      );

      expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
    });
  });

  describe('go-version-file', () => {
    const goModContents = `module example.com/mymodule

go 1.14

require (
	example.com/othermodule v1.2.3
	example.com/thismodule v1.2.3
	example.com/thatmodule v1.2.3
)

replace example.com/thatmodule => ../thatmodule
exclude example.com/thismodule v1.3.0
`;

    const goWorkContents = `go 1.19

use .

`;

    const toolVersionsContents = `golang 1.23
`;

    it('reads version from go.mod', async () => {
      inputs['go-version-file'] = 'go.mod';
      existsSpy.mockImplementation(() => true);
      readFileSpy.mockImplementation(() => Buffer.from(goModContents));

      await main.run();

      expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.14');
      expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.14...');
      expect(logSpy).toHaveBeenCalledWith('matching 1.14...');
    });

    it('reads version from go.work', async () => {
      inputs['go-version-file'] = 'go.work';
      existsSpy.mockImplementation(() => true);
      readFileSpy.mockImplementation(() => Buffer.from(goWorkContents));

      await main.run();

      expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.19');
      expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.19...');
      expect(logSpy).toHaveBeenCalledWith('matching 1.19...');
    });

    it('reads version from .tool-versions', async () => {
      inputs['go-version-file'] = '.tool-versions';
      existsSpy.mockImplementation(() => true);
      readFileSpy.mockImplementation(() => Buffer.from(toolVersionsContents));

      await main.run();

      expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.23');
      expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.23...');
      expect(logSpy).toHaveBeenCalledWith('matching 1.23...');
    });

    it('reads version from .go-version', async () => {
      inputs['go-version-file'] = '.go-version';
      existsSpy.mockImplementation(() => true);
      readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`));

      await main.run();

      expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.13.0');
      expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.13.0...');
      expect(logSpy).toHaveBeenCalledWith('matching 1.13.0...');
    });

    it('is overwritten by go-version', async () => {
      inputs['go-version'] = '1.13.1';
      inputs['go-version-file'] = 'go.mod';
      existsSpy.mockImplementation(() => true);
      readFileSpy.mockImplementation(() => Buffer.from(goModContents));

      await main.run();

      expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.13.1');
      expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.13.1...');
      expect(logSpy).toHaveBeenCalledWith('matching 1.13.1...');
    });

    it('reports a read failure', async () => {
      inputs['go-version-file'] = 'go.mod';
      existsSpy.mockImplementation(() => false);

      await main.run();

      expect(cnSpy).toHaveBeenCalledWith(
        `::error::The specified go version file at: go.mod does not exist${osm.EOL}`
      );
    });

    it('acquires specified architecture of go', async () => {
      for (const {arch, version, osSpec} of [
        {arch: 'amd64', version: '1.13.7', osSpec: 'linux'},
        {arch: 'armv6l', version: '1.12.2', osSpec: 'linux'}
      ]) {
        os.platform = osSpec;
        os.arch = arch;

        const fileExtension = os.platform === 'win32' ? 'zip' : 'tar.gz';

        const platform = os.platform === 'win32' ? 'win' : os.platform;

        inputs['go-version'] = version;
        inputs['architecture'] = arch;

        const expectedUrl =
          platform === 'win32'
            ? `https://github.com/actions/go-versions/releases/download/${version}/go-${version}-${platform}-${arch}.${fileExtension}`
            : `https://go.dev/dl/go${version}.${osSpec}-${arch}.${fileExtension}`;

        // ... but not in the local cache
        findSpy.mockImplementation(() => '');

        dlSpy.mockImplementation(async () => '/some/temp/path');
        const toolPath = path.normalize(`/cache/go/${version}/${arch}`);
        cacheSpy.mockImplementation(async () => toolPath);

        await main.run();

        expect(logSpy).toHaveBeenCalledWith(
          `Acquiring go${version} from ${expectedUrl}`
        );
      }
    }, 100000);

    it.each(['stable', 'oldstable'])(
      'acquires latest go version with %s go-version input',
      async (alias: string) => {
        const arch = 'x64';
        os.platform = 'darwin';
        os.arch = arch;

        inputs['go-version'] = alias;
        inputs['architecture'] = os.arch;

        // ... but not in the local cache
        findSpy.mockImplementation(() => '');

        dlSpy.mockImplementation(async () => '/some/temp/path');
        const toolPath = path.normalize(`/cache/go/${alias}/${arch}`);
        cacheSpy.mockImplementation(async () => toolPath);

        await main.run();

        const releaseIndex = alias === 'stable' ? 0 : 1;

        expect(logSpy).toHaveBeenCalledWith(
          `${alias} version resolved as ${goTestManifest[releaseIndex].version}`
        );
      }
    );
  });

  describe('go-version-file-toolchain', () => {
    const goVersions = ['1.22.0', '1.21rc2', '1.18'];
    const placeholderVersion = '1.19';
    const buildGoMod = (
      goVersion: string,
      toolchainVersion: string
    ) => `module example.com/mymodule

go ${goVersion}

toolchain go${toolchainVersion}

require (
	example.com/othermodule v1.2.3
	example.com/thismodule v1.2.3
	example.com/thatmodule v1.2.3
)

replace example.com/thatmodule => ../thatmodule
exclude example.com/thismodule v1.3.0
`;

    const buildGoWork = (
      goVersion: string,
      toolchainVersion: string
    ) => `go 1.19

toolchain go${toolchainVersion}

use .

`;

    goVersions.forEach(version => {
      [
        {
          goVersionfile: 'go.mod',
          fileContents: Buffer.from(buildGoMod(placeholderVersion, version)),
          expected_version: version,
          desc: 'from toolchain directive'
        },
        {
          goVersionfile: 'go.work',
          fileContents: Buffer.from(buildGoMod(placeholderVersion, version)),
          expected_version: version,
          desc: 'from toolchain directive'
        },
        {
          goVersionfile: 'go.mod',
          fileContents: Buffer.from(buildGoMod(placeholderVersion, version)),
          gotoolchain_env: 'local',
          expected_version: placeholderVersion,
          desc: 'from go directive when GOTOOLCHAIN is local'
        },
        {
          goVersionfile: 'go.work',
          fileContents: Buffer.from(buildGoMod(placeholderVersion, version)),
          gotoolchain_env: 'local',
          expected_version: placeholderVersion,
          desc: 'from go directive when GOTOOLCHAIN is local'
        }
      ].forEach(test => {
        it(`reads version (${version}) in ${test.goVersionfile} ${test.desc}`, async () => {
          inputs['go-version-file'] = test.goVersionfile;
          if (test.gotoolchain_env !== undefined) {
            process.env[im.GOTOOLCHAIN_ENV_VAR] = test.gotoolchain_env;
          }
          existsSpy.mockImplementation(() => true);
          readFileSpy.mockImplementation(() => Buffer.from(test.fileContents));

          await main.run();

          expect(logSpy).toHaveBeenCalledWith(
            `Setup go version spec ${test.expected_version}`
          );
          expect(logSpy).toHaveBeenCalledWith(
            `Attempting to download ${test.expected_version}...`
          );
          expect(logSpy).toHaveBeenCalledWith(
            `matching ${test.expected_version}...`
          );
        });
      });
    });
  });

  it('exports GOTOOLCHAIN and sets it in current process env', async () => {
    inputs['go-version'] = '1.21.0';
    inSpy.mockImplementation(name => inputs[name]);

    const vars: {[key: string]: string} = {};
    exportVarSpy.mockImplementation((name: string, val: string) => {
      vars[name] = val;
    });

    await main.run();
    expect(vars).toStrictEqual({GOTOOLCHAIN: 'local'});
    expect(process.env).toHaveProperty('GOTOOLCHAIN', 'local');
  });

  describe('go-download-base-url', () => {
    it('downloads a version from custom base URL using version listing', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      const versionSpec = '1.13.1';
      const customBaseUrl = 'https://example.com/golang';

      inputs['go-version'] = versionSpec;
      inputs['go-download-base-url'] = customBaseUrl;

      findSpy.mockImplementation(() => '');
      dlSpy.mockImplementation(async () => '/some/temp/path');
      const toolPath = path.normalize('/cache/go/1.13.1/x64');
      extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
      cacheSpy.mockImplementation(async () => toolPath);

      await main.run();

      const expPath = path.join(toolPath, 'bin');
      expect(logSpy).toHaveBeenCalledWith(
        `Using custom Go download base URL: ${customBaseUrl}`
      );
      expect(logSpy).toHaveBeenCalledWith('Install from custom download URL');
      // Version listing should use custom base URL, not go.dev
      expect(getSpy).toHaveBeenCalledWith(
        `${customBaseUrl}/?mode=json&include=all`
      );
      expect(dlSpy).toHaveBeenCalled();
      expect(extractTarSpy).toHaveBeenCalled();
      expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
    });

    it('skips version listing for known direct-download URL (aka.ms)', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      const versionSpec = '1.25.0';
      const customBaseUrl = 'https://aka.ms/golang/release/latest';

      inputs['go-version'] = versionSpec;
      inputs['go-download-base-url'] = customBaseUrl;

      findSpy.mockImplementation(() => '');
      dlSpy.mockImplementation(async () => '/some/temp/path');
      const toolPath = path.normalize('/cache/go/1.25.0/x64');
      extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
      cacheSpy.mockImplementation(async () => toolPath);

      await main.run();

      const expPath = path.join(toolPath, 'bin');
      expect(logSpy).toHaveBeenCalledWith(
        'Skipping version listing for known direct-download URL. Constructing download URL directly.'
      );
      expect(logSpy).toHaveBeenCalledWith(
        `Constructed direct download URL: ${customBaseUrl}/go1.25.0.linux-amd64.tar.gz`
      );
      expect(logSpy).toHaveBeenCalledWith('Install from custom download URL');
      expect(getSpy).not.toHaveBeenCalled();
      expect(dlSpy).toHaveBeenCalled();
      expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
    });

    it('constructs correct direct download URL for windows (aka.ms)', async () => {
      os.platform = 'win32';
      os.arch = 'x64';

      const versionSpec = '1.25.0';
      const customBaseUrl = 'https://aka.ms/golang/release/latest';

      inputs['go-version'] = versionSpec;
      inputs['go-download-base-url'] = customBaseUrl;
      process.env['RUNNER_TEMP'] = 'C:\\temp\\';

      findSpy.mockImplementation(() => '');
      dlSpy.mockImplementation(async () => 'C:\\temp\\some\\path');
      extractZipSpy.mockImplementation(() => 'C:\\temp\\some\\other\\path');
      const toolPath = path.normalize('C:\\cache\\go\\1.25.0\\x64');
      cacheSpy.mockImplementation(async () => toolPath);

      await main.run();

      expect(getSpy).not.toHaveBeenCalled();
      expect(dlSpy).toHaveBeenCalledWith(
        `${customBaseUrl}/go1.25.0.windows-amd64.zip`,
        'C:\\temp\\go1.25.0.windows-amd64.zip',
        undefined
      );
    });

    it('skips manifest and downloads directly from custom URL', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      const versionSpec = '1.12.16';
      const customBaseUrl = 'https://example.com/golang';

      inputs['go-version'] = versionSpec;
      inputs['go-download-base-url'] = customBaseUrl;
      inputs['token'] = 'faketoken';

      findSpy.mockImplementation(() => '');
      dlSpy.mockImplementation(async () => '/some/temp/path');
      const toolPath = path.normalize('/cache/go/1.12.16/x64');
      extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
      cacheSpy.mockImplementation(async () => toolPath);

      await main.run();

      // Should not try to use the manifest at all
      expect(logSpy).not.toHaveBeenCalledWith(
        expect.stringContaining('Not found in manifest')
      );
      expect(logSpy).toHaveBeenCalledWith('Install from custom download URL');
    });

    it('strips trailing slashes from custom base URL', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      const versionSpec = '1.13.1';
      const customBaseUrl = 'https://example.com/golang/';

      inputs['go-version'] = versionSpec;
      inputs['go-download-base-url'] = customBaseUrl;

      findSpy.mockImplementation(() => '');
      dlSpy.mockImplementation(async () => '/some/temp/path');
      const toolPath = path.normalize('/cache/go/1.13.1/x64');
      extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
      cacheSpy.mockImplementation(async () => toolPath);

      await main.run();

      expect(logSpy).toHaveBeenCalledWith(
        `Acquiring go1.13.1 from https://example.com/golang/go1.13.1.linux-amd64.tar.gz`
      );
    });

    it('reads custom base URL from environment variable', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      const versionSpec = '1.13.1';
      const customBaseUrl = 'https://example.com/golang';

      inputs['go-version'] = versionSpec;
      process.env['GO_DOWNLOAD_BASE_URL'] = customBaseUrl;

      findSpy.mockImplementation(() => '');
      dlSpy.mockImplementation(async () => '/some/temp/path');
      const toolPath = path.normalize('/cache/go/1.13.1/x64');
      extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
      cacheSpy.mockImplementation(async () => toolPath);

      await main.run();

      expect(logSpy).toHaveBeenCalledWith(
        `Using custom Go download base URL: ${customBaseUrl}`
      );
      expect(logSpy).toHaveBeenCalledWith('Install from custom download URL');
    });

    it('input takes precedence over environment variable', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      const versionSpec = '1.13.1';
      const inputUrl = 'https://input.example.com/golang';
      const envUrl = 'https://env.example.com/golang';

      inputs['go-version'] = versionSpec;
      inputs['go-download-base-url'] = inputUrl;
      process.env['GO_DOWNLOAD_BASE_URL'] = envUrl;

      findSpy.mockImplementation(() => '');
      dlSpy.mockImplementation(async () => '/some/temp/path');
      const toolPath = path.normalize('/cache/go/1.13.1/x64');
      extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
      cacheSpy.mockImplementation(async () => toolPath);

      await main.run();

      expect(logSpy).toHaveBeenCalledWith(
        `Using custom Go download base URL: ${inputUrl}`
      );
      expect(logSpy).toHaveBeenCalledWith(
        `Acquiring go1.13.1 from ${inputUrl}/go1.13.1.linux-amd64.tar.gz`
      );
    });

    it('errors when stable alias is used with custom URL', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      inputs['go-version'] = 'stable';
      inputs['go-download-base-url'] = 'https://example.com/golang';

      findSpy.mockImplementation(() => '');
      await main.run();

      expect(cnSpy).toHaveBeenCalledWith(
        `::error::Version aliases 'stable' are not supported with a custom download base URL. Please specify an exact Go version.${osm.EOL}`
      );
    });

    it('logs info when check-latest is used with custom URL', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      const versionSpec = '1.13.1';
      const customBaseUrl = 'https://example.com/golang';

      inputs['go-version'] = versionSpec;
      inputs['go-download-base-url'] = customBaseUrl;
      inputs['check-latest'] = true;

      findSpy.mockImplementation(() => '');
      dlSpy.mockImplementation(async () => '/some/temp/path');
      const toolPath = path.normalize('/cache/go/1.13.1/x64');
      extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
      cacheSpy.mockImplementation(async () => toolPath);

      await main.run();

      expect(logSpy).toHaveBeenCalledWith(
        'check-latest is not supported with a custom download base URL. Using the provided version spec directly.'
      );
    });

    it('constructs direct download info correctly', () => {
      os.platform = 'linux';
      os.arch = 'x64';

      const info = im.getInfoFromDirectDownload(
        '1.25.0',
        'x64',
        'https://aka.ms/golang/release/latest'
      );

      expect(info.type).toBe('dist');
      expect(info.downloadUrl).toBe(
        'https://aka.ms/golang/release/latest/go1.25.0.linux-amd64.tar.gz'
      );
      expect(info.fileName).toBe('go1.25.0.linux-amd64.tar.gz');
      expect(info.resolvedVersion).toBe('1.25.0');
    });

    it('constructs direct download info for windows', () => {
      os.platform = 'win32';
      os.arch = 'x64';

      const info = im.getInfoFromDirectDownload(
        '1.25.0',
        'x64',
        'https://aka.ms/golang/release/latest'
      );

      expect(info.type).toBe('dist');
      expect(info.downloadUrl).toBe(
        'https://aka.ms/golang/release/latest/go1.25.0.windows-amd64.zip'
      );
      expect(info.fileName).toBe('go1.25.0.windows-amd64.zip');
    });

    it('constructs direct download info for arm64', () => {
      os.platform = 'darwin';
      os.arch = 'arm64';

      const info = im.getInfoFromDirectDownload(
        '1.25.0',
        'arm64',
        'https://aka.ms/golang/release/latest'
      );

      expect(info.type).toBe('dist');
      expect(info.downloadUrl).toBe(
        'https://aka.ms/golang/release/latest/go1.25.0.darwin-arm64.tar.gz'
      );
      expect(info.fileName).toBe('go1.25.0.darwin-arm64.tar.gz');
    });

    it('caches under actual installed version when it differs from input spec', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      const versionSpec = '1.20';
      const customBaseUrl = 'https://aka.ms/golang/release/latest';

      inputs['go-version'] = versionSpec;
      inputs['go-download-base-url'] = customBaseUrl;

      findSpy.mockImplementation(() => '');
      dlSpy.mockImplementation(async () => '/some/temp/path');
      extractTarSpy.mockImplementation(async () => '/some/other/temp/path');

      // Mock the installed Go binary reporting a different patch version
      execFileSpy.mockImplementation(() => 'go version go1.20.14 linux/amd64');

      const expectedToolName = im.customToolCacheName(customBaseUrl);
      const toolPath = path.normalize(`/cache/${expectedToolName}/1.20.14/x64`);
      cacheSpy.mockImplementation(async () => toolPath);

      await main.run();

      expect(logSpy).toHaveBeenCalledWith(
        "Requested version '1.20' resolved to installed version '1.20.14'"
      );
      // Cache key should use actual version, not the input spec
      expect(cacheSpy).toHaveBeenCalledWith(
        expect.any(String),
        expectedToolName,
        '1.20.14',
        'x64'
      );
    });

    it('shows clear error with platform/arch and URL on 404', async () => {
      os.platform = 'linux';
      os.arch = 'arm64';

      const versionSpec = '1.25.0';
      const customBaseUrl = 'https://example.com/golang';

      inputs['go-version'] = versionSpec;
      inputs['go-download-base-url'] = customBaseUrl;

      getSpy.mockImplementationOnce(() => {
        throw new Error('Not a JSON endpoint');
      });

      findSpy.mockImplementation(() => '');
      const httpError = new tc.HTTPError(404);
      dlSpy.mockImplementation(() => {
        throw httpError;
      });

      await main.run();

      expect(cnSpy).toHaveBeenCalledWith(
        expect.stringContaining(
          'The requested Go version 1.25.0 is not available for platform linux/arm64'
        )
      );
      expect(cnSpy).toHaveBeenCalledWith(expect.stringContaining('HTTP 404'));
    });

    it('shows clear error with platform/arch and URL on download failure', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      const versionSpec = '1.25.0';
      const customBaseUrl = 'https://example.com/golang';

      inputs['go-version'] = versionSpec;
      inputs['go-download-base-url'] = customBaseUrl;

      getSpy.mockImplementationOnce(() => {
        throw new Error('Not a JSON endpoint');
      });

      findSpy.mockImplementation(() => '');
      dlSpy.mockImplementation(() => {
        throw new Error('connection refused');
      });

      await main.run();

      expect(cnSpy).toHaveBeenCalledWith(
        expect.stringContaining(
          'Failed to download Go 1.25.0 for platform linux/x64'
        )
      );
      expect(cnSpy).toHaveBeenCalledWith(
        expect.stringContaining(customBaseUrl)
      );
    });

    it.each(['^1.25.0', '~1.25', '>=1.25.0', '<1.26.0', '1.25.x', '1.x'])(
      'errors on version range "%s" when version listing is unavailable',
      async versionSpec => {
        os.platform = 'linux';
        os.arch = 'x64';

        inputs['go-version'] = versionSpec;
        inputs['go-download-base-url'] = 'https://example.com/golang';

        // Simulate version listing not available
        getSpy.mockImplementationOnce(() => {
          throw new Error('Not a JSON endpoint');
        });

        findSpy.mockImplementation(() => '');

        await main.run();

        expect(cnSpy).toHaveBeenCalledWith(
          expect.stringContaining(
            `Version range '${versionSpec}' is not supported with a custom download base URL`
          )
        );
      }
    );

    it('rejects version range in getInfoFromDirectDownload', () => {
      os.platform = 'linux';
      os.arch = 'x64';

      expect(() =>
        im.getInfoFromDirectDownload(
          '^1.25.0',
          'x64',
          'https://example.com/golang'
        )
      ).toThrow(
        "Version range '^1.25.0' is not supported with a custom download base URL"
      );
    });

    it('passes token as auth header for custom URL downloads', async () => {
      os.platform = 'linux';
      os.arch = 'x64';

      const versionSpec = '1.25.0';
      const customBaseUrl = 'https://private-mirror.example.com/golang';

      inputs['go-version'] = versionSpec;
      inputs['go-download-base-url'] = customBaseUrl;
      inputs['token'] = 'ghp_testtoken123';

      getSpy.mockImplementationOnce(() => {
        throw new Error('Not a JSON endpoint');
      });

      findSpy.mockImplementation(() => '');
      dlSpy.mockImplementation(async () => '/some/temp/path');
      extractTarSpy.mockImplementation(async () => '/some/other/temp/path');
      const expectedToolName = im.customToolCacheName(customBaseUrl);
      const toolPath = path.normalize(`/cache/${expectedToolName}/1.25.0/x64`);
      cacheSpy.mockImplementation(async () => toolPath);

      await main.run();

      expect(dlSpy).toHaveBeenCalledWith(
        `${customBaseUrl}/go1.25.0.linux-amd64.tar.gz`,
        undefined,
        'token ghp_testtoken123'
      );
    });
  });
});
