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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
package slashcommands
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
"codeberg.org/snonux/hexai/internal/appconfig"
"codeberg.org/snonux/hexai/internal/promptstore"
)
func TestNewSyncer_Disabled(t *testing.T) {
cfg := appconfig.App{
MCPSlashCommandSync: false,
}
syncer, err := NewSyncer(cfg)
if err != nil {
t.Fatalf("NewSyncer() with disabled sync failed: %v", err)
}
if syncer.enabled {
t.Error("NewSyncer() should create disabled syncer when MCPSlashCommandSync is false")
}
}
func TestNewSyncer_NoDirectory(t *testing.T) {
cfg := appconfig.App{
MCPSlashCommandSync: true,
MCPSlashCommandDir: "",
}
_, err := NewSyncer(cfg)
if err == nil {
t.Error("NewSyncer() should fail when directory is not configured")
}
}
func TestNewSyncer_CreatesDirectory(t *testing.T) {
tmpDir := t.TempDir()
testDir := filepath.Join(tmpDir, "test-commands")
cfg := appconfig.App{
MCPSlashCommandSync: true,
MCPSlashCommandDir: testDir,
}
syncer, err := NewSyncer(cfg)
if err != nil {
t.Fatalf("NewSyncer() failed: %v", err)
}
if !syncer.enabled {
t.Error("NewSyncer() should create enabled syncer")
}
// Verify directory was created
if _, err := os.Stat(testDir); os.IsNotExist(err) {
t.Error("NewSyncer() should create commands directory")
}
}
func TestNewSyncer_ExpandsHomeDirectory(t *testing.T) {
tmpDir := t.TempDir()
home := os.Getenv("HOME")
// Set temporary HOME for test
os.Setenv("HOME", tmpDir)
defer os.Setenv("HOME", home)
cfg := appconfig.App{
MCPSlashCommandSync: true,
MCPSlashCommandDir: "~/test-commands",
}
syncer, err := NewSyncer(cfg)
if err != nil {
t.Fatalf("NewSyncer() failed: %v", err)
}
expectedDir := filepath.Join(tmpDir, "test-commands")
if syncer.commandsDir != expectedDir {
t.Errorf("NewSyncer() commandsDir = %q, want %q", syncer.commandsDir, expectedDir)
}
}
func TestSync_Disabled(t *testing.T) {
syncer := &Syncer{enabled: false}
prompt := &promptstore.Prompt{Name: "test"}
err := syncer.Sync(prompt, OpCreate)
if err != nil {
t.Errorf("Sync() with disabled syncer should not error, got: %v", err)
}
}
func TestSync_Create(t *testing.T) {
tmpDir := t.TempDir()
syncer := &Syncer{
commandsDir: tmpDir,
enabled: true,
}
prompt := &promptstore.Prompt{
Name: "test-prompt",
Title: "Test Prompt",
Description: "A test prompt",
Arguments: []promptstore.PromptArgument{
{Name: "arg1", Description: "First argument", Required: true},
},
Messages: []promptstore.PromptMessage{
{Role: "user", Content: promptstore.MessageContent{Type: "text", Text: "Hello {{arg1}}"}},
},
Tags: []string{"test", "example"},
Created: time.Now(),
Updated: time.Now(),
}
err := syncer.Sync(prompt, OpCreate)
if err != nil {
t.Fatalf("Sync() failed: %v", err)
}
// Verify file was created
filename := filepath.Join(tmpDir, "hexai-test-prompt.md")
content, err := os.ReadFile(filename)
if err != nil {
t.Fatalf("Failed to read synced file: %v", err)
}
contentStr := string(content)
if !strings.Contains(contentStr, "# Test Prompt") {
t.Error("Synced file should contain prompt title")
}
if !strings.Contains(contentStr, "A test prompt") {
t.Error("Synced file should contain description")
}
if !strings.Contains(contentStr, "Hello {{arg1}}") {
t.Error("Synced file should contain message template")
}
if !strings.Contains(contentStr, "test, example") {
t.Error("Synced file should contain tags")
}
}
func TestSync_Update(t *testing.T) {
tmpDir := t.TempDir()
syncer := &Syncer{
commandsDir: tmpDir,
enabled: true,
}
// Create initial prompt
prompt := &promptstore.Prompt{
Name: "test-prompt",
Title: "Original Title",
Messages: []promptstore.PromptMessage{{Role: "user", Content: promptstore.MessageContent{Text: "Original"}}},
}
if err := syncer.Sync(prompt, OpCreate); err != nil {
t.Fatalf("Initial Sync() failed: %v", err)
}
// Update prompt
prompt.Title = "Updated Title"
prompt.Messages[0].Content.Text = "Updated"
if err := syncer.Sync(prompt, OpUpdate); err != nil {
t.Fatalf("Update Sync() failed: %v", err)
}
// Verify file was updated
filename := filepath.Join(tmpDir, "hexai-test-prompt.md")
content, err := os.ReadFile(filename)
if err != nil {
t.Fatalf("Failed to read updated file: %v", err)
}
contentStr := string(content)
if !strings.Contains(contentStr, "Updated Title") {
t.Error("Updated file should contain new title")
}
if strings.Contains(contentStr, "Original Title") {
t.Error("Updated file should not contain old title")
}
}
func TestDelete_Disabled(t *testing.T) {
syncer := &Syncer{enabled: false}
err := syncer.Delete("test")
if err != nil {
t.Errorf("Delete() with disabled syncer should not error, got: %v", err)
}
}
func TestDelete_ExistingFile(t *testing.T) {
tmpDir := t.TempDir()
syncer := &Syncer{
commandsDir: tmpDir,
enabled: true,
}
// Create a test file
filename := filepath.Join(tmpDir, "hexai-test-prompt.md")
if err := os.WriteFile(filename, []byte("test content"), 0o644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Delete it
if err := syncer.Delete("test-prompt"); err != nil {
t.Fatalf("Delete() failed: %v", err)
}
// Verify file was deleted
if _, err := os.Stat(filename); !os.IsNotExist(err) {
t.Error("Delete() should remove the file")
}
}
func TestDelete_NonExistentFile(t *testing.T) {
tmpDir := t.TempDir()
syncer := &Syncer{
commandsDir: tmpDir,
enabled: true,
}
// Delete non-existent file (should be idempotent)
err := syncer.Delete("non-existent")
if err != nil {
t.Errorf("Delete() of non-existent file should not error, got: %v", err)
}
}
func TestSyncAll(t *testing.T) {
tmpDir := t.TempDir()
syncer := &Syncer{
commandsDir: tmpDir,
enabled: true,
}
// Create a mock store with test prompts
storeDir := t.TempDir()
store, err := promptstore.NewJSONLStore(storeDir)
if err != nil {
t.Fatalf("Failed to create test store: %v", err)
}
// Add test prompts
prompts := []*promptstore.Prompt{
{Name: "prompt1", Title: "Prompt 1", Messages: []promptstore.PromptMessage{{Role: "user", Content: promptstore.MessageContent{Text: "Test 1"}}}},
{Name: "prompt2", Title: "Prompt 2", Messages: []promptstore.PromptMessage{{Role: "user", Content: promptstore.MessageContent{Text: "Test 2"}}}},
{Name: "prompt3", Title: "Prompt 3", Messages: []promptstore.PromptMessage{{Role: "user", Content: promptstore.MessageContent{Text: "Test 3"}}}},
}
for _, p := range prompts {
if err := store.Create(p); err != nil {
t.Fatalf("Failed to create test prompt: %v", err)
}
}
// Sync all
if err := syncer.SyncAll(store); err != nil {
t.Fatalf("SyncAll() failed: %v", err)
}
// Verify all files were created
for _, p := range prompts {
filename := filepath.Join(tmpDir, MakeFilename(p.Name))
if _, err := os.Stat(filename); os.IsNotExist(err) {
t.Errorf("SyncAll() should create file for prompt %q", p.Name)
}
}
}
func TestSyncAll_Disabled(t *testing.T) {
syncer := &Syncer{enabled: false}
err := syncer.SyncAll(nil)
if err == nil {
t.Error("SyncAll() with disabled syncer should return error")
}
}
func TestAtomicWrite(t *testing.T) {
tmpDir := t.TempDir()
syncer := &Syncer{
commandsDir: tmpDir,
enabled: true,
}
filename := filepath.Join(tmpDir, "test-atomic.md")
content := []byte("test content")
err := syncer.atomicWrite(filename, content)
if err != nil {
t.Fatalf("atomicWrite() failed: %v", err)
}
// Verify file was created with correct content
actual, err := os.ReadFile(filename)
if err != nil {
t.Fatalf("Failed to read file: %v", err)
}
if string(actual) != string(content) {
t.Errorf("atomicWrite() content = %q, want %q", actual, content)
}
// Verify no temp files left behind
entries, err := os.ReadDir(tmpDir)
if err != nil {
t.Fatalf("Failed to read directory: %v", err)
}
for _, entry := range entries {
if strings.HasPrefix(entry.Name(), ".tmp-") {
t.Errorf("atomicWrite() left temp file: %s", entry.Name())
}
}
}
func TestConcurrentSync(t *testing.T) {
tmpDir := t.TempDir()
syncer := &Syncer{
commandsDir: tmpDir,
enabled: true,
}
// Sync multiple prompts concurrently
done := make(chan bool)
for i := 0; i < 10; i++ {
go func(n int) {
prompt := &promptstore.Prompt{
Name: "prompt-" + string(rune('0'+n)),
Title: "Concurrent Test",
Messages: []promptstore.PromptMessage{{Role: "user", Content: promptstore.MessageContent{Text: "Test"}}},
}
if err := syncer.Sync(prompt, OpCreate); err != nil {
t.Errorf("Concurrent Sync() failed: %v", err)
}
done <- true
}(i)
}
// Wait for all goroutines
for i := 0; i < 10; i++ {
<-done
}
// Verify all files were created
entries, err := os.ReadDir(tmpDir)
if err != nil {
t.Fatalf("Failed to read directory: %v", err)
}
if len(entries) != 10 {
t.Errorf("Concurrent sync created %d files, want 10", len(entries))
}
}
|