2.6 KB97 lines
Blame
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8import type {CommitRev} from '../commitStackState';
9
10import {reorderWithDeps} from '../reorderState';
11
12describe('reorderWithDeps', () => {
13 const depMap = new Map<CommitRev, Set<CommitRev>>([
14 [3 as CommitRev, new Set([2] as CommitRev[])],
15 [4 as CommitRev, new Set([2] as CommitRev[])],
16 [5 as CommitRev, new Set([3, 1] as CommitRev[])],
17 ]);
18
19 it('moves nothing if offset is 0', () => {
20 expect(reorderWithDeps(5, 3 as CommitRev, 0, depMap)).toMatchObject({
21 order: [0, 1, 2, 3, 4],
22 deps: [3],
23 offset: 0,
24 });
25 });
26
27 it('moves down without deps', () => {
28 expect(reorderWithDeps(5, 4 as CommitRev, -1, depMap)).toMatchObject({
29 order: [0, 1, 2, 4, 3],
30 deps: [4],
31 offset: -1,
32 });
33 });
34
35 it('moves up without deps', () => {
36 expect(reorderWithDeps(5, 0 as CommitRev, 1, depMap)).toMatchObject({
37 order: [1, 0, 2, 3, 4],
38 deps: [0],
39 offset: 1,
40 });
41
42 expect(reorderWithDeps(5, 0 as CommitRev, 4, depMap)).toMatchObject({
43 order: [1, 2, 3, 4, 0],
44 deps: [0],
45 offset: 4,
46 });
47 });
48
49 it('bounds out of range offsets', () => {
50 expect(reorderWithDeps(5, 3 as CommitRev, 999, new Map())).toMatchObject({
51 order: [0, 1, 2, 4, 3],
52 deps: [3],
53 offset: 1,
54 });
55
56 expect(reorderWithDeps(5, 3 as CommitRev, -999, new Map())).toMatchObject({
57 order: [3, 0, 1, 2, 4],
58 deps: [3],
59 offset: -3,
60 });
61 });
62
63 it('moves down with deps', () => {
64 // Move 4 to before 2, [4, 2] changed to [2, 4] for deps.
65 expect(reorderWithDeps(5, 4 as CommitRev, -2, depMap)).toMatchObject({
66 order: [0, 1, 2, 4, 3],
67 deps: [2, 4],
68 });
69
70 // Move 4 to before 1, [2, 4] are moved together.
71 expect(reorderWithDeps(5, 4 as CommitRev, -3, depMap)).toMatchObject({
72 order: [0, 2, 4, 1, 3],
73 deps: [2, 4],
74 });
75
76 // Move 5 to the bottom. 5->3, 5->1, 3->2 deps are considered.
77 expect(reorderWithDeps(6, 5 as CommitRev, -5, depMap)).toMatchObject({
78 order: [1, 2, 3, 5, 0, 4],
79 deps: [1, 2, 3, 5],
80 });
81 });
82
83 it('moves up with deps', () => {
84 // Moves 1 up and 1->5 dep is considered.
85 expect(reorderWithDeps(6, 1 as CommitRev, 4, depMap)).toMatchObject({
86 order: [0, 2, 3, 4, 1, 5],
87 deps: [1, 5],
88 });
89
90 // Moves 2 up and 2->3, 2->4, 3->5 deps are considered.
91 expect(reorderWithDeps(6, 2 as CommitRev, 3, depMap)).toMatchObject({
92 order: [0, 1, 2, 3, 4, 5],
93 deps: [2, 3, 4, 5],
94 });
95 });
96});
97