Repository navigation
Expand file tree
/
Copy pathplugin_middleware_cli_test.go
More file actions
70 lines (66 loc) · 2.93 KB
/
Copy pathplugin_middleware_cli_test.go
File metadata and controls
70 lines (66 loc) · 2.93 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
package main
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/middleware"
"github.com/yetone/magpie/internal/plugin"
)
// magpie plugin says which plugins are gateway middleware and their hooks,
// or why one didn't load; a middleware plugin has no provider to sign in
// to, so the list had nothing under it.
func TestPluginListSaysMiddleware(t *testing.T) {
groupsHome(t)
dir := t.TempDir()
good := filepath.Join(dir, "alias.middleware.js")
bad := filepath.Join(dir, "bad.middleware.js")
os.WriteFile(good, []byte("export const events = [\"message_start\"]\nexport function onRequest(b, ctx) {}\nexport function onEvent(e, ctx) {}\n"), 0o644)
os.WriteFile(bad, []byte("export function onRequest( {\n"), 0o644)
for _, spec := range []string{good, bad} {
if _, err := plugin.Add(context.Background(), spec); err != nil {
t.Fatal(err)
}
}
middleware.Reload()
out, err := stdoutOf(t, func() error { return listPlugins(context.Background(), false) })
if err != nil {
t.Fatal(err)
}
if !strings.Contains(out, "gateway middleware onRequest, onEvent") {
t.Errorf("the middleware's hooks aren't listed:\n%s", out)
}
if !strings.Contains(out, "didn't load: ") || !strings.Contains(out, "SyntaxError") {
t.Errorf("the broken middleware doesn't say why:\n%s", out)
}
js, _ := stdoutOf(t, func() error { return listPlugins(context.Background(), true) })
if !strings.Contains(js, `"middleware"`) || !strings.Contains(js, `"message_start"`) {
t.Errorf("--json has no middleware:\n%s", js)
}
}
// magpie plugin off, on and rm take a plugin's short name, as options
// does: `magpie plugin off think-tags` said there was no such plugin.
func TestPluginOnOffRmShortName(t *testing.T) {
groupsHome(t)
dir := filepath.Join(t.TempDir(), "middleware-zz-tags")
os.MkdirAll(dir, 0o755)
os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{"name": "@magpie-community/middleware-zz-tags", "magpie": {"middleware": "./zz.middleware.js"}}`), 0o644)
os.WriteFile(filepath.Join(dir, "zz.middleware.js"), []byte("export function onRequest(b) {}\n"), 0o644)
if _, err := plugin.Add(context.Background(), dir); err != nil {
t.Fatal(err)
}
off := func() bool { return plugin.Load().Plugins[0].Off }
if _, err := stdoutOf(t, func() error { return pluginCmd([]string{"plugin", "off", "zz-tags"}) }); err != nil || !off() {
t.Fatalf("off by short name: err %v, off %v", err, off())
}
if _, err := stdoutOf(t, func() error { return pluginCmd([]string{"plugin", "on", "zz-tags"}) }); err != nil || off() {
t.Fatalf("on by short name: err %v, off %v", err, off())
}
if _, err := stdoutOf(t, func() error { return pluginCmd([]string{"plugin", "rm", "zz-tags"}) }); err != nil || len(plugin.Load().Plugins) != 0 {
t.Fatalf("rm by short name: err %v, left %v", err, plugin.Load().Plugins)
}
if err := pluginCmd([]string{"plugin", "off", "nothing-here"}); err == nil {
t.Error("a name no plugin has was taken")
}
}