|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + v1pb "buf.build/gen/go/bytebase/bytebase/protocolbuffers/go/v1" |
| 8 | +) |
| 9 | + |
| 10 | +func TestNormalizeObjectSchemaJSON_ProducesStableOutput(t *testing.T) { |
| 11 | + // Two inputs with different whitespace and key order should normalize |
| 12 | + // to the same canonical JSON. |
| 13 | + inputA := `{"type":"OBJECT","structKind":{"properties":{"email":{"type":"STRING","semanticType":"abc"}}}}` |
| 14 | + inputB := `{ |
| 15 | + "structKind": { |
| 16 | + "properties": { |
| 17 | + "email": { "semanticType": "abc", "type": "STRING" } |
| 18 | + } |
| 19 | + }, |
| 20 | + "type": "OBJECT" |
| 21 | +}` |
| 22 | + |
| 23 | + gotA, err := NormalizeObjectSchemaJSON(inputA) |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("normalize A: %v", err) |
| 26 | + } |
| 27 | + gotB, err := NormalizeObjectSchemaJSON(inputB) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("normalize B: %v", err) |
| 30 | + } |
| 31 | + if gotA != gotB { |
| 32 | + t.Errorf("canonical forms differ:\nA=%s\nB=%s", gotA, gotB) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestNormalizeObjectSchemaJSON_RejectsInvalidProto(t *testing.T) { |
| 37 | + _, err := NormalizeObjectSchemaJSON(`{"type":"NOT_A_REAL_TYPE"}`) |
| 38 | + if err == nil { |
| 39 | + t.Fatal("expected error for invalid enum value, got nil") |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestNormalizeObjectSchemaJSON_RejectsInvalidJSON(t *testing.T) { |
| 44 | + _, err := NormalizeObjectSchemaJSON(`not json at all`) |
| 45 | + if err == nil { |
| 46 | + t.Fatal("expected error for malformed JSON, got nil") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestNormalizeObjectSchemaJSON_EmptyString(t *testing.T) { |
| 51 | + got, err := NormalizeObjectSchemaJSON("") |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("empty: %v", err) |
| 54 | + } |
| 55 | + if got != "" { |
| 56 | + t.Errorf("expected empty output for empty input, got %q", got) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestParseObjectSchemaJSON_RoundTripsThroughProto(t *testing.T) { |
| 61 | + input := `{"type":"OBJECT","structKind":{"properties":{"x":{"type":"STRING","semanticType":"s"}}}}` |
| 62 | + schema, err := ParseObjectSchemaJSON(input) |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("parse: %v", err) |
| 65 | + } |
| 66 | + if schema.GetType() != v1pb.ObjectSchema_OBJECT { |
| 67 | + t.Errorf("expected OBJECT, got %v", schema.GetType()) |
| 68 | + } |
| 69 | + props := schema.GetStructKind().GetProperties() |
| 70 | + if props["x"].GetSemanticType() != "s" { |
| 71 | + t.Errorf("expected semanticType s, got %q", props["x"].GetSemanticType()) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestMarshalObjectSchemaToJSON_DeterministicOrder(t *testing.T) { |
| 76 | + // Build a proto with multiple map keys and verify marshal is |
| 77 | + // byte-identical across calls AND that keys are sorted. |
| 78 | + schema := &v1pb.ObjectSchema{ |
| 79 | + Type: v1pb.ObjectSchema_OBJECT, |
| 80 | + Kind: &v1pb.ObjectSchema_StructKind_{ |
| 81 | + StructKind: &v1pb.ObjectSchema_StructKind{ |
| 82 | + Properties: map[string]*v1pb.ObjectSchema{ |
| 83 | + "zeta": {Type: v1pb.ObjectSchema_STRING, SemanticType: "z"}, |
| 84 | + "alpha": {Type: v1pb.ObjectSchema_STRING, SemanticType: "a"}, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | + a, err := MarshalObjectSchemaToJSON(schema) |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("marshal a: %v", err) |
| 92 | + } |
| 93 | + b, err := MarshalObjectSchemaToJSON(schema) |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("marshal b: %v", err) |
| 96 | + } |
| 97 | + if a != b { |
| 98 | + t.Errorf("marshal not deterministic: %q vs %q", a, b) |
| 99 | + } |
| 100 | + // Alpha must appear before zeta since we sort map keys. |
| 101 | + if strings.Index(a, "alpha") > strings.Index(a, "zeta") { |
| 102 | + t.Errorf("expected sorted map keys in output, got %s", a) |
| 103 | + } |
| 104 | +} |
0 commit comments