Skip to content

Commit

Permalink
fix: decoding_in_progress metric and flaky decoding_latency metri…
Browse files Browse the repository at this point in the history
…cs tests (#29852)

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

Fix: 
1. Unreachable `decoding_in_progress` metric when decoding is loading
  - Add metric when decoding is loading
2. Flaky `decoding_latency` metric test
  - Add faker timer

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
3. What is the improvement/solution?
-->

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/29852?quickstart=1)

## **Related issues**

Fixes: #29851
Fixes: #29509

## **Manual testing steps**

1. Go to this page...
2.
4.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

Flaky test appearing locally:
![CleanShot 2025-01-21 at 17 38
18@2x](https://github.com/user-attachments/assets/970e8aed-8cf1-4e06-83fc-97d28d7e4835)


### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
digiwand authored Jan 28, 2025
1 parent 39e4fbe commit a7049a6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
21 changes: 19 additions & 2 deletions ui/pages/confirmations/hooks/useDecodedSignatureMetrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ const decodingData: DecodingData = {
};

describe('useDecodedSignatureMetrics', () => {
/**
* Use fake timer since the tests test the decoding_latency value which is flaky.
* Without the fake timer, the value may show as 0 or 0.001.
*/
beforeAll(() => {
jest.useFakeTimers({ now: 10 });
});

afterAll(() => {
jest.useRealTimers();
});

it('should not call updateSignatureEventFragment if supportedByDecodingAPI is false', async () => {
const state = getMockTypedSignConfirmStateForRequest({
...permitSignatureMsg,
Expand All @@ -43,7 +55,7 @@ describe('useDecodedSignatureMetrics', () => {
expect(mockUpdateSignatureEventFragment).toHaveBeenCalledTimes(0);
});

it('should not call updateSignatureEventFragment if decodingLoading is true', async () => {
it('calls updateSignatureEventFragment with "decoding_in_progress" if decoding is loading ', async () => {
const state = getMockTypedSignConfirmStateForRequest({
...permitSignatureMsg,
decodingLoading: true,
Expand All @@ -61,7 +73,12 @@ describe('useDecodedSignatureMetrics', () => {
state,
);

expect(mockUpdateSignatureEventFragment).toHaveBeenCalledTimes(0);
expect(mockUpdateSignatureEventFragment).toHaveBeenCalledTimes(1);
expect(mockUpdateSignatureEventFragment).toHaveBeenLastCalledWith({
properties: {
decoding_response: 'decoding_in_progress',
},
});
});

it('should call updateSignatureEventFragment with correct parameters if there are no state changes', async () => {
Expand Down
17 changes: 13 additions & 4 deletions ui/pages/confirmations/hooks/useDecodedSignatureMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useSignatureEventFragment } from './useSignatureEventFragment';
enum DecodingResponseType {
Change = 'CHANGE',
NoChange = 'NO_CHANGE',
InProgress = 'decoding_in_progress',
}

export function useDecodedSignatureMetrics(supportedByDecodingAPI: boolean) {
Expand All @@ -32,7 +33,17 @@ export function useDecodedSignatureMetrics(supportedByDecodingAPI: boolean) {
: DecodingResponseType.NoChange);

useEffect(() => {
if (decodingLoading || !supportedByDecodingAPI) {
if (!supportedByDecodingAPI) {
return;
}

if (decodingLoading) {
updateSignatureEventFragment({
properties: {
decoding_response: DecodingResponseType.InProgress,
},
});

return;
}

Expand All @@ -41,9 +52,7 @@ export function useDecodedSignatureMetrics(supportedByDecodingAPI: boolean) {
decoding_change_types: decodingChangeTypes,
decoding_description: decodingData?.error?.message ?? null,
decoding_latency: loadingTime ?? null,
decoding_response: decodingLoading
? 'decoding_in_progress'
: decodingResponse,
decoding_response: decodingResponse,
},
});
}, [
Expand Down

0 comments on commit a7049a6

Please sign in to comment.