Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
18.9 KB
Raw Blame
package lark

import (
	"context"
	"errors"
	"strconv"
	"sync"
	"testing"
	"time"

	"github.com/jackc/pgx/v5"
	"github.com/jackc/pgx/v5/pgtype"
	db "github.com/multica-ai/multica/server/pkg/db/generated"
)

// fakeTypingAPIClient records reaction calls and can be programmed to fail.
type fakeTypingAPIClient struct {
	mu           sync.Mutex
	addCalled    []addReactionCall
	deleteCalled []deleteReactionCall
	listCalled   []string
	listReturn   []MessageReaction
	listErr      error
	addErr       error
	deleteErr    error
	addReturn    string
}

type addReactionCall struct {
	creds     InstallationCredentials
	messageID string
	emojiType string
}

type deleteReactionCall struct {
	creds      InstallationCredentials
	messageID  string
	reactionID string
}

func (f *fakeTypingAPIClient) IsConfigured() bool { return true }
func (f *fakeTypingAPIClient) SendInteractiveCard(context.Context, SendCardParams) (string, error) {
	return "", nil
}
func (f *fakeTypingAPIClient) PatchInteractiveCard(context.Context, PatchCardParams) error {
	return nil
}
func (f *fakeTypingAPIClient) SendTextMessage(context.Context, SendTextParams) (string, error) {
	return "", nil
}
func (f *fakeTypingAPIClient) SendMarkdownCard(context.Context, SendMarkdownCardParams) (string, error) {
	return "", nil
}
func (f *fakeTypingAPIClient) SendBindingPromptCard(context.Context, BindingPromptParams) error {
	return nil
}
func (f *fakeTypingAPIClient) GetBotInfo(context.Context, InstallationCredentials) (BotInfo, error) {
	return BotInfo{}, nil
}
func (f *fakeTypingAPIClient) GetMessage(context.Context, InstallationCredentials, string) ([]LarkMessage, error) {
	return nil, nil
}
func (f *fakeTypingAPIClient) ListChatMessages(context.Context, InstallationCredentials, ListMessagesParams) ([]LarkMessage, error) {
	return nil, nil
}
func (f *fakeTypingAPIClient) DownloadMessageResource(context.Context, InstallationCredentials, DownloadResourceParams) (DownloadedResource, error) {
	return DownloadedResource{}, nil
}
func (f *fakeTypingAPIClient) BatchGetUsers(context.Context, InstallationCredentials, []string) (map[string]string, error) {
	return nil, nil
}
func (f *fakeTypingAPIClient) AddMessageReaction(_ context.Context, p AddReactionParams) (string, error) {
	f.mu.Lock()
	defer f.mu.Unlock()
	f.addCalled = append(f.addCalled, addReactionCall{p.InstallationID, p.MessageID, p.EmojiType})
	return f.addReturn, f.addErr
}
func (f *fakeTypingAPIClient) DeleteMessageReaction(_ context.Context, p DeleteReactionParams) error {
	f.mu.Lock()
	defer f.mu.Unlock()
	f.deleteCalled = append(f.deleteCalled, deleteReactionCall{p.InstallationID, p.MessageID, p.ReactionID})
	return f.deleteErr
}

func (f *fakeTypingAPIClient) ListMessageReactions(_ context.Context, p ListMessageReactionsParams) ([]MessageReaction, error) {
	f.mu.Lock()
	defer f.mu.Unlock()
	f.listCalled = append(f.listCalled, p.MessageID)
	return f.listReturn, f.listErr
}

// noListerClient wraps an APIClient without promoting any extra methods, so it
// satisfies APIClient but not ReactionLister — the shape of a client that
// cannot answer "what is already on this message".
type noListerClient struct{ APIClient }

type fakeTypingQueries struct {
	ledgerMu     sync.Mutex
	ledger       map[pgtype.UUID]db.ChannelTypingReaction
	active       func(context.Context, db.IsChannelMessageTypingActiveParams) (bool, error)
	binding      ChatSessionBinding
	installation Installation
	bindingErr   error
	installErr   error
}

func (f *fakeTypingQueries) IsChannelMessageTypingActive(ctx context.Context, arg db.IsChannelMessageTypingActiveParams) (bool, error) {
	if f.active != nil {
		return f.active(ctx, arg)
	}
	return true, nil
}

