Files
pages/internal/server/content_detect_test.go
T
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

83 lines
1.9 KiB
Go

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)
}
}