package brain_test import ( "os" "path/filepath" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/mathiasbq/hyperguild/ingestion/internal/brain" ) // hyperguild#178: StripUnresolvedLinks runs before every brain_write and // brain_update. An unreadable directory under knowledge/ or wiki/ made it // error, the write was refused and the learning lost. When the link index // cannot be built, the content is now returned unchanged (no link is // unwrapped, none is invented) and no error stops the write. func TestStripUnresolvedLinksKeepsContentWhenTheIndexCannotBeBuilt(t *testing.T) { if os.Geteuid() == 0 { t.Skip("root ignores directory permissions") } b := t.TempDir() require.NoError(t, os.MkdirAll(filepath.Join(b, "wiki", "w", "facts"), 0o755)) require.NoError(t, os.WriteFile(filepath.Join(b, "wiki", "w", "facts", "real.md"), []byte("x"), 0o644)) locked := filepath.Join(b, "wiki", "other", "locked") require.NoError(t, os.MkdirAll(locked, 0o755)) require.NoError(t, os.Chmod(locked, 0o000)) t.Cleanup(func() { _ = os.Chmod(locked, 0o755) }) in := "learning [[real]] and [[gone]]" out, unlinked, err := brain.StripUnresolvedLinks(b, in) require.NoError(t, err) assert.Equal(t, in, out, "content unchanged: no link unwrapped on a partial index") assert.Empty(t, unlinked) require.NoError(t, os.Chmod(locked, 0o755)) out, _, err = brain.StripUnresolvedLinks(b, in) require.NoError(t, err) assert.NotContains(t, out, "[[gone]]", "positive control: with a readable tree the strip runs") }