Files
Reims-Go/internal/plugin/rule_settings_test.go
T

74 lines
3.1 KiB
Go

package plugin
import (
"reflect"
"testing"
"github.com/Nwflower/reims-go/internal/i18n"
)
func TestRuleEnvironmentUsesVisiblePluginSettings(t *testing.T) {
reg := NewRegistry(nil)
reg.ruleSettings = func(pluginID, vmName string, maskSecrets bool) map[string]any {
if pluginID != "local.capture-shortcut" || vmName != "test-vm" || !maskSecrets {
t.Fatalf("unexpected settings lookup: %q %q %v", pluginID, vmName, maskSecrets)
}
return map[string]any{"shortcut": "Ctrl+F12", "autoRelease": false, "token": "private"}
}
intent := StartIntent{PluginID: "local.capture-shortcut"}
schema := &SettingsSchema{Scope: "vm", Properties: map[string]SettingProperty{
"shortcut": {}, "autoRelease": {}, "token": {Secret: true},
}}
rule := RuleHook{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_TOKEN": "token",
"PATH": "shortcut",
}}}}
reg.applyRuleEnvSettings(&intent, rule, schema, "test-vm")
want := map[string]string{
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS": "Ctrl+F12",
"REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_RELEASE_ON_LEAVE": "false",
}
if !reflect.DeepEqual(intent.Env, want) {
t.Fatalf("unexpected environment: %#v", intent.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)
}
}