package parser import ( "context" "testing" "github.com/lukaszraczylo/mcp-filepuff/pkg/protocol" ) func TestParseYAML(t *testing.T) { registry := NewRegistry() defer registry.Close() tests := []struct { name string content string shouldError bool }{ { name: "valid simple YAML", content: `name: test version: 1.0.0 enabled: true`, shouldError: false, }, { name: "valid nested YAML", content: `metadata: name: test-app namespace: default spec: replicas: 3 selector: matchLabels: app: test`, shouldError: false, }, { name: "valid list YAML", content: `items: - name: item1 value: 100 - name: item2 value: 200`, shouldError: false, }, { name: "invalid YAML - bad syntax", content: `name: test\n bad: indent\n wrong: [unclosed`, shouldError: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result, err := registry.ParseYAML(context.Background(), "test.yaml", []byte(tt.content)) if tt.shouldError { if err == nil { t.Error("expected error but got none") } return } if err != nil { t.Errorf("unexpected error: %v", err) return } if result == nil { t.Error("expected result but got nil") return } if result.Language != protocol.LangYAML { t.Errorf("expected language YAML, got %s", result.Language) } if len(result.Errors) > 0 { t.Errorf("expected no syntax errors, got %d", len(result.Errors)) } }) } } func TestParseJSON(t *testing.T) { registry := NewRegistry() defer registry.Close() tests := []struct { name string content string shouldError bool }{ { name: "valid simple JSON", content: `{"name": "test", "version": "1.0.0", "enabled": true}`, shouldError: false, }, { name: "valid nested JSON", content: `{ "metadata": { "name": "test-app", "namespace": "default" }, "spec": { "replicas": 3, "selector": { "matchLabels": { "app": "test" } } } }`, shouldError: false, }, { name: "valid array JSON", content: `[{"name": "item1", "value": 100}, {"name": "item2", "value": 200}]`, shouldError: false, }, { name: "invalid JSON - unclosed brace", content: `{"name": "test", "value": 100`, shouldError: true, }, { name: "invalid JSON - trailing comma", content: `{"name": "test", "value": 100,}`, shouldError: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result, err := registry.ParseJSON(context.Background(), "test.json", []byte(tt.content)) if tt.shouldError { if err == nil { t.Error("expected error but got none") } return } if err != nil { t.Errorf("unexpected error: %v", err) return } if result == nil { t.Error("expected result but got nil") return } if result.Language != protocol.LangJSON { t.Errorf("expected language JSON, got %s", result.Language) } if len(result.Errors) > 0 { t.Errorf("expected no syntax errors, got %d", len(result.Errors)) } }) } } func TestRegistryParse_YAML_JSON(t *testing.T) { registry := NewRegistry() defer registry.Close() yamlContent := []byte(`name: test version: 1.0.0`) jsonContent := []byte(`{"name": "test", "version": "1.0.0"}`) // Test YAML through main Parse method yamlResult, err := registry.Parse(context.Background(), "config.yaml", yamlContent) if err != nil { t.Errorf("failed to parse YAML: %v", err) } if yamlResult.Language != protocol.LangYAML { t.Errorf("expected YAML language, got %s", yamlResult.Language) } // Test JSON through main Parse method jsonResult, err := registry.Parse(context.Background(), "config.json", jsonContent) if err != nil { t.Errorf("failed to parse JSON: %v", err) } if jsonResult.Language != protocol.LangJSON { t.Errorf("expected JSON language, got %s", jsonResult.Language) } // Test .yml extension ymlResult, err := registry.Parse(context.Background(), "config.yml", yamlContent) if err != nil { t.Errorf("failed to parse .yml: %v", err) } if ymlResult.Language != protocol.LangYAML { t.Errorf("expected YAML language for .yml extension, got %s", ymlResult.Language) } }