func (f *fakeTypingQueries) GetLarkChatSessionBindingBySession(context.Context, pgtype.UUID) (ChatSessionBinding, error) {
	return f.binding, f.bindingErr
}
func (f *fakeTypingQueries) GetLarkInstallation(context.Context, pgtype.UUID) (Installation, error) {
	return f.installation, f.installErr
}

type fakeTypingCreds struct{ secret string }

func (f fakeTypingCreds) DecryptAppSecret(inst Installation) (string, error) {
	return f.secret, nil
}

func TestTypingIndicatorAddRecordsState(t *testing.T) {
	api := &fakeTypingAPIClient{addReturn: "reaction-123"}
	queries := &fakeTypingQueries{}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, queries, newDiscardLogger())

	inst := Installation{AppID: "cli_test", Region: "feishu"}
	session := pgtype.UUID{Bytes: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, Valid: true}

	mgr.Add(context.Background(), inst, session, "msg-1", "", session)

	if len(api.addCalled) != 1 {
		t.Fatalf("expected 1 add call, got %d", len(api.addCalled))
	}
	if api.addCalled[0].messageID != "msg-1" || api.addCalled[0].emojiType != typingEmoji {
		t.Fatalf("unexpected add call params: %+v", api.addCalled[0])
	}

	key := uuidString(session)
	mgr.mu.RLock()
	states := mgr.states[key]
	mgr.mu.RUnlock()
	if len(states) != 1 || states[0].MessageID != "msg-1" || states[0].ReactionID != "reaction-123" {
		t.Fatalf("unexpected state: %+v", states)
	}
}

func TestTypingIndicatorAddSkipsOnEmptyMessageID(t *testing.T) {
	api := &fakeTypingAPIClient{addReturn: "reaction-123"}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, &fakeTypingQueries{}, newDiscardLogger())

	inst := Installation{AppID: "cli_test", Region: "feishu"}
	session := pgtype.UUID{Bytes: [16]byte{1}, Valid: true}

	mgr.Add(context.Background(), inst, session, "", "", session)

	if len(api.addCalled) != 0 {
		t.Fatalf("expected 0 add calls, got %d", len(api.addCalled))
	}
}

func TestTypingIndicatorAddSkipsOldMessages(t *testing.T) {
	api := &fakeTypingAPIClient{addReturn: "reaction-123"}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, &fakeTypingQueries{}, newDiscardLogger())

	inst := Installation{AppID: "cli_test", Region: "feishu"}
	session := pgtype.UUID{Bytes: [16]byte{1}, Valid: true}

	oldTime := time.Now().Add(-3 * time.Minute).UnixMilli()
	mgr.Add(context.Background(), inst, session, "msg-old", strconv.FormatInt(oldTime, 10), session)

	if len(api.addCalled) != 0 {
		t.Fatalf("expected 0 add calls for old message, got %d", len(api.addCalled))
	}
}

func TestTypingIndicatorAddLogsOnAPIError(t *testing.T) {
	api := &fakeTypingAPIClient{addErr: errors.New("lark down")}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, &fakeTypingQueries{}, newDiscardLogger())

	inst := Installation{AppID: "cli_test", Region: "feishu"}
	session := pgtype.UUID{Bytes: [16]byte{1}, Valid: true}

	mgr.Add(context.Background(), inst, session, "msg-1", "", session)

	if len(api.addCalled) != 1 {
		t.Fatalf("expected 1 add call, got %d", len(api.addCalled))
	}

	key := uuidString(session)
	mgr.mu.RLock()
	states := mgr.states[key]
	mgr.mu.RUnlock()
	if len(states) != 0 {
		t.Fatalf("expected 0 states after error, got %d", len(states))
	}
}

