Files
Reims-Go/internal/plugin/rule_settings_test.go
T
YXC0915 01ec599088 Merge main and resolve plugin runtime compatibility
Preserve plugin navigation and generate the runtime warning messages. Read startup overrides using the canonical VM settings key and keep set_env action order.
2026-12-04 15:52:08 +08:00

111 lines
4.3 KiB
Go

package plugin
import (
"context"
"reflect"
"testing"
"github.com/Nwflower/reims-go/internal/i18n"
)
func TestRuleEnvironmentUsesVisiblePluginSettings(t *testing.T) {
const pluginID = "local.capture-shortcut"
const vmKey = "builtin.guest.macos:test-vm"
reg := NewRegistry(nil)
reg.ruleSettings = func(id, key string, maskSecrets bool) map[string]any {
if id != pluginID || !maskSecrets {
t.Errorf("unexpected settings lookup: %q %q %v", id, key, maskSecrets)
return nil
}
switch key {
case vmKey:
return map[string]any{"shortcut": "Ctrl+F12", "autoRelease": false, "token": "private"}
case "":
return map[string]any{"shortcut": "Ctrl+F11", "label": "app"}
default:
t.Errorf("unexpected VM settings lookup: %q", key)
return nil
}
}
schema := &SettingsSchema{Scope: "vm", Properties: map[string]SettingProperty{
"shortcut": {Scope: "both"}, "autoRelease": {}, "label": {Scope: "app"}, "token": {Secret: true},
}}
rule := RuleHook{Event: EventVMBeforeStart, Actions: []RuleAction{
{Kind: "set_env", EnvFromSettings: map[string]string{
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS": "shortcut",
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_RELEASE_ON_LEAVE": "autoRelease",
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_LABEL": "label",
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_TOKEN": "token",
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_OVERRIDE": "shortcut",
"PATH": "shortcut",
}},
{Kind: "set_env", Env: map[string]string{"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_OVERRIDE": "fixed"}},
}}
if err := reg.Register(PluginDef{
Manifest: Manifest{
ID: pluginID,
Version: "1.0.0",
Permissions: []string{"vm:intent"},
Settings: schema,
Contributes: Contributes{RuleHooks: []RuleHook{rule}},
},
Source: SourceLocal,
Desired: true,
}); err != nil {
t.Fatal(err)
}
reg.Reconcile(context.Background())
intents, err := reg.Hooks().DispatchBeforeStart(context.Background(), BeforeStartPayload{
VMName: "test-vm", VMSettingsKey: vmKey,
})
if err != nil || len(intents) != 1 {
t.Fatalf("unexpected startup intents: %#v, %v", intents, err)
}
want := map[string]string{
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS": "Ctrl+F12",
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_RELEASE_ON_LEAVE": "false",
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_LABEL": "app",
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_OVERRIDE": "fixed",
}
if !reflect.DeepEqual(intents[0].Env, want) {
t.Fatalf("unexpected environment: %#v", intents[0].Env)
}
}
func TestRuntimeUnavailableSettings(t *testing.T) {
const capability = "window-release-shortcut"
manifest := Manifest{
ID: "local.capture-shortcut",
Settings: &SettingsSchema{Properties: map[string]SettingProperty{
"releaseKeys": {Title: LocalizedText{ZhCN: "释放快捷键"}},
"releaseOnLeave": {Title: LocalizedText{ZhCN: "鼠标移出时释放"}},
"private": {Secret: true},
}},
Contributes: Contributes{RuleHooks: []RuleHook{{
Event: EventVMBeforeStart,
Conditions: []RuleCondition{{Fact: "runtime.capabilities", Operator: "contains", Value: capability}},
Actions: []RuleAction{{Kind: "set_env", EnvFromSettings: map[string]string{
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS": "releaseKeys",
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_RELEASE_ON_LEAVE": "releaseOnLeave",
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_PRIVATE": "private",
}}},
}}},
}
locale := i18n.Locale("zh-CN")
if got := RuntimeUnavailableSettings(manifest, nil, locale); !reflect.DeepEqual(got, []string{"释放快捷键", "鼠标移出时释放"}) {
t.Fatalf("unexpected settings for incompatible runtime: %v", got)
}
if got := RuntimeUnavailableSettings(manifest, []string{capability}, locale); len(got) != 0 {
t.Fatalf("compatible runtime should have no warning: %v", got)
}
manifest.Contributes.RuleHooks = append(manifest.Contributes.RuleHooks, RuleHook{
Event: EventVMBeforeStart,
Actions: []RuleAction{{Kind: "set_env", EnvFromSettings: map[string]string{
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS": "releaseKeys",
}}},
})
if got := RuntimeUnavailableSettings(manifest, nil, locale); !reflect.DeepEqual(got, []string{"鼠标移出时释放"}) {
t.Fatalf("settings covered by a fallback should have no warning: %v", got)
}
}