forked from scalameta/metals-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add test for "needCheckForUpdates"
scalameta#1144 This commit adds a test for `needCheckForUpdates`. For adding unit tests, this commit abstract away the logic that needs access to vscode APIs, becuase we need to run an integration test for accessing vscode APIs. see: microsoft/vscode-wordcount#5 (comment) microsoft/vscode#82471 (comment)
- Loading branch information
1 parent
dacf72c
commit a2f1cd1
Showing
8 changed files
with
302 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum ConfigurationTarget { | ||
Global, | ||
Workspace, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { ExtensionContext, Memento } from "vscode"; | ||
import { ConfigurationTarget } from "../ConfigurationTarget"; | ||
|
||
type LastUpdated = { | ||
prevVersion?: string; | ||
lastUpdatedAt?: string; | ||
}; | ||
|
||
export interface CheckForUpdateRepo { | ||
getLastUpdated(target: ConfigurationTarget): LastUpdated; | ||
saveLastUpdated( | ||
serverVersion: string, | ||
lastUpdatedAt: string, | ||
target: ConfigurationTarget | ||
): void; | ||
} | ||
|
||
export class DefaultCheckForUpdateRepo implements CheckForUpdateRepo { | ||
constructor(private context: ExtensionContext) {} | ||
|
||
private CurrentVersionKey = "currentVersion"; | ||
private LastUpdatedAtKey = "lastUpdatedAt"; | ||
|
||
getLastUpdated(target: ConfigurationTarget): LastUpdated { | ||
const state = this.storage(target); | ||
const prevVersion = state.get<string>(this.CurrentVersionKey); | ||
const lastUpdatedAt = state.get<string>(this.LastUpdatedAtKey); | ||
return { | ||
prevVersion, | ||
lastUpdatedAt, | ||
}; | ||
} | ||
|
||
saveLastUpdated( | ||
serverVersion: string, | ||
lastUpdatedAt: string, | ||
target: ConfigurationTarget | ||
): void { | ||
const state = this.storage(target); | ||
state.update(this.CurrentVersionKey, serverVersion); | ||
state.update(this.LastUpdatedAtKey, lastUpdatedAt); | ||
return; | ||
} | ||
|
||
private storage(target: ConfigurationTarget): Memento { | ||
return target === ConfigurationTarget.Global | ||
? this.context.globalState | ||
: this.context.workspaceState; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ConfigurationTarget } from "../ConfigurationTarget"; | ||
import { CheckForUpdateRepo } from "../repository/CheckForUpdateRepo"; | ||
|
||
/** | ||
* The logic is the following: | ||
* - if version was set more than a day ago - update is needed | ||
* - if version is seen for the first time (user changed version in config by it self) - the update will be delayed for a day | ||
*/ | ||
export async function needCheckForUpdates( | ||
currentVersion: string, | ||
today: string, | ||
target: ConfigurationTarget, | ||
repo: CheckForUpdateRepo | ||
): Promise<boolean> { | ||
const { prevVersion, lastUpdatedAt } = repo.getLastUpdated(target); | ||
|
||
if (prevVersion !== currentVersion) { | ||
repo.saveLastUpdated(currentVersion, today, target); | ||
return false; | ||
} else { | ||
return lastUpdatedAt !== today; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import assert from "assert"; | ||
import { ConfigurationTarget } from "../../ConfigurationTarget"; | ||
import { needCheckForUpdates } from "../../service/checkForUpdate"; | ||
import { CheckForUpdateRepo } from "../../repository/CheckForUpdateRepo"; | ||
import sinon from "sinon"; | ||
|
||
class MockRepo implements CheckForUpdateRepo { | ||
constructor(private prevVersion?: string, private lastUpdatedAt?: string) {} | ||
getLastUpdated(_target: ConfigurationTarget): { | ||
prevVersion?: string | undefined; | ||
lastUpdatedAt?: string | undefined; | ||
} { | ||
return { | ||
prevVersion: this.prevVersion, | ||
lastUpdatedAt: this.lastUpdatedAt, | ||
}; | ||
} | ||
saveLastUpdated( | ||
_serverVersion: string, | ||
_lastUpdatedAt: string, | ||
_target: ConfigurationTarget | ||
): void { | ||
return; | ||
} | ||
} | ||
|
||
describe("needCheckForUpdates", () => { | ||
it("should false if nothing has saved / save current versions", async () => { | ||
const currentVersion = "0.11.8"; | ||
const today = "2022-01-01"; | ||
const repo = new MockRepo(undefined, undefined); | ||
const spy = sinon.spy(repo, "saveLastUpdated"); | ||
const actual = await needCheckForUpdates( | ||
currentVersion, | ||
today, | ||
ConfigurationTarget.Global, | ||
repo | ||
); | ||
assert(actual === false); | ||
assert(spy.calledWith(currentVersion, today, ConfigurationTarget.Global)); | ||
}); | ||
|
||
it("should false if currentVersion was seen for the first time / save current versions", async () => { | ||
const prevVersion = "0.11.8"; | ||
const currentVersion = "0.11.9"; | ||
const today = "2022-01-01"; | ||
const repo = new MockRepo(prevVersion, today); | ||
const spy = sinon.spy(repo, "saveLastUpdated"); | ||
const actual = await needCheckForUpdates( | ||
currentVersion, | ||
today, | ||
ConfigurationTarget.Global, | ||
repo | ||
); | ||
assert(actual === false); | ||
assert(spy.calledWith(currentVersion, today, ConfigurationTarget.Global)); | ||
}); | ||
|
||
it("should false if currentVersion is set today", async () => { | ||
const currentVersion = "0.11.8"; | ||
const today = "2022-01-01"; | ||
const repo = new MockRepo(currentVersion, today); | ||
const spy = sinon.spy(repo, "saveLastUpdated"); | ||
const actual = await needCheckForUpdates( | ||
currentVersion, | ||
today, | ||
ConfigurationTarget.Global, | ||
repo | ||
); | ||
assert(actual === false); | ||
assert(spy.notCalled); | ||
}); | ||
|
||
it("should true if currentVersion is set more than a day ago", async () => { | ||
const currentVersion = "0.11.8"; | ||
const lastUpdatedAt = "2022-01-01"; | ||
const today = "2022-01-02"; | ||
const repo = new MockRepo(currentVersion, lastUpdatedAt); | ||
const spy = sinon.spy(repo, "saveLastUpdated"); | ||
const actual = await needCheckForUpdates( | ||
currentVersion, | ||
today, | ||
ConfigurationTarget.Global, | ||
repo | ||
); | ||
assert(actual === true); | ||
assert(spy.notCalled); | ||
}); | ||
}); |
Oops, something went wrong.