func TestTypingIndicatorReconcileDeletesReactions(t *testing.T) {
	api := &fakeTypingAPIClient{addReturn: "reaction-abc"}
	queries := &fakeTypingQueries{
		binding: ChatSessionBinding{
			InstallationID: pgtype.UUID{Bytes: [16]byte{9, 9, 9, 9}, Valid: true},
		},
		installation: Installation{
			ID:     pgtype.UUID{Bytes: [16]byte{9, 9, 9, 9}, Valid: true},
			AppID:  "cli_test",
			Region: "feishu",
		},
	}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, queries, newDiscardLogger())

	inst := Installation{AppID: "cli_test", Region: "feishu"}
	session := pgtype.UUID{Bytes: [16]byte{1, 2, 3, 4}, Valid: true}

	mgr.Add(context.Background(), inst, session, "msg-1", "", session)
	if len(api.addCalled) != 1 {
		t.Fatal("add should have been called")
	}

	mgr.Reconcile(context.Background(), session)

	if len(api.deleteCalled) != 1 {
		t.Fatalf("expected 1 delete call, got %d", len(api.deleteCalled))
	}
	if api.deleteCalled[0].messageID != "msg-1" || api.deleteCalled[0].reactionID != "reaction-abc" {
		t.Fatalf("unexpected delete params: %+v", api.deleteCalled[0])
	}

	key := uuidString(session)
	mgr.mu.RLock()
	states := mgr.states[key]
	mgr.mu.RUnlock()
	if len(states) != 0 {
		t.Fatalf("expected 0 states after clear, got %d", len(states))
	}
}

func TestTypingIndicatorReconcileNoOpWhenEmpty(t *testing.T) {
	api := &fakeTypingAPIClient{}
	queries := &fakeTypingQueries{
		binding: ChatSessionBinding{
			InstallationID: pgtype.UUID{Bytes: [16]byte{9}, Valid: true},
		},
		installation: Installation{
			ID:     pgtype.UUID{Bytes: [16]byte{9}, Valid: true},
			AppID:  "cli_test",
			Region: "feishu",
		},
	}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, queries, newDiscardLogger())

	session := pgtype.UUID{Bytes: [16]byte{1, 2, 3, 4}, Valid: true}
	mgr.Reconcile(context.Background(), session)

	if len(api.deleteCalled) != 0 {
		t.Fatalf("expected 0 delete calls when empty, got %d", len(api.deleteCalled))
	}
}

func TestTypingIndicatorReconcileLogsOnDeleteError(t *testing.T) {
	api := &fakeTypingAPIClient{addReturn: "reaction-xyz", deleteErr: errors.New("delete failed")}
	queries := &fakeTypingQueries{
		binding: ChatSessionBinding{
			InstallationID: pgtype.UUID{Bytes: [16]byte{9}, Valid: true},
		},
		installation: Installation{
			ID:     pgtype.UUID{Bytes: [16]byte{9}, Valid: true},
			AppID:  "cli_test",
			Region: "feishu",
		},
	}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, queries, newDiscardLogger())

	inst := Installation{AppID: "cli_test", Region: "feishu"}
	session := pgtype.UUID{Bytes: [16]byte{1, 2, 3, 4}, Valid: true}

	mgr.Add(context.Background(), inst, session, "msg-1", "", session)
	mgr.Reconcile(context.Background(), session)

	if len(api.deleteCalled) != 1 {
		t.Fatalf("expected 1 delete call attempt, got %d", len(api.deleteCalled))
	}
}

func TestTypingIndicatorMultipleMessagesPerSession(t *testing.T) {
	api := &fakeTypingAPIClient{addReturn: "reaction-n"}
	queries := &fakeTypingQueries{
		binding: ChatSessionBinding{
			InstallationID: pgtype.UUID{Bytes: [16]byte{9}, Valid: true},
		},
		installation: Installation{
			ID:     pgtype.UUID{Bytes: [16]byte{9}, Valid: true},
			AppID:  "cli_test",
			Region: "feishu",
		},
	}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, queries, newDiscardLogger())

	inst := Installation{AppID: "cli_test", Region: "feishu"}
	session := pgtype.UUID{Bytes: [16]byte{1, 2, 3, 4}, Valid: true}

	mgr.Add(context.Background(), inst, session, "msg-a", "", session)
	mgr.Add(context.Background(), inst, session, "msg-b", "", session)

	if len(api.addCalled) != 2 {
		t.Fatalf("expected 2 add calls, got %d", len(api.addCalled))
	}

	mgr.Reconcile(context.Background(), session)

	if len(api.deleteCalled) != 2 {
		t.Fatalf("expected 2 delete calls, got %d", len(api.deleteCalled))
	}
}

