Files
llink/go/internal/human/service_test.go
T
Arjun Patel 4facc6b371 feat: avatars for humans (#273)
* implement avatar backend functionality

* add avatar endpoints

* typo

* implement client side avatar upload and handling

* fixes

* Update go/internal/handler/handler.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update js/desktop/src/lib/avatar-image.ts

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* bug in order

* remove unused component

* fix invalid migration

* fix syntax errors

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2026-06-11 15:30:21 -07:00

75 lines
2.2 KiB
Go

package human_test
import (
"context"
"os"
"testing"
"github.com/flowy-live/llink/internal/human"
"github.com/flowy-live/llink/internal/testhelper"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/assert"
)
var dbPool *pgxpool.Pool
func TestMain(m *testing.M) {
dbPool = testhelper.SetupTestDB()
defer testhelper.TeardownTestDB()
ret := m.Run()
os.Exit(ret)
}
func TestHumanService(t *testing.T) {
ctx := context.Background()
svc := human.NewService(dbPool)
// Test GetByEmail with non-existent email
_, err := svc.GetByEmail(ctx, "newuser@example.com")
assert.Error(t, err)
assert.ErrorIs(t, err, human.ErrNotFound)
// Test GetOrCreateByEmail creates new human
createdHuman, err := svc.GetOrCreateByEmail(ctx, "newuser@example.com")
assert.NoError(t, err)
assert.NotEmpty(t, createdHuman.ID)
assert.Equal(t, "newuser@example.com", createdHuman.Email)
assert.Equal(t, "newuser", createdHuman.EmailPrefix)
assert.NotZero(t, createdHuman.CreatedAt)
// Test GetOrCreateByEmail returns existing human
existingHuman, err := svc.GetOrCreateByEmail(ctx, "newuser@example.com")
assert.NoError(t, err)
assert.Equal(t, createdHuman.ID, existingHuman.ID)
assert.Equal(t, createdHuman.Email, existingHuman.Email)
// Test GetByEmail with existing email
foundHuman, err := svc.GetByEmail(ctx, "newuser@example.com")
assert.NoError(t, err)
assert.Equal(t, createdHuman.ID, foundHuman.ID)
assert.Equal(t, createdHuman.Email, foundHuman.Email)
// Test with another email
anotherHuman, err := svc.GetOrCreateByEmail(ctx, "another@example.com")
assert.NoError(t, err)
assert.NotEqual(t, createdHuman.ID, anotherHuman.ID)
assert.Equal(t, "another@example.com", anotherHuman.Email)
assert.Equal(t, "another", anotherHuman.EmailPrefix)
// Test avatar handling
objectID := "obj_xxx"
err = svc.UpdateAvatar(ctx, anotherHuman.ID, objectID)
assert.NoError(t, err)
anotherHuman, err = svc.GetByID(ctx, anotherHuman.ID)
assert.NoError(t, err)
assert.Equal(t, objectID, *anotherHuman.AvatarObjectID)
err = svc.DeleteAvatar(ctx, anotherHuman.ID)
assert.NoError(t, err)
anotherHuman, err = svc.GetByID(ctx, anotherHuman.ID)
assert.NoError(t, err)
assert.Nil(t, anotherHuman.AvatarObjectID)
}