Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(test): correct test for core state API response handling

Steven Enamakel300c619c0574
diff --git a/app/src/services/coreStateApi.test.ts b/app/src/services/coreStateApi.test.ts
index 26d1c67a21..5d0ee84f34 100644
--- a/app/src/services/coreStateApi.test.ts
+++ b/app/src/services/coreStateApi.test.ts
@@ -114,6 +114,57 @@ describe('coreStateApi.fetchCoreAppSnapshot', () => {
const { fetchCoreAppSnapshot } = await import('./coreStateApi');
await expect(fetchCoreAppSnapshot()).rejects.toThrow('snapshot failed');
});
+
+ // #6318 — a logout or an A→B login can land while the session owner's
+ // `/auth/me` call is in flight; merging its (now stale-identity) result
+ // with the snapshot's `auth`/`sessionToken` would pair one user's live
+ // data with another's credential.
+ it('retries the whole snapshot when the active credential changed underneath the current-user fetch', async () => {
+ mockCallCoreRpc
+ // 1) app_state_snapshot for user A.
+ .mockResolvedValueOnce({ result: makeSnapshotResult({ auth: { isAuthenticated: true, userId: 'user-a', user: null, profileId: 'p-a' } }) })
+ // 2) re-check: userId moved on to user B (or signed out and back in)
+ // while `fetchCurrentUser` was in flight.
+ .mockResolvedValueOnce({ result: { isAuthenticated: true, userId: 'user-b' } })
+ // 3) the retried app_state_snapshot, now consistently for user B.
+ .mockResolvedValueOnce({ result: makeSnapshotResult({ auth: { isAuthenticated: true, userId: 'user-b', user: null, profileId: 'p-b' } }) })
+ // 4) re-check for the retry: matches this time.
+ .mockResolvedValueOnce({ result: { isAuthenticated: true, userId: 'user-b' } });
+ mockFetchCurrentUser
+ .mockResolvedValueOnce({ user: { _id: 'user-b', name: 'B' }, stale: false, staleSeconds: 0 })
+ .mockResolvedValueOnce({ user: { _id: 'user-b', name: 'B' }, stale: false, staleSeconds: 0 });
+
+ const { fetchCoreAppSnapshot } = await import('./coreStateApi');
+ const out = await fetchCoreAppSnapshot();
+
+ // The final result is entirely user B's — never A's auth paired with B's
+ // live user (or vice versa).
+ expect(out.auth.userId).toBe('user-b');
+ expect((out.currentUser as { _id: string }))._id === 'user-b';
+ expect(mockCallCoreRpc).toHaveBeenCalledTimes(4);
+ });
+
+ it('merges the current user when the credential did not change', async () => {
+ mockCallCoreRpc
+ .mockResolvedValueOnce({
+ result: makeSnapshotResult({
+ auth: { isAuthenticated: true, userId: 'user-a', user: null, profileId: 'p-a' },
+ }),
+ })
+ .mockResolvedValueOnce({ result: { isAuthenticated: true, userId: 'user-a' } });
+ mockFetchCurrentUser.mockResolvedValueOnce({
+ user: { _id: 'user-a', name: 'A' },
+ stale: false,
+ staleSeconds: 0,
+ });
+
+ const { fetchCoreAppSnapshot } = await import('./coreStateApi');
+ const out = await fetchCoreAppSnapshot();
+
+ expect(out.auth.userId).toBe('user-a');
+ expect(out.currentUser).toEqual({ _id: 'user-a', name: 'A' });
+ expect(mockCallCoreRpc).toHaveBeenCalledTimes(2);
+ });
});
describe('coreStateApi.updateCoreLocalState', () => {