190 рядки
4.4 KiB
Go
190 рядки
4.4 KiB
Go
package item
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.entityseven.com/rilaxik/pllist/internal/domain"
|
|
"git.entityseven.com/rilaxik/pllist/internal/service"
|
|
)
|
|
|
|
func TestItemTextUsesUrgencyIconAndQuantityInTitle(t *testing.T) {
|
|
itemAccess := service.ItemAccess{
|
|
Item: domain.Item{
|
|
Title: "Milk",
|
|
Quantity: 2,
|
|
Urgency: domain.UrgencyHigh,
|
|
Status: domain.ItemStatusOpen,
|
|
},
|
|
}
|
|
|
|
text := itemText(itemAccess, 1)
|
|
for _, want := range []string{"🔴 Milk x2", "Status: open", "Reserved: no"} {
|
|
if !strings.Contains(text, want) {
|
|
t.Fatalf("itemText() = %q, missing %q", text, want)
|
|
}
|
|
}
|
|
for _, unwanted := range []string{"Role:", "Urgency:", "Quantity:"} {
|
|
if strings.Contains(text, unwanted) {
|
|
t.Fatalf("itemText() = %q, contains %q", text, unwanted)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestItemTextUsesDoneIcon(t *testing.T) {
|
|
itemAccess := service.ItemAccess{
|
|
Item: domain.Item{
|
|
Title: "Milk",
|
|
Quantity: 1,
|
|
Urgency: domain.UrgencyLow,
|
|
Status: domain.ItemStatusDone,
|
|
},
|
|
}
|
|
|
|
text := itemText(itemAccess, 1)
|
|
if !strings.Contains(text, "✓ Milk") {
|
|
t.Fatalf("itemText() = %q, want done icon", text)
|
|
}
|
|
if !strings.Contains(text, "Status: done") {
|
|
t.Fatalf("itemText() = %q, want done status", text)
|
|
}
|
|
}
|
|
|
|
func TestItemLabel(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
item domain.Item
|
|
user int64
|
|
want string
|
|
}{
|
|
{
|
|
name: "normal urgency with quantity",
|
|
item: domain.Item{Title: "Milk", Quantity: 3, Urgency: domain.UrgencyNormal},
|
|
user: 1,
|
|
want: "🟡 Milk x3",
|
|
},
|
|
{
|
|
name: "fractional quantity",
|
|
item: domain.Item{Title: "Cheese", Quantity: 1.5, Urgency: domain.UrgencyNormal},
|
|
user: 1,
|
|
want: "🟡 Cheese x1.5",
|
|
},
|
|
{
|
|
name: "single quantity still shows suffix",
|
|
item: domain.Item{Title: "Bread", Quantity: 1, Urgency: domain.UrgencyLow},
|
|
user: 1,
|
|
want: "🟢 Bread x1",
|
|
},
|
|
{
|
|
name: "done item",
|
|
item: domain.Item{Title: "Eggs", Quantity: 1, Urgency: domain.UrgencyHigh, Status: domain.ItemStatusDone},
|
|
user: 1,
|
|
want: "✓ Eggs x1",
|
|
},
|
|
{
|
|
name: "reserved by viewer",
|
|
item: domain.Item{
|
|
Title: "Butter",
|
|
Quantity: 2,
|
|
Urgency: domain.UrgencyNormal,
|
|
ReservedByID: int64Ptr(1),
|
|
},
|
|
user: 1,
|
|
want: "🟡 Butter x2 · yours",
|
|
},
|
|
{
|
|
name: "reserved by someone else",
|
|
item: domain.Item{
|
|
Title: "Butter",
|
|
Quantity: 2,
|
|
Urgency: domain.UrgencyNormal,
|
|
ReservedByID: int64Ptr(2),
|
|
},
|
|
user: 1,
|
|
want: "🟡 Butter x2 · reserved",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := itemLabel(tt.item, tt.user); got != tt.want {
|
|
t.Fatalf("itemLabel() = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func int64Ptr(value int64) *int64 { return &value }
|
|
|
|
func TestListMenuTextShowsItems(t *testing.T) {
|
|
text := listMenuText(service.ItemPage{
|
|
Access: domain.Access{
|
|
List: domain.List{Title: "Groceries"},
|
|
Role: domain.RoleOwner,
|
|
},
|
|
Items: []domain.Item{
|
|
{Title: "Milk", Quantity: 2, Urgency: domain.UrgencyHigh},
|
|
{Title: "Bread", Quantity: 1, Urgency: domain.UrgencyLow},
|
|
},
|
|
Total: 2,
|
|
})
|
|
|
|
for _, want := range []string{
|
|
"<b>Groceries</b>",
|
|
"Role: owner",
|
|
"Items:",
|
|
"🔴 Milk x2",
|
|
"🟢 Bread x1",
|
|
"2 items",
|
|
} {
|
|
if !strings.Contains(text, want) {
|
|
t.Fatalf("listMenuText() = %q, missing %q", text, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestListMenuTextNotesHiddenItems(t *testing.T) {
|
|
text := listMenuText(service.ItemPage{
|
|
Access: domain.Access{
|
|
List: domain.List{Title: "Groceries"},
|
|
Role: domain.RoleReader,
|
|
},
|
|
Items: []domain.Item{
|
|
{Title: "Milk", Quantity: 1, Urgency: domain.UrgencyNormal},
|
|
},
|
|
Total: service.ItemPageSize + 1,
|
|
})
|
|
|
|
for _, want := range []string{"11 items", "Showing 1 of 11", "Manage items"} {
|
|
if !strings.Contains(text, want) {
|
|
t.Fatalf("listMenuText() = %q, missing %q", text, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestItemsTextShowsItemsAndPagination(t *testing.T) {
|
|
text := itemsText(service.ItemPage{
|
|
Access: domain.Access{
|
|
List: domain.List{Title: "Groceries"},
|
|
Role: domain.RoleEditor,
|
|
},
|
|
Items: []domain.Item{
|
|
{Title: "Milk", Quantity: 2, Urgency: domain.UrgencyHigh},
|
|
},
|
|
Page: 2,
|
|
Total: service.ItemPageSize + 1,
|
|
})
|
|
|
|
for _, want := range []string{
|
|
"Items:",
|
|
"🔴 Milk x2",
|
|
"11 items",
|
|
"Page 2/2",
|
|
"Tap an item to manage it.",
|
|
} {
|
|
if !strings.Contains(text, want) {
|
|
t.Fatalf("itemsText() = %q, missing %q", text, want)
|
|
}
|
|
}
|
|
}
|