package services import "testing" func TestExtractJSON_FromCodeBlock(t *testing.T) { input := "Here is the result:\n```json\n{\"key\": \"value\"}\n```\nDone." got := extractJSON(input) want := "{\"key\": \"value\"}" if got != want { t.Errorf("extractJSON() = %q, want %q", got, want) } } func TestExtractJSON_FromPlainCodeBlock(t *testing.T) { input := "Result:\n```\n[{\"a\": 1}]\n```\nEnd." got := extractJSON(input) want := "[{\"a\": 1}]" if got != want { t.Errorf("extractJSON() = %q, want %q", got, want) } } func TestExtractJSON_RawObject(t *testing.T) { input := "Some text before {\"score\": 8, \"overall\": \"good\"} after text" got := extractJSON(input) want := "{\"score\": 8, \"overall\": \"good\"}" if got != want { t.Errorf("extractJSON() = %q, want %q", got, want) } } func TestExtractJSON_RawArray(t *testing.T) { input := "Output: [{\"severity\": \"info\", \"description\": \"test\"}] end" got := extractJSON(input) want := "[{\"severity\": \"info\", \"description\": \"test\"}]" if got != want { t.Errorf("extractJSON() = %q, want %q", got, want) } } func TestExtractJSON_NestedBrackets(t *testing.T) { input := `{"outer": {"inner": [1, 2, 3]}}` got := extractJSON(input) if got != input { t.Errorf("extractJSON() = %q, want %q", got, input) } } func TestExtractJSON_NestedBracketsInText(t *testing.T) { input := `prefix {"arr": [{"k": "v"}]} suffix` got := extractJSON(input) want := `{"arr": [{"k": "v"}]}` if got != want { t.Errorf("extractJSON() = %q, want %q", got, want) } } func TestExtractJSON_NoJSON(t *testing.T) { input := "This is plain text with no JSON at all" got := extractJSON(input) if got != input { t.Errorf("extractJSON() = %q, want %q (same as input)", got, input) } } func TestExtractJSON_EmptyString(t *testing.T) { got := extractJSON("") if got != "" { t.Errorf("extractJSON(\"\") = %q, want %q", got, "") } } func TestExtractJSON_CodeBlockWithLanguageAndNewline(t *testing.T) { input := "```json\n[\n {\"a\": 1},\n {\"b\": 2}\n]\n```" got := extractJSON(input) want := "[\n {\"a\": 1},\n {\"b\": 2}\n]" if got != want { t.Errorf("extractJSON() = %q, want %q", got, want) } } func TestExtractJSON_ArrayBeforeObject(t *testing.T) { // When [ comes before { , array should be extracted input := "[1, 2, 3] and {\"key\": \"val\"}" got := extractJSON(input) want := "[1, 2, 3]" if got != want { t.Errorf("extractJSON() = %q, want %q", got, want) } } func TestExtractJSON_ObjectBeforeArray(t *testing.T) { input := "{\"key\": \"val\"} and [1, 2, 3]" got := extractJSON(input) want := "{\"key\": \"val\"}" if got != want { t.Errorf("extractJSON() = %q, want %q", got, want) } } func TestExtractJSON_MultipleCodeBlocks(t *testing.T) { // Should extract from the first code block input := "```json\n{\"first\": true}\n```\n\nSome text\n\n```json\n{\"second\": true}\n```" got := extractJSON(input) want := "{\"first\": true}" if got != want { t.Errorf("extractJSON() = %q, want %q", got, want) } }