Files
3x-ui/internal/web/controller/dist_test.go
T
korsun009 3a2f9b48da feat(web): add network-only PWA installability (#6190)
* feat(web): add network-only PWA installability

Serve the manifest, registration script, network-only service worker, and icons under the runtime web base path so panels remain installable at arbitrary configured URLs.

This does not add offline caching or change panel, API, database, or Xray behavior.

* chore(docs): remove development planning notes

Keep the pull request focused on the PWA implementation, tests, and user-facing verification documentation.

* feat(web): adopt the 3X logo PWA icon set from #1865

Replace the two placeholder SVG icons with the six-size PNG set
(16/24/32/64/192/512) contributed by @Incognito-Coder in PR #1865.
The PNGs have transparent rounded corners, so the manifest entries
drop the maskable purpose claim and rely on the default any.

---------

Co-authored-by: korsun009 <277924786+korsun009@users.noreply.github.com>
Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
2026-08-18 15:33:20 +02:00

74 lines
2.2 KiB
Go

package controller
import (
"encoding/json"
"strings"
"testing"
)
func TestWithServerBasePath(t *testing.T) {
spec := []byte(`{"openapi":"3.0.3","info":{"title":"x"},"servers":[{"url":"/","description":"old"}],"paths":{"/p":{"get":{"summary":"s"}}}}`)
out, err := withServerBasePath(spec, "/test/")
if err != nil {
t.Fatalf("withServerBasePath: %v", err)
}
var doc map[string]any
if err := json.Unmarshal(out, &doc); err != nil {
t.Fatalf("unmarshal result: %v", err)
}
servers, ok := doc["servers"].([]any)
if !ok || len(servers) != 1 {
t.Fatalf("servers = %v, want one entry", doc["servers"])
}
srv, _ := servers[0].(map[string]any)
if srv["url"] != "/test" {
t.Errorf("server url = %v, want /test (trailing slash trimmed)", srv["url"])
}
if doc["openapi"] != "3.0.3" {
t.Errorf("openapi field not preserved: %v", doc["openapi"])
}
if _, ok := doc["paths"].(map[string]any)["/p"]; !ok {
t.Errorf("paths content not preserved verbatim")
}
}
func TestWithServerBasePathInvalidJSON(t *testing.T) {
if _, err := withServerBasePath([]byte("not json"), "/test/"); err == nil {
t.Errorf("expected error on invalid spec, got nil")
}
}
func TestPWAHeadInjectionUsesRuntimeBasePath(t *testing.T) {
tests := []struct {
name string
basePath string
wantPath string
}{
{name: "root", basePath: "/", wantPath: "/manifest.webmanifest"},
{name: "secret path", basePath: "panel-secret", wantPath: "/panel-secret/manifest.webmanifest"},
{name: "trailing slash", basePath: "/panel-secret/", wantPath: "/panel-secret/manifest.webmanifest"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
head := string(pwaHeadInjection(test.basePath, "login.html"))
if !strings.Contains(head, `href="`+test.wantPath+`"`) {
t.Fatalf("manifest URL = %q, want %q", head, test.wantPath)
}
if !strings.Contains(head, `src="`+strings.Replace(test.wantPath, "manifest.webmanifest", "pwa-register.js", 1)+`"`) {
t.Fatalf("registration URL = %q", head)
}
})
}
}
func TestPWAHeadInjectionSkipsSubscriptionPage(t *testing.T) {
if got := pwaHeadInjection("/panel-secret/", "subpage.html"); got != nil {
t.Fatalf("subpage injection = %q, want nil", got)
}
}