Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import * as vscode from 'vscode'
import assert from 'assert'
import * as sinon from 'sinon'
import { resetCodeWhispererGlobalVariables } from 'aws-core-vscode/test'
import { assertTelemetryCurried, getTestWindow, getTestLogger } from 'aws-core-vscode/test'
import { assertTelemetryCurried, getTestWindow } from 'aws-core-vscode/test'
import { AuthUtil, awsIdSignIn, showCodeWhispererConnectionPrompt } from 'aws-core-vscode/codewhisperer'
import { SsoAccessTokenProvider, constants } from 'aws-core-vscode/auth'

describe('showConnectionPrompt', function () {
let isBuilderIdConnection: sinon.SinonStub
Expand All @@ -17,13 +18,18 @@ describe('showConnectionPrompt', function () {
await resetCodeWhispererGlobalVariables()
isBuilderIdConnection = sinon.stub(AuthUtil.instance, 'isBuilderIdConnection')
isBuilderIdConnection.resolves()

// Stub useDeviceFlow so we always use DeviceFlow for auth
sinon.stub(SsoAccessTokenProvider, 'useDeviceFlow').returns(true)
})

afterEach(function () {
sinon.restore()
})

it('can select connect to AwsBuilderId', async function () {
sinon.stub(AuthUtil.instance, 'login').resolves()

getTestWindow().onDidShowQuickPick(async (picker) => {
await picker.untilReady()
picker.acceptItem(picker.items[0])
Expand All @@ -36,12 +42,13 @@ describe('showConnectionPrompt', function () {
assert.ok(isBuilderIdConnection)
})

it('connectToAwsBuilderId logs that AWS ID sign in was selected', async function () {
it('connectToAwsBuilderId calls AuthUtil login with builderIdStartUrl', async function () {
sinon.stub(vscode.commands, 'executeCommand')
const loginStub = sinon.stub(AuthUtil.instance, 'login').resolves()

await awsIdSignIn()

const loggedEntries = getTestLogger().getLoggedEntries()
assert.ok(loggedEntries.find((entry) => entry === 'selected AWS ID sign in'))
assert.strictEqual(loginStub.called, true)
assert.strictEqual(loginStub.firstCall.args[0], constants.builderIdStartUrl)
Comment on lines +51 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't we test the logged messages now ? can an equivalent log message be added in the relevant place in the new module ? that allows testing the real codepath (or something closer to it), instead of stubs.

Copy link
Contributor Author

@opieter-aws opieter-aws May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getTestLogger().getLoggedEntries() call was failing to pick up the logger. The issue with only testing a log line is that the test won't fail if the functionality is removed or changes (the login call with AWS ID is what we care about asserting here, not a logged string). I did not spend time to dive deeper into it because it's a secondary assertion IMO

Copy link
Contributor

@justinmk3 justinmk3 May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with only testing a log line is that the test won't fail if the functionality is removed or changes (the login call with AWS ID is what we care about asserting here, not a logged string)

Sure, but that applies to any kind of telemetry (aka logs): the telemetry is wrong if logic changes without updating the telemetry. And a similar situation applies when stubs/mocks are used: asserting that a method was called doesn't actually test that the functionality works, and can easily drift (also, asserting stubs leads to brittle tests).

})
})
1 change: 1 addition & 0 deletions packages/core/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export { LoginManager } from './deprecated/loginManager'
export * as constants from './sso/constants'
export * as authUtils from './utils'
export * as auth2 from './auth2'
export * as SsoAccessTokenProvider from './sso/ssoAccessTokenProvider'