Skip to content

Commit fbe1f75

Browse files
committed
added tests w/ schema and logging change
1 parent 813be15 commit fbe1f75

File tree

9 files changed

+370
-44
lines changed

9 files changed

+370
-44
lines changed

packages/core/src/notifications/controller.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ export class NotificationsController {
6363
}
6464

6565
public pollForEmergencies(ruleEngine: RuleEngine) {
66-
getLogger().info('polling .....')
6766
return this.poll(ruleEngine, 'emergency')
6867
}
6968

@@ -134,12 +133,12 @@ export class NotificationsController {
134133
const addedNotifications = newNotifications.filter((n: any) => !currentNotificationIds.has(n.id))
135134

136135
if (addedNotifications.length > 0) {
137-
getLogger('notifications').info(
136+
getLogger('notifications').verbose(
138137
'New notifications received for category %s, ids: %s',
139138
category,
140139
addedNotifications.map((n: any) => n.id).join(', ')
141140
)
142-
await this.onReceiveNotifications(addedNotifications)
141+
await this.notificationsNode.onReceiveNotifications(addedNotifications)
143142
}
144143

145144
this.state[category].payload = JSON.parse(response.content)
@@ -154,23 +153,6 @@ export class NotificationsController {
154153
)
155154
}
156155

157-
private async onReceiveNotifications(notifications: ToolkitNotification[]) {
158-
for (const notification of notifications) {
159-
switch (notification.uiRenderInstructions.onRecieve) {
160-
case 'modal':
161-
// Handle modal case
162-
void this.notificationsNode.renderModal(notification)
163-
break
164-
case 'toast':
165-
// toast case, no user input needed
166-
void vscode.window.showInformationMessage(
167-
notification.uiRenderInstructions.content['en-US'].descriptionPreview ??
168-
notification.uiRenderInstructions.content['en-US'].title
169-
)
170-
break
171-
}
172-
}
173-
}
174156
/**
175157
* Write the latest memory state to global state.
176158
*/

packages/core/src/notifications/panelNode.ts

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,24 +130,25 @@ export class NotificationsNode implements TreeNode {
130130
/**
131131
* Fired when a notification is clicked on in the panel. It will run any rendering
132132
* instructions included in the notification. See {@link ToolkitNotification.uiRenderInstructions}.
133-
*
134-
* TODO: implement more rendering possibilites.
135133
*/
136134
public async openNotification(notification: ToolkitNotification) {
137135
switch (notification.uiRenderInstructions.onClick.type) {
138136
case 'modal':
139-
// Handle modal case
137+
// Render blocking modal
138+
getLogger('notifications').verbose(`rendering modal for notificaiton: ${notification.id} ...`)
140139
await this.renderModal(notification)
141140
break
142141
case 'openUrl':
143142
if (!notification.uiRenderInstructions.onClick.url) {
144143
throw new ToolkitError('No url provided for onclick open url')
145144
}
146-
// Handle openUrl case
145+
// Show open url option
146+
getLogger('notifications').verbose(`opening url for notification: ${notification.id} ...`)
147147
await vscode.env.openExternal(vscode.Uri.parse(notification.uiRenderInstructions.onClick.url))
148148
break
149149
case 'openTextDocument':
150-
// Handle openTextDocument case
150+
// Display read-only txt document
151+
getLogger('notifications').verbose(`showing txt document for notification: ${notification.id} ...`)
151152
await this.showReadonlyTextDocument(notification.uiRenderInstructions.content['en-US'].description)
152153
break
153154
}
@@ -158,7 +159,6 @@ export class NotificationsNode implements TreeNode {
158159
* It's read-only so that the "save" option doesn't appear when user closes the notification
159160
*/
160161
private async showReadonlyTextDocument(content: string): Promise<void> {
161-
getLogger('notifications').info('showing txt document ... ')
162162
try {
163163
const tempFilePath = path.join(tempDirPath, 'AWSToolkitNotifications.txt')
164164

@@ -187,16 +187,19 @@ export class NotificationsNode implements TreeNode {
187187
}
188188
}
189189

190+
/**
191+
* Renders a blocking modal with the notification's content and buttons.
192+
* Handles the button click actions based on the button type.
193+
*/
190194
public async renderModal(notification: ToolkitNotification) {
191-
getLogger('notifications').info('rendering modal ... ')
192-
if (!notification.uiRenderInstructions.buttons) {
195+
if (!notification.uiRenderInstructions.actions) {
193196
throw new ToolkitError('no button defined for modal')
194197
return
195198
}
196199

197-
const buttons = notification.uiRenderInstructions.buttons
200+
const buttons = notification.uiRenderInstructions.actions
198201

199-
const buttonLabels = buttons.map((buttons) => buttons.displayText['en-US'])
202+
const buttonLabels = buttons.map((actions) => actions.displayText['en-US'])
200203

201204
const detail = notification.uiRenderInstructions.content['en-US'].description
202205

@@ -207,12 +210,17 @@ export class NotificationsNode implements TreeNode {
207210
)
208211

209212
if (selectedButton) {
210-
const buttons = notification.uiRenderInstructions.buttons.find(
211-
(buttons) => buttons.displayText['en-US'] === selectedButton
213+
const buttons = notification.uiRenderInstructions.actions.find(
214+
(actions) => actions.displayText['en-US'] === selectedButton
212215
)
213-
216+
// Different button options
214217
if (buttons) {
215218
switch (buttons.type) {
219+
case 'openTxt':
220+
await this.showReadonlyTextDocument(
221+
notification.uiRenderInstructions.content['en-US'].description
222+
)
223+
break
216224
case 'updateAndReload':
217225
await this.updateAndReload(notification.displayIf.extensionId)
218226
break
@@ -222,14 +230,32 @@ export class NotificationsNode implements TreeNode {
222230
}
223231
break
224232
default:
225-
getLogger().warn(`Unhandled button type: ${buttons.type}`)
233+
throw new ToolkitError('button action not defined')
226234
}
227235
}
228236
}
229237
}
230238

239+
public async onReceiveNotifications(notifications: ToolkitNotification[]) {
240+
for (const notification of notifications) {
241+
switch (notification.uiRenderInstructions.onRecieve) {
242+
case 'modal':
243+
// Handle modal case
244+
void this.renderModal(notification)
245+
break
246+
case 'toast':
247+
// toast case, no user input needed
248+
void vscode.window.showInformationMessage(
249+
notification.uiRenderInstructions.content['en-US'].descriptionPreview ??
250+
notification.uiRenderInstructions.content['en-US'].title
251+
)
252+
break
253+
}
254+
}
255+
}
256+
231257
private async updateAndReload(id: string) {
232-
getLogger('notifications').info('Updating and reloading the extension...')
258+
getLogger('notifications').verbose('Updating and reloading the extension...')
233259
await vscode.commands.executeCommand('workbench.extensions.installExtension', id)
234260
await vscode.commands.executeCommand('workbench.action.reloadWindow')
235261
}
@@ -258,3 +284,5 @@ export class NotificationsNode implements TreeNode {
258284
export function registerProvider(provider: ResourceTreeDataProvider) {
259285
NotificationsNode.instance.provider = provider
260286
}
287+
288+
export const testNotificationsNode = new NotificationsNode()

packages/core/src/notifications/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export interface UIRenderInstructions {
5959
type: string
6060
url?: string // optional property for 'openUrl'
6161
}
62-
buttons?: Array<{
62+
actions?: Array<{
6363
type: string
6464
displayText: {
6565
[`en-US`]: string

packages/core/src/test/notifications/controller.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import { randomUUID } from '../../shared'
1414
import { installFakeClock } from '../testUtil'
1515
import { NotificationFetcher, RemoteFetcher, ResourceResponse } from '../../notifications/controller'
1616
import { HttpResourceFetcher } from '../../shared/resourcefetcher/httpResourceFetcher'
17+
import { testNotificationsNode } from '../../notifications/panelNode'
1718

1819
describe('Notifications Controller', function () {
19-
const panelNode: NotificationsNode = new NotificationsNode()
20+
const panelNode: NotificationsNode = testNotificationsNode
2021
const ruleEngine: RuleEngine = new RuleEngine({
2122
ideVersion: '1.83.0',
2223
extensionVersion: '1.20.0',
@@ -502,6 +503,11 @@ function getValidTestNotification(id: string) {
502503
description: 'test',
503504
},
504505
},
506+
onRecieve: 'toast',
507+
onClick: {
508+
type: 'openUrl',
509+
url: 'https://aws.amazon.com/visualstudiocode/',
510+
},
505511
},
506512
}
507513
}

0 commit comments

Comments
 (0)