commands_test.go

 1package commands
 2
 3import (
 4	"os"
 5	"path/filepath"
 6	"testing"
 7
 8	"github.com/stretchr/testify/require"
 9)
10
11func TestLoadFromSource_NonExistentDir(t *testing.T) {
12	t.Parallel()
13
14	dir := filepath.Join(t.TempDir(), "does-not-exist")
15
16	cmds, err := loadFromSource(commandSource{path: dir, prefix: userCommandPrefix})
17	require.NoError(t, err)
18	require.Empty(t, cmds)
19
20	// directory must NOT have been created
21	_, statErr := os.Stat(dir)
22	require.True(t, os.IsNotExist(statErr))
23}
24
25func TestLoadFromSource_ExistingDir(t *testing.T) {
26	t.Parallel()
27
28	dir := t.TempDir()
29	require.NoError(t, os.WriteFile(filepath.Join(dir, "hello.md"), []byte("say hello"), 0o644))
30
31	cmds, err := loadFromSource(commandSource{path: dir, prefix: userCommandPrefix})
32	require.NoError(t, err)
33	require.Len(t, cmds, 1)
34	require.Equal(t, "user:hello", cmds[0].ID)
35	require.Equal(t, "say hello", cmds[0].Content)
36}
37
38func TestLoadAll_MixedSources(t *testing.T) {
39	t.Parallel()
40
41	existing := t.TempDir()
42	require.NoError(t, os.WriteFile(filepath.Join(existing, "cmd.md"), []byte("content"), 0o644))
43
44	missing := filepath.Join(t.TempDir(), "nope")
45
46	cmds, err := loadAll([]commandSource{
47		{path: existing, prefix: userCommandPrefix},
48		{path: missing, prefix: projectCommandPrefix},
49	})
50	require.NoError(t, err)
51	require.Len(t, cmds, 1)
52	require.Equal(t, "user:cmd", cmds[0].ID)
53}