Skip to content

refactor: remove unsafe assertions from shared inputs, contexts and filters - #104082

Open
KJ21-ENG wants to merge 3 commits into
Expensify:mainfrom
KJ21-ENG:seatbelt-v2/batch-20261015-run-3-0ec72882
Open

KJ21-ENG wants to merge 3 commits into
Expensify:mainfrom
KJ21-ENG:seatbelt-v2/batch-20261015-run-3-0ec72882

Conversation

@KJ21-ENG

@KJ21-ENG KJ21-ENG commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

Remove unsafe assertions from shared text/code inputs, Onyx contexts, Search filter projections and web CSS insertion. Use the actual input ref and checked focus/modifier properties while preserving supported input behavior, avatar fallbacks and permission loading states.

Fixed Issues

$ #94739
PROPOSAL: #94739 (comment)

Tests

Current-head upstream Jest and typecheck passed. Focused checks: npx jest --runInBand TextInputWebRefAndFocusTest ValidateCodeInputEventTest DomUtilsWebTest createOnyxContextTest.

Setup: Use an email address you control to reach normal sign-in code entry.

Platforms: Android native, Android mWeb Chrome, iOS native, iOS mWeb Safari and macOS Chrome/Safari. Run hardware-key steps where a keyboard is available.

Paste

  1. Select the first code slot and paste 12. Verify only the first two slots fill.
  2. Paste nonnumeric text. Verify the code does not change.
  3. Focus Resend without activating it and paste digits. Verify the code does not change.

Keyboard navigation

  1. Focus an interior code slot and press Tab. Verify focus moves one slot forward without changing digits or submitting.
  2. Press Shift+Tab. Verify focus returns one slot without changing digits or submitting.
  3. Focus the first slot and press Shift+Tab. Verify focus leaves for the preceding page control.
  4. Focus the last slot and press Tab. Verify focus leaves for the next page control.
  5. Repeat on the base build. Compare the focus sequence to establish whether any failure is new.

Offline tests

No additional offline case: submission/reconnect handling is unchanged; focused event tests cover blocked offline Enter and online submission.

QA Steps

Same as tests on staging containing this PR.

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Recordings (1)

Windows desktop Chrome: paste, forward Tab and boundary checks were observed, but interior Shift+Tab left the code input instead of moving back. Attribution remains unconfirmed without a base comparison. Native, mobile-browser and macOS results are unverified.

pr104082-code-paste-focus-desktop.mp4

Seatbelt-Operation: adc93f02-11ed-4ef2-bace-381d00d62b1f
Seatbelt-Operation: add2564d-f961-4201-85ce-d47084230597
@melvin-bot

melvin-bot Bot commented Oct 15, 2026

Copy link
Copy Markdown

Hey! I see that you made changes to our Form component. Make sure to update the docs in FORMS.md accordingly. Cheers!

@KJ21-ENG
KJ21-ENG marked this pull request as ready for review October 16, 2026 21:22
@KJ21-ENG
KJ21-ENG requested review from a team as code owners October 16, 2026 21:22
@melvin-bot
melvin-bot Bot requested review from JmillsExpensify and removed request for a team October 16, 2026 21:22
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T21:41:13.968170Z d572d8e Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@melvin-bot
melvin-bot Bot requested a review from amyevans October 16, 2026 21:23
@melvin-bot

melvin-bot Bot commented Oct 16, 2026

Copy link
Copy Markdown

@amyevans Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot
melvin-bot Bot removed the request for review from a team October 16, 2026 21:23

@JmillsExpensify JmillsExpensify left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No product review required.

@amyevans
amyevans requested review from ikevin127 and removed request for amyevans October 19, 2026 11:59
@KJ21-ENG KJ21-ENG changed the title Remove unsafe assertions from shared inputs, contexts, filters, and DOM utilities refactor: remove unsafe assertions from shared inputs, contexts and filters Oct 19, 2026
@ikevin127

ikevin127 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Screen.Recording.2026-10-19.at.17.49.10.mov

Comment thread src/libs/SearchQueryUtils.ts Outdated
return acc;
},
[] as Array<OnyxCollectionValuesMapping[T]>,
[] as Array<OnyxCollectionValuesMapping[T] | Record<string, Pick<OnyxTypes.PolicyCategory, 'name'>>>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 src/libs/SearchQueryUtils.ts:1615 This is still a type assertion ([] as Array<...>), and this PR is meant to remove them. Pass the type to reduce instead:

return Object.keys(policyData).reduce<Array<OnyxCollectionValuesMapping[T] | Record<string, Pick<OnyxTypes.PolicyCategory, 'name'>>>>((acc, curr) => {
    ...
}, []);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed the empty-array assertion and supplied TValue[] directly to reduce. The accumulator is now typed without a cast.

