|
| 1 | +package pidl |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestValidateFile(t *testing.T) { |
| 10 | + // Create a temp file with valid content |
| 11 | + dir := t.TempDir() |
| 12 | + filename := filepath.Join(dir, "test.json") |
| 13 | + |
| 14 | + p := NewMinimalProtocol("test", "Test Protocol") |
| 15 | + if err := WriteProtocolFile(filename, p); err != nil { |
| 16 | + t.Fatalf("WriteProtocolFile() error = %v", err) |
| 17 | + } |
| 18 | + |
| 19 | + // Validate it |
| 20 | + parsed, errs, err := ValidateFile(filename) |
| 21 | + if err != nil { |
| 22 | + t.Fatalf("ValidateFile() error = %v", err) |
| 23 | + } |
| 24 | + if errs.HasErrors() { |
| 25 | + t.Errorf("ValidateFile() validation errors = %v", errs) |
| 26 | + } |
| 27 | + if parsed.ProtocolMeta.ID != "test" { |
| 28 | + t.Errorf("Protocol ID = %q, want %q", parsed.ProtocolMeta.ID, "test") |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestValidateFileNotFound(t *testing.T) { |
| 33 | + _, _, err := ValidateFile("/nonexistent/file.json") |
| 34 | + if err == nil { |
| 35 | + t.Error("ValidateFile() should error on missing file") |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestValidateFileInvalidJSON(t *testing.T) { |
| 40 | + dir := t.TempDir() |
| 41 | + filename := filepath.Join(dir, "invalid.json") |
| 42 | + |
| 43 | + if err := os.WriteFile(filename, []byte("not json"), 0644); err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + |
| 47 | + _, _, err := ValidateFile(filename) |
| 48 | + if err == nil { |
| 49 | + t.Error("ValidateFile() should error on invalid JSON") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestValidateFiles(t *testing.T) { |
| 54 | + dir := t.TempDir() |
| 55 | + |
| 56 | + // Create valid file |
| 57 | + valid := filepath.Join(dir, "valid.json") |
| 58 | + p := NewMinimalProtocol("valid", "Valid") |
| 59 | + if err := WriteProtocolFile(valid, p); err != nil { |
| 60 | + t.Fatal(err) |
| 61 | + } |
| 62 | + |
| 63 | + // Create invalid file |
| 64 | + invalid := filepath.Join(dir, "invalid.json") |
| 65 | + if err := os.WriteFile(invalid, []byte("{}"), 0644); err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + |
| 69 | + results := ValidateFiles([]string{valid, invalid}) |
| 70 | + |
| 71 | + if len(results) != 2 { |
| 72 | + t.Fatalf("ValidateFiles() returned %d results, want 2", len(results)) |
| 73 | + } |
| 74 | + |
| 75 | + if !results[0].IsValid() { |
| 76 | + t.Errorf("results[0] should be valid") |
| 77 | + } |
| 78 | + |
| 79 | + if results[1].IsValid() { |
| 80 | + t.Errorf("results[1] should be invalid") |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestFileValidationResultIsValid(t *testing.T) { |
| 85 | + tests := []struct { |
| 86 | + name string |
| 87 | + result FileValidationResult |
| 88 | + want bool |
| 89 | + }{ |
| 90 | + { |
| 91 | + name: "valid", |
| 92 | + result: FileValidationResult{}, |
| 93 | + want: true, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "parse error", |
| 97 | + result: FileValidationResult{ |
| 98 | + ParseErr: os.ErrNotExist, |
| 99 | + }, |
| 100 | + want: false, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "validation errors", |
| 104 | + result: FileValidationResult{ |
| 105 | + Errors: ValidationErrors{{Field: "x", Message: "y"}}, |
| 106 | + }, |
| 107 | + want: false, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tt := range tests { |
| 112 | + t.Run(tt.name, func(t *testing.T) { |
| 113 | + if got := tt.result.IsValid(); got != tt.want { |
| 114 | + t.Errorf("IsValid() = %v, want %v", got, tt.want) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestNewProtocol(t *testing.T) { |
| 121 | + p := NewProtocol("test-id", "Test Name") |
| 122 | + |
| 123 | + if p.ProtocolMeta.ID != "test-id" { |
| 124 | + t.Errorf("ID = %q, want %q", p.ProtocolMeta.ID, "test-id") |
| 125 | + } |
| 126 | + if p.ProtocolMeta.Name != "Test Name" { |
| 127 | + t.Errorf("Name = %q, want %q", p.ProtocolMeta.Name, "Test Name") |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestNewMinimalProtocol(t *testing.T) { |
| 132 | + p := NewMinimalProtocol("test", "Test") |
| 133 | + |
| 134 | + if !p.IsValid() { |
| 135 | + errs := p.Validate() |
| 136 | + t.Errorf("NewMinimalProtocol() should be valid, got errors: %v", errs) |
| 137 | + } |
| 138 | + |
| 139 | + if len(p.Entities) < 2 { |
| 140 | + t.Errorf("NewMinimalProtocol() should have at least 2 entities") |
| 141 | + } |
| 142 | + if len(p.Flows) < 1 { |
| 143 | + t.Errorf("NewMinimalProtocol() should have at least 1 flow") |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func TestWriteProtocolFile(t *testing.T) { |
| 148 | + dir := t.TempDir() |
| 149 | + filename := filepath.Join(dir, "subdir", "test.json") |
| 150 | + |
| 151 | + p := NewMinimalProtocol("test", "Test") |
| 152 | + if err := WriteProtocolFile(filename, p); err != nil { |
| 153 | + t.Fatalf("WriteProtocolFile() error = %v", err) |
| 154 | + } |
| 155 | + |
| 156 | + // Verify file exists |
| 157 | + if _, err := os.Stat(filename); err != nil { |
| 158 | + t.Errorf("File should exist: %v", err) |
| 159 | + } |
| 160 | + |
| 161 | + // Read it back |
| 162 | + p2, err := ParseFile(filename) |
| 163 | + if err != nil { |
| 164 | + t.Fatalf("ParseFile() error = %v", err) |
| 165 | + } |
| 166 | + |
| 167 | + if p2.ProtocolMeta.ID != p.ProtocolMeta.ID { |
| 168 | + t.Errorf("Round-trip ID = %q, want %q", p2.ProtocolMeta.ID, p.ProtocolMeta.ID) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func TestTitleCase(t *testing.T) { |
| 173 | + tests := []struct { |
| 174 | + input string |
| 175 | + want string |
| 176 | + }{ |
| 177 | + {"hello world", "Hello World"}, |
| 178 | + {"HELLO WORLD", "Hello World"}, |
| 179 | + {"hello", "Hello"}, |
| 180 | + {"", ""}, |
| 181 | + {"my protocol", "My Protocol"}, |
| 182 | + {"oauth 2 0", "Oauth 2 0"}, |
| 183 | + } |
| 184 | + |
| 185 | + for _, tt := range tests { |
| 186 | + t.Run(tt.input, func(t *testing.T) { |
| 187 | + got := TitleCase(tt.input) |
| 188 | + if got != tt.want { |
| 189 | + t.Errorf("TitleCase(%q) = %q, want %q", tt.input, got, tt.want) |
| 190 | + } |
| 191 | + }) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +func TestSanitizeID(t *testing.T) { |
| 196 | + tests := []struct { |
| 197 | + input string |
| 198 | + want string |
| 199 | + }{ |
| 200 | + {"test", "test"}, |
| 201 | + {"Test", "test"}, |
| 202 | + {"TEST", "test"}, |
| 203 | + {"test-id", "test-id"}, |
| 204 | + {"test_id", "test_id"}, |
| 205 | + {"Test ID", "test_id"}, |
| 206 | + {"My Protocol", "my_protocol"}, |
| 207 | + {"123abc", "p123abc"}, |
| 208 | + {"", "protocol"}, |
| 209 | + {"---", "protocol"}, |
| 210 | + {"OAuth 2.0", "oauth_2_0"}, |
| 211 | + {"MCP Tool", "mcp_tool"}, |
| 212 | + } |
| 213 | + |
| 214 | + for _, tt := range tests { |
| 215 | + t.Run(tt.input, func(t *testing.T) { |
| 216 | + got := SanitizeID(tt.input) |
| 217 | + if got != tt.want { |
| 218 | + t.Errorf("SanitizeID(%q) = %q, want %q", tt.input, got, tt.want) |
| 219 | + } |
| 220 | + }) |
| 221 | + } |
| 222 | +} |
0 commit comments