package production import ( "context" "testing" "github.com/google/uuid" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/cursor" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/model/dto" ) // storeStub permits only the interactions explicitly arranged by each service scenario. type storeStub struct { t *testing.T createUser func(ctx context.Context, user dto.UserDTO) (dto.UserDTO, error) user func(ctx context.Context, id uuid.UUID) (dto.UserDTO, error) usersExist func(ctx context.Context, ids []uuid.UUID) (bool, error) createChat func(ctx context.Context, chat dto.ChatDTO) (dto.ChatDTO, error) chat func(ctx context.Context, id uuid.UUID) (dto.ChatDTO, error) chats func(ctx context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.ChatDTO, error) createMessage func(ctx context.Context, message dto.MessageDTO) (dto.MessageDTO, error) messages func(ctx context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.MessageDTO, error) } // CreateUser delegates to the arranged behavior and rejects unexpected storage access. func (repo *storeStub) CreateUser(ctx context.Context, user dto.UserDTO) (dto.UserDTO, error) { repo.t.Helper() if repo.createUser == nil { repo.t.Fatalf("unexpected store.CreateUser call") return dto.UserDTO{}, nil } return repo.createUser(ctx, user) } // User delegates to the arranged behavior and rejects unexpected storage access. func (repo *storeStub) User(ctx context.Context, id uuid.UUID) (dto.UserDTO, error) { repo.t.Helper() if repo.user == nil { repo.t.Fatalf("unexpected store.User call") return dto.UserDTO{}, nil } return repo.user(ctx, id) } // UsersExist delegates to the arranged behavior and rejects unexpected storage access. func (repo *storeStub) UsersExist(ctx context.Context, ids []uuid.UUID) (bool, error) { repo.t.Helper() if repo.usersExist == nil { repo.t.Fatalf("unexpected store.UsersExist call") return false, nil } return repo.usersExist(ctx, ids) } // CreateChat delegates to the arranged behavior and rejects unexpected storage access. func (repo *storeStub) CreateChat(ctx context.Context, chat dto.ChatDTO) (dto.ChatDTO, error) { repo.t.Helper() if repo.createChat == nil { repo.t.Fatalf("unexpected store.CreateChat call") return dto.ChatDTO{}, nil } return repo.createChat(ctx, chat) } // Chat delegates to the arranged behavior and rejects unexpected storage access. func (repo *storeStub) Chat(ctx context.Context, id uuid.UUID) (dto.ChatDTO, error) { repo.t.Helper() if repo.chat == nil { repo.t.Fatalf("unexpected store.Chat call") return dto.ChatDTO{}, nil } return repo.chat(ctx, id) } // Chats delegates to the arranged behavior and rejects unexpected storage access. func (repo *storeStub) Chats(ctx context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.ChatDTO, error) { repo.t.Helper() if repo.chats == nil { repo.t.Fatalf("unexpected store.Chats call") return nil, nil } return repo.chats(ctx, id, position, limit) } // CreateMessage delegates to the arranged behavior and rejects unexpected storage access. func (repo *storeStub) CreateMessage(ctx context.Context, message dto.MessageDTO) (dto.MessageDTO, error) { repo.t.Helper() if repo.createMessage == nil { repo.t.Fatalf("unexpected store.CreateMessage call") return dto.MessageDTO{}, nil } return repo.createMessage(ctx, message) } // Messages delegates to the arranged behavior and rejects unexpected storage access. func (repo *storeStub) Messages(ctx context.Context, id uuid.UUID, position dto.PositionDTO, limit int) ([]dto.MessageDTO, error) { repo.t.Helper() if repo.messages == nil { repo.t.Fatalf("unexpected store.Messages call") return nil, nil } return repo.messages(ctx, id, position, limit) } // newStoreStub creates a repository that fails on every unarranged interaction. func newStoreStub(t *testing.T) *storeStub { t.Helper() return &storeStub{t: t} } // newStubService composes the service with controlled storage and a real cursor codec. func newStubService(t *testing.T, repo *storeStub) *Service { t.Helper() codec, err := cursor.New() if err != nil { t.Fatal(err) } svc, err := New(repo, codec) if err != nil { t.Fatal(err) } return svc } // allowIdentity permits exactly the arranged actor's existence lookup, never a profile read. func allowIdentity(repo *storeStub, actor uuid.UUID) { repo.t.Helper() repo.usersExist = func(_ context.Context, ids []uuid.UUID) (bool, error) { if len(ids) != 1 || ids[0] != actor { repo.t.Fatalf("unexpected identity query: %v", ids) } return true, nil } } // allowChatAccess supplies a typed chat after checking the lookup key at the service boundary. func allowChatAccess(repo *storeStub, actor uuid.UUID, chat dto.ChatDTO) { repo.t.Helper() allowIdentity(repo, actor) repo.chat = func(_ context.Context, id uuid.UUID) (dto.ChatDTO, error) { if id != chat.ID { repo.t.Fatalf("chat queried for %s, want %s", id, chat.ID) } return chat, nil } }