106 lines
3.6 KiB
Go
106 lines
3.6 KiB
Go
package kunstzeal
|
|
|
|
import (
|
|
"go/parser"
|
|
"go/token"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func TestAdapterSelectionAndOrdering(t *testing.T) {
|
|
for _, test := range []struct {
|
|
mime, filename, id, kind string
|
|
delivery Delivery
|
|
needsText, window bool
|
|
}{
|
|
{"text/html", "scene.excalidraw", "html", "html", DeliveryHTML, false, false},
|
|
{"application/json", "scene.EXCALIDRAW", "excalidraw", "excalidraw", DeliveryText, true, true},
|
|
{"text/plain", "data.CSV", "csv", "csv", DeliveryText, true, true},
|
|
{"application/zip", "archive", "archive", "archive", DeliveryDownload, false, false},
|
|
{"application/octet-stream", "package.deb", "debian-package", "metadata", DeliveryDownload, false, false},
|
|
{"text/plain", "mail.eml", "eml", "email", DeliveryText, false, false},
|
|
{"application/octet-stream", "program.exe", "pe", "metadata", DeliveryDownload, false, false},
|
|
{"application/json", "data.json", "json", "json", DeliveryText, false, true},
|
|
{"text/x-python", "code.py", "text", "code", DeliveryText, true, true},
|
|
{"image/png", "image", "image", "image", DeliveryRawMedia, false, false},
|
|
{"audio/flac", "music", "audio", "audio", DeliveryRawMedia, false, false},
|
|
{"video/mp4", "movie", "video", "video", DeliveryRawMedia, false, false},
|
|
{"application/pdf", "manual", "pdf", "pdf", DeliveryRawFrame, false, false},
|
|
{"application/octet-stream", "unknown", "download", "download", DeliveryDownload, false, false},
|
|
} {
|
|
got := SelectAdapter(Descriptor{MediaType: test.mime, Filename: test.filename, SizeBytes: InlineTextWindowThreshold + 1})
|
|
if got.ID != test.id || got.Kind != test.kind || got.Delivery != test.delivery || got.NeedsText != test.needsText || got.WindowableText != test.window {
|
|
t.Fatalf("%s/%s: %+v", test.mime, test.filename, got)
|
|
}
|
|
}
|
|
if got := SelectAdapter(Descriptor{}); got.ID != "download" {
|
|
t.Fatal(got)
|
|
}
|
|
}
|
|
|
|
func TestAdapterCatalogIsCopyAndConcurrent(t *testing.T) {
|
|
catalog := Catalog()
|
|
ids := []string{}
|
|
for _, a := range catalog {
|
|
ids = append(ids, a.ID)
|
|
}
|
|
if strings.Join(ids, ",") != "html,excalidraw,csv,archive,debian-package,eml,pe,json,text,image,audio,video,pdf,download,reveal" {
|
|
t.Fatal("catalog order changed", ids)
|
|
}
|
|
catalog[0].ID = "corrupt"
|
|
catalog[0].Match = func(Descriptor) bool { return false }
|
|
if Catalog()[0].ID != "html" || SelectAdapter(Descriptor{MediaType: "text/html"}).ID != "html" {
|
|
t.Fatal("catalog mutation changed shared selection")
|
|
}
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 8; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for j := 0; j < 100; j++ {
|
|
Catalog()[0].ID = "copy"
|
|
if SelectAdapter(Descriptor{MediaType: "text/html"}).ID != "html" {
|
|
t.Error("registry changed")
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestComponentBoundary(t *testing.T) {
|
|
if Component != "kunstzeal" {
|
|
t.Fatal("component identity changed")
|
|
}
|
|
files, err := filepath.Glob("*.go")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, file := range files {
|
|
if strings.HasSuffix(file, "_test.go") {
|
|
continue
|
|
}
|
|
ast, err := parser.ParseFile(token.NewFileSet(), file, nil, parser.ImportsOnly)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, spec := range ast.Imports {
|
|
path, err := strconv.Unquote(spec.Path.Value)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, forbidden := range []string{"pageswiki/internal", "database", "os/exec"} {
|
|
if path == forbidden || strings.HasPrefix(path, forbidden+"/") {
|
|
t.Errorf("%s imports %s: providers must not own application state or arbitrary process execution", file, path)
|
|
}
|
|
}
|
|
if file == "adapters.go" && (strings.HasPrefix(path, "net") || path == "os") {
|
|
t.Error("adapter selection acquired I/O")
|
|
}
|
|
}
|
|
}
|
|
}
|