Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

4.1 KB
Raw Blame
package voice

import (
	"context"
	"errors"
	"fmt"
	"testing"
	"time"

	"github.com/simplexai-labs/linger/apps/teamhub/internal/document"
	"github.com/simplexai-labs/linger/apps/teamhub/internal/messagecreation"
)

func TestLiveKitCaptureCompletionFailureCode(t *testing.T) {
	t.Parallel()
	for _, tt := range []struct {
		name string
		err  error
		want string
	}{
		{name: "success", err: nil},
		{name: "document not found", err: document.ErrNotFound, want: liveKitCaptureCompletionFailureDocumentNotFound},
		{name: "wrapped document not found", err: fmt.Errorf("load version: %w", document.ErrNotFound), want: liveKitCaptureCompletionFailureDocumentNotFound},
		{name: "authority incomplete", err: errLiveKitCaptureCompletionAuthorityIncomplete, want: liveKitCaptureCompletionFailureAuthorityIncomplete},
		{name: "retryable", err: errors.New("Capture Direct Room Agent is unavailable")},
		{name: "incomplete transcript", err: errCaptureTranscriptIncomplete},
		{name: "canceled", err: context.Canceled},
		{name: "deadline exceeded", err: context.DeadlineExceeded},
	} {
		t.Run(tt.name, func(t *testing.T) {
			if got := liveKitCaptureCompletionFailureCode(tt.err); got != tt.want {
				t.Fatalf("failure code=%q want=%q", got, tt.want)
			}
		})
	}
}

func TestLiveKitCaptureCompletionShouldDelay(t *testing.T) {
	t.Parallel()
	for _, tt := range []struct {
		name      string
		cause     error
		workerErr error
		delay     bool
	}{
		{name: "incomplete transcript", cause: errCaptureTranscriptIncomplete, delay: true},
		{name: "worker canceled", cause: context.Canceled, workerErr: context.Canceled, delay: true},
		{name: "worker deadline", cause: context.DeadlineExceeded, workerErr: context.DeadlineExceeded, delay: true},
		{name: "downstream canceled", cause: context.Canceled},
		{name: "downstream deadline", cause: context.DeadlineExceeded},
		{name: "wrapped downstream deadline", cause: fmt.Errorf("get version: %w", context.DeadlineExceeded)},
		{name: "unrelated worker error", cause: errors.New("Capture Direct Room Agent is unavailable"), workerErr: errors.New("pool exhausted")},
		{name: "document not found", cause: document.ErrNotFound},
		{name: "retryable", cause: errors.New("Capture Direct Room Agent is unavailable")},
	} {
		t.Run(tt.name, func(t *testing.T) {
			if got := liveKitCaptureCompletionShouldDelay(tt.cause, tt.workerErr); got != tt.delay {
				t.Fatalf("delay=%v want=%v", got, tt.delay)
			}
		})
	}
}

func TestLiveKitCaptureCompletionRetryDelay(t *testing.T) {
	t.Parallel()
	want := []time.Duration{
		5 * time.Second,
		10 * time.Second,
		20 * time.Second,
		40 * time.Second,
		80 * time.Second,
		160 * time.Second,
		5 * time.Minute,
		5 * time.Minute,
	}
	for attempts, delay := range want {
		if got := liveKitCaptureCompletionRetryDelay(int32(attempts)); got != delay {
			t.Fatalf("attempts=%d delay=%s want=%s", attempts, got, delay)
		}
	}
	if got := liveKitCaptureCompletionRetryDelay(-1); got != 5*time.Second {
		t.Fatalf("negative attempts delay=%s", got)
	}
	if LiveKitCaptureCompletionMaxAttempts < 1 {
		t.Fatal("max attempts must fail a retryable Capture")
	}
}

func TestCaptureCompletionSegmentsPutQuickNotesAfterTheFiles(t *testing.T) {
	t.Parallel()
	audio := document.Version{DocumentID: "document_audio", Filename: "source.m4a"}
	transcript := document.Version{DocumentID: "document_transcript", Filename: "transcript.md"}
	plain, err := messagecreation.Normalize(captureCompletionSegments("capture_1", audio, transcript, false))
	if err != nil || plain.Segments[0].Content != captureCompletionMessage+"\n" || len(plain.Segments) != 4 {
		t.Fatalf("without notes: %+v err=%v", plain, err)
	}
	content, err := messagecreation.Normalize(captureCompletionSegments("capture_1", audio, transcript, true))
	if err != nil {
		t.Fatal(err)
	}
	last := content.Segments[len(content.Segments)-1]
	if len(content.Segments) != 6 || content.Segments[1].DocumentID != "document_audio" || content.Segments[3].DocumentID != "document_transcript" ||
		last.Type != "meeting_notes" || last.CaptureID != "capture_1" || last.Content == "" {
		t.Fatalf("with notes: %+v", content.Segments)
	}
}