1package daemon
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/require"
7
8 "github.com/bnema/vev/internal/domain"
9)
10
11func TestMovePaneRejectsStaleDestinationIncarnationWithoutMutation(t *testing.T) {
12 movedPTY, releaseMoved := newBlockingPTY(t)
13 destinationPTY, releaseDestination := newBlockingPTY(t)
14 defer releaseMoved()
15 defer releaseDestination()
16
17 d, source, _, _ := newManualSessionWithPTYs(t, movedPTY)
18 sourceTab := source.tabs[0]
19 sourceTab.stableID = "source-tab"
20 moved := sourceTab.focusedPane()
21 destination := &session{sessionCore: sessionCore{id: "destination", name: "destination", ephemeral: true}, tabs: []*tab{newTabWithStableID("destination-tab", "destination-pane", destinationPTY, domain.Size{Cols: 80, Rows: 23})}}
22 destinationTab := destination.tabs[0]
23 publishTiledPaneOwners(destination, destinationTab)
24 d.mu.Lock()
25 d.sessions[destination.id] = destination
26 d.mu.Unlock()
27
28 sourceTab.mu.Lock()
29 beforeSource := sourceTab.tree.Clone()
30 sourceTab.mu.Unlock()
31 destinationTab.mu.Lock()
32 beforeDestination := destinationTab.tree.Clone()
33 destinationTab.mu.Unlock()
34 beforeOwner := moved.ownerSnapshot()
35 staleIncarnation := destination.incarnation
36 staleIncarnation[0]++
37 err := d.movePane(movePaneRequest{
38 Source: moveSessionLocator{ID: source.id, Incarnation: source.incarnation},
39 SourceTabID: domain.TabStableID(sourceTab.stableID),
40 SourcePaneID: domain.PaneStableID(moved.stableID),
41 Destination: moveSessionLocator{ID: destination.id, Incarnation: staleIncarnation},
42 DestinationTabID: domain.TabStableID(destinationTab.stableID),
43 })
44 require.ErrorIs(t, err, errMoveStaleTarget)
45 sourceTab.mu.Lock()
46 sourceAfter := sourceTab.tree.Clone()
47 sourceMoved := sourceTab.panes[moved.id]
48 sourceTab.mu.Unlock()
49 destinationTab.mu.Lock()
50 destinationAfter := destinationTab.tree.Clone()
51 destinationTab.mu.Unlock()
52 require.Equal(t, beforeSource, sourceAfter)
53 require.Equal(t, beforeDestination, destinationAfter)
54 require.Same(t, beforeOwner, moved.ownerSnapshot())
55 require.Same(t, moved, sourceMoved)
56}