|
| 1 | +package edtypes_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/aisa-it/aiplan/aiplan.go/internal/aiplan/editor/edtypes" |
| 8 | + _ "github.com/aisa-it/aiplan/aiplan.go/internal/aiplan/editor/tiptap" // Регистрация парсера |
| 9 | +) |
| 10 | + |
| 11 | +func TestDocument_UnmarshalJSON(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + json string |
| 15 | + wantElemCount int |
| 16 | + wantErr bool |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "simple paragraph", |
| 20 | + json: `{ |
| 21 | + "type": "doc", |
| 22 | + "content": [ |
| 23 | + { |
| 24 | + "type": "paragraph", |
| 25 | + "content": [ |
| 26 | + { |
| 27 | + "type": "text", |
| 28 | + "text": "Hello World" |
| 29 | + } |
| 30 | + ] |
| 31 | + } |
| 32 | + ] |
| 33 | + }`, |
| 34 | + wantElemCount: 1, |
| 35 | + wantErr: false, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "multiple paragraphs", |
| 39 | + json: `{ |
| 40 | + "type": "doc", |
| 41 | + "content": [ |
| 42 | + { |
| 43 | + "type": "paragraph", |
| 44 | + "content": [ |
| 45 | + { |
| 46 | + "type": "text", |
| 47 | + "text": "First" |
| 48 | + } |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "type": "paragraph", |
| 53 | + "content": [ |
| 54 | + { |
| 55 | + "type": "text", |
| 56 | + "text": "Second" |
| 57 | + } |
| 58 | + ] |
| 59 | + } |
| 60 | + ] |
| 61 | + }`, |
| 62 | + wantElemCount: 2, |
| 63 | + wantErr: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "empty document", |
| 67 | + json: `{ |
| 68 | + "type": "doc", |
| 69 | + "content": [] |
| 70 | + }`, |
| 71 | + wantElemCount: 0, |
| 72 | + wantErr: false, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "invalid json", |
| 76 | + json: `{"type": "doc", "content": [}`, |
| 77 | + wantElemCount: 0, |
| 78 | + wantErr: true, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + for _, tt := range tests { |
| 83 | + t.Run(tt.name, func(t *testing.T) { |
| 84 | + var doc edtypes.Document |
| 85 | + err := json.Unmarshal([]byte(tt.json), &doc) |
| 86 | + |
| 87 | + if (err != nil) != tt.wantErr { |
| 88 | + t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + if tt.wantErr { |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + if len(doc.Elements) != tt.wantElemCount { |
| 97 | + t.Errorf("Elements count = %v, want %v", len(doc.Elements), tt.wantElemCount) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestDocument_UnmarshalJSON_Integration(t *testing.T) { |
| 104 | + // Тест для проверки что UnmarshalJSON работает в составе DTO структуры |
| 105 | + type IssueDTO struct { |
| 106 | + Title string `json:"title"` |
| 107 | + Description edtypes.Document `json:"description"` |
| 108 | + } |
| 109 | + |
| 110 | + jsonData := `{ |
| 111 | + "title": "Test Issue", |
| 112 | + "description": { |
| 113 | + "type": "doc", |
| 114 | + "content": [ |
| 115 | + { |
| 116 | + "type": "paragraph", |
| 117 | + "content": [ |
| 118 | + { |
| 119 | + "type": "text", |
| 120 | + "text": "This is a test description" |
| 121 | + } |
| 122 | + ] |
| 123 | + } |
| 124 | + ] |
| 125 | + } |
| 126 | + }` |
| 127 | + |
| 128 | + var issue IssueDTO |
| 129 | + err := json.Unmarshal([]byte(jsonData), &issue) |
| 130 | + if err != nil { |
| 131 | + t.Fatalf("UnmarshalJSON() error = %v", err) |
| 132 | + } |
| 133 | + |
| 134 | + if issue.Title != "Test Issue" { |
| 135 | + t.Errorf("Title = %v, want %v", issue.Title, "Test Issue") |
| 136 | + } |
| 137 | + |
| 138 | + if len(issue.Description.Elements) != 1 { |
| 139 | + t.Errorf("Description.Elements count = %v, want %v", len(issue.Description.Elements), 1) |
| 140 | + } |
| 141 | + |
| 142 | + // Проверяем что первый элемент - это Paragraph |
| 143 | + if para, ok := issue.Description.Elements[0].(*edtypes.Paragraph); ok { |
| 144 | + if len(para.Content) != 1 { |
| 145 | + t.Errorf("Paragraph content count = %v, want %v", len(para.Content), 1) |
| 146 | + } |
| 147 | + if text, ok := para.Content[0].(edtypes.Text); ok { |
| 148 | + if text.Content != "This is a test description" { |
| 149 | + t.Errorf("Text content = %v, want %v", text.Content, "This is a test description") |
| 150 | + } |
| 151 | + } else { |
| 152 | + t.Errorf("First content element is not Text, got %T", para.Content[0]) |
| 153 | + } |
| 154 | + } else { |
| 155 | + t.Error("First element is not *Paragraph") |
| 156 | + } |
| 157 | +} |
0 commit comments