-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapi_test.go
More file actions
124 lines (104 loc) · 3.23 KB
/
Copy pathapi_test.go
File metadata and controls
124 lines (104 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"context"
"github.com/unxed/f4/vfs"
"github.com/unxed/vtinput"
"github.com/unxed/vtui"
"testing"
"time"
)
type mockVFSProvider struct{}
func (m *mockVFSProvider) Name() string { return "mock-vfs-provider" }
func (m *mockVFSProvider) Priority() int { return 1 }
func (m *mockVFSProvider) CanOpen(ctx context.Context, parent vfs.VFS, path string) bool {
return path == "api-test-path"
}
func (m *mockVFSProvider) Open(ctx context.Context, parent vfs.VFS, path string) (vfs.VFS, error) {
return nil, nil
}
type mockHighlighter struct{}
func (m *mockHighlighter) Highlight(line string, prev any, base uint64) ([]uint64, any) {
return nil, nil
}
type mockHighlighterProvider struct{}
func (m *mockHighlighterProvider) Name() string { return "mock-highlighter" }
func (m *mockHighlighterProvider) Match(filename, content string) bool {
return filename == "api-test.mock"
}
func (m *mockHighlighterProvider) Create(filename, content string) vtui.Highlighter {
return &mockHighlighter{}
}
func TestCoreAPI_GetVersion(t *testing.T) {
api := &coreAPI{}
ver := api.GetVersion()
if ver != "v0.1.1-alpha" {
t.Errorf("Unexpected version: %q", ver)
}
}
func TestCoreAPI_Log(t *testing.T) {
api := &coreAPI{}
// Should not panic
api.Log("test log message")
}
func TestCoreAPI_Message(t *testing.T) {
vtui.FrameManager.Init(vtui.NewSilentScreenBuf())
SetDefaultF4Palette()
api := &coreAPI{}
api.Message("api test message")
// Process tasks
timeout := time.After(1 * time.Second)
found := false
Loop:
for {
select {
case task := <-vtui.FrameManager.TaskChan:
task()
if vtui.FrameManager.GetTopFrameType() == vtui.TypeDialog {
found = true
break Loop
}
case <-timeout:
break Loop
}
}
if !found {
t.Error("api.Message did not result in a dialog being pushed to FrameManager")
}
// Clean up dialog
vtui.FrameManager.Pop()
}
func TestCoreAPI_Registrations(t *testing.T) {
api := &coreAPI{}
// 1. RegisterVFSProvider
p := &mockVFSProvider{}
api.RegisterVFSProvider(p)
found := vfs.FindProvider(context.Background(), nil, "api-test-path")
if found != p {
t.Error("VFS provider was not registered correctly")
}
// 2. RegisterHighlighter
hp := &mockHighlighterProvider{}
api.RegisterHighlighter(hp)
hl := vtui.GetHighlighter("api-test.mock", "")
if _, ok := hl.(*mockHighlighter); !ok {
t.Error("Highlighter was not registered correctly")
}
// 3. RegisterDrive
initialLen := len(DriveRegistry)
api.RegisterDrive("MockDrive", func() vfs.VFS { return nil })
if len(DriveRegistry) != initialLen+1 || DriveRegistry[len(DriveRegistry)-1].Name != "MockDrive" {
t.Error("Drive was not registered correctly")
}
// 4. RegisterGlobalHotkey
initialHotkeys := len(GlobalHotkeys)
api.RegisterGlobalHotkey(0x41, vtinput.ShiftPressed, func(app vfs.App) {})
if len(GlobalHotkeys) != initialHotkeys+1 || GlobalHotkeys[len(GlobalHotkeys)-1].VK != 0x41 {
t.Error("Hotkey was not registered correctly")
}
// 5. RegisterPluginMenuItem
initialPlugins := len(PluginMenuItems)
api.RegisterPluginMenuItem("My Plugin", func(app vfs.App) {})
if len(PluginMenuItems) != initialPlugins+1 || PluginMenuItems[len(PluginMenuItems)-1].Label != "My Plugin" {
t.Error("Plugin menu item was not registered correctly")
}
}