|
| 1 | +import * as sinon from "sinon"; |
| 2 | +import { MessageType, Messages, asyncSafety } from "@cursorless/common"; |
| 3 | +import type { ExtensionContext, Uri } from "vscode"; |
| 4 | +import { ReleaseNotes, VERSION_KEY, WHATS_NEW } from "./ReleaseNotes"; |
| 5 | +import { VscodeApi } from "@cursorless/vscode-common"; |
| 6 | + |
| 7 | +interface Input { |
| 8 | + /** Whether the VSCode window is focused */ |
| 9 | + isFocused: boolean; |
| 10 | + storedVersion: string | undefined; |
| 11 | + currentVersion: string; |
| 12 | + /** `true` if they pressed `What's new?` */ |
| 13 | + pressedButton?: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +interface Output { |
| 17 | + /** The new version added to storage, or `false` if none added */ |
| 18 | + storedVersion: string | false; |
| 19 | + showedMessage: boolean; |
| 20 | + openedUrl?: boolean; |
| 21 | +} |
| 22 | + |
| 23 | +interface TestCase { |
| 24 | + input: Input; |
| 25 | + expectedOutput: Output; |
| 26 | +} |
| 27 | + |
| 28 | +const testCases: TestCase[] = [ |
| 29 | + { |
| 30 | + input: { |
| 31 | + isFocused: false, |
| 32 | + storedVersion: undefined, |
| 33 | + currentVersion: "0.28.0", |
| 34 | + }, |
| 35 | + expectedOutput: { |
| 36 | + storedVersion: "0.28.0", |
| 37 | + showedMessage: false, |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + input: { |
| 42 | + isFocused: true, |
| 43 | + storedVersion: undefined, |
| 44 | + currentVersion: "0.28.0", |
| 45 | + }, |
| 46 | + expectedOutput: { |
| 47 | + storedVersion: "0.28.0", |
| 48 | + showedMessage: false, |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + input: { |
| 53 | + isFocused: false, |
| 54 | + storedVersion: "0.28.0", |
| 55 | + currentVersion: "0.28.0", |
| 56 | + }, |
| 57 | + expectedOutput: { |
| 58 | + storedVersion: false, |
| 59 | + showedMessage: false, |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + input: { |
| 64 | + isFocused: true, |
| 65 | + storedVersion: "0.28.0", |
| 66 | + currentVersion: "0.28.0", |
| 67 | + }, |
| 68 | + expectedOutput: { |
| 69 | + storedVersion: false, |
| 70 | + showedMessage: false, |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + input: { |
| 75 | + isFocused: false, |
| 76 | + storedVersion: "0.28.0", |
| 77 | + currentVersion: "0.28.10", |
| 78 | + }, |
| 79 | + expectedOutput: { |
| 80 | + storedVersion: false, |
| 81 | + showedMessage: false, |
| 82 | + }, |
| 83 | + }, |
| 84 | + { |
| 85 | + input: { |
| 86 | + isFocused: true, |
| 87 | + storedVersion: "0.28.0", |
| 88 | + currentVersion: "0.28.10", |
| 89 | + }, |
| 90 | + expectedOutput: { |
| 91 | + storedVersion: false, |
| 92 | + showedMessage: false, |
| 93 | + }, |
| 94 | + }, |
| 95 | + { |
| 96 | + input: { |
| 97 | + isFocused: false, |
| 98 | + storedVersion: "0.28.0", |
| 99 | + currentVersion: "0.26.0", |
| 100 | + }, |
| 101 | + expectedOutput: { |
| 102 | + storedVersion: false, |
| 103 | + showedMessage: false, |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + input: { |
| 108 | + isFocused: true, |
| 109 | + storedVersion: "0.28.0", |
| 110 | + currentVersion: "0.26.0", |
| 111 | + }, |
| 112 | + expectedOutput: { |
| 113 | + storedVersion: false, |
| 114 | + showedMessage: false, |
| 115 | + }, |
| 116 | + }, |
| 117 | + { |
| 118 | + input: { |
| 119 | + isFocused: false, |
| 120 | + storedVersion: "0.28.0", |
| 121 | + currentVersion: "0.29.10", |
| 122 | + }, |
| 123 | + expectedOutput: { |
| 124 | + storedVersion: false, |
| 125 | + showedMessage: false, |
| 126 | + }, |
| 127 | + }, |
| 128 | + { |
| 129 | + input: { |
| 130 | + isFocused: true, |
| 131 | + storedVersion: "0.28.0", |
| 132 | + currentVersion: "0.29.10", |
| 133 | + }, |
| 134 | + expectedOutput: { |
| 135 | + storedVersion: "0.29.0", |
| 136 | + showedMessage: true, |
| 137 | + }, |
| 138 | + }, |
| 139 | + { |
| 140 | + input: { |
| 141 | + isFocused: true, |
| 142 | + storedVersion: "0.28.0", |
| 143 | + currentVersion: "0.29.10", |
| 144 | + pressedButton: true, |
| 145 | + }, |
| 146 | + expectedOutput: { |
| 147 | + storedVersion: "0.29.0", |
| 148 | + showedMessage: true, |
| 149 | + openedUrl: true, |
| 150 | + }, |
| 151 | + }, |
| 152 | +]; |
| 153 | + |
| 154 | +suite("release notes", async function () { |
| 155 | + teardown(() => { |
| 156 | + sinon.restore(); |
| 157 | + }); |
| 158 | + |
| 159 | + testCases.forEach(({ input, expectedOutput }) => { |
| 160 | + test( |
| 161 | + getTestName(input), |
| 162 | + asyncSafety(() => runTest(input, expectedOutput)), |
| 163 | + ); |
| 164 | + }); |
| 165 | +}); |
| 166 | + |
| 167 | +function getTestName(input: Input) { |
| 168 | + const nameComponents = [ |
| 169 | + input.isFocused ? "focused" : "unfocused", |
| 170 | + String(input.storedVersion), |
| 171 | + input.currentVersion, |
| 172 | + ]; |
| 173 | + |
| 174 | + if (input.pressedButton) { |
| 175 | + nameComponents.push("pressed"); |
| 176 | + } |
| 177 | + |
| 178 | + return nameComponents.join(" "); |
| 179 | +} |
| 180 | + |
| 181 | +async function runTest(input: Input, expectedOutput: Output) { |
| 182 | + const { |
| 183 | + extensionContext, |
| 184 | + messages, |
| 185 | + openExternal, |
| 186 | + update, |
| 187 | + showMessage, |
| 188 | + vscodeApi, |
| 189 | + } = await getFakes(input); |
| 190 | + |
| 191 | + await new ReleaseNotes(vscodeApi, extensionContext, messages).maybeShow(); |
| 192 | + |
| 193 | + if (expectedOutput.storedVersion === false) { |
| 194 | + sinon.assert.notCalled(update); |
| 195 | + } else { |
| 196 | + sinon.assert.calledOnceWithExactly( |
| 197 | + update, |
| 198 | + VERSION_KEY, |
| 199 | + expectedOutput.storedVersion, |
| 200 | + ); |
| 201 | + } |
| 202 | + |
| 203 | + if (expectedOutput.showedMessage) { |
| 204 | + sinon.assert.calledOnceWithExactly( |
| 205 | + showMessage, |
| 206 | + sinon.match.any, |
| 207 | + "releaseNotes", |
| 208 | + sinon.match.any, |
| 209 | + sinon.match.any, |
| 210 | + ); |
| 211 | + |
| 212 | + if (expectedOutput.openedUrl) { |
| 213 | + sinon.assert.calledOnce(openExternal); |
| 214 | + } else { |
| 215 | + sinon.assert.notCalled(openExternal); |
| 216 | + } |
| 217 | + } else { |
| 218 | + sinon.assert.notCalled(showMessage); |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +async function getFakes(input: Input) { |
| 223 | + const openExternal = sinon.fake.resolves<[Uri], Promise<boolean>>(true); |
| 224 | + const vscodeApi = { |
| 225 | + window: { |
| 226 | + state: { |
| 227 | + focused: input.isFocused, |
| 228 | + }, |
| 229 | + }, |
| 230 | + env: { |
| 231 | + openExternal, |
| 232 | + }, |
| 233 | + } as unknown as VscodeApi; |
| 234 | + |
| 235 | + const update = sinon.fake<[string, string], Promise<void>>(); |
| 236 | + const extensionContext = { |
| 237 | + globalState: { |
| 238 | + get() { |
| 239 | + return input.storedVersion; |
| 240 | + }, |
| 241 | + update, |
| 242 | + }, |
| 243 | + extension: { |
| 244 | + packageJSON: { |
| 245 | + version: input.currentVersion, |
| 246 | + }, |
| 247 | + }, |
| 248 | + } as unknown as ExtensionContext; |
| 249 | + |
| 250 | + const showMessage = sinon.fake.resolves< |
| 251 | + [MessageType, string, string, ...string[]], |
| 252 | + Promise<string | undefined> |
| 253 | + >(input.pressedButton ? WHATS_NEW : undefined); |
| 254 | + const messages: Messages = { |
| 255 | + showMessage, |
| 256 | + }; |
| 257 | + |
| 258 | + return { |
| 259 | + extensionContext, |
| 260 | + messages, |
| 261 | + openExternal, |
| 262 | + update, |
| 263 | + showMessage, |
| 264 | + vscodeApi, |
| 265 | + }; |
| 266 | +} |
0 commit comments