// SPDX-License-Identifier: AGPL-3.0-or-later package main import "testing" func TestRouteFromDeepLink(t *testing.T) { ok := map[string]string{ "docwell://": "/", "docwell://p/abc123": "/p/abc123", "docwell://p/abc123/": "/p/abc123", "DOCWELL://s/space01": "/s/space01", "docwell:///p/abc123": "/p/abc123", " docwell://p/abc123\n ": "/p/abc123", } for in, want := range ok { got, err := routeFromDeepLink(in) if err != nil || got != want { t.Errorf("routeFromDeepLink(%q) = %q, %v; want %q", in, got, err, want) } } bad := []string{ "https://p/abc123", "docwell://settings/server", "docwell://p/../../etc", "docwell://p/abc123/extra", "docwell://p/", "docwell://p/a%2Fb", "docwell://p/javascript:alert(1)", "not a url", } for _, in := range bad { if got, err := routeFromDeepLink(in); err == nil { t.Errorf("routeFromDeepLink(%q) = %q, want an error", in, got) } } } func TestDeepLinkFromArgs(t *testing.T) { if got, ok := deepLinkFromArgs([]string{`C:\Docwell\docwell.exe`, "docwell://p/abc"}); !ok || got != "docwell://p/abc" { t.Errorf("got %q, %v", got, ok) } if _, ok := deepLinkFromArgs([]string{"docwell.exe", "--verbose"}); ok { t.Error("found a link where there is none") } } func TestLinkServiceKeepsEarlyLinkUntilTaken(t *testing.T) { var s LinkService var sent []string s.setEmitter(func(r string) { sent = append(sent, r) }) s.open("docwell://p/early") s.open("docwell://settings/server") // ignored if got := s.TakePending(); got != "/p/early" { t.Fatalf("pending = %q", got) } if got := s.TakePending(); got != "" { t.Fatalf("pending taken twice: %q", got) } if len(sent) != 0 { t.Fatalf("emitted before the frontend listened: %v", sent) } s.open("docwell://s/space1") if len(sent) != 1 || sent[0] != "/s/space1" || s.TakePending() != "" { t.Fatalf("sent = %v", sent) } }