package mcp_test import ( "os" "path/filepath" "syscall" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/mathiasbq/hyperguild/ingestion/internal/mcp" ) // hyperguild#192: brain_edit snapshotted the note with os.ReadFile by path, // which blocks on a FIFO held open by a writer, and hung the call. func TestBrainEditReturnsPromptlyOnAFIFONoteWithAWriter(t *testing.T) { dir := newBrainRepo(t) fifo := filepath.Join(dir, "wiki", "homelab", "facts", "f.md") require.NoError(t, syscall.Mkfifo(fifo, 0o644)) w, err := os.OpenFile(fifo, os.O_RDWR, 0) require.NoError(t, err) t.Cleanup(func() { _ = w.Close() }) srv := mcp.NewServer(dir, nil, nil, nil) done := make(chan map[string]any, 1) go func() { done <- call(t, srv, "brain_edit", map[string]any{"path": "wiki/homelab/facts/f.md", "old_str": "a", "new_str": "b"}) }() select { case resp := <-done: assert.Contains(t, resp, "error", "a FIFO note is refused") case <-time.After(3 * time.Second): t.Fatal("brain_edit hung on a FIFO note") } resp := call(t, srv, "brain_edit", map[string]any{"path": "wiki/homelab/facts/n.md", "old_str": "beta", "new_str": "BETA"}) require.NotContains(t, resp, "error", "positive control: a regular note is edited: %v", resp["error"]) } // hyperguild#192: the edit lock ".lock" was opened by path with // O_CREATE, so a planted dangling .lock symlink made a successful edit create // a file at the link's target, outside the brain or elsewhere in it. func TestBrainEditDoesNotCreateAFileThroughADanglingLockSymlink(t *testing.T) { for name, target := range map[string]func(t *testing.T, dir string) string{ "outside": func(t *testing.T, _ string) string { return filepath.Join(t.TempDir(), "created") }, "in-brain": func(t *testing.T, dir string) string { return filepath.Join(dir, "raw", "created") }, } { t.Run(name, func(t *testing.T) { dir := newBrainRepo(t) require.NoError(t, os.MkdirAll(filepath.Join(dir, "raw"), 0o755)) tgt := target(t, dir) require.NoError(t, os.Symlink(tgt, filepath.Join(dir, "wiki", "homelab", "facts", "n.md.lock"))) srv := mcp.NewServer(dir, nil, nil, nil) _ = call(t, srv, "brain_edit", map[string]any{"path": "wiki/homelab/facts/n.md", "old_str": "beta", "new_str": "BETA"}) _, err := os.Lstat(tgt) assert.True(t, os.IsNotExist(err), "a file was created at the lock symlink's target") }) } }