|
| 1 | +import type { Method } from "../../../../src/model-editor/method"; |
| 2 | +import type { ModeledMethod } from "../../../../src/model-editor/modeled-method"; |
| 3 | +import { sortMethods } from "../../../../src/model-editor/shared/sorting"; |
| 4 | +import { |
| 5 | + createMethod, |
| 6 | + createUsage, |
| 7 | +} from "../../../factories/model-editor/method-factories"; |
| 8 | +import { createSinkModeledMethod } from "../../../factories/model-editor/modeled-method-factories"; |
| 9 | +import { shuffle } from "../../../vscode-tests/utils/list-helpers"; |
| 10 | + |
| 11 | +describe("sortMethods", () => { |
| 12 | + it("uses primary sort order", () => { |
| 13 | + const unsavedPositiveAutoModelPrediction = createMethod({ |
| 14 | + signature: "org.sql2o.Sql2o#open1()", |
| 15 | + }); |
| 16 | + const negativeAutoModelPrediction = createMethod({ |
| 17 | + signature: "org.sql2o.Sql2o#open2()", |
| 18 | + }); |
| 19 | + const unsavedManualModel = createMethod({ |
| 20 | + signature: "org.sql2o.Sql2o#open3()", |
| 21 | + }); |
| 22 | + const unmodeledMethod = createMethod({ |
| 23 | + signature: "org.sql2o.Sql2o#open4()", |
| 24 | + }); |
| 25 | + const savedAutoModelPrediction = createMethod({ |
| 26 | + signature: "org.sql2o.Sql2o#open5()", |
| 27 | + }); |
| 28 | + const savedManualModel = createMethod({ |
| 29 | + signature: "org.sql2o.Sql2o#open6()", |
| 30 | + }); |
| 31 | + |
| 32 | + const methods: Method[] = shuffle([ |
| 33 | + unsavedPositiveAutoModelPrediction, |
| 34 | + negativeAutoModelPrediction, |
| 35 | + unsavedManualModel, |
| 36 | + unmodeledMethod, |
| 37 | + savedAutoModelPrediction, |
| 38 | + savedManualModel, |
| 39 | + ]); |
| 40 | + |
| 41 | + const modeledMethodsMap: Record<string, readonly ModeledMethod[]> = {}; |
| 42 | + modeledMethodsMap[unsavedPositiveAutoModelPrediction.signature] = [ |
| 43 | + createSinkModeledMethod(), |
| 44 | + ]; |
| 45 | + modeledMethodsMap[unsavedManualModel.signature] = [ |
| 46 | + createSinkModeledMethod(), |
| 47 | + ]; |
| 48 | + modeledMethodsMap[savedAutoModelPrediction.signature] = [ |
| 49 | + createSinkModeledMethod(), |
| 50 | + ]; |
| 51 | + modeledMethodsMap[savedManualModel.signature] = [createSinkModeledMethod()]; |
| 52 | + |
| 53 | + const modifiedSignatures: Set<string> = new Set([ |
| 54 | + unsavedPositiveAutoModelPrediction.signature, |
| 55 | + unsavedManualModel.signature, |
| 56 | + ]); |
| 57 | + |
| 58 | + const processedByAutoModelMethods: Set<string> = new Set([ |
| 59 | + unsavedPositiveAutoModelPrediction.signature, |
| 60 | + negativeAutoModelPrediction.signature, |
| 61 | + savedAutoModelPrediction.signature, |
| 62 | + ]); |
| 63 | + |
| 64 | + expect( |
| 65 | + sortMethods( |
| 66 | + methods, |
| 67 | + modeledMethodsMap, |
| 68 | + modifiedSignatures, |
| 69 | + processedByAutoModelMethods, |
| 70 | + ), |
| 71 | + ).toEqual([ |
| 72 | + unsavedPositiveAutoModelPrediction, |
| 73 | + negativeAutoModelPrediction, |
| 74 | + unsavedManualModel, |
| 75 | + unmodeledMethod, |
| 76 | + savedAutoModelPrediction, |
| 77 | + savedManualModel, |
| 78 | + ]); |
| 79 | + }); |
| 80 | + |
| 81 | + it("uses secondary sort order based on usages and signature", () => { |
| 82 | + const negativeAutoModelPrediction = createMethod({ |
| 83 | + signature: "org.sql2o.Sql2o#negative()", |
| 84 | + usages: [], |
| 85 | + }); |
| 86 | + |
| 87 | + const unmodeledMethodWithTwoUsages = createMethod({ |
| 88 | + signature: "org.sql2o.Sql2o#two()", |
| 89 | + usages: [createUsage(), createUsage()], |
| 90 | + }); |
| 91 | + const unmodeledMethodWithOneUsage = createMethod({ |
| 92 | + signature: "org.sql2o.Sql2o#one()", |
| 93 | + usages: [createUsage()], |
| 94 | + }); |
| 95 | + |
| 96 | + const unmodeledMethodWithEarlierSignature = createMethod({ |
| 97 | + signature: "org.sql2o.Sql2o#aaa()", |
| 98 | + usages: [], |
| 99 | + }); |
| 100 | + const unmodeledMethodWithLaterSignature = createMethod({ |
| 101 | + signature: "org.sql2o.Sql2o#bbb()", |
| 102 | + usages: [], |
| 103 | + }); |
| 104 | + |
| 105 | + const methods: Method[] = shuffle([ |
| 106 | + negativeAutoModelPrediction, |
| 107 | + unmodeledMethodWithTwoUsages, |
| 108 | + unmodeledMethodWithOneUsage, |
| 109 | + unmodeledMethodWithEarlierSignature, |
| 110 | + unmodeledMethodWithLaterSignature, |
| 111 | + ]); |
| 112 | + |
| 113 | + const modeledMethodsMap: Record<string, readonly ModeledMethod[]> = {}; |
| 114 | + |
| 115 | + const modifiedSignatures: Set<string> = new Set([]); |
| 116 | + |
| 117 | + const processedByAutoModelMethods: Set<string> = new Set([ |
| 118 | + negativeAutoModelPrediction.signature, |
| 119 | + ]); |
| 120 | + |
| 121 | + expect( |
| 122 | + sortMethods( |
| 123 | + methods, |
| 124 | + modeledMethodsMap, |
| 125 | + modifiedSignatures, |
| 126 | + processedByAutoModelMethods, |
| 127 | + ), |
| 128 | + ).toEqual([ |
| 129 | + negativeAutoModelPrediction, |
| 130 | + unmodeledMethodWithTwoUsages, |
| 131 | + unmodeledMethodWithOneUsage, |
| 132 | + unmodeledMethodWithEarlierSignature, |
| 133 | + unmodeledMethodWithLaterSignature, |
| 134 | + ]); |
| 135 | + }); |
| 136 | +}); |
0 commit comments