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