Skip to content

A local-first personal finance app

[AI] Rank exact payee matches above tied substring matches (#8811)

J-LCRX5bb7d6f6fdae
diff --git a/packages/desktop-client/src/components/autocomplete/PayeeAutocomplete.test.tsx b/packages/desktop-client/src/components/autocomplete/PayeeAutocomplete.test.tsx
index 3d22130da2..b56f8fb7ee 100644
--- a/packages/desktop-client/src/components/autocomplete/PayeeAutocomplete.test.tsx
+++ b/packages/desktop-client/src/components/autocomplete/PayeeAutocomplete.test.tsx
@@ -416,4 +416,27 @@ describe('PayeeAutocomplete.getPayeeSuggestions', () => {
.flatMap(firstOrIncorrect),
).toStrictEqual(['Payees']);
});
+
+ test('ranks an exact payee match above a longer tied substring match', async () => {
+ // Payees are already alpha-sorted, so 'AAA Google' is listed first;
+ // a stable sort on a tied fzf score would otherwise leave it ahead of
+ // the exact 'Google' match.
+ const payees = [makePayee('AAA Google'), makePayee('Google')];
+ const autocomplete = renderPayeeAutocomplete({ payees });
+ await clickAutocomplete(autocomplete);
+
+ const input = autocomplete.querySelector('input')!;
+ await userEvent.type(input, 'Google');
+ await waitForAutocomplete();
+
+ expect(
+ [
+ ...screen
+ .getByTestId('autocomplete')
+ .querySelectorAll(ALL_PAYEE_ITEMS_SELECTOR),
+ ]
+ .map(e => e.getAttribute('data-testid'))
+ .flatMap(firstOrIncorrect),
+ ).toStrictEqual(['Google', 'AAA Google']);
+ });
});
diff --git a/packages/desktop-client/src/components/autocomplete/PayeeAutocomplete.tsx b/packages/desktop-client/src/components/autocomplete/PayeeAutocomplete.tsx
index 74582ed28d..fb416612a9 100644
--- a/packages/desktop-client/src/components/autocomplete/PayeeAutocomplete.tsx
+++ b/packages/desktop-client/src/components/autocomplete/PayeeAutocomplete.tsx
@@ -30,7 +30,7 @@ import type {
PayeeEntity,
} from '@actual-app/core/types/models';
import { css, cx } from '@emotion/css';
-import { Fzf } from 'fzf';
+import { byLengthAsc, byStartAsc, Fzf } from 'fzf';
import { useAccounts } from '#hooks/useAccounts';
import { useCommonPayees } from '#hooks/useCommonPayees';
@@ -461,6 +461,9 @@ export function PayeeAutocomplete({
selector: item => item.name ?? '',
limit: 100,
casing: 'case-insensitive',
+ // Prefer exact/shorter matches over longer ones that merely contain
+ // the query as a substring when fzf scores them equally.
+ tiebreakers: [byLengthAsc, byStartAsc],
})
.find(rawPayee)
.map(result => result.item);
@@ -515,6 +518,9 @@ export function PayeeAutocomplete({
selector: item => item.name ?? '',
limit: 100,
casing: 'case-insensitive',
+ // Prefer exact/shorter matches over longer ones that merely contain
+ // the query as a substring when fzf scores them equally.
+ tiebreakers: [byLengthAsc, byStartAsc],
})
.find(value)
.map(result => result.item);
diff --git a/upcoming-release-notes/fix-exact-match-payee-autocomplete-ranking.md b/upcoming-release-notes/fix-exact-match-payee-autocomplete-ranking.md
new file mode 100644
index 0000000000..7c9417b977
--- /dev/null
+++ b/upcoming-release-notes/fix-exact-match-payee-autocomplete-ranking.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [J-LCRX]
+---
+
+Rank exact payee matches above longer tied substring matches in autocomplete