|
| 1 | +package export_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + exp "github.com/xinnjie/onekeymap-cli/internal/export" |
| 10 | + "github.com/xinnjie/onekeymap-cli/internal/keymap" |
| 11 | + "github.com/xinnjie/onekeymap-cli/pkg/pluginapi" |
| 12 | + keymapv1 "github.com/xinnjie/onekeymap-cli/protogen/keymap/v1" |
| 13 | +) |
| 14 | + |
| 15 | +func newKeymapWithTwoBindings(action string, k1, k2 string) (*keymapv1.Keymap, *keymapv1.Action) { |
| 16 | + a := keymap.NewActioinBinding(action, k1) |
| 17 | + a.Bindings = append(a.Bindings, &keymapv1.KeybindingReadable{KeyChords: keymap.MustParseKeyBinding(k2).KeyChords}) |
| 18 | + return &keymapv1.Keymap{Actions: []*keymapv1.Action{a}}, a |
| 19 | +} |
| 20 | + |
| 21 | +func TestMarker_ImplicitSkipForUnexportedBindings(t *testing.T) { |
| 22 | + km, a := newKeymapWithTwoBindings("actions.test.dual", "cmd+a", "cmd+c") |
| 23 | + m := exp.NewMarker(km) |
| 24 | + |
| 25 | + // Export only first binding |
| 26 | + m.MarkExported(a.GetName(), a.GetBindings()[0].GetKeyChords()) |
| 27 | + |
| 28 | + rep := m.Report() |
| 29 | + require.Len(t, rep.SkipActions, 1) |
| 30 | + // The skipped one should be the second binding with default not supported |
| 31 | + sk := rep.SkipActions[0] |
| 32 | + assert.Equal(t, a.GetName(), sk.Action) |
| 33 | + assert.ErrorIs(t, sk.Error, pluginapi.ErrActionNotSupported) |
| 34 | +} |
| 35 | + |
| 36 | +func TestMarker_ExplicitKeySkip(t *testing.T) { |
| 37 | + km, a := newKeymapWithTwoBindings("actions.test.dual", "cmd+a", "cmd+b") |
| 38 | + m := exp.NewMarker(km) |
| 39 | + |
| 40 | + // Export first, explicitly skip second |
| 41 | + m.MarkExported(a.GetName(), a.GetBindings()[0].GetKeyChords()) |
| 42 | + m.MarkSkippedForReason( |
| 43 | + a.GetName(), |
| 44 | + keymap.MustParseKeyBinding("cmd+b").KeyChords, |
| 45 | + &pluginapi.EditorSupportOnlyOneKeybindingPerActionError{ |
| 46 | + SkipKeybinding: keymap.MustParseKeyBinding("cmd+b").KeyChords, |
| 47 | + }, |
| 48 | + ) |
| 49 | + |
| 50 | + rep := m.Report() |
| 51 | + require.Len(t, rep.SkipActions, 1) |
| 52 | + sk := rep.SkipActions[0] |
| 53 | + assert.Equal(t, a.GetName(), sk.Action) |
| 54 | + var ose *pluginapi.EditorSupportOnlyOneKeybindingPerActionError |
| 55 | + require.ErrorAs(t, sk.Error, &ose) |
| 56 | + require.NotNil(t, ose) |
| 57 | + require.NotNil(t, ose.SkipKeybinding) |
| 58 | + assert.Equal(t, ose.SkipKeybinding, keymap.MustParseKeyBinding("cmd+b").KeyChords) |
| 59 | +} |
| 60 | + |
| 61 | +func TestMarker_ActionLevelSkipAppliesToAllUnexported(t *testing.T) { |
| 62 | + km, a := newKeymapWithTwoBindings("actions.test.multi", "cmd+a", "cmd+b") |
| 63 | + m := exp.NewMarker(km) |
| 64 | + |
| 65 | + // Export only first, mark action-level skip |
| 66 | + note := "not available on this editor" |
| 67 | + m.MarkExported(a.GetName(), keymap.MustParseKeyBinding("cmd+a").KeyChords) |
| 68 | + m.MarkSkippedForReason(a.GetName(), nil, &pluginapi.NotSupportedError{Note: note}) |
| 69 | + |
| 70 | + rep := m.Report() |
| 71 | + require.Len(t, rep.SkipActions, 1) |
| 72 | + sk := rep.SkipActions[0] |
| 73 | + assert.Equal(t, a.GetName(), sk.Action) |
| 74 | + require.ErrorContains(t, sk.Error, note) |
| 75 | +} |
| 76 | + |
| 77 | +func TestMarker_PerKeyReasonOverridesActionLevel(t *testing.T) { |
| 78 | + km, a := newKeymapWithTwoBindings("actions.test.override", "cmd+x", "cmd+b") |
| 79 | + m := exp.NewMarker(km) |
| 80 | + |
| 81 | + // No exported; set action-level reason and a different per-key reason for second |
| 82 | + m.MarkSkippedForReason(a.GetName(), nil, &pluginapi.NotSupportedError{Note: "action"}) |
| 83 | + m.MarkSkippedForReason( |
| 84 | + a.GetName(), |
| 85 | + keymap.MustParseKeyBinding("cmd+b").KeyChords, |
| 86 | + &pluginapi.EditorSupportOnlyOneKeybindingPerActionError{ |
| 87 | + SkipKeybinding: keymap.MustParseKeyBinding("cmd+b").KeyChords, |
| 88 | + }, |
| 89 | + ) |
| 90 | + |
| 91 | + rep := m.Report() |
| 92 | + // Both keybindings should be reported as skipped |
| 93 | + require.Len(t, rep.SkipActions, 2) |
| 94 | + // Identify entries by error type and its embedded keybinding |
| 95 | + var first, second *pluginapi.SkipAction |
| 96 | + for i := range rep.SkipActions { |
| 97 | + var ose *pluginapi.EditorSupportOnlyOneKeybindingPerActionError |
| 98 | + if errors.As(rep.SkipActions[i].Error, &ose) { |
| 99 | + second = &rep.SkipActions[i] |
| 100 | + } else { |
| 101 | + first = &rep.SkipActions[i] |
| 102 | + } |
| 103 | + } |
| 104 | + require.NotNil(t, first) |
| 105 | + require.NotNil(t, second) |
| 106 | + // First uses action-level reason |
| 107 | + require.ErrorContains(t, first.Error, "action") |
| 108 | + // Second uses per-key reason |
| 109 | + var ose2 *pluginapi.EditorSupportOnlyOneKeybindingPerActionError |
| 110 | + require.ErrorAs(t, second.Error, &ose2) |
| 111 | + assert.Equal(t, ose2.SkipKeybinding, keymap.MustParseKeyBinding("cmd+b").KeyChords) |
| 112 | +} |
0 commit comments