|
| 1 | +/*! |
| 2 | + * Copyright (c) Microsoft Corporation and contributors. All rights reserved. |
| 3 | + * Licensed under the MIT License. |
| 4 | + */ |
| 5 | + |
| 6 | +import { strict as assert } from "node:assert"; |
| 7 | + |
| 8 | +import { |
| 9 | + checkLayerCompatibility, |
| 10 | + type ILayerCompatDetails, |
| 11 | + type ILayerCompatSupportRequirements, |
| 12 | + type LayerCompatCheckResult, |
| 13 | +} from "../../layerCompat.js"; |
| 14 | + |
| 15 | +const pkgVersion = "1.0.0"; |
| 16 | + |
| 17 | +describe("checkLayerCompatibility", () => { |
| 18 | + it("should return not compatible when other layer doesn't support ILayerCompatDetails", () => { |
| 19 | + const compatSupportRequirementsLayer1: ILayerCompatSupportRequirements = { |
| 20 | + requiredFeatures: ["feature1", "feature2"], |
| 21 | + minSupportedGeneration: 1, |
| 22 | + }; |
| 23 | + |
| 24 | + const result: LayerCompatCheckResult = checkLayerCompatibility( |
| 25 | + compatSupportRequirementsLayer1, |
| 26 | + undefined /* compatDetailsLayer2 */, |
| 27 | + ); |
| 28 | + const expectedResults: LayerCompatCheckResult = { |
| 29 | + isCompatible: false, |
| 30 | + isGenerationCompatible: false, |
| 31 | + unsupportedFeatures: compatSupportRequirementsLayer1.requiredFeatures, |
| 32 | + }; |
| 33 | + assert.deepStrictEqual(result, expectedResults, "Layers should not be compatible"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should return compatible when other layer doesn't support ILayerCompatDetails (back compat)", () => { |
| 37 | + // For backwards compatibility, the minSupportedGeneration is 0 and there are no required features. |
| 38 | + const compatSupportRequirementsLayer1: ILayerCompatSupportRequirements = { |
| 39 | + requiredFeatures: [], |
| 40 | + minSupportedGeneration: 0, |
| 41 | + }; |
| 42 | + |
| 43 | + const result: LayerCompatCheckResult = checkLayerCompatibility( |
| 44 | + compatSupportRequirementsLayer1, |
| 45 | + undefined /* compatDetailsLayer2 */, |
| 46 | + ); |
| 47 | + const expectedResults: LayerCompatCheckResult = { |
| 48 | + isCompatible: true, |
| 49 | + }; |
| 50 | + assert.deepStrictEqual(result, expectedResults, "Layers should be compatible"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should return compatible when both generation and features are compatible", () => { |
| 54 | + const compatSupportRequirementsLayer1: ILayerCompatSupportRequirements = { |
| 55 | + requiredFeatures: ["feature1", "feature2"], |
| 56 | + minSupportedGeneration: 1, |
| 57 | + }; |
| 58 | + |
| 59 | + const compatDetailsLayer2: ILayerCompatDetails = { |
| 60 | + pkgVersion, |
| 61 | + generation: 1, |
| 62 | + supportedFeatures: new Set(["feature1", "feature2", "feature3"]), |
| 63 | + }; |
| 64 | + const result: LayerCompatCheckResult = checkLayerCompatibility( |
| 65 | + compatSupportRequirementsLayer1, |
| 66 | + compatDetailsLayer2, |
| 67 | + ); |
| 68 | + const expectedResults: LayerCompatCheckResult = { |
| 69 | + isCompatible: true, |
| 70 | + }; |
| 71 | + assert.deepStrictEqual(result, expectedResults, "Layers should be compatible"); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should return not compatible when generation is incompatible", () => { |
| 75 | + const compatSupportRequirementsLayer1: ILayerCompatSupportRequirements = { |
| 76 | + requiredFeatures: ["feature1", "feature2"], |
| 77 | + minSupportedGeneration: 2, |
| 78 | + }; |
| 79 | + // Layer 2 has lower generation (1) than the minimum supported generation of Layer 1 (2). |
| 80 | + const compatDetailsLayer2: ILayerCompatDetails = { |
| 81 | + pkgVersion, |
| 82 | + generation: 1, |
| 83 | + supportedFeatures: new Set(["feature1", "feature2"]), |
| 84 | + }; |
| 85 | + |
| 86 | + const result: LayerCompatCheckResult = checkLayerCompatibility( |
| 87 | + compatSupportRequirementsLayer1, |
| 88 | + compatDetailsLayer2, |
| 89 | + ); |
| 90 | + const expectedResults: LayerCompatCheckResult = { |
| 91 | + isCompatible: false, |
| 92 | + isGenerationCompatible: false, |
| 93 | + unsupportedFeatures: undefined, |
| 94 | + }; |
| 95 | + |
| 96 | + assert.deepStrictEqual( |
| 97 | + result, |
| 98 | + expectedResults, |
| 99 | + "Layers should not be compatible because generation is not compatible", |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should return not compatible when features are incompatible", () => { |
| 104 | + const compatSupportRequirementsLayer1: ILayerCompatSupportRequirements = { |
| 105 | + requiredFeatures: ["feature1", "feature2"], |
| 106 | + minSupportedGeneration: 1, |
| 107 | + }; |
| 108 | + // Layer 2 doesn't support feature2. |
| 109 | + const compatDetailsLayer2: ILayerCompatDetails = { |
| 110 | + pkgVersion, |
| 111 | + generation: 1, |
| 112 | + supportedFeatures: new Set(["feature1", "feature3"]), |
| 113 | + }; |
| 114 | + |
| 115 | + const result: LayerCompatCheckResult = checkLayerCompatibility( |
| 116 | + compatSupportRequirementsLayer1, |
| 117 | + compatDetailsLayer2, |
| 118 | + ); |
| 119 | + const expectedResults: LayerCompatCheckResult = { |
| 120 | + isCompatible: false, |
| 121 | + isGenerationCompatible: true, |
| 122 | + unsupportedFeatures: ["feature2"], |
| 123 | + }; |
| 124 | + |
| 125 | + assert.deepStrictEqual( |
| 126 | + result, |
| 127 | + expectedResults, |
| 128 | + "Layers should not be compatible because required features are not supported", |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it("should return not compatible when both generation and features are incompatible", () => { |
| 133 | + const compatSupportRequirementsLayer1: ILayerCompatSupportRequirements = { |
| 134 | + requiredFeatures: ["feature1", "feature2"], |
| 135 | + minSupportedGeneration: 2, |
| 136 | + }; |
| 137 | + // Layer 2 doesn't support feature1 or feature2. |
| 138 | + const compatDetailsLayer2: ILayerCompatDetails = { |
| 139 | + pkgVersion, |
| 140 | + generation: 1, |
| 141 | + supportedFeatures: new Set(["feature3"]), |
| 142 | + }; |
| 143 | + |
| 144 | + const result: LayerCompatCheckResult = checkLayerCompatibility( |
| 145 | + compatSupportRequirementsLayer1, |
| 146 | + compatDetailsLayer2, |
| 147 | + ); |
| 148 | + const expectedResults: LayerCompatCheckResult = { |
| 149 | + isCompatible: false, |
| 150 | + isGenerationCompatible: false, |
| 151 | + unsupportedFeatures: compatSupportRequirementsLayer1.requiredFeatures, |
| 152 | + }; |
| 153 | + |
| 154 | + assert.deepStrictEqual( |
| 155 | + result, |
| 156 | + expectedResults, |
| 157 | + "Layers should not be compatible because no required features are supported", |
| 158 | + ); |
| 159 | + }); |
| 160 | +}); |
0 commit comments