Skip to content

Commit fecfa59

Browse files
committed
test: add tests for logic
1 parent e033744 commit fecfa59

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

packages/amazonq/src/lsp/config.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { DevSettings, getServiceEnvVarConfig, Settings } from 'aws-core-vscode/shared'
6+
import { DevSettings, getLogger, getServiceEnvVarConfig, Settings } from 'aws-core-vscode/shared'
77
import { LspConfig } from 'aws-core-vscode/amazonq'
88

9+
// Taken from https://github.com/aws/language-server-runtimes/blob/eae85672c345d8adaf4c8cbd741260b8a59750c4/runtimes/runtimes/util/loggingUtil.ts#L4-L10
10+
const validLspLogLevels = ['error', 'warn', 'info', 'log', 'debug']
911
export interface ExtendedAmazonQLSPConfig extends LspConfig {
1012
ui?: string
1113
}
@@ -31,9 +33,21 @@ export function getAmazonQLspConfig(): ExtendedAmazonQLSPConfig {
3133
export function getLspLogSettings(clientId: string) {
3234
const traceServerSetting = `${clientId}.trace.server`
3335
const lspLogLevelSetting = `${clientId}.lsp.logLevel`
36+
const seperateTraceChannel = Settings.instance.get(traceServerSetting)
37+
const lspLogLevel = Settings.instance.get(lspLogLevelSetting, String, 'info')
38+
39+
if (!validLspLogLevels.includes(lspLogLevel)) {
40+
getLogger('amazonqLsp').warn(
41+
`Invalid log level for ${lspLogLevelSetting}: ${lspLogLevel}. Defaulting to 'info'.`
42+
)
43+
return {
44+
seperateTraceChannel,
45+
lspLogLevel: 'info',
46+
}
47+
}
3448

3549
return {
36-
seperateTraceChannel: Settings.instance.get(traceServerSetting),
37-
lspLogLevel: Settings.instance.get(lspLogLevelSetting, String, 'info'),
50+
seperateTraceChannel,
51+
lspLogLevel: lspLogLevel,
3852
}
3953
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import assert from 'assert'
7+
import { sanitizeLogLevel } from '../../../../../src/lsp/config'
8+
9+
describe('sanitizeLogLevel', function () {
10+
it('should return the log level if it is valid', function () {
11+
const logLevel = 'info'
12+
const sanitizedLogLevel = sanitizeLogLevel(logLevel)
13+
assert.strictEqual(sanitizedLogLevel, logLevel)
14+
})
15+
16+
it('should default to info if it is invalid', function () {
17+
const logLevel = 'verbose'
18+
const sanitizedLogLevel = sanitizeLogLevel(logLevel)
19+
assert.strictEqual(sanitizedLogLevel, 'info')
20+
})
21+
})

0 commit comments

Comments
 (0)