33 * SPDX-License-Identifier: Apache-2.0
44 */
55
6+ import * as vscode from 'vscode'
67import assert from 'assert'
7- import { RuleEngine } from '../../notifications/rules'
8+ import { RuleEngine , getRuleContext } from '../../notifications/rules'
89import { DisplayIf , ToolkitNotification , RuleContext } from '../../notifications/types'
9- import { globals } from '../../shared'
10+ import globals from '../../shared/extensionGlobals'
11+ import { Connection , scopesCodeCatalyst } from '../../auth/connection'
12+ import { getOperatingSystem } from '../../shared/telemetry/util'
13+ import { getAuthFormIdsFromConnection } from '../../auth/utils'
14+ import { builderIdStartUrl } from '../../auth/sso/model'
15+ import { amazonQScopes } from '../../codewhisperer'
1016
1117describe ( 'Notifications Rule Engine' , function ( ) {
1218 const context : RuleContext = {
@@ -126,6 +132,18 @@ describe('Notifications Rule Engine', function () {
126132 ) ,
127133 true
128134 )
135+ assert . equal (
136+ ruleEngine . shouldDisplayNotification (
137+ buildNotification ( {
138+ extensionVersion : {
139+ type : 'range' ,
140+ lowerInclusive : '-inf' ,
141+ upperExclusive : '1.23.0' ,
142+ } ,
143+ } )
144+ ) ,
145+ true
146+ )
129147
130148 assert . equal (
131149 ruleEngine . shouldDisplayNotification (
@@ -138,6 +156,31 @@ describe('Notifications Rule Engine', function () {
138156 ) ,
139157 true
140158 )
159+ assert . equal (
160+ ruleEngine . shouldDisplayNotification (
161+ buildNotification ( {
162+ extensionVersion : {
163+ type : 'range' ,
164+ lowerInclusive : '1.0.0' ,
165+ upperExclusive : '+inf' ,
166+ } ,
167+ } )
168+ ) ,
169+ true
170+ )
171+
172+ assert . equal (
173+ ruleEngine . shouldDisplayNotification (
174+ buildNotification ( {
175+ extensionVersion : {
176+ type : 'range' ,
177+ lowerInclusive : '-inf' ,
178+ upperExclusive : '+inf' ,
179+ } ,
180+ } )
181+ ) ,
182+ true
183+ )
141184 } )
142185
143186 it ( 'should NOT display notification with invalid version range criteria' , function ( ) {
@@ -153,6 +196,18 @@ describe('Notifications Rule Engine', function () {
153196 ) ,
154197 false
155198 )
199+ assert . equal (
200+ ruleEngine . shouldDisplayNotification (
201+ buildNotification ( {
202+ extensionVersion : {
203+ type : 'range' ,
204+ lowerInclusive : '-inf' ,
205+ upperExclusive : '1.20.0' ,
206+ } ,
207+ } )
208+ ) ,
209+ false
210+ )
156211 } )
157212
158213 it ( 'should display notification with version OR criteria' , function ( ) {
@@ -365,7 +420,7 @@ describe('Notifications Rule Engine', function () {
365420 assert . equal (
366421 ruleEngine . shouldDisplayNotification (
367422 buildNotification ( {
368- additionalCriteria : [ { type : 'InstalledExtensions' , values : [ 'ext1' , 'ext2' , 'unkownExtension ' ] } ] ,
423+ additionalCriteria : [ { type : 'InstalledExtensions' , values : [ 'ext1' , 'ext2' , 'unknownExtension ' ] } ] ,
369424 } )
370425 ) ,
371426 false
@@ -473,3 +528,56 @@ describe('Notifications Rule Engine', function () {
473528 )
474529 } )
475530} )
531+
532+ describe ( 'Notifications getRuleContext()' , function ( ) {
533+ it ( 'should return the correct rule context' , async function ( ) {
534+ const context = globals . context
535+ context . extension . packageJSON . version = '0.0.1'
536+ const authEnabledConnections = (
537+ [
538+ {
539+ type : 'iam' ,
540+ } ,
541+ {
542+ type : 'sso' ,
543+ scopes : amazonQScopes ,
544+ startUrl : builderIdStartUrl ,
545+ } ,
546+ {
547+ type : 'sso' ,
548+ scopes : scopesCodeCatalyst ,
549+ startUrl : builderIdStartUrl ,
550+ } ,
551+ {
552+ type : 'sso' ,
553+ scopes : amazonQScopes ,
554+ startUrl : 'something' ,
555+ } ,
556+ {
557+ type : 'unknown' ,
558+ } ,
559+ ] as Connection [ ]
560+ )
561+ . map ( ( c ) => getAuthFormIdsFromConnection ( c ) )
562+ . join ( ',' )
563+
564+ const ruleContext = await getRuleContext ( context , {
565+ authEnabledConnections,
566+ authScopes : amazonQScopes . join ( ',' ) ,
567+ authStatus : 'connected' ,
568+ awsRegion : 'us-east-1' ,
569+ } )
570+ assert . deepStrictEqual ( ruleContext , {
571+ ideVersion : vscode . version ,
572+ extensionVersion : '0.0.1' ,
573+ os : getOperatingSystem ( ) ,
574+ computeEnv : 'test' ,
575+ authTypes : [ 'credentials' , 'builderId' , 'identityCenter' , 'unknown' ] ,
576+ authRegions : [ 'us-east-1' ] ,
577+ authStates : [ 'connected' ] ,
578+ authScopes : amazonQScopes ,
579+ installedExtensions : vscode . extensions . all . map ( ( e ) => e . id ) ,
580+ activeExtensions : vscode . extensions . all . filter ( ( e ) => e . isActive ) . map ( ( e ) => e . id ) ,
581+ } )
582+ } )
583+ } )
0 commit comments