// Vikunja is a to-do list application to facilitate your life. // Copyright 2018-present Vikunja and contributors. All rights reserved. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . package richtext import ( "testing" "code.vikunja.io/api/pkg/db" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestMarkdownToHTMLWithMentions(t *testing.T) { t.Run("known mention is rebuilt", func(t *testing.T) { db.LoadAndAssertFixtures(t) s := db.NewSession() defer s.Close() got, err := MarkdownToHTMLWithMentions(s, "hi @user1") require.NoError(t, err) assert.Equal(t, `

hi @user1

`, got) }) t.Run("unknown mention stays literal text", func(t *testing.T) { db.LoadAndAssertFixtures(t) s := db.NewSession() defer s.Close() got, err := MarkdownToHTMLWithMentions(s, "hi @nosuchuser") require.NoError(t, err) assert.Equal(t, "

hi @nosuchuser

", got) }) t.Run("mention next to punctuation", func(t *testing.T) { db.LoadAndAssertFixtures(t) s := db.NewSession() defer s.Close() got, err := MarkdownToHTMLWithMentions(s, "cc @user1, please review") require.NoError(t, err) assert.Equal(t, `

cc @user1, please review

`, got) }) t.Run("multiple mentions resolve in one pass", func(t *testing.T) { db.LoadAndAssertFixtures(t) s := db.NewSession() defer s.Close() got, err := MarkdownToHTMLWithMentions(s, "ping @user1 and @user2") require.NoError(t, err) assert.Contains(t, got, `@user1`) assert.Contains(t, got, `@user2`) }) t.Run("email is not a mention", func(t *testing.T) { db.LoadAndAssertFixtures(t) s := db.NewSession() defer s.Close() got, err := MarkdownToHTMLWithMentions(s, "reach me at user1@example.com please") require.NoError(t, err) assert.NotContains(t, got, "mention-user") }) t.Run("mention inside code span is ignored", func(t *testing.T) { db.LoadAndAssertFixtures(t) s := db.NewSession() defer s.Close() got, err := MarkdownToHTMLWithMentions(s, "use `@user1` literally") require.NoError(t, err) assert.NotContains(t, got, "mention-user") assert.Contains(t, got, "@user1") }) t.Run("mention inside task list item", func(t *testing.T) { db.LoadAndAssertFixtures(t) s := db.NewSession() defer s.Close() got, err := MarkdownToHTMLWithMentions(s, "- [ ] ping @user1") require.NoError(t, err) assert.Contains(t, got, `data-type="taskItem"`) assert.Contains(t, got, `@user1`) }) t.Run("no session leaves mention as text", func(t *testing.T) { got, err := MarkdownToHTML("hi @user1") require.NoError(t, err) assert.Equal(t, "

hi @user1

", got) }) }