Files
zeekay 8feb24ea1f One name for this fork, and it is ours
The server module named github.com/hanzoai/team/server/v8 — a path that
resolves to the retired workspace's repository, not this one — while
server/public still published github.com/mattermost/mattermost/server/public.
Both are github.com/hanzoteam/server/... now, which is where this code
actually lives.

That second half is what made the binary unbootable. Upstream's public path
stayed claimed by upstream, mattermost-plugin-ai imported it, and the build
linked public/model twice; public/plugin's gob.Register then ran twice under
one name and panicked at init, before main. Reverting the rename traded the
brand for a working binary. Rebranding the plugin alongside it needs no such
trade: the agents fork is github.com/hanzoteam/agents/v2 against our public
module, so no module named mattermost/mattermost is in the graph at all.

Measured: `go list -deps ./cmd/mattermost` names exactly one public/model, and
it is ours.
2026-08-28 23:35:36 -07:00

144 lines
4.0 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package plugin
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/hanzoteam/server/server/public/model"
"github.com/hanzoteam/server/server/public/plugin/utils"
"github.com/hanzoteam/server/server/public/shared/mlog"
)
func TestAvailablePlugins(t *testing.T) {
dir, err1 := os.MkdirTemp("", "mm-plugin-test")
require.NoError(t, err1)
t.Cleanup(func() {
os.RemoveAll(dir)
})
logger := mlog.CreateConsoleTestLogger(t)
env := Environment{
pluginDir: dir,
logger: logger,
}
t.Run("Should be able to load available plugins", func(t *testing.T) {
bundle1 := model.BundleInfo{
Manifest: &model.Manifest{
Id: "someid",
Version: "1",
},
}
err := os.Mkdir(filepath.Join(dir, "plugin1"), 0700)
require.NoError(t, err)
defer os.RemoveAll(filepath.Join(dir, "plugin1"))
path := filepath.Join(dir, "plugin1", "plugin.json")
manifestJSON, jsonErr := json.Marshal(bundle1.Manifest)
require.NoError(t, jsonErr)
err = os.WriteFile(path, manifestJSON, 0644)
require.NoError(t, err)
bundles, err := env.Available()
require.NoError(t, err)
require.Len(t, bundles, 1)
})
t.Run("Should not be able to load plugins without a valid manifest file", func(t *testing.T) {
err := os.Mkdir(filepath.Join(dir, "plugin2"), 0700)
require.NoError(t, err)
defer os.RemoveAll(filepath.Join(dir, "plugin2"))
path := filepath.Join(dir, "plugin2", "manifest.json")
err = os.WriteFile(path, []byte("{}"), 0644)
require.NoError(t, err)
bundles, err := env.Available()
require.NoError(t, err)
require.Len(t, bundles, 0)
})
t.Run("Should not be able to load plugins without a manifest file", func(t *testing.T) {
err := os.Mkdir(filepath.Join(dir, "plugin3"), 0700)
require.NoError(t, err)
defer os.RemoveAll(filepath.Join(dir, "plugin3"))
bundles, err := env.Available()
require.NoError(t, err)
require.Len(t, bundles, 0)
})
}
func TestHasPluginImplementing(t *testing.T) {
pluginDir, err := os.MkdirTemp("", "mm-haspluginimpl-plugin")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(pluginDir) })
webappPluginDir, err := os.MkdirTemp("", "mm-haspluginimpl-webapp")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(webappPluginDir) })
pluginID := "test-has-plugin-implementing"
require.NoError(t, os.MkdirAll(filepath.Join(pluginDir, pluginID), 0700))
backend := filepath.Join(pluginDir, pluginID, "backend.exe")
// This plugin implements MessageHasBeenPosted but not MessageHasBeenUpdated.
utils.CompileGo(t, `
package main
import (
"github.com/hanzoteam/server/server/public/model"
"github.com/hanzoteam/server/server/public/plugin"
)
type MyPlugin struct {
plugin.MattermostPlugin
}
func (p *MyPlugin) MessageHasBeenPosted(c *plugin.Context, post *model.Post) {}
func main() {
plugin.ClientMain(&MyPlugin{})
}
`, backend)
require.NoError(t, os.WriteFile(
filepath.Join(pluginDir, pluginID, "plugin.json"),
[]byte(`{"id":"`+pluginID+`","server":{"executable":"backend.exe"}}`),
0600,
))
logger := mlog.CreateConsoleTestLogger(t)
apiImpl := func(*model.Manifest) API { return nil }
env, err := NewEnvironment(apiImpl, nil, pluginDir, webappPluginDir, logger, nil)
require.NoError(t, err)
t.Cleanup(env.Shutdown)
t.Run("no plugins registered", func(t *testing.T) {
require.False(t, env.HasPluginImplementing(MessageHasBeenPostedID))
})
_, _, err = env.Activate(pluginID)
require.NoError(t, err)
require.True(t, env.IsActive(pluginID))
t.Run("active plugin implementing the hook", func(t *testing.T) {
require.True(t, env.HasPluginImplementing(MessageHasBeenPostedID))
})
t.Run("active plugin not implementing the hook", func(t *testing.T) {
require.False(t, env.HasPluginImplementing(MessageHasBeenUpdatedID))
})
t.Run("deactivated plugin no longer counts", func(t *testing.T) {
require.True(t, env.Deactivate(pluginID))
require.False(t, env.HasPluginImplementing(MessageHasBeenPostedID))
})
}