Comment thread src/libs/SearchQueryUtils.ts Outdated
policies?: OnyxCollection<OnyxTypes.Policy>,
): Array<OnyxCollectionValuesMapping[T]> {
): Array<OnyxCollectionValuesMapping[T]>;
function getAllPolicyValues(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 src/libs/SearchQueryUtils.ts:1587 Adding a second overload and widening the main signature just for the category projection makes this hard to follow. The function only uses key as a prefix, and isPolicyCollection already takes data: unknown, so it can simply be generic over the value type.

That also removes the cast above:

function getAllPolicyValues<TValue>(
    policyID: Filter | undefined,
    key: OnyxCollectionKey,
    policyData: OnyxCollection<TValue>,
    policies?: OnyxCollection<OnyxTypes.Policy>,
): TValue[] {
    ...
    return Object.keys(policyData).reduce<TValue[]>((acc, curr) => { ... }, []);
}

The full-model callers keep their types, and buildFilterFormValuesFromQuery gets the name-only projection without a special overload.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Replaced the overloads with one TValue-based getAllPolicyValues function. Full-model callers retain their inferred result types, and the name-only category projection works without a special overload.

expect(projected.at(0)).toBe(names[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}1`]);
expect(negated).toEqual([{travelKey: {name: 'Travel'}}]);
expect(all).toEqual([...projected, ...negated]);
// @ts-expect-error -- name projections do not promise the omitted enabled metadata.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟠 tests/unit/Search/SearchQueryUtilsTest.ts:5574 Like on #103920, I'd rather not add new @ts-expect-errors in a PR that removes type workarounds. The three suppressions plus the IsEqual checks at line 5564 test the overloads' types, not their behaviour.

The calls under @ts-expect-error also really run with the wrong data. If you switch to the generic version above, this block can shrink to the runtime checks (projected, negated, all).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed all three @ts-expect-error suppressions, their invalid calls, and the IsEqual block. Kept the runtime checks for projected, negated, and all-policy values, including ordering and reference identity.

Comment thread tests/unit/createOnyxContextTest.tsx Outdated
// Then no reconstruction drops the undefined member and no key/model correlation is lost
expect(result.current).toBe(cards);
expect(result.current).toHaveProperty(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}2`, undefined);
const sessionType: IsEqual<ReturnType<typeof useSession>, UseOnyxResult<OnyxValue<typeof ONYXKEYS.SESSION>>[0]> = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 tests/unit/createOnyxContextTest.tsx:93 expect([sessionType, betasType, cardsType]).toEqual([true, true, true]) can never fail at runtime. The types are already checked by tsc when the IsEqual constants are assigned.

Please drop the IsEqual block, since the hydrate/update tests above already cover the hook's behaviour.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed the IsEqual block and its always-true assertion. The four hydration and update tests remain and pass.

act(() => ref.current?.focusLastSelected());
act(() => ref.current?.resetFocus());
act(() => ref.current?.blur());
act(() => ref.current?.clear());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 tests/ui/ValidateCodeInputEventTest.tsx:216 onChangeText was already last called with '' by the backspace step above, so toHaveBeenLastCalledWith('') passes even if clear() does nothing.

Please call onChangeText.mockClear() before clear(), so the assertion actually checks it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Cleared the onChangeText mock immediately before clear(), then asserted exactly one new callback with an empty string. The earlier backspace callback can no longer satisfy this assertion.

@ikevin127 ikevin127 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PR is not ready yet. Most of the code change is sound, but the description fails one of its own test steps, and a few test files add type-check workarounds of the kind I asked you to remove on #103920.

Nobody has left other inline comments yet. Codex found nothing, and CI fails Check independent approval and checklist, which wait on your approval and your unfinished checklist.

The 🟠 Shift+Tab item: the recording shows Shift+Tab leaving the code input, which the test step says shouldn't happen. The Tab logic is the same as on main; only the shift check changed. React's synthetic keyboard events carry shiftKey directly, so 'shiftKey' in event is true on web.

The bug is not present on staging, I checked - meaning it'd be a regression caused by this PR. QA will run that step as written and fail it.

Screen.Recording.2026-10-19.at.18.09.57.mov

@KJ21-ENG Please tag me once the regression and comments are addressed.

Restore baseline Shift modifier access with a typed web modifier.

Seatbelt-Operation: reviewer-shift-rollback-sign-104082-49f03569-71bc-4ad9-88a4-d61534f2a9f9
@KJ21-ENG

Copy link
Copy Markdown
Contributor Author

@ikevin127 Restored the original direct event.shiftKey check and described the optional web modifier using React’s keyboard-event type, without a cast.

Please check this when you get a chnace

@ikevin127

Copy link
Copy Markdown
Contributor

@KJ21-ENG Update on the 🟠 Shift+Tab item from my review: I'm withdrawing it as a regression from this PR.

I pulled the latest head (6ea7022d), did a clean rebuild (cleared node_modules/.cache/rspack, ran npm install) and tested in incognito. Shift+Tab from a middle code slot still leaves the code input and jumps to the language selector.

But the same thing happens on a local build of latest main, while current staging moves back one slot as expected. So it only shows up in local dev builds, and the PR doesn't cause it. The shift check at ValidateCodeInput.tsx:418 now behaves exactly like main.

Two things before I approve:

  1. Can you test Shift+Tab from a middle slot on your local build ? Check whether it moves back one slot, and compare with staging. If it works for you locally, please share your setup (browser, npm run web vs anything else), since I can't get it to work locally on either main or this branch.
  2. Please update the Screenshots/Videos note and video. It still says "interior Shift+Tab left the code input instead of moving back" (the bug I mentioned), and since QA runs on staging, that note will read as a known failure. Either remove it, or say the step was verified on staging and that local dev builds behave differently on main too.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants