mirror of
https://github.com/tinyhumansai/openhuman.git
synced 2026-07-30 23:14:37 +00:00
* refactor(core): integrate CoreStateProvider and streamline state management - Replaced UserProvider with CoreStateProvider in App component to centralize state management. - Updated various components to utilize the new CoreStateProvider for accessing session tokens and user data. - Refactored hooks and components to eliminate direct Redux store dependencies, enhancing modularity and maintainability. - Introduced a new core state management structure to improve the handling of user authentication and onboarding states. This refactor aims to simplify state access across the application and improve overall code clarity. * fix: restore polyfills import and clean up component imports - Reintroduced the import of polyfills in main.tsx to ensure compatibility. - Adjusted import statements in PrivacyPanel and SocketProvider for consistency. - Simplified conditional expressions in TeamInvitesPanel and TeamMembersPanel for better readability. - Refactored CoreStateProvider and store.ts to streamline state initialization. - Enhanced formatting in various files for improved code clarity and maintainability. * refactor(tests): streamline onboarding and protected route tests - Updated OnboardingOverlay tests to utilize mock state management and simplify assertions. - Refactored ProtectedRoute tests to improve readability and ensure consistent use of mock state. - Enhanced teamApi tests to replace direct API calls with core RPC method calls, improving test isolation and clarity. - Adjusted socketSelectors tests to utilize a centralized core state snapshot for better state management during tests. * refactor(onboarding): simplify onboarding state management and improve loading logic - Removed unnecessary local state for onboarding completion in OnboardingOverlay, directly utilizing snapshot data. - Enhanced loading logic to prevent unnecessary renders and improve user experience during onboarding. - Updated PrivacyPanel to handle analytics consent persistence with error handling. - Refactored TeamManagementPanel and TeamPanel to improve team data fetching logic and loading states. - Streamlined CoreStateProvider to manage session token synchronization and state updates more effectively. * chore(deps): update tempfile dependency and refactor app state management - Added `tempfile` dependency to Cargo.toml for improved temporary file handling. - Refactored app state loading and saving functions to enhance error handling and ensure data integrity. - Introduced quarantine mechanism for corrupted app state files to prevent application crashes. - Updated URL parsing logic to ensure proper formatting of API URLs. - Adjusted type definitions in schemas for better clarity and consistency. * refactor(tests): simplify mock state usage in onboarding and protected route tests - Consolidated mock state management in OnboardingOverlay and ProtectedRoute tests for improved readability. - Streamlined assertions by reducing unnecessary lines in test setups. - Enhanced consistency in mock return values across tests to ensure clarity and maintainability. * refactor(tests): enhance PublicRoute tests with mock state and routing - Updated PublicRoute tests to utilize MemoryRouter for improved routing simulation. - Simplified mock state management by integrating `useCoreState` for user authentication scenarios. - Streamlined test assertions and removed redundant preloaded state configurations for clarity and maintainability. * refactor(tests): streamline mock state usage in PublicRoute and Mnemonic tests - Simplified mock state management in PublicRoute and Mnemonic tests for improved readability. - Consolidated mock return values to reduce redundancy and enhance clarity in test setups. - Improved consistency in the usage of `useCoreState` across test files. * Refactor billing components for improved readability - Cleaned up import statements in BillingPanel.tsx for better organization. - Enhanced formatting of billing plan descriptions in billingHelpers.ts for consistency. - Improved readability of conditional checks in BillingPanel component. * Refactor API endpoint handling for clarity and consistency - Updated references from `/settings` to `/auth/me` in various modules to standardize user authentication flows. - Renamed `parse_settings_response_json` to `parse_api_response_json` for improved clarity in response handling. - Adjusted user ID extraction functions to reflect the new endpoint structure, enhancing maintainability across the codebase. - Updated test cases to align with the new endpoint naming conventions, ensuring consistency in API interactions.
86 lines
2.7 KiB
TypeScript
86 lines
2.7 KiB
TypeScript
import { beforeEach, describe, expect, it } from 'vitest';
|
|
|
|
import { type CoreState, setCoreStateSnapshot } from '../../lib/coreState/store';
|
|
import type { RootState } from '../index';
|
|
import { selectSocketId, selectSocketStatus } from '../socketSelectors';
|
|
|
|
function encodeJwt(payload: Record<string, unknown>): string {
|
|
const header = btoa(JSON.stringify({ alg: 'HS256', typ: 'JWT' }));
|
|
const body = btoa(JSON.stringify(payload));
|
|
return `${header}.${body}.signature`;
|
|
}
|
|
|
|
function makeCoreState(token: string | null): CoreState {
|
|
return {
|
|
isBootstrapping: false,
|
|
isReady: true,
|
|
snapshot: {
|
|
auth: { isAuthenticated: !!token, userId: null, user: null, profileId: null },
|
|
sessionToken: token,
|
|
currentUser: null,
|
|
onboardingCompleted: false,
|
|
analyticsEnabled: false,
|
|
localState: { encryptionKey: null, primaryWalletAddress: null, onboardingTasks: null },
|
|
},
|
|
teams: [],
|
|
teamMembersById: {},
|
|
teamInvitesById: {},
|
|
};
|
|
}
|
|
|
|
function makeState(
|
|
byUser: Record<string, { status: string; socketId: string | null }> = {}
|
|
): RootState {
|
|
return { socket: { byUser } } as RootState;
|
|
}
|
|
|
|
describe('selectSocketStatus', () => {
|
|
beforeEach(() => {
|
|
setCoreStateSnapshot(makeCoreState(null));
|
|
});
|
|
|
|
it('returns disconnected when no token', () => {
|
|
const state = makeState();
|
|
expect(selectSocketStatus(state)).toBe('disconnected');
|
|
});
|
|
|
|
it('returns status from user state based on JWT tgUserId', () => {
|
|
setCoreStateSnapshot(makeCoreState(encodeJwt({ tgUserId: 'tg123' })));
|
|
const state = makeState({ tg123: { status: 'connected', socketId: 'sock-1' } });
|
|
|
|
expect(selectSocketStatus(state)).toBe('connected');
|
|
});
|
|
|
|
it('returns disconnected when JWT user has no socket state', () => {
|
|
setCoreStateSnapshot(makeCoreState(encodeJwt({ tgUserId: 'tg123' })));
|
|
const state = makeState();
|
|
|
|
expect(selectSocketStatus(state)).toBe('disconnected');
|
|
});
|
|
|
|
it('uses __pending__ for invalid JWT', () => {
|
|
setCoreStateSnapshot(makeCoreState('not-a-jwt'));
|
|
const state = makeState({ __pending__: { status: 'connecting', socketId: null } });
|
|
|
|
expect(selectSocketStatus(state)).toBe('connecting');
|
|
});
|
|
});
|
|
|
|
describe('selectSocketId', () => {
|
|
beforeEach(() => {
|
|
setCoreStateSnapshot(makeCoreState(null));
|
|
});
|
|
|
|
it('returns null when no token', () => {
|
|
const state = makeState();
|
|
expect(selectSocketId(state)).toBeNull();
|
|
});
|
|
|
|
it('returns socketId from user state', () => {
|
|
setCoreStateSnapshot(makeCoreState(encodeJwt({ tgUserId: 'tg123' })));
|
|
const state = makeState({ tg123: { status: 'connected', socketId: 'sock-abc' } });
|
|
|
|
expect(selectSocketId(state)).toBe('sock-abc');
|
|
});
|
|
});
|