func TestTypingIndicatorConcurrentAddAndClear(t *testing.T) {
	api := &fakeTypingAPIClient{addReturn: "reaction-concurrent"}
	queries := &fakeTypingQueries{
		binding: ChatSessionBinding{
			InstallationID: pgtype.UUID{Bytes: [16]byte{9}, Valid: true},
		},
		installation: Installation{
			ID:     pgtype.UUID{Bytes: [16]byte{9}, Valid: true},
			AppID:  "cli_test",
			Region: "feishu",
		},
	}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, queries, newDiscardLogger())

	inst := Installation{AppID: "cli_test", Region: "feishu"}
	session := pgtype.UUID{Bytes: [16]byte{1, 2, 3, 4}, Valid: true}

	done := make(chan struct{})
	go func() {
		for i := 0; i < 50; i++ {
			mgr.Add(context.Background(), inst, session, "msg", "", session)
		}
		close(done)
	}()
	go func() {
		for i := 0; i < 50; i++ {
			mgr.Reconcile(context.Background(), session)
		}
	}()
	<-done
	time.Sleep(10 * time.Millisecond)
}

// A runtime teardown deletes the installation inside the transaction that
// cancels its tasks, so the row the clear resolves credentials from is gone by
// the time the cancel arrives. The reaction is still on the message and the
// state has already been taken off the map, so the snapshot recorded at add
// time is the only thing left that can remove it.
func TestTypingIndicatorReconcilesAfterTheInstallationIsDeleted(t *testing.T) {
	api := &fakeTypingAPIClient{addReturn: "reaction-123"}
	queries := &fakeTypingQueries{}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, queries, newDiscardLogger())

	inst := Installation{
		ID:     pgtype.UUID{Bytes: [16]byte{9}, Valid: true},
		AppID:  "cli_test",
		Region: "feishu",
	}
	session := pgtype.UUID{Bytes: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, Valid: true}

	mgr.Add(context.Background(), inst, session, "msg-1", "", session)
	if len(api.addCalled) != 1 {
		t.Fatalf("setup: the reaction should be on the message; adds = %d", len(api.addCalled))
	}

	// The teardown transaction has committed.
	queries.installErr = pgx.ErrNoRows

	mgr.Reconcile(context.Background(), session)

	if len(api.deleteCalled) != 1 {
		t.Fatalf("the runtime was torn down and its installation deleted, but the reaction is still "+
			"on msg-1 with nothing left to take it off (deletes = %d)", len(api.deleteCalled))
	}
}

// The snapshot must not shadow a live row, and must not stand in for a lookup
// that merely failed: a transient error says nothing about whether the
// installation still exists, and clearing through a possibly-rotated secret is
// worse than leaving the badge for the next ending.
func TestTypingIndicatorDoesNotFallBackOnATransientLookupFailure(t *testing.T) {
	api := &fakeTypingAPIClient{addReturn: "reaction-123"}
	queries := &fakeTypingQueries{}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, queries, newDiscardLogger())

	inst := Installation{
		ID:     pgtype.UUID{Bytes: [16]byte{9}, Valid: true},
		AppID:  "cli_test",
		Region: "feishu",
	}
	session := pgtype.UUID{Bytes: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, Valid: true}

	mgr.Add(context.Background(), inst, session, "msg-1", "", session)
	queries.installErr = errors.New("connection reset")

	mgr.Reconcile(context.Background(), session)

	if len(api.deleteCalled) != 0 {
		t.Fatalf("a transient lookup failure fell back to the snapshot; deletes = %d", len(api.deleteCalled))
	}
}

// ---- SweepMessage: the restart- and replica-proof half of the lifecycle ----

// The sweep is what clears the badge when the state map has nothing on file:
// it must list the message's Typing reactions from Lark, delete the ones the
// bot itself added, and leave everyone else's alone — a human who reacted with
// the same emoji owns that reaction, and a bot cannot delete it anyway.
func TestTypingIndicatorSweepDeletesOnlyTheBotTypingReactions(t *testing.T) {
	api := &fakeTypingAPIClient{
		listReturn: []MessageReaction{
			{ReactionID: "r-bot", OperatorType: "app", OperatorID: "cli_test", EmojiType: typingEmoji},
			{ReactionID: "r-human", OperatorType: "user", EmojiType: typingEmoji},
			{ReactionID: "r-bot-lower", OperatorType: "app", OperatorID: "cli_test", EmojiType: "typing"},
			{ReactionID: "r-bot-smile", OperatorType: "app", OperatorID: "cli_test", EmojiType: "SMILE"},
		},
	}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, &fakeTypingQueries{}, newDiscardLogger())

	mgr.SweepMessage(context.Background(), InstallationCredentials{AppID: "cli_test"}, "om_trigger")

	if len(api.listCalled) != 1 || api.listCalled[0] != "om_trigger" {
		t.Fatalf("expected one list call for om_trigger, got %v", api.listCalled)
	}
	if len(api.deleteCalled) != 2 {
		t.Fatalf("expected the two bot-authored Typing reactions to be deleted, deletes = %+v", api.deleteCalled)
	}
	deleted := map[string]bool{}
	for _, d := range api.deleteCalled {
		deleted[d.reactionID] = true
		if d.messageID != "om_trigger" {
			t.Errorf("deleted reaction %s from the wrong message %q", d.reactionID, d.messageID)
		}
	}
	if !deleted["r-bot"] || !deleted["r-bot-lower"] {
		t.Errorf("bot-authored Typing reactions survived the sweep: %+v", deleted)
	}
	if deleted["r-human"] || deleted["r-bot-smile"] {
		t.Errorf("the sweep deleted reactions it did not own: %+v", deleted)
	}
}

