package server import ( "strings" "testing" "golang.org/x/text/encoding/japanese" ) func TestDetectRevisionTextWithoutExtension(t *testing.T) { tests := []struct { name string data []byte contentType string isText bool language string }{ { name: "shell shebang", data: []byte("#!/bin/sh\necho hello\n"), contentType: "application/x-sh", isText: true, language: "shell", }, { name: "json", data: []byte("{\"enabled\":true}\n"), contentType: "application/json", isText: true, language: "json", }, { name: "PDF magic", data: []byte("%PDF-1.7\n%\x80\x81\x82\n"), contentType: "application/pdf", isText: false, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { detected := detectRevisionText(test.data, "") if detected.IsText != test.isText { t.Fatalf("IsText = %v, want %v", detected.IsText, test.isText) } if !strings.HasPrefix(detected.ContentType, test.contentType) { t.Fatalf( "ContentType = %q, want prefix %q", detected.ContentType, test.contentType, ) } if test.language != "" { if got := codeLanguage( detected.ContentType, "", detected.Text, ); got != test.language { t.Fatalf("language = %q, want %q", got, test.language) } } }) } } func TestDetectRevisionTextDecodesShiftJIS(t *testing.T) { data, err := japanese.ShiftJIS.NewEncoder().Bytes( []byte("日本語のテキストです\n"), ) if err != nil { t.Fatal(err) } detected := detectRevisionText(data, "") if !detected.IsText { t.Fatal("Shift_JIS text was classified as binary") } if detected.Text != "日本語のテキストです\n" { t.Fatalf("decoded text = %q", detected.Text) } if !strings.Contains(detected.ContentType, "charset=shift_jis") { t.Fatalf("ContentType = %q", detected.ContentType) } }