Plugins that need their own UI and richer themes should not force breaking contract changes later, so the full shape is published now (uiApi 2.0, bridgeApi 1.0) and every entry is marked stable or experimental: - docs/plugin-architecture.md: plugin-owned views (sidebar, vm.tab, settings, dialog) and the bridge methods (§5.6), the finer theme contract with slot catalogue, layers, layout and icons (§5.1), and the contract evolution rules (§6.3). - ui-contract.ts and the new bridgeContract.ts list stable and experimental entries; the build fails if a stable entry is not emitted or an emitted entry is still experimental, and writes bridge-contract.json next to ui-contract.json. - internal/plugin/contract.go mirrors them as catalogs (contribution points, slots with accepted content, view locations, layers, layout, icons, bridge methods and events) and derives HostFeatures; tests keep Go and TypeScript identical and require every contributes field to be catalogued. - Manifests gain views, theme layout and engines.bridgeApi. Unknown slots, locations, layout values and icon names are rejected; experimental use, unknown fields and missing engines ranges are doctor warnings. Declared-but-unrendered entries (vmCards, statusItems, settingsPages, vmTemplates, runtimeSources, runtimes, uiScripts, diagnostics, theme icons and brandMark, panels outside settings.plugin) are now marked experimental. - The panel bridge resolves methods through the catalog, adds hello and refuses experimental methods; panels get theme.changed and locale.changed events; the Sidecar handshake and the Go SDK carry uiApi, bridgeApi and features. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
277 lines
10 KiB
Go
277 lines
10 KiB
Go
package plugin
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func readFrontendContract(t *testing.T, rel string) string {
|
|
t.Helper()
|
|
raw, err := os.ReadFile(filepath.Join("..", "..", "frontend", "src", filepath.FromSlash(rel)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return string(raw)
|
|
}
|
|
|
|
func tsArray(t *testing.T, src, name string) []string {
|
|
t.Helper()
|
|
m := regexp.MustCompile(`export const ` + name + ` = \[([\s\S]*?)\] as const`).FindStringSubmatch(src)
|
|
if m == nil {
|
|
t.Fatalf("cannot find %s in frontend contract", name)
|
|
}
|
|
var out []string
|
|
for _, s := range regexp.MustCompile(`'([^']+)'`).FindAllStringSubmatch(m[1], -1) {
|
|
out = append(out, s[1])
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|
|
|
|
func tsVersion(t *testing.T, src, name string) string {
|
|
t.Helper()
|
|
m := regexp.MustCompile(name + `\s*=\s*'([^']+)'`).FindStringSubmatch(src)
|
|
if m == nil {
|
|
t.Fatalf("cannot find %s", name)
|
|
}
|
|
return m[1]
|
|
}
|
|
|
|
func sorted(in []string) []string {
|
|
out := append([]string(nil), in...)
|
|
sort.Strings(out)
|
|
return out
|
|
}
|
|
|
|
func assertSameSet(t *testing.T, what string, goSide, tsSide []string) {
|
|
t.Helper()
|
|
if !reflect.DeepEqual(sorted(goSide), sorted(tsSide)) {
|
|
t.Errorf("%s differ:\n Go: %v\n TS: %v", what, sorted(goSide), sorted(tsSide))
|
|
}
|
|
}
|
|
|
|
// The Go catalogs and the frontend contract files describe the same contract.
|
|
func TestContractCatalogsMatchFrontend(t *testing.T) {
|
|
ui := readFrontendContract(t, "styles/ui-contract.ts")
|
|
if got := tsVersion(t, ui, "UI_API_VERSION"); got != UIAPIVersion {
|
|
t.Errorf("UI_API_VERSION = %s, Go UIAPIVersion = %s", got, UIAPIVersion)
|
|
}
|
|
var stableSlots, expSlots []string
|
|
for id, spec := range UISlotCatalog {
|
|
if spec.Status == ContractStable {
|
|
stableSlots = append(stableSlots, id)
|
|
} else {
|
|
expSlots = append(expSlots, id)
|
|
}
|
|
}
|
|
assertSameSet(t, "stable slots", stableSlots, tsArray(t, ui, "UI_CONTRACT_SLOTS"))
|
|
assertSameSet(t, "experimental slots", expSlots, tsArray(t, ui, "UI_CONTRACT_SLOTS_EXPERIMENTAL"))
|
|
|
|
var stableLayers, expLayers []string
|
|
for id, st := range UILayerCatalog {
|
|
if st == ContractStable {
|
|
stableLayers = append(stableLayers, id)
|
|
} else {
|
|
expLayers = append(expLayers, id)
|
|
}
|
|
}
|
|
assertSameSet(t, "stable layers", stableLayers, tsArray(t, ui, "UI_CONTRACT_LAYERS"))
|
|
assertSameSet(t, "experimental layers", expLayers, tsArray(t, ui, "UI_CONTRACT_LAYERS_EXPERIMENTAL"))
|
|
|
|
var layout []string
|
|
for opt, vals := range ThemeLayoutCatalog {
|
|
for _, v := range vals {
|
|
layout = append(layout, opt+"="+v)
|
|
}
|
|
}
|
|
assertSameSet(t, "layout options", layout, tsArray(t, ui, "UI_CONTRACT_LAYOUT_OPTIONS_EXPERIMENTAL"))
|
|
assertSameSet(t, "icon names", ThemeIconNames, tsArray(t, ui, "UI_CONTRACT_ICONS_EXPERIMENTAL"))
|
|
|
|
var locations []string
|
|
for loc, spec := range ViewLocationCatalog {
|
|
if spec.Status != ContractExperimental {
|
|
t.Errorf("view location %s is stable in Go but listed as experimental in TS", loc)
|
|
}
|
|
locations = append(locations, loc)
|
|
}
|
|
assertSameSet(t, "view locations", locations, tsArray(t, ui, "UI_CONTRACT_VIEW_LOCATIONS_EXPERIMENTAL"))
|
|
|
|
bridge := readFrontendContract(t, "lib/bridgeContract.ts")
|
|
if got := tsVersion(t, bridge, "BRIDGE_API_VERSION"); got != BridgeAPIVersion {
|
|
t.Errorf("BRIDGE_API_VERSION = %s, Go BridgeAPIVersion = %s", got, BridgeAPIVersion)
|
|
}
|
|
var stableMethods, expMethods []string
|
|
for m, spec := range BridgeMethodCatalog {
|
|
if spec.Status == ContractStable {
|
|
stableMethods = append(stableMethods, m)
|
|
} else {
|
|
expMethods = append(expMethods, m)
|
|
}
|
|
}
|
|
assertSameSet(t, "stable bridge methods", stableMethods, tsArray(t, bridge, "BRIDGE_METHODS"))
|
|
assertSameSet(t, "experimental bridge methods", expMethods, tsArray(t, bridge, "BRIDGE_METHODS_EXPERIMENTAL"))
|
|
var stableEvents, expEvents []string
|
|
for ev, st := range BridgeEventCatalog {
|
|
if st == ContractStable {
|
|
stableEvents = append(stableEvents, ev)
|
|
} else {
|
|
expEvents = append(expEvents, ev)
|
|
}
|
|
}
|
|
assertSameSet(t, "stable bridge events", stableEvents, tsArray(t, bridge, "BRIDGE_EVENTS"))
|
|
assertSameSet(t, "experimental bridge events", expEvents, tsArray(t, bridge, "BRIDGE_EVENTS_EXPERIMENTAL"))
|
|
}
|
|
|
|
// A new contributes field must be added to ContributionCatalog with a status.
|
|
func TestContributionCatalogCoversManifest(t *testing.T) {
|
|
ty := reflect.TypeOf(Contributes{})
|
|
var fields []string
|
|
for i := 0; i < ty.NumField(); i++ {
|
|
name, _, _ := strings.Cut(ty.Field(i).Tag.Get("json"), ",")
|
|
fields = append(fields, name)
|
|
}
|
|
var catalog []string
|
|
for k := range ContributionCatalog {
|
|
catalog = append(catalog, k)
|
|
}
|
|
assertSameSet(t, "contributes fields", fields, catalog)
|
|
}
|
|
|
|
func TestHostFeaturesAreStableOnly(t *testing.T) {
|
|
features := HostFeatures()
|
|
if !sort.StringsAreSorted(features) {
|
|
t.Fatal("HostFeatures must be sorted")
|
|
}
|
|
has := map[string]bool{}
|
|
for _, f := range features {
|
|
has[f] = true
|
|
}
|
|
for _, want := range []string{"bridge.hello", "bridge.vm.list", "event.theme.changed", "slot.sidebar.footer", "slot.settings.plugin.panel", "slot.vm.toolbar.menu", "contrib.themes"} {
|
|
if !has[want] {
|
|
t.Errorf("missing stable feature %s", want)
|
|
}
|
|
}
|
|
for _, unwanted := range []string{"bridge.file.pick", "contrib.views", "view.sidebar", "layer.overlay", "slot.vm.toolbar", "slot.sidebar.footer.panel", "theme.icons"} {
|
|
if has[unwanted] {
|
|
t.Errorf("experimental entry %s advertised as a feature", unwanted)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestResolveBridgeMethodAliases(t *testing.T) {
|
|
if name, spec, ok := ResolveBridgeMethod("notify"); !ok || name != "ui.notify" || spec.Status != ContractStable {
|
|
t.Fatalf("notify alias = %q %+v %v", name, spec, ok)
|
|
}
|
|
if _, spec, ok := ResolveBridgeMethod("file.pick"); !ok || spec.Status != ContractExperimental || spec.Permission != "fs:pick" {
|
|
t.Fatalf("file.pick = %+v %v", spec, ok)
|
|
}
|
|
if _, _, ok := ResolveBridgeMethod("fs.readAny"); ok {
|
|
t.Fatal("unknown bridge method resolved")
|
|
}
|
|
}
|
|
|
|
func uiManifest() Manifest {
|
|
return Manifest{Schema: 1, ID: "vendor.ui", Version: "1.0.0", Name: LocalizedText{EnUS: "UI"},
|
|
Entry: Entry{PanelDir: "ui"}, Permissions: []string{"ui:slot"}}
|
|
}
|
|
|
|
func TestValidateUIContract(t *testing.T) {
|
|
view := func(id, loc string) ViewContribution {
|
|
return ViewContribution{ID: id, Title: LocalizedText{EnUS: id}, Location: loc, Entry: "index.html"}
|
|
}
|
|
cases := []struct {
|
|
name string
|
|
mutate func(*Manifest)
|
|
wantErr string
|
|
}{
|
|
{"sidebar view", func(m *Manifest) { m.Contributes.Views = []ViewContribution{view("console", "sidebar")} }, ""},
|
|
{"unknown location", func(m *Manifest) { m.Contributes.Views = []ViewContribution{view("x", "titlebar")} }, "unknown location"},
|
|
{"too many sidebar views", func(m *Manifest) {
|
|
m.Contributes.Views = []ViewContribution{view("a", "sidebar"), view("b", "sidebar"), view("c", "sidebar")}
|
|
}, "too many views"},
|
|
{"duplicate view", func(m *Manifest) {
|
|
m.Contributes.Views = []ViewContribution{view("a", "vm.tab"), view("a", "dialog")}
|
|
}, "duplicate view"},
|
|
{"view without panelDir", func(m *Manifest) {
|
|
m.Entry.PanelDir = ""
|
|
m.Contributes.Views = []ViewContribution{view("a", "dialog")}
|
|
}, "requires entry.panelDir"},
|
|
{"view escaping entry", func(m *Manifest) {
|
|
v := view("a", "dialog")
|
|
v.Entry = "../secret.html"
|
|
m.Contributes.Views = []ViewContribution{v}
|
|
}, "invalid view"},
|
|
{"panel in unknown slot", func(m *Manifest) {
|
|
m.Contributes.Panels = []PanelContribution{{ID: "p", Slot: "titlebar.left", Entry: "p.html"}}
|
|
}, "unknown slot"},
|
|
{"panel in slot without panels", func(m *Manifest) {
|
|
m.Contributes.Panels = []PanelContribution{{ID: "p", Slot: "vm.preview.tl", Entry: "p.html"}}
|
|
}, "does not accept panel"},
|
|
{"menu outside toolbar", func(m *Manifest) {
|
|
m.Contributes.Menus = []MenuContribution{{ID: "m", Slot: "empty.state", Command: "c"}}
|
|
}, "does not accept menu"},
|
|
{"layout value", func(m *Manifest) {
|
|
m.Contributes.Themes = []ThemeContribution{{ID: "t", Name: LocalizedText{EnUS: "t"}, Layout: &ThemeLayout{SidebarMode: "hidden"}}}
|
|
}, "layout.sidebarMode"},
|
|
{"unknown icon", func(m *Manifest) {
|
|
m.Contributes.Themes = []ThemeContribution{{ID: "t", Name: LocalizedText{EnUS: "t"}, Icons: map[string]string{"rocket": "r.svg"}}}
|
|
}, "unknown icon"},
|
|
{"bridgeApi range", func(m *Manifest) { m.Engines.BridgeAPI = "^2.0.0" }, "incompatible bridgeApi"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
m := uiManifest()
|
|
tc.mutate(&m)
|
|
err := validateUIContract(m)
|
|
if tc.wantErr == "" && err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if tc.wantErr != "" && (err == nil || !strings.Contains(err.Error(), tc.wantErr)) {
|
|
t.Fatalf("err = %v, want %q", err, tc.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestContractFindings(t *testing.T) {
|
|
m := uiManifest()
|
|
m.Contributes.Views = []ViewContribution{{ID: "console", Title: LocalizedText{EnUS: "c"}, Location: "sidebar", Entry: "index.html"}}
|
|
m.Contributes.Panels = []PanelContribution{{ID: "card", Slot: "vm.card", Entry: "card.html"}, {ID: "s", Slot: "settings.plugin", Entry: "s.html"}}
|
|
m.Contributes.Themes = []ThemeContribution{{ID: "t", Name: LocalizedText{EnUS: "t"}, Layout: &ThemeLayout{Density: "compact"}}}
|
|
raw := []byte(`{"schema":1,"id":"vendor.ui","version":"1.0.0","name":{"en-US":"UI"},"futureField":true}`)
|
|
|
|
checks := map[string][]string{}
|
|
for _, f := range ContractFindings(m, raw) {
|
|
if f.Severity != ScanSeverityPending {
|
|
t.Errorf("finding %+v must be non-blocking", f)
|
|
}
|
|
checks[f.Check] = append(checks[f.Check], f.Target)
|
|
}
|
|
exp := strings.Join(checks["contract-experimental"], " ")
|
|
for _, want := range []string{"contributes.views", "panel:card", "theme:t"} {
|
|
if !strings.Contains(exp, want) {
|
|
t.Errorf("experimental findings %q miss %s", exp, want)
|
|
}
|
|
}
|
|
if strings.Contains(exp, "panel:s") {
|
|
t.Error("settings.plugin panels are stable and must not be reported")
|
|
}
|
|
if len(checks["contract-unknown-field"]) != 1 {
|
|
t.Errorf("unknown field findings = %v", checks["contract-unknown-field"])
|
|
}
|
|
if engines := strings.Join(checks["contract-engines"], " "); !strings.Contains(engines, "engines.uiApi") || !strings.Contains(engines, "engines.bridgeApi") {
|
|
t.Errorf("engines findings = %q", engines)
|
|
}
|
|
|
|
m.Engines = Engines{UIAPI: "^2.0.0", BridgeAPI: "^1.0.0"}
|
|
m.Contributes = Contributes{Panels: []PanelContribution{{ID: "s", Slot: "settings.plugin", Entry: "s.html"}}}
|
|
if got := ContractFindings(m, []byte(`{"schema":1,"id":"vendor.ui"}`)); len(got) != 0 {
|
|
t.Fatalf("a manifest using only stable entries got findings: %+v", got)
|
|
}
|
|
}
|