// A restart or a second replica empties the state map without touching Lark;
// the sweep must clear the badge anyway, because it answers only to Lark.
func TestTypingIndicatorSweepClearsWithoutAnyRecordedState(t *testing.T) {
	api := &fakeTypingAPIClient{
		listReturn: []MessageReaction{
			{ReactionID: "r-from-another-process", OperatorType: "app", OperatorID: "cli_test", EmojiType: typingEmoji},
		},
	}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, &fakeTypingQueries{}, newDiscardLogger())

	// No Add ever ran in this process: the map is empty by construction.
	mgr.SweepMessage(context.Background(), InstallationCredentials{AppID: "cli_test"}, "om_left_behind")

	if len(api.deleteCalled) != 1 || api.deleteCalled[0].reactionID != "r-from-another-process" {
		t.Fatalf("the badge added by another process was not swept: deletes = %+v", api.deleteCalled)
	}
}

// A client that cannot list reactions (the stub, minimal fakes) must skip the
// sweep quietly instead of panicking or firing blind deletes.
func TestTypingIndicatorSweepSkipsWhenClientCannotList(t *testing.T) {
	inner := &fakeTypingAPIClient{}
	api := &noListerClient{APIClient: inner}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, &fakeTypingQueries{}, newDiscardLogger())

	mgr.SweepMessage(context.Background(), InstallationCredentials{AppID: "cli_test"}, "om_trigger")

	if len(inner.listCalled) != 0 || len(inner.deleteCalled) != 0 {
		t.Fatalf("a client without ReactionLister must not be consulted; lists = %d deletes = %d",
			len(inner.listCalled), len(inner.deleteCalled))
	}
}

// A failed list must not turn into deletes of a stale picture of the message.
func TestTypingIndicatorSweepDoesNotDeleteWhenTheListFails(t *testing.T) {
	api := &fakeTypingAPIClient{listErr: errors.New("lark 5xx")}
	mgr := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, &fakeTypingQueries{}, newDiscardLogger())

	mgr.SweepMessage(context.Background(), InstallationCredentials{AppID: "cli_test"}, "om_trigger")

	if len(api.deleteCalled) != 0 {
		t.Fatalf("deleted reactions without a successful list; deletes = %+v", api.deleteCalled)
	}
}

func TestTypingAddRetractsUnverifiedInputWithIndependentContext(t *testing.T) {
	api := &fakeTypingAPIClient{addReturn: "unverified-reaction"}
	queries := &fakeTypingQueries{active: func(ctx context.Context, _ db.IsChannelMessageTypingActiveParams) (bool, error) {
		if ctx.Err() != nil {
			t.Fatalf("lifecycle check inherited expired Add context: %v", ctx.Err())
		}
		return false, errors.New("database unavailable")
	}}
	manager := NewTypingIndicatorManager(api, fakeTypingCreds{secret: "shh"}, queries, newDiscardLogger())
	ctx, cancel := context.WithCancel(context.Background())
	cancel() // Model a successful response delivered after the caller cancelled.
	session := uuidFromString(t, "ee777777-ee77-ee77-ee77-eeeeeeeeeeee")
	manager.Add(ctx, Installation{AppID: "cli_test", Region: "feishu"}, session, "trigger", "", session)
	if len(api.deleteCalled) != 1 || api.deleteCalled[0].reactionID != "unverified-reaction" || len(manager.states) != 0 {
		t.Fatalf("unverified Add survived: deletes=%+v states=%+v", api.deleteCalled, manager.states)
	}
}