src/goMain: adjust showGoWelcomePage to show for prereleases

Previously, it showed pages only if the version string matches
the version we want to show welcome page for. And, it stored
the version in the global storage only if its major.minor.patch
verseion string is greater than the previously stored version. This
made it difficult to test the functionality with the prerelease
version.

This CL changes the logic a bit so
  - prerelease versions can exercise the code path
  - store the version info in the global state if it is different
    from the previously stored version. That means, if a user
    used v0.22.0, rolled back to v0.21.1, and reinstall v0.22.0,
    she will see the welcome page again. I hope that is a rare
    case and not too bad to show the welcome page again in this
    case.

    If a user rolls back to v0.22.0 from v0.23.0, she will not
    see the welcome page.

Change-Id: I1a0f782ab98d870dd9d90d1a9dfcc2b15af4401a
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/285837
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/src/goMain.ts b/src/goMain.ts
index 940d2d1..122c8be 100644
--- a/src/goMain.ts
+++ b/src/goMain.ts
@@ -592,12 +592,22 @@
 	const goExtensionVersion = goExtension.packageJSON.version;
 	const savedGoExtensionVersion = getFromGlobalState(goExtensionVersionKey, '0.0.0');
 
-	if (semver.gt(semver.coerce(goExtensionVersion), semver.coerce(savedGoExtensionVersion))) {
-		updateGlobalState(goExtensionVersionKey, goExtensionVersion);
-		if (showVersions.includes(goExtensionVersion)) {
-			WelcomePanel.createOrShow(ctx.extensionUri);
-		}
+	if (shouldShowGoWelcomePage(showVersions, goExtensionVersion, savedGoExtensionVersion)) {
+		WelcomePanel.createOrShow(ctx.extensionUri);
 	}
+	if (goExtensionVersion !== savedGoExtensionVersion) {
+		updateGlobalState(goExtensionVersionKey, goExtensionVersion);
+	}
+}
+
+export function shouldShowGoWelcomePage(showVersions: string[], newVersion: string, oldVersion: string): boolean {
+	if (newVersion === oldVersion) {
+		return false;
+	}
+	const coercedNew = semver.coerce(newVersion);
+	const coercedOld = semver.coerce(oldVersion);
+	// Both semver.coerce(0.22.0) and semver.coerce(0.22.0-rc.1) will be 0.22.0.
+	return semver.gte(coercedNew, coercedOld) && showVersions.includes(coercedNew.toString());
 }
 
 async function showGoNightlyWelcomeMessage() {
diff --git a/test/integration/welcome.test.ts b/test/integration/welcome.test.ts
new file mode 100644
index 0000000..a3856a1
--- /dev/null
+++ b/test/integration/welcome.test.ts
@@ -0,0 +1,59 @@
+/*---------------------------------------------------------
+ * Copyright 2020 The Go Authors. All rights reserved.
+ * Licensed under the MIT License. See LICENSE in the project root for license information.
+ *--------------------------------------------------------*/
+
+import * as assert from 'assert';
+import { shouldShowGoWelcomePage } from '../../src/goMain';
+
+suite('WelcomePanel Tests', () => {
+	// 0:showVersions, 1:newVersion, 2:oldVersion, 3:expected
+	type testCase = [string[], string, string, boolean];
+	const testCases: testCase[] = [
+		[[], '0.22.0', '0.0.0', false],
+		[[], '0.22.0', '0.21.0', false],
+		[[], '0.22.0', '0.22.0-rc.1', false],
+		[[], '0.22.0', '0.22.0', false],
+		[[], '0.22.0', '0.23.0', false],
+
+		[['0.22.0'], '0.22.0', '0.0.0', true],
+		[['0.22.0'], '0.22.0', '0.21.0-rc.1', true],
+		[['0.22.0'], '0.22.0', '0.21.0', true],
+		[['0.22.0'], '0.22.0', '0.22.0-rc.1', true],
+		[['0.22.0'], '0.22.0', '0.22.0', false],
+		[['0.22.0'], '0.22.0', '0.22.1', false],
+		[['0.22.0'], '0.22.0', '0.23.0', false],
+		[['0.22.0'], '0.22.0', '1.0.0', false],
+		[['0.22.0'], '0.22.0', '2021.1.100', false],
+
+		[['0.22.0'], '0.22.0-rc.2', '0.0.0', true],
+		[['0.22.0'], '0.22.0-rc.2', '0.21.0-rc.1', true],
+		[['0.22.0'], '0.22.0-rc.2', '0.21.0', true],
+		[['0.22.0'], '0.22.0-rc.2', '0.22.0-rc.1', true],
+		[['0.22.0'], '0.22.0-rc.2', '0.22.0-rc.2', false],
+		[['0.22.0'], '0.22.0-rc.2', '0.22.0-rc.3', true],
+		[['0.22.0'], '0.22.0-rc.2', '0.22.0', true],
+		[['0.22.0'], '0.22.0-rc.2', '0.22.1', false],
+		[['0.22.0'], '0.22.0-rc.2', '0.23.0', false],
+		[['0.22.0'], '0.22.0-rc.2', '1.0.0', false],
+		[['0.22.0'], '0.22.0-rc.2', '2021.1.100', false],
+
+		[['0.22.0'], '0.22.1', '0.0.0', false],
+		[['0.22.0'], '0.22.1', '0.21.0-rc.1', false],
+		[['0.22.0'], '0.22.1', '0.21.0', false],
+		[['0.22.0'], '0.22.1', '0.22.0-rc.1', false],
+		[['0.22.0'], '0.22.1', '0.22.0', false],
+		[['0.22.0'], '0.22.1', '0.23.0', false],
+		[['0.22.0'], '0.22.1', '1.0.0', false],
+		[['0.22.0'], '0.22.1', '2021.1.100', false],
+	];
+	testCases.forEach((c: testCase) => {
+		const [showVersions, newVersion, oldVersion, expected] = c;
+
+		test(`shouldShowGoWelcomePage(${JSON.stringify(showVersions)}, ${newVersion}, ${oldVersion})`, () => {
+			assert.strictEqual(
+				shouldShowGoWelcomePage(showVersions, newVersion, oldVersion), expected);
+		});
+	});
+
+});