This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathCloudSettingsService.parsing.test.ts
More file actions
171 lines (145 loc) · 4.22 KB
/
Copy pathCloudSettingsService.parsing.test.ts
File metadata and controls
171 lines (145 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// pnpm test src/__tests__/CloudSettingsService.parsing.test.ts
import type { ExtensionContext } from "vscode"
import type { AuthService } from "@roo-code/types"
import { CloudSettingsService } from "../CloudSettingsService.js"
describe("CloudSettingsService - Response Parsing", () => {
let mockContext: ExtensionContext
let mockAuthService: AuthService
let service: CloudSettingsService
beforeEach(() => {
// Mock ExtensionContext
mockContext = {
globalState: {
get: vi.fn(),
update: vi.fn().mockResolvedValue(undefined),
},
} as unknown as ExtensionContext
// Mock AuthService with active session
mockAuthService = {
getState: vi.fn().mockReturnValue("active-session"),
hasActiveSession: vi.fn().mockReturnValue(true),
getSessionToken: vi.fn().mockReturnValue("test-token"),
on: vi.fn(),
removeListener: vi.fn(),
} as unknown as AuthService
service = new CloudSettingsService(mockContext, mockAuthService, vi.fn())
})
it("should successfully parse valid extension settings response", async () => {
// Mock fetch response with a valid settings structure
const mockResponse = {
organization: {
version: 1,
defaultSettings: {},
allowList: {
allowAll: true,
providers: {},
},
},
user: {
features: {},
settings: {},
version: 1,
},
}
global.fetch = vi.fn().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue(mockResponse),
})
// Initialize the service
await service.initialize()
// Wait for the fetch to be called (timer executes immediately but asynchronously)
await vi.waitFor(() => {
expect(global.fetch).toHaveBeenCalled()
})
// Wait a bit for the async processing to complete
await new Promise((resolve) => setTimeout(resolve, 10))
// Verify settings were parsed correctly
const orgSettings = service.getSettings()
const userSettings = service.getUserSettings()
expect(orgSettings).toEqual(mockResponse.organization)
expect(userSettings).toEqual(mockResponse.user)
})
it("should handle complex nested provider settings without type errors", async () => {
// Mock response with complex nested provider settings
const mockResponse = {
organization: {
version: 2,
defaultSettings: {
maxOpenTabsContext: 10,
},
allowList: {
allowAll: false,
providers: {
anthropic: {
allowAll: true,
},
openai: {
allowAll: false,
models: ["gpt-4", "gpt-3.5-turbo"],
},
},
},
providerProfiles: {
default: {
id: "default",
apiProvider: "anthropic",
apiModelId: "claude-3-opus-20240229",
apiKey: "test-key",
modelTemperature: 0.7,
},
},
},
user: {
features: {
roomoteControlEnabled: true,
},
settings: {
extensionBridgeEnabled: true,
},
version: 1,
},
}
global.fetch = vi.fn().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue(mockResponse),
})
// Initialize the service
await service.initialize()
// Wait for the fetch to be called (timer executes immediately but asynchronously)
await vi.waitFor(() => {
expect(global.fetch).toHaveBeenCalled()
})
// Wait a bit for the async processing to complete
await new Promise((resolve) => setTimeout(resolve, 10))
// Verify complex settings were parsed correctly
const orgSettings = service.getSettings()
const userSettings = service.getUserSettings()
expect(orgSettings).toEqual(mockResponse.organization)
expect(userSettings).toEqual(mockResponse.user)
expect(orgSettings?.providerProfiles?.default).toBeDefined()
})
it("should handle invalid response gracefully", async () => {
// Mock invalid response
const mockResponse = {
organization: {
// Missing required fields
version: 1,
},
user: {
// Missing required fields
version: 1,
},
}
global.fetch = vi.fn().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue(mockResponse),
})
// Initialize the service
await service.initialize()
// Settings should remain undefined due to validation failure
const orgSettings = service.getSettings()
const userSettings = service.getUserSettings()
expect(orgSettings).toBeUndefined()
expect(userSettings).toBeUndefined()
})
})