|
1 | 1 | package param_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "encoding/json" |
| 6 | + "reflect" |
5 | 7 | "testing" |
6 | 8 | "time" |
7 | 9 |
|
| 10 | + shimjson "github.com/browserbase/stagehand-go/v3/internal/encoding/json" |
8 | 11 | "github.com/browserbase/stagehand-go/v3/packages/param" |
9 | 12 | ) |
10 | 13 |
|
@@ -375,3 +378,176 @@ func TestNullStructUnion(t *testing.T) { |
375 | 378 | t.Fatalf("expected null, received %s", string(b)) |
376 | 379 | } |
377 | 380 | } |
| 381 | + |
| 382 | +// |
| 383 | +// Compaction optimization |
| 384 | +// |
| 385 | + |
| 386 | +type NonCompactedDoubleParent struct { |
| 387 | + Prop string `json:"prop"` |
| 388 | + Parent NonCompactedParent `json:"parent"` |
| 389 | + |
| 390 | + param.APIObject |
| 391 | +} |
| 392 | + |
| 393 | +type NonCompactedParent struct { |
| 394 | + BadChild NonCompacted `json:"bad_child"` |
| 395 | + |
| 396 | + param.APIObject |
| 397 | +} |
| 398 | + |
| 399 | +type NonCompacted struct { |
| 400 | + Raw string |
| 401 | + |
| 402 | + param.APIObject |
| 403 | +} |
| 404 | + |
| 405 | +func (a NonCompactedDoubleParent) MarshalJSON() ([]byte, error) { |
| 406 | + type shadow NonCompactedDoubleParent |
| 407 | + return param.MarshalObject(a, (*shadow)(&a)) |
| 408 | +} |
| 409 | + |
| 410 | +func (a NonCompactedParent) MarshalJSON() ([]byte, error) { |
| 411 | + type shadow NonCompactedParent |
| 412 | + return param.MarshalObject(a, (*shadow)(&a)) |
| 413 | +} |
| 414 | + |
| 415 | +func (a NonCompacted) MarshalJSON() ([]byte, error) { |
| 416 | + if a.Raw == "" { |
| 417 | + a.Raw = nonCompactedRaw |
| 418 | + } |
| 419 | + return []byte(a.Raw), nil |
| 420 | +} |
| 421 | + |
| 422 | +var nonCompactedRaw string = ` { "foo": "bar" } ` |
| 423 | + |
| 424 | +func TestAppendCompactBroken(t *testing.T) { |
| 425 | + tests := map[string]struct { |
| 426 | + value json.Marshaler |
| 427 | + }{ |
| 428 | + "red/illegal-json": { |
| 429 | + NonCompacted{Raw: `{ "broken": "json" `}, |
| 430 | + }, |
| 431 | + "red/nested-with-illegal-json": { |
| 432 | + NonCompactedParent{BadChild: NonCompacted{ |
| 433 | + Raw: `{ "broken": "json" `, |
| 434 | + }}, |
| 435 | + }, |
| 436 | + } |
| 437 | + |
| 438 | + for name, test := range tests { |
| 439 | + t.Run(name, func(t *testing.T) { |
| 440 | + v, err := json.Marshal(test.value) |
| 441 | + if err == nil { |
| 442 | + t.Fatal("expected error got", v) |
| 443 | + } |
| 444 | + }) |
| 445 | + } |
| 446 | +} |
| 447 | + |
| 448 | +// TestAppendCompact validates an optimization for internal SDK types to |
| 449 | +// avoid O(keys^2) iteration over each JSON object. |
| 450 | +// |
| 451 | +// It's possible to intentionally trigger this behavior as both a user and |
| 452 | +// SDK developer. However, the edge case is quite pathological and requires |
| 453 | +// calling [json.Marshaler.MarshalJSON] rather than [json.Marshal]. |
| 454 | +func TestAppendCompact(t *testing.T) { |
| 455 | + |
| 456 | + tests := map[string]struct { |
| 457 | + value json.Marshaler |
| 458 | + expected string |
| 459 | + }{ |
| 460 | + // |
| 461 | + // Non-compacted cases |
| 462 | + // |
| 463 | + // Note this is how to exploit the compacter to fail, you must call [json.Marshaler.MarshalJSON] rather than [json.Marshal]. |
| 464 | + // The type must also embed [param.APIObject] and return non-compacted JSON. |
| 465 | + // |
| 466 | + |
| 467 | + "no-compact/fails-compaction": { |
| 468 | + NonCompacted{Raw: nonCompactedRaw}, |
| 469 | + nonCompactedRaw, |
| 470 | + }, |
| 471 | + "no-compact/nested-with-bad-child": { |
| 472 | + NonCompactedParent{BadChild: NonCompacted{ |
| 473 | + Raw: nonCompactedRaw, |
| 474 | + }}, |
| 475 | + `{"bad_child":` + nonCompactedRaw + `}`, |
| 476 | + }, |
| 477 | + "no-compact/double-nested-with-bad-child": { |
| 478 | + NonCompactedDoubleParent{Prop: "1", Parent: NonCompactedParent{BadChild: NonCompacted{ |
| 479 | + Raw: nonCompactedRaw, |
| 480 | + }}}, |
| 481 | + `{"prop":"1","parent":{"bad_child":` + nonCompactedRaw + `}}`, |
| 482 | + }, |
| 483 | + |
| 484 | + // |
| 485 | + // Compacted cases |
| 486 | + // |
| 487 | + |
| 488 | + "override/spaces-within": { |
| 489 | + param.Override[NonCompactedDoubleParent](json.RawMessage(`{"com": "pact"}`)), |
| 490 | + `{"com":"pact"}`, |
| 491 | + }, |
| 492 | + "override/spaces-after": { |
| 493 | + param.Override[NonCompactedDoubleParent](json.RawMessage(`{"com":"pact"} `)), |
| 494 | + `{"com":"pact"}`, |
| 495 | + }, |
| 496 | + "override/spaces-before": { |
| 497 | + param.Override[NonCompactedDoubleParent](json.RawMessage(` {"com":"pact"}`)), |
| 498 | + `{"com":"pact"}`, |
| 499 | + }, |
| 500 | + "override/spaces-around": { |
| 501 | + param.Override[NonCompactedDoubleParent](json.RawMessage(` { "com": "pact" }`)), |
| 502 | + `{"com":"pact"}`, |
| 503 | + }, |
| 504 | + "override/override-with-nested": { |
| 505 | + param.Override[NonCompactedDoubleParent](NonCompactedParent{}), |
| 506 | + `{"bad_child":{"foo":"bar"}}`, |
| 507 | + }, |
| 508 | + "override/override-with-non-compacted": { |
| 509 | + param.Override[NonCompactedDoubleParent](NonCompacted{}), |
| 510 | + `{"foo":"bar"}`, |
| 511 | + }, |
| 512 | + } |
| 513 | + |
| 514 | + for name, test := range tests { |
| 515 | + t.Run(name+"/marshal-json", func(t *testing.T) { |
| 516 | + b, err := test.value.MarshalJSON() |
| 517 | + if err != nil { |
| 518 | + t.Fatalf("didn't expect error %v, expected %s", err, test.expected) |
| 519 | + } |
| 520 | + if string(b) != test.expected { |
| 521 | + t.Fatalf("expected %s (%s), received %s", test.expected, reflect.TypeOf(test.value), string(b)) |
| 522 | + } |
| 523 | + }) |
| 524 | + |
| 525 | + t.Run(name+"/json-marshal", func(t *testing.T) { |
| 526 | + b, err := json.Marshal(test.value) |
| 527 | + if err != nil { |
| 528 | + t.Fatalf("didn't expect error %v, expected %s", err, test.expected) |
| 529 | + } |
| 530 | + |
| 531 | + // expected output of JSON Marshal should always be compacted |
| 532 | + var compactedExpected bytes.Buffer |
| 533 | + err = json.Compact(&compactedExpected, []byte(test.expected)) |
| 534 | + if err != nil { |
| 535 | + t.Fatalf("didn't expect error %v, expected %s", err, test.expected) |
| 536 | + } |
| 537 | + |
| 538 | + if string(b) != compactedExpected.String() { |
| 539 | + t.Fatalf("expected %s (%s), received %s", test.expected, reflect.TypeOf(test.value), string(b)) |
| 540 | + } |
| 541 | + }) |
| 542 | + |
| 543 | + t.Run(name+"/shimjson-marshal", func(t *testing.T) { |
| 544 | + b, err := shimjson.Marshal(test.value) |
| 545 | + if err != nil { |
| 546 | + t.Fatalf("didn't expect error %v, expected %s", err, test.expected) |
| 547 | + } |
| 548 | + if string(b) != test.expected { |
| 549 | + t.Logf("expected %s (%s), received %s", test.expected, reflect.TypeOf(test.value), string(b)) |
| 550 | + } |
| 551 | + }) |
| 552 | + } |
| 553 | +} |
0 commit comments