Skip to content

The agent that grows with you

test(desktop): consolidate the rotation-foreground suite into two invariants

teknium1c2b94ca273d9
diff --git a/apps/desktop/src/app/session/hooks/use-session-state-cache.test.tsx b/apps/desktop/src/app/session/hooks/use-session-state-cache.test.tsx
index 0025aedcc08..0b6e3699980 100644
--- a/apps/desktop/src/app/session/hooks/use-session-state-cache.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-session-state-cache.test.tsx
@@ -26,7 +26,6 @@ import {
setTurnStartedAt
} from '@/store/session'
import {
- $focusedStoredSessionId,
$sessionStates,
$sessionTiles,
clearAllSessionStates,
@@ -98,13 +97,44 @@ describe('useSessionStateCache — stored-id rotation provenance', () => {
expect(cache.runtimeIdByStoredSessionIdRef.current.get('stored-A-next')).toBe('runtime-A')
})
- it('does not steal the foreground route when a fast A -> B switch beats A\'s rotation (#86106)', () => {
+ // The foreground has three voices — the stored selection, the HashRouter
+ // route and the focused layout pane. Any one of them naming a session
+ // outside the rotating lineage means the user already moved on (#86106).
+ it.each([
+ {
+ surface: 'selection',
+ arm: () => setSelectedStoredSessionId('stored-B'),
+ selected: 'stored-B'
+ },
+ {
+ surface: 'hash route',
+ arm: () => window.history.pushState({}, '', '/#/stored-B'),
+ selected: null
+ },
+ {
+ surface: 'focused tile',
+ arm: () => {
+ // Route and selection still name A while tile B holds the layout
+ // focus. The selection is armed BEFORE the layout: its listener homes
+ // focus to the workspace, so the tile focus must be noted last.
+ setSessions([{ id: 'stored-A' }, { id: 'stored-B' }] as never)
+ $sessionTiles.set([{ storedSessionId: 'stored-B' }])
+ setSelectedStoredSessionId('stored-A')
+ window.history.pushState({}, '', '/#/stored-A')
+ $layoutTree.set(
+ group(['workspace', 'session-tile:stored-B'], { active: 'session-tile:stored-B', id: 'grp-main' })
+ )
+ noteActiveTreeGroup('grp-main')
+ },
+ selected: 'stored-A'
+ }
+ ])('does not steal the foreground when the $surface names another session while A rotates', ({ arm, selected }) => {
let cache!: Cache
setActiveSessionId('runtime-A')
- setSelectedStoredSessionId('stored-B')
+ arm()
render(
- <Harness activeSessionId="runtime-A" onReady={value => (cache = value)} selectedStoredSessionId="stored-B" />
+ <Harness activeSessionId="runtime-A" onReady={value => (cache = value)} selectedStoredSessionId={selected} />
)
act(() => {
@@ -117,110 +147,30 @@ describe('useSessionStateCache — stored-id rotation provenance', () => {
expect(cache.runtimeIdByStoredSessionIdRef.current.get('stored-A-next')).toBe('runtime-A')
})
- it('does not steal the foreground when the primary route is another session', () => {
- let cache!: Cache
-
- // Desktop mounts HashRouter: the app route lives in location.hash.
- window.history.pushState({}, '', '/#/stored-B')
- setActiveSessionId('runtime-A')
- setSelectedStoredSessionId(null)
- render(
- <Harness activeSessionId="runtime-A" onReady={value => (cache = value)} selectedStoredSessionId={null} />
- )
-
- act(() => {
- cache.updateSessionState('runtime-A', state => state, 'stored-A')
- cache.updateSessionState('runtime-A', state => state, 'stored-A-next')
- })
-
- expect($activeSessionStoredIdRotation.get()).toBeNull()
- })
-
- it('follows A -> A-next when the primary route is that same session', () => {
- let cache!: Cache
-
- window.history.pushState({}, '', '/#/stored-A')
- setActiveSessionId('runtime-A')
- setSelectedStoredSessionId(null)
- render(
- <Harness activeSessionId="runtime-A" onReady={value => (cache = value)} selectedStoredSessionId={null} />
- )
-
- act(() => {
- cache.updateSessionState('runtime-A', state => state, 'stored-A')
- cache.updateSessionState('runtime-A', state => state, 'stored-A-next')
- })
-
- expect($activeSessionStoredIdRotation.get()).toEqual({
- nextStoredSessionId: 'stored-A-next',
- previousStoredSessionId: 'stored-A',
- runtimeSessionId: 'runtime-A'
- })
- })
-
- it('follows A -> A-next when there is no route and no store selection', () => {
- let cache!: Cache
-
- window.history.pushState({}, '', '/')
- setActiveSessionId('runtime-A')
- setSelectedStoredSessionId(null)
- render(
- <Harness activeSessionId="runtime-A" onReady={value => (cache = value)} selectedStoredSessionId={null} />
- )
-
- act(() => {
- cache.updateSessionState('runtime-A', state => state, 'stored-A')
- cache.updateSessionState('runtime-A', state => state, 'stored-A-next')
- })
-
- expect($activeSessionStoredIdRotation.get()).toEqual({
- nextStoredSessionId: 'stored-A-next',
- previousStoredSessionId: 'stored-A',
- runtimeSessionId: 'runtime-A'
- })
- })
-
- it('does not steal the focused session tile when primary A rotates (#86106)', () => {
- let cache!: Cache
-
- // Route and selection still name A while tile B holds the layout focus —
- // the shape the review called out: A's rotation must not run
- // setSelectedStoredSessionId(A-next) and reveal the workspace over B. The
- // selection is armed BEFORE the layout: its listener homes focus to the
- // workspace, so the tile focus must be noted last.
- setSessions([{ id: 'stored-A' }, { id: 'stored-B' }] as never)
- $sessionTiles.set([{ storedSessionId: 'stored-B' }])
- setActiveSessionId('runtime-A')
- setSelectedStoredSessionId('stored-A')
- window.history.pushState({}, '', '/#/stored-A')
- $layoutTree.set(group(['workspace', 'session-tile:stored-B'], { active: 'session-tile:stored-B', id: 'grp-main' }))
- noteActiveTreeGroup('grp-main')
- render(
- <Harness activeSessionId="runtime-A" onReady={value => (cache = value)} selectedStoredSessionId="stored-A" />
- )
-
- expect($focusedStoredSessionId.get()).toBe('stored-B')
-
- act(() => {
- cache.updateSessionState('runtime-A', state => state, 'stored-A')
- cache.updateSessionState('runtime-A', state => state, 'stored-A-next')
- })
-
- expect($activeSessionStoredIdRotation.get()).toBeNull()
- expect(cache.runtimeIdByStoredSessionIdRef.current.get('stored-A-next')).toBe('runtime-A')
- })
-
- it('follows A -> A-next when the focused tile belongs to the same lineage', () => {
+ it.each([
+ { shape: 'the primary route is that same session', arm: () => window.history.pushState({}, '', '/#/stored-A') },
+ { shape: 'there is no route and no store selection', arm: () => undefined },
+ {
+ shape: 'the focused tile belongs to the same lineage',
+ arm: () => {
+ // A tile keyed by an OLDER segment id of the same conversation still
+ // counts as the foreground: the rotation carries it to the new tip.
+ setSessions([
+ { _lineage_ids: ['stored-A', 'stored-A-next'], _lineage_root_id: 'stored-A', id: 'stored-A-next' }
+ ] as never)
+ $sessionTiles.set([{ storedSessionId: 'stored-A' }])
+ $layoutTree.set(
+ group(['workspace', 'session-tile:stored-A'], { active: 'session-tile:stored-A', id: 'grp-main' })
+ )
+ noteActiveTreeGroup('grp-main')
+ }
+ }
+ ])('follows A -> A-next when $shape', ({ arm }) => {
let cache!: Cache
- // A tile keyed by an OLDER segment id of the same conversation still counts
- // as the foreground: the rotation carries the surface to the new tip.
- setSessions([{ _lineage_ids: ['stored-A', 'stored-A-next'], _lineage_root_id: 'stored-A', id: 'stored-A-next' }] as never)
- $sessionTiles.set([{ storedSessionId: 'stored-A' }])
setActiveSessionId('runtime-A')
setSelectedStoredSessionId(null)
- $layoutTree.set(group(['workspace', 'session-tile:stored-A'], { active: 'session-tile:stored-A', id: 'grp-main' }))
- noteActiveTreeGroup('grp-main')
+ arm()
render(
<Harness activeSessionId="runtime-A" onReady={value => (cache = value)} selectedStoredSessionId={null} />
)