|
| 1 | +/** |
| 2 | + * Config Merging Behavior Tests for JSON-LD Builder |
| 3 | + * |
| 4 | + * Tests the new consistent merging behavior and clear methods |
| 5 | + */ |
| 6 | + |
| 7 | +import { createJsonLdConfig } from '../../src'; |
| 8 | + |
| 9 | +describe('Config Merging Behavior', () => { |
| 10 | + describe('Default Merging', () => { |
| 11 | + test('includeIds merges by default', () => { |
| 12 | + const config = createJsonLdConfig().includeIds(['a', 'b']).includeIds(['c', 'd']).getConfig(); |
| 13 | + |
| 14 | + expect(config.filters?.includeIds).toEqual(['a', 'b', 'c', 'd']); |
| 15 | + }); |
| 16 | + |
| 17 | + test('excludeIds merges by default', () => { |
| 18 | + const config = createJsonLdConfig().excludeIds(['a', 'b']).excludeIds(['c', 'd']).getConfig(); |
| 19 | + |
| 20 | + expect(config.filters?.excludeIds).toEqual(['a', 'b', 'c', 'd']); |
| 21 | + }); |
| 22 | + |
| 23 | + test('includeTypes merges by default', () => { |
| 24 | + const config = createJsonLdConfig() |
| 25 | + .includeTypes(['Person', 'Organization']) |
| 26 | + .includeTypes(['Article', 'WebSite']) |
| 27 | + .getConfig(); |
| 28 | + |
| 29 | + expect(config.filters?.includeTypes).toEqual([ |
| 30 | + 'Person', |
| 31 | + 'Organization', |
| 32 | + 'Article', |
| 33 | + 'WebSite', |
| 34 | + ]); |
| 35 | + }); |
| 36 | + |
| 37 | + test('excludeTypes merges by default', () => { |
| 38 | + const config = createJsonLdConfig() |
| 39 | + .excludeTypes(['ImageObject']) |
| 40 | + .excludeTypes(['VideoObject']) |
| 41 | + .getConfig(); |
| 42 | + |
| 43 | + expect(config.filters?.excludeTypes).toEqual(['ImageObject', 'VideoObject']); |
| 44 | + }); |
| 45 | + |
| 46 | + test('requiredProperties merges by default', () => { |
| 47 | + const config = createJsonLdConfig() |
| 48 | + .requiredProperties(['name']) |
| 49 | + .requiredProperties(['url']) |
| 50 | + .getConfig(); |
| 51 | + |
| 52 | + expect(config.filters?.requiredProperties).toEqual(['name', 'url']); |
| 53 | + }); |
| 54 | + |
| 55 | + test('excludeEntitiesWithProperties merges by default', () => { |
| 56 | + const config = createJsonLdConfig() |
| 57 | + .excludeEntitiesWithProperties(['internal']) |
| 58 | + .excludeEntitiesWithProperties(['draft']) |
| 59 | + .getConfig(); |
| 60 | + |
| 61 | + expect(config.filters?.excludeEntitiesWithProperties).toEqual(['internal', 'draft']); |
| 62 | + }); |
| 63 | + |
| 64 | + test('multiple calls with empty arrays still work', () => { |
| 65 | + const config = createJsonLdConfig() |
| 66 | + .includeIds(['a']) |
| 67 | + .includeIds([]) |
| 68 | + .includeIds(['b']) |
| 69 | + .getConfig(); |
| 70 | + |
| 71 | + expect(config.filters?.includeIds).toEqual(['a', 'b']); |
| 72 | + }); |
| 73 | + |
| 74 | + test('mixed configuration merging works correctly', () => { |
| 75 | + const config = createJsonLdConfig() |
| 76 | + .includeIds(['id1', 'id2']) |
| 77 | + .includeTypes(['Person']) |
| 78 | + .excludeIds(['exclude1']) |
| 79 | + .includeIds(['id3']) |
| 80 | + .excludeTypes(['ImageObject']) |
| 81 | + .getConfig(); |
| 82 | + |
| 83 | + expect(config.filters?.includeIds).toEqual(['id1', 'id2', 'id3']); |
| 84 | + expect(config.filters?.includeTypes).toEqual(['Person']); |
| 85 | + expect(config.filters?.excludeIds).toEqual(['exclude1']); |
| 86 | + expect(config.filters?.excludeTypes).toEqual(['ImageObject']); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('Clear Methods', () => { |
| 91 | + test('clearIds clears both include and exclude IDs', () => { |
| 92 | + const config = createJsonLdConfig() |
| 93 | + .includeIds(['a', 'b']) |
| 94 | + .excludeIds(['c', 'd']) |
| 95 | + .clearIds() |
| 96 | + .getConfig(); |
| 97 | + |
| 98 | + expect(config.filters?.includeIds).toBeUndefined(); |
| 99 | + expect(config.filters?.excludeIds).toBeUndefined(); |
| 100 | + }); |
| 101 | + |
| 102 | + test('clearTypes clears both include and exclude types', () => { |
| 103 | + const config = createJsonLdConfig() |
| 104 | + .includeTypes(['Person']) |
| 105 | + .excludeTypes(['ImageObject']) |
| 106 | + .clearTypes() |
| 107 | + .getConfig(); |
| 108 | + |
| 109 | + expect(config.filters?.includeTypes).toBeUndefined(); |
| 110 | + expect(config.filters?.excludeTypes).toBeUndefined(); |
| 111 | + }); |
| 112 | + |
| 113 | + test('clearPropertyRequirements clears both property requirements', () => { |
| 114 | + const config = createJsonLdConfig() |
| 115 | + .requiredProperties(['name']) |
| 116 | + .excludeEntitiesWithProperties(['internal']) |
| 117 | + .clearPropertyRequirements() |
| 118 | + .getConfig(); |
| 119 | + |
| 120 | + expect(config.filters?.requiredProperties).toBeUndefined(); |
| 121 | + expect(config.filters?.excludeEntitiesWithProperties).toBeUndefined(); |
| 122 | + }); |
| 123 | + |
| 124 | + test('clearPropertyFilters clears all property filters', () => { |
| 125 | + const config = createJsonLdConfig() |
| 126 | + .filterPropertiesByIds(['org:1'], { exclude: ['internal'] }) |
| 127 | + .filterPropertiesByTypes(['Person'], { include: ['name'] }) |
| 128 | + .clearPropertyFilters() |
| 129 | + .getConfig(); |
| 130 | + |
| 131 | + expect(config.propertyFiltersByIds).toBeUndefined(); |
| 132 | + expect(config.propertyFiltersByTypes).toBeUndefined(); |
| 133 | + }); |
| 134 | + |
| 135 | + test('clearSubgraph clears subgraph roots', () => { |
| 136 | + const config = createJsonLdConfig().subgraph(['org:hyperweb']).clearSubgraph().getConfig(); |
| 137 | + |
| 138 | + expect(config.subgraphRoots).toBeUndefined(); |
| 139 | + }); |
| 140 | + |
| 141 | + test('clearAll clears everything except baseGraph', () => { |
| 142 | + const graph = [{ '@id': 'test', '@type': 'Thing' }]; |
| 143 | + const config = createJsonLdConfig() |
| 144 | + .baseGraph(graph) |
| 145 | + .includeIds(['a']) |
| 146 | + .excludeTypes(['ImageObject']) |
| 147 | + .addEntities([{ '@id': 'extra', '@type': 'Thing' }]) |
| 148 | + .clearAll() |
| 149 | + .getConfig(); |
| 150 | + |
| 151 | + expect(config.baseGraph).toBe(graph); |
| 152 | + expect(config.filters).toBeUndefined(); |
| 153 | + expect(config.additionalEntities).toBeUndefined(); |
| 154 | + }); |
| 155 | + |
| 156 | + test('clear methods are selective - only clear specified config', () => { |
| 157 | + const config = createJsonLdConfig() |
| 158 | + .includeIds(['keep-these']) |
| 159 | + .includeTypes(['Person']) |
| 160 | + .requiredProperties(['name']) |
| 161 | + .clearTypes() |
| 162 | + .getConfig(); |
| 163 | + |
| 164 | + // Types should be cleared |
| 165 | + expect(config.filters?.includeTypes).toBeUndefined(); |
| 166 | + expect(config.filters?.excludeTypes).toBeUndefined(); |
| 167 | + |
| 168 | + // Other config should remain |
| 169 | + expect(config.filters?.includeIds).toEqual(['keep-these']); |
| 170 | + expect(config.filters?.requiredProperties).toEqual(['name']); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe('Clear and Rebuild Patterns', () => { |
| 175 | + test('clear then add creates fresh configuration', () => { |
| 176 | + const config = createJsonLdConfig() |
| 177 | + .includeIds(['old1', 'old2']) |
| 178 | + .clearIds() |
| 179 | + .includeIds(['new1', 'new2']) |
| 180 | + .getConfig(); |
| 181 | + |
| 182 | + expect(config.filters?.includeIds).toEqual(['new1', 'new2']); |
| 183 | + }); |
| 184 | + |
| 185 | + test('chaining clear methods works correctly', () => { |
| 186 | + const config = createJsonLdConfig() |
| 187 | + .includeIds(['a']) |
| 188 | + .excludeIds(['b']) |
| 189 | + .includeTypes(['Person']) |
| 190 | + .excludeTypes(['ImageObject']) |
| 191 | + .clearIds() |
| 192 | + .clearTypes() |
| 193 | + .includeIds(['new']) |
| 194 | + .includeTypes(['Article']) |
| 195 | + .getConfig(); |
| 196 | + |
| 197 | + expect(config.filters?.includeIds).toEqual(['new']); |
| 198 | + expect(config.filters?.excludeIds).toBeUndefined(); |
| 199 | + expect(config.filters?.includeTypes).toEqual(['Article']); |
| 200 | + expect(config.filters?.excludeTypes).toBeUndefined(); |
| 201 | + }); |
| 202 | + |
| 203 | + test('clear in middle of chain works correctly', () => { |
| 204 | + const config = createJsonLdConfig() |
| 205 | + .includeIds(['first']) |
| 206 | + .includeTypes(['Person']) |
| 207 | + .clearIds() |
| 208 | + .includeIds(['second']) |
| 209 | + .includeTypes(['Organization']) |
| 210 | + .getConfig(); |
| 211 | + |
| 212 | + expect(config.filters?.includeIds).toEqual(['second']); |
| 213 | + expect(config.filters?.includeTypes).toEqual(['Person', 'Organization']); |
| 214 | + }); |
| 215 | + }); |
| 216 | + |
| 217 | + describe('Immutability', () => { |
| 218 | + test('clear methods return new instances', () => { |
| 219 | + const original = createJsonLdConfig().includeIds(['a', 'b']); |
| 220 | + const cleared = original.clearIds(); |
| 221 | + |
| 222 | + expect(original).not.toBe(cleared); |
| 223 | + expect(original.getConfig().filters?.includeIds).toEqual(['a', 'b']); |
| 224 | + expect(cleared.getConfig().filters?.includeIds).toBeUndefined(); |
| 225 | + }); |
| 226 | + |
| 227 | + test('merging methods return new instances', () => { |
| 228 | + const original = createJsonLdConfig().includeIds(['a', 'b']); |
| 229 | + const extended = original.includeIds(['c', 'd']); |
| 230 | + |
| 231 | + expect(original).not.toBe(extended); |
| 232 | + expect(original.getConfig().filters?.includeIds).toEqual(['a', 'b']); |
| 233 | + expect(extended.getConfig().filters?.includeIds).toEqual(['a', 'b', 'c', 'd']); |
| 234 | + }); |
| 235 | + }); |
| 236 | + |
| 237 | + describe('Edge Cases', () => { |
| 238 | + test('clearing empty configuration works', () => { |
| 239 | + const config = createJsonLdConfig().clearIds().clearTypes().clearAll().getConfig(); |
| 240 | + |
| 241 | + expect(config).toEqual({}); |
| 242 | + }); |
| 243 | + |
| 244 | + test('duplicate values in merging are preserved', () => { |
| 245 | + const config = createJsonLdConfig().includeIds(['a', 'b']).includeIds(['b', 'c']).getConfig(); |
| 246 | + |
| 247 | + expect(config.filters?.includeIds).toEqual(['a', 'b', 'b', 'c']); |
| 248 | + }); |
| 249 | + |
| 250 | + test('undefined and empty array handling', () => { |
| 251 | + const config = createJsonLdConfig() |
| 252 | + .includeIds([]) |
| 253 | + .includeIds(['a']) |
| 254 | + .includeIds([]) |
| 255 | + .getConfig(); |
| 256 | + |
| 257 | + expect(config.filters?.includeIds).toEqual(['a']); |
| 258 | + }); |
| 259 | + }); |
| 260 | +}); |
0 commit comments