|
18 | 18 | package json |
19 | 19 |
|
20 | 20 | import ( |
| 21 | + "encoding/json" |
21 | 22 | "testing" |
22 | 23 |
|
23 | 24 | "github.com/onflow/flow-go-sdk" |
@@ -255,3 +256,136 @@ func Test_SerializeConfigToJsonEmulatorNotDefault(t *testing.T) { |
255 | 256 | assert.JSONEq(t, string(configJson), string(conf)) |
256 | 257 |
|
257 | 258 | } |
| 259 | + |
| 260 | +// Test_RoundTripPreservesKeyOrder verifies that loading a flow.json and then |
| 261 | +// re-serializing it preserves the user's chosen ordering of keys within each |
| 262 | +// section, including nested deployments. This was the motivating bug for the |
| 263 | +// orderedMap data structure. |
| 264 | +func Test_RoundTripPreservesKeyOrder(t *testing.T) { |
| 265 | + input := []byte(`{ |
| 266 | + "contracts": { |
| 267 | + "ZetaContract": "./z.cdc", |
| 268 | + "AlphaContract": "./a.cdc", |
| 269 | + "MuContract": "./m.cdc" |
| 270 | + }, |
| 271 | + "dependencies": { |
| 272 | + "Burner": "testnet://9a0766d93b6608b7.Burner", |
| 273 | + "AAA": "testnet://9a0766d93b6608b7.AAA" |
| 274 | + }, |
| 275 | + "networks": { |
| 276 | + "mainnet": "access.mainnet.nodes.onflow.org:9000", |
| 277 | + "testnet": "access.testnet.nodes.onflow.org:9000", |
| 278 | + "emulator": "127.0.0.1:3569" |
| 279 | + }, |
| 280 | + "accounts": { |
| 281 | + "zoo-account": { |
| 282 | + "address": "f8d6e0586b0a20c7", |
| 283 | + "key": "11c5dfdeb0ff03a7a73ef39788563b62c89adea67bbb21ab95e5f710bd1d40b7" |
| 284 | + }, |
| 285 | + "alpha-account": { |
| 286 | + "address": "f8d6e0586b0a20c7", |
| 287 | + "key": "11c5dfdeb0ff03a7a73ef39788563b62c89adea67bbb21ab95e5f710bd1d40b7" |
| 288 | + } |
| 289 | + }, |
| 290 | + "deployments": { |
| 291 | + "testnet": { |
| 292 | + "zoo-account": ["ZetaContract"] |
| 293 | + }, |
| 294 | + "emulator": { |
| 295 | + "alpha-account": ["MuContract", "AlphaContract"] |
| 296 | + } |
| 297 | + } |
| 298 | +}`) |
| 299 | + |
| 300 | + parser := NewParser() |
| 301 | + conf, err := parser.Deserialize(input) |
| 302 | + assert.NoError(t, err) |
| 303 | + |
| 304 | + out, err := parser.Serialize(conf) |
| 305 | + assert.NoError(t, err) |
| 306 | + |
| 307 | + // Re-parse into the JSON layer directly to compare structural ordering |
| 308 | + // without depending on whitespace. |
| 309 | + var original, roundTripped jsonConfig |
| 310 | + assert.NoError(t, json.Unmarshal(input, &original)) |
| 311 | + assert.NoError(t, json.Unmarshal(out, &roundTripped)) |
| 312 | + |
| 313 | + assert.Equal(t, |
| 314 | + sectionKeys(original.Contracts.orderedMap), |
| 315 | + sectionKeys(roundTripped.Contracts.orderedMap), |
| 316 | + "contracts order changed") |
| 317 | + assert.Equal(t, |
| 318 | + sectionKeys(original.Dependencies.orderedMap), |
| 319 | + sectionKeys(roundTripped.Dependencies.orderedMap), |
| 320 | + "dependencies order changed") |
| 321 | + assert.Equal(t, |
| 322 | + sectionKeys(original.Networks.orderedMap), |
| 323 | + sectionKeys(roundTripped.Networks.orderedMap), |
| 324 | + "networks order changed") |
| 325 | + assert.Equal(t, |
| 326 | + sectionKeys(original.Accounts.orderedMap), |
| 327 | + sectionKeys(roundTripped.Accounts.orderedMap), |
| 328 | + "accounts order changed") |
| 329 | + assert.Equal(t, |
| 330 | + sectionKeys(original.Deployments.orderedMap), |
| 331 | + sectionKeys(roundTripped.Deployments.orderedMap), |
| 332 | + "deployments order changed") |
| 333 | + |
| 334 | + // Verify inner deployment account ordering is also preserved (this is the |
| 335 | + // nested orderedMap[[]deployment] case). |
| 336 | + originalEmu, _ := original.Deployments.Get("emulator") |
| 337 | + roundTrippedEmu, _ := roundTripped.Deployments.Get("emulator") |
| 338 | + assert.Equal(t, |
| 339 | + sectionKeys(originalEmu.orderedMap), |
| 340 | + sectionKeys(roundTrippedEmu.orderedMap), |
| 341 | + "deployments.emulator account order changed") |
| 342 | +} |
| 343 | + |
| 344 | +// Test_NewKeysAppendAtEnd verifies that when a new entry is added programmatically |
| 345 | +// (e.g. a new dependency is fetched), it is appended at the end rather than |
| 346 | +// inserted alphabetically. |
| 347 | +func Test_NewKeysAppendAtEnd(t *testing.T) { |
| 348 | + input := []byte(`{ |
| 349 | + "contracts": { |
| 350 | + "ZetaContract": "./z.cdc", |
| 351 | + "AlphaContract": "./a.cdc" |
| 352 | + }, |
| 353 | + "networks": { |
| 354 | + "emulator": "127.0.0.1:3569" |
| 355 | + }, |
| 356 | + "accounts": { |
| 357 | + "emulator-account": { |
| 358 | + "address": "f8d6e0586b0a20c7", |
| 359 | + "key": "11c5dfdeb0ff03a7a73ef39788563b62c89adea67bbb21ab95e5f710bd1d40b7" |
| 360 | + } |
| 361 | + } |
| 362 | +}`) |
| 363 | + |
| 364 | + parser := NewParser() |
| 365 | + conf, err := parser.Deserialize(input) |
| 366 | + assert.NoError(t, err) |
| 367 | + |
| 368 | + conf.Contracts.AddOrUpdate(config.Contract{ |
| 369 | + Name: "MuContract", |
| 370 | + Location: "./m.cdc", |
| 371 | + }) |
| 372 | + |
| 373 | + out, err := parser.Serialize(conf) |
| 374 | + assert.NoError(t, err) |
| 375 | + |
| 376 | + var result jsonConfig |
| 377 | + assert.NoError(t, json.Unmarshal(out, &result)) |
| 378 | + |
| 379 | + assert.Equal(t, |
| 380 | + []string{"ZetaContract", "AlphaContract", "MuContract"}, |
| 381 | + sectionKeys(result.Contracts.orderedMap), |
| 382 | + "new contract should be appended at the end, not inserted alphabetically") |
| 383 | +} |
| 384 | + |
| 385 | +func sectionKeys[V any](m orderedMap[V]) []string { |
| 386 | + keys := make([]string, 0, m.Len()) |
| 387 | + for k := range m.All { |
| 388 | + keys = append(keys, k) |
| 389 | + } |
| 390 | + return keys |
| 391 | +} |
0 commit comments