mirror of
https://github.com/coder/coder.git
synced 2026-06-07 06:58:17 +00:00
51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
package chatdebug
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"unicode/utf8"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestBeginStep_SkipsNilRunID(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := ContextWithRun(context.Background(), &RunContext{ChatID: uuid.New()})
|
|
handle, enriched := beginStep(ctx, &Service{}, RecorderOptions{ChatID: uuid.New()}, OperationGenerate, nil)
|
|
require.Nil(t, handle)
|
|
require.Equal(t, ctx, enriched)
|
|
}
|
|
|
|
func TestTruncateLabel(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
maxLen int
|
|
want string
|
|
}{
|
|
{name: "Empty", input: "", maxLen: 10, want: ""},
|
|
{name: "WhitespaceOnly", input: " \t\n ", maxLen: 10, want: ""},
|
|
{name: "ShortText", input: "hello world", maxLen: 20, want: "hello world"},
|
|
{name: "ExactLength", input: "abcde", maxLen: 5, want: "abcde"},
|
|
{name: "LongTextTruncated", input: "abcdefghij", maxLen: 5, want: "abcd…"},
|
|
{name: "NegativeMaxLen", input: "hello", maxLen: -1, want: ""},
|
|
{name: "ZeroMaxLen", input: "hello", maxLen: 0, want: ""},
|
|
{name: "SingleRuneLimit", input: "hello", maxLen: 1, want: "…"},
|
|
{name: "MultipleWhitespaceRuns", input: " hello world \t again ", maxLen: 100, want: "hello world again"},
|
|
{name: "UnicodeRunes", input: "こんにちは世界", maxLen: 3, want: "こん…"},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
got := TruncateLabel(tc.input, tc.maxLen)
|
|
require.Equal(t, tc.want, got)
|
|
require.LessOrEqual(t, utf8.RuneCountInString(got), max(tc.maxLen, 0))
|
|
})
|
|
}
|
|
}
|