| 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 | |
| 8 | import type {CommitInfo} from '../types'; |
| 9 | |
| 10 | import {Dag, DagCommitInfo} from '../dag/dag'; |
| 11 | import {getFoldableRange} from '../fold'; |
| 12 | import {COMMIT} from '../testUtils'; |
| 13 | |
| 14 | describe('fold', () => { |
| 15 | describe('getFoldableRange', () => { |
| 16 | const COMMITS = [ |
| 17 | COMMIT('d', 'Commit D', 'c'), |
| 18 | COMMIT('c', 'Commit C', 'b'), |
| 19 | COMMIT('b', 'Commit B', 'a'), |
| 20 | COMMIT('a', 'Commit A', '1'), |
| 21 | COMMIT('1', 'base', '2', {phase: 'public'}), |
| 22 | ]; |
| 23 | const [, CC, CB, CA] = COMMITS; |
| 24 | |
| 25 | const makeDag = (commits: CommitInfo[]) => |
| 26 | new Dag().add(commits.map(c => DagCommitInfo.fromCommitInfo(c))); |
| 27 | const DAG = makeDag(COMMITS); |
| 28 | |
| 29 | it('get correct selection', () => { |
| 30 | expect(getFoldableRange(new Set(['a', 'b', 'c']), DAG)).toMatchObject([CA, CB, CC]); |
| 31 | }); |
| 32 | |
| 33 | it('does not care about selection order', () => { |
| 34 | expect(getFoldableRange(new Set(['b', 'a', 'c']), DAG)).toMatchObject([CA, CB, CC]); |
| 35 | expect(getFoldableRange(new Set(['c', 'b', 'a']), DAG)).toMatchObject([CA, CB, CC]); |
| 36 | }); |
| 37 | |
| 38 | it('fails for singular selection', () => { |
| 39 | expect(getFoldableRange(new Set(['a']), DAG)).toEqual(undefined); |
| 40 | }); |
| 41 | |
| 42 | it('fails for public commits', () => { |
| 43 | expect(getFoldableRange(new Set(['1', 'a', 'b', 'c']), DAG)).toEqual(undefined); |
| 44 | }); |
| 45 | |
| 46 | it('fails for non-contiguous selections', () => { |
| 47 | expect(getFoldableRange(new Set(['a', 'c']), DAG)).toEqual(undefined); |
| 48 | }); |
| 49 | |
| 50 | it('fails if there are branches in the middle of the range', () => { |
| 51 | const COMMITS = [ |
| 52 | COMMIT('d', 'Commit D', 'c'), |
| 53 | COMMIT('e', 'Commit E', 'b'), |
| 54 | COMMIT('c', 'Commit C', 'b'), |
| 55 | COMMIT('b', 'Commit B', 'a'), |
| 56 | COMMIT('a', 'Commit A', '1'), |
| 57 | COMMIT('1', 'base', '2', {phase: 'public'}), |
| 58 | ]; |
| 59 | expect(getFoldableRange(new Set(['a', 'b', 'c']), makeDag(COMMITS))).toEqual(undefined); |
| 60 | }); |
| 61 | |
| 62 | it('the top of the stack may have multiple children', () => { |
| 63 | const COMMITS = [ |
| 64 | COMMIT('e', 'Commit E', 'c'), |
| 65 | COMMIT('d', 'Commit D', 'c'), |
| 66 | COMMIT('c', 'Commit C', 'b'), |
| 67 | COMMIT('b', 'Commit B', 'a'), |
| 68 | COMMIT('a', 'Commit A', '1'), |
| 69 | COMMIT('1', 'base', '2', {phase: 'public'}), |
| 70 | ]; |
| 71 | const [, , CC, CB, CA] = COMMITS; |
| 72 | expect(getFoldableRange(new Set(['a', 'b', 'c']), makeDag(COMMITS))).toMatchObject([ |
| 73 | CA, |
| 74 | CB, |
| 75 | CC, |
| 76 | ]); |
| 77 | }); |
| 78 | }); |
| 79 | }); |
| 80 | |