-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add a did you mean action to AuthInfo (#1079)
* fix: add a did you mean action to AuthInfo * chore: only add action when we have a suggestion
- Loading branch information
1 parent
f8876d8
commit da5483e
Showing
6 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) 2023, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import Levenshtein from 'fast-levenshtein'; | ||
|
||
/** | ||
* From the haystack, will find the closest value to the needle | ||
* | ||
* @param needle - what the user provided - find results similar to this | ||
* @param haystack - possible results to search against | ||
*/ | ||
export const findSuggestion = (needle: string, haystack: string[]): string => { | ||
// we'll use this array to keep track of which piece of hay is the closest to the users entered value. | ||
// keys closer to the index 0 will be a closer guess than keys indexed further from 0 | ||
// an entry at 0 would be a direct match, an entry at 1 would be a single character off, etc. | ||
const index: string[] = []; | ||
haystack.map((hay) => { | ||
index[Levenshtein.get(needle, hay)] = hay; | ||
}); | ||
|
||
return index.find((item) => item !== undefined) ?? ''; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,8 @@ import { OrgAccessor } from '../../../src/stateAggregator/accessors/orgAccessor' | |
import { Crypto } from '../../../src/crypto/crypto'; | ||
import { Config } from '../../../src/config/config'; | ||
import { SfdcUrl } from '../../../src/util/sfdcUrl'; | ||
import * as suggestion from '../../../src/util/findSuggestion'; | ||
import { SfError } from '../../../src'; | ||
|
||
class AuthInfoMockOrg extends MockTestOrgData { | ||
public privateKey = 'authInfoTest/jwt/server.key'; | ||
|
@@ -2083,11 +2085,29 @@ describe('AuthInfo No fs mock', () => { | |
|
||
it('invalid devhub username', async () => { | ||
const expectedErrorName = 'NamedOrgNotFoundError'; | ||
stubMethod($$.SANDBOX, suggestion, 'findSuggestion').returns('[email protected]'); | ||
try { | ||
await shouldThrow(AuthInfo.create({ username: '[email protected]', isDevHub: true })); | ||
} catch (e) { | ||
expect(e).to.have.property('name', expectedErrorName); | ||
expect(e).to.have.property('message', 'No authorization information found for [email protected].'); | ||
expect(e).to.have.property('actions'); | ||
expect((e as SfError).actions).to.deep.equal([ | ||
'It looks like you mistyped the username or alias. Did you mean "[email protected]"?', | ||
]); | ||
} | ||
}); | ||
|
||
it('invalid devhub username without suggestion', async () => { | ||
const expectedErrorName = 'NamedOrgNotFoundError'; | ||
stubMethod($$.SANDBOX, suggestion, 'findSuggestion').returns(''); | ||
try { | ||
await shouldThrow(AuthInfo.create({ username: '[email protected]', isDevHub: true })); | ||
} catch (e) { | ||
expect(e).to.have.property('name', expectedErrorName); | ||
expect(e).to.have.property('message', 'No authorization information found for [email protected].'); | ||
expect(e).to.have.property('actions'); | ||
expect((e as SfError).actions).to.equal(undefined); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2023, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import { expect } from 'chai'; | ||
import { findSuggestion } from '../../../src/util/findSuggestion'; | ||
|
||
describe('findSuggestion Unit Tests', () => { | ||
it('will find one suggestion', () => { | ||
const res = findSuggestion('needl', ['haystack', 'needle']); | ||
expect(res).to.equal('needle'); | ||
}); | ||
|
||
it('will return empty string when no haystack', () => { | ||
const res = findSuggestion('needl', []); | ||
expect(res).to.equal(''); | ||
}); | ||
|
||
it('will return last closest result', () => { | ||
// j-k-l-m-n | ||
// 'needl' should be right between 'needk' and 'needm' - but we found 'needm' last, which overwrites 'needk' | ||
const res = findSuggestion('needl', ['needk', 'needm']); | ||
expect(res).to.equal('needm'); | ||
}); | ||
|
||
it('will find closest result', () => { | ||
const res = findSuggestion('a', ['z', 'x', 'y']); | ||
expect(res).to.equal('y'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters