Sub task/on frontend update send a background request to update backend core 82#684
Conversation
irumvanselme
left a comment
There was a problem hiding this comment.
Overall Review
Great work on implementing bidirectional locale synchronization! The PR shows solid engineering with comprehensive tests and proper singleton patterns. However, I've identified several critical issues that need to be addressed before merging.
Critical Issues ⚠️
1. Incorrect Test Assertion (SocialAuth.test.tsx:204)
The language field check was removed entirely from the test expectation. This should verify that language: Locale.EN_GB is passed.
2. Type Safety Issues
Multiple locations use i18n.language as Locale without validation. This needs a validation helper function.
3. Error Masking in waitForPendingSync
The method catches errors but still resolves successfully, potentially allowing messages to be sent with incorrect locale context.
Other Findings
- Silent failures without user feedback when locale sync fails
- Missing integration tests for the full sync flow
- Inconsistent error message formatting
I'll add detailed inline comments and implement fixes locally.
🔴 Critical Issue #1: Incorrect Test AssertionFile: expect(createUserPreferencesSpy).toHaveBeenCalledWith({
user_id: givenUser.id,
invitation_code: "valid-code",
// language: Language.en, // <-- REMOVED but should be verified
});Problem: The test removed the Fix: The expectation should verify: expect(createUserPreferencesSpy).toHaveBeenCalledWith({
user_id: givenUser.id,
invitation_code: "valid-code",
language: Locale.EN_GB,
});This is critical because it means the test is not properly verifying the contract. |
🔴 Critical Issue #2: Type Safety ProblemsFiles:
Problem: Multiple locations use unsafe type assertions: const currentLocale = i18n.language as Locale;If Recommendation: Create a validation helper: // In src/i18n/constants.ts or utils
export function validateLocale(locale: string): Locale {
const localeValue = locale as Locale;
if (SupportedLocales.includes(localeValue)) {
return localeValue;
}
console.warn(`Invalid locale: ${locale}, falling back to ${FALL_BACK_LOCALE}`);
return FALL_BACK_LOCALE;
}Then use: const currentLocale = validateLocale(i18n.language); |
🔴 Critical Issue #3: Error Masking in waitForPendingSyncFile: public async waitForPendingSync(): Promise<void> {
try {
if (this.pendingSync) {
await this.pendingSync;
}
} catch (e) {
console.error("[LocaleSyncService.waitForPendingSync] Failed to wait for pending sync", e);
}
}Problem: The error is caught and logged, but the promise resolves successfully even if sync failed. This means:
Recommendation: Either:
Usage in Chat.tsx: // Ensure the locale is synced before sending a message
await LocaleSyncService.getInstance().waitForPendingSync();
// Should this be: if (!await waitForPendingSync()) { /* warn user */ } |
🟡 Medium Priority: Silent FailuresFile: When a user changes their language in the UI, if the backend sync fails, they receive no feedback: LocaleSyncService.getInstance()
.syncLocaleToBackend(locale)
.catch((e) => {
console.debug("Locale sync to backend failed, will retry on next login");
});Problem: The user might think their preference was saved, but it wasn't. On next login or refresh, they'll be surprised to see a different language. Recommendation: Show a non-blocking snackbar warning: .catch((e) => {
enqueueSnackbar(
t("language.sync_failed_warning"),
{ variant: "warning" }
);
});Message could be: "Language changed to {locale}, but we couldn't save your preference. It will revert on next login." |
🟡 Medium Priority: Unclear ESLint DisableFile: // Why: Adding `t` to dependencies will trigger a refresh everytime language is changed and backend will be called.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [enqueueSnackbar, activeSessionId]);Problem: The comment is confusing. It suggests avoiding backend calls, but the real issue is more nuanced. Better explanation: // Intentionally omitting `t` from dependencies:
// - This effect should only run when activeSessionId changes (conversation switching)
// - Including `t` would cause unnecessary conversation reloads on language change
// - The i18n translation system automatically updates translated strings when language changes
// eslint-disable-next-line react-hooks/exhaustive-deps |
✅ Fixes Implemented LocallyI've implemented all the critical and medium-priority fixes in the current directory: 1. ✅ Fixed Test Assertion (SocialAuth.test.tsx:661)Before: expect(createUserPreferencesSpy).toHaveBeenCalledWith({
user_id: givenUser.id,
invitation_code: "valid-code",
// language field was removed
});After: expect(createUserPreferencesSpy).toHaveBeenCalledWith({
user_id: givenUser.id,
invitation_code: "valid-code",
language: Locale.EN_GB,
});✅ Test passes 2. ✅ Added Type Safety Helper (
|
🎉 Update: Translation Keys AddedI've added the proper translation keys to all locale files: Translation Keys Added:
Files Updated:
Test Mocks Added:Also added
All tests now pass! ✅ |
… adjusting useEffect dependencies
0a436a3 to
0b3dee0
Compare
|
|
||
| // On page refresh, apply backend locale to frontend i18n | ||
| // This ensures the UI language matches the user's stored preference | ||
| await LocaleSyncService.getInstance().applyBackendLocale(preferences.language); |
There was a problem hiding this comment.
I dont see any new assertions in the index.test.tsx for this
| import { AuthChannelMessage } from "src/auth/services/authBroadcastChannel/authBroadcastChannel"; | ||
|
|
||
| // Mock i18n to return a consistent locale for testing | ||
| jest.mock("src/i18n/i18n", () => ({ |
There was a problem hiding this comment.
since we have this in a few places, maybe worth making this a generalized mock in test_utilities?
5469431 to
76bdafe
Compare
…r preferences - Add LocaleSyncService singleton to handle syncing locale changes between frontend and backend - Update authentication and login flows to sync locale on login and apply backend locale on refresh - Ensure backend locale is validated and applied to i18n on page load - Update language change logic to trigger background sync to backend when user changes language in UI - Refactor user preferences to use Locale type instead of Language enum - Update tests and stories to use Locale and reflect new locale sync logic
76bdafe to
d37eae1
Compare
No description provided.