Files
pages/internal/server/content_view_test.go
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

202 lines
5.5 KiB
Go

package server
import (
"strings"
"testing"
"pageswiki/internal/store"
)
func TestRevisionContentViewSelectsSafeAdapters(t *testing.T) {
tests := []struct {
contentType string
kind string
}{
{"text/html; charset=utf-8", "html"},
{"text/x-python; charset=utf-8", "code"},
{"application/x-sh", "code"},
{"application/json", "json"},
{"image/png", "image"},
{"audio/mpeg", "audio"},
{"video/mp4", "video"},
{"application/pdf", "pdf"},
{
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"download",
},
}
for _, test := range tests {
view := revisionContentView(
&store.Revision{
ContentType: test.contentType,
Filename: "sample",
Body: "<script>alert(1)</script>",
},
"/sample",
)
if view.Kind != test.kind {
t.Errorf(
"Content-Type %q kind = %q, want %q",
test.contentType,
view.Kind,
test.kind,
)
}
if test.kind == "code" && view.Text == "" {
t.Errorf("code content was not passed to escaped template rendering")
}
}
}
func TestRevisionContentViewSniffsExtensionlessOctetStream(t *testing.T) {
script := revisionContentView(
&store.Revision{
ContentType: "application/octet-stream",
Body: "#!/usr/bin/env bash\necho hello\n",
},
"/run",
)
if script.Kind != "code" || script.Language != "shell" {
t.Fatalf("script view = %#v", script)
}
if script.Text == "" {
t.Fatal("extensionless script did not expose decoded text")
}
pdf := revisionContentView(
&store.Revision{
ContentType: "application/octet-stream",
Body: "%PDF-1.7\n%\x80\x81\x82\n",
},
"/manual",
)
if pdf.Kind != "pdf" {
t.Fatalf("PDF kind = %q, want pdf", pdf.Kind)
}
}
func TestRevisionContentViewSelectsSpecializedAdapterBoundaries(t *testing.T) {
tests := []struct {
name string
contentType string
filename string
adapterID string
kind string
}{
{"CSV", "text/csv", "report.csv", "csv", "csv"},
{"ZIP", "application/zip", "bundle.zip", "archive", "archive"},
{
"DEB",
"application/octet-stream",
"pageswiki.deb",
"debian-package",
"metadata",
},
{
"Excalidraw",
"application/json",
"diagram.excalidraw",
"excalidraw",
"excalidraw",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
view := revisionContentView(
&store.Revision{
ContentType: test.contentType,
Filename: test.filename,
Body: "{}",
},
"/"+test.filename,
)
if view.AdapterID != test.adapterID || view.Kind != test.kind {
t.Fatalf(
"view adapter/kind = %q/%q, want %q/%q",
view.AdapterID,
view.Kind,
test.adapterID,
test.kind,
)
}
})
}
}
func TestCSVProjectionIsBounded(t *testing.T) {
body := "name,value\n" + strings.Repeat("row,1\n", maxCSVRows+10)
view := revisionContentView(&store.Revision{ContentType: "text/csv", Filename: "report.csv", Body: body}, "/report.csv")
if view.Kind != "csv" || len(view.CSVRows) != maxCSVRows {
t.Fatalf("CSV projection kind/rows = %q/%d", view.Kind, len(view.CSVRows))
}
if !view.CSVTruncated {
t.Fatal("bounded CSV projection did not report truncation")
}
}
func TestCodeLanguageStopsWhenDetectionReturnsCurrentMediaType(t *testing.T) {
if language := codeLanguage("text/csv; charset=utf-8", "report.csv", "name,value\na,1\n"); language != "text" {
t.Fatalf("CSV fallback language = %q, want text", language)
}
}
func TestExcalidrawProjectionValidatesScene(t *testing.T) {
valid := revisionContentView(&store.Revision{
ContentType: "application/json", Filename: "map.excalidraw",
Body: `{"type":"excalidraw","elements":[{"type":"rectangle","x":1,"y":2,"width":30,"height":20}]}`,
}, "/map.excalidraw")
if valid.Kind != "excalidraw" || !valid.ExcalidrawValid || valid.ExcalidrawElements != 1 {
t.Fatalf("valid scene projection = %#v", valid)
}
invalid := revisionContentView(&store.Revision{
ContentType: "application/json", Filename: "map.excalidraw", Body: `{"elements":[]}`,
}, "/map.excalidraw")
if invalid.ExcalidrawValid || invalid.ParseError == "" {
t.Fatalf("invalid scene was accepted: %#v", invalid)
}
}
func TestExcalidrawProjectionRejectsExcessivePointComplexity(t *testing.T) {
points := strings.Repeat("[0,0],", maxExcalidrawPoints+1)
points = strings.TrimSuffix(points, ",")
view := contentView{}
parseExcalidrawView(&view, []byte(`{"type":"excalidraw","elements":[{"type":"line","x":0,"y":0,"width":1,"height":1,"points":[`+points+`]}]}`))
if view.ExcalidrawValid || view.ParseError == "" {
t.Fatal("excessive Excalidraw point complexity was accepted")
}
}
func TestTextAdapterSignalsWindowingForLargeDocuments(t *testing.T) {
view := revisionContentView(
&store.Revision{
ContentType: "text/plain; charset=utf-8",
Filename: "large.txt",
Body: strings.Repeat("x", int(inlineTextWindowThreshold+1)),
},
"/large.txt",
)
if view.AdapterID != "text" {
t.Fatalf("AdapterID = %q, want text", view.AdapterID)
}
if !view.Windowed {
t.Fatal("large text adapter did not request windowed delivery")
}
}
func TestPreviewUsesViewerAdapterForNonHTMLRevision(t *testing.T) {
view := previewContentView(&store.Revision{
UUID: "revision-1",
ContentType: "text/plain; charset=utf-8",
Filename: "payload.txt",
Body: "<script>alert('preview')</script>",
}, "/payload.txt")
if view.Kind != "code" {
t.Fatalf("preview kind = %q, want code", view.Kind)
}
if view.Text != "<script>alert('preview')</script>" {
t.Fatalf("preview text = %q", view.Text)
}
}