Skip to content

Commit 74c41b7

Browse files
More test cleanup
1 parent 62e27b4 commit 74c41b7

4 files changed

Lines changed: 73 additions & 67 deletions

File tree

src/test/cloud/api.test.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,15 @@ suite("cloud/api", () => {
3232

3333
const result = await ApiService.requestDeviceCode("test-client")
3434

35-
assert.strictEqual(result.device_code, "dc_123")
36-
assert.strictEqual(result.user_code, "UC-456")
37-
assert.strictEqual(result.verification_uri, "https://example.com/verify")
38-
assert.strictEqual(
39-
result.verification_uri_complete,
40-
"https://example.com/verify?code=UC-456",
41-
)
42-
assert.strictEqual(result.expires_in, 900)
43-
assert.strictEqual(result.interval, 5)
35+
assert.deepStrictEqual(result, {
36+
device_code: "dc_123",
37+
user_code: "UC-456",
38+
verification_uri: "https://example.com/verify",
39+
verification_uri_complete: "https://example.com/verify?code=UC-456",
40+
expires_in: 900,
41+
interval: 5,
42+
})
4443

45-
// Verify fetch was called with correct params
4644
const [url, options] = fetchStub.firstCall.args
4745
assert.strictEqual(url, `${BASE_URL}/login/device/authorization`)
4846
assert.strictEqual(options?.method, "POST")
@@ -88,6 +86,39 @@ suite("cloud/api", () => {
8886
})
8987
})
9088

89+
suite("getUser", () => {
90+
test("returns user on success", async () => {
91+
sinon
92+
.stub(globalThis, "fetch")
93+
.resolves(
94+
mockResponse({ email: "test@example.com", full_name: "Test User" }),
95+
)
96+
97+
const result = await ApiService.getUser("test_token")
98+
99+
assert.deepStrictEqual(result, {
100+
email: "test@example.com",
101+
full_name: "Test User",
102+
})
103+
})
104+
105+
test("returns null on non-ok response", async () => {
106+
sinon.stub(globalThis, "fetch").resolves(mockResponse({}, false, 401))
107+
108+
const result = await ApiService.getUser("bad_token")
109+
110+
assert.strictEqual(result, null)
111+
})
112+
113+
test("returns null on network error", async () => {
114+
sinon.stub(globalThis, "fetch").rejects(new Error("fetch failed"))
115+
116+
const result = await ApiService.getUser("test_token")
117+
118+
assert.strictEqual(result, null)
119+
})
120+
})
121+
91122
suite("pollDeviceToken", () => {
92123
test("returns token on success", async () => {
93124
sinon
@@ -113,10 +144,9 @@ suite("cloud/api", () => {
113144
const resultPromise = ApiService.pollDeviceToken(
114145
"test-client",
115146
"dc_123",
116-
100, // short interval for test
147+
100,
117148
)
118149

119-
// Advance past the polling interval
120150
await clock.tickAsync(150)
121151

122152
const result = await resultPromise

src/test/cloud/auth.test.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as assert from "node:assert"
22
import sinon from "sinon"
33
import * as vscode from "vscode"
44
import { CloudAuthenticationProvider, isTokenExpired } from "../../cloud/auth"
5-
import { mockResponse } from "../testUtils"
5+
import { mockResponse, stubFs } from "../testUtils"
66

77
function createJwtToken(payload: Record<string, unknown>): string {
88
const header = { alg: "HS256", typ: "JWT" }
@@ -24,33 +24,6 @@ function expiredToken(): string {
2424
return createJwtToken({ exp: Math.floor(Date.now() / 1000) - 3600 })
2525
}
2626

27-
function stubFs() {
28-
const original = vscode.workspace.fs
29-
const fake = {
30-
readFile: sinon.stub(),
31-
writeFile: sinon.stub(),
32-
delete: sinon.stub(),
33-
createDirectory: sinon.stub(),
34-
} as unknown as typeof vscode.workspace.fs & {
35-
readFile: sinon.SinonStub
36-
writeFile: sinon.SinonStub
37-
delete: sinon.SinonStub
38-
createDirectory: sinon.SinonStub
39-
}
40-
Object.defineProperty(vscode.workspace, "fs", {
41-
value: fake,
42-
configurable: true,
43-
})
44-
return {
45-
fake,
46-
restore: () =>
47-
Object.defineProperty(vscode.workspace, "fs", {
48-
value: original,
49-
configurable: true,
50-
}),
51-
}
52-
}
53-
5427
function createMockContext(): vscode.ExtensionContext {
5528
const secrets: Record<string, string> = {}
5629
return {

src/test/cloud/config.test.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,7 @@ import * as assert from "node:assert"
22
import sinon from "sinon"
33
import * as vscode from "vscode"
44
import { ConfigService } from "../../cloud/config"
5-
6-
function stubFs() {
7-
const original = vscode.workspace.fs
8-
const fake = {
9-
readFile: sinon.stub(),
10-
writeFile: sinon.stub(),
11-
delete: sinon.stub(),
12-
createDirectory: sinon.stub(),
13-
} as unknown as typeof vscode.workspace.fs & {
14-
readFile: sinon.SinonStub
15-
writeFile: sinon.SinonStub
16-
delete: sinon.SinonStub
17-
createDirectory: sinon.SinonStub
18-
}
19-
Object.defineProperty(vscode.workspace, "fs", {
20-
value: fake,
21-
configurable: true,
22-
})
23-
return {
24-
fake,
25-
restore: () =>
26-
Object.defineProperty(vscode.workspace, "fs", {
27-
value: original,
28-
configurable: true,
29-
}),
30-
}
31-
}
5+
import { stubFs } from "../testUtils"
326

337
suite("cloud/config", () => {
348
let fsStub: ReturnType<typeof stubFs>

src/test/testUtils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { existsSync, readFileSync } from "node:fs"
22
import { dirname, join } from "node:path"
3+
import sinon from "sinon"
4+
import * as vscode from "vscode"
35
import type { FileSystem } from "../core/filesystem"
46

57
declare const __DIST_ROOT__: string
@@ -113,6 +115,33 @@ export const nodeFileSystem: FileSystem = {
113115

114116
// Cloud helpers
115117

118+
export function stubFs() {
119+
const original = vscode.workspace.fs
120+
const fake = {
121+
readFile: sinon.stub(),
122+
writeFile: sinon.stub(),
123+
delete: sinon.stub(),
124+
createDirectory: sinon.stub(),
125+
} as unknown as typeof vscode.workspace.fs & {
126+
readFile: sinon.SinonStub
127+
writeFile: sinon.SinonStub
128+
delete: sinon.SinonStub
129+
createDirectory: sinon.SinonStub
130+
}
131+
Object.defineProperty(vscode.workspace, "fs", {
132+
value: fake,
133+
configurable: true,
134+
})
135+
return {
136+
fake,
137+
restore: () =>
138+
Object.defineProperty(vscode.workspace, "fs", {
139+
value: original,
140+
configurable: true,
141+
}),
142+
}
143+
}
144+
116145
export function mockResponse(body: unknown, ok = true, status = 200): Response {
117146
return {
118147
ok,

0 commit comments

Comments
 (0)