diff --git a/internal/store/store.go b/internal/store/store.go index 98f147b12..9b18e439f 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -10,6 +10,7 @@ import ( "os" "path/filepath" "strings" + "unicode/utf8" _ "modernc.org/sqlite" ) @@ -320,5 +321,14 @@ func clip(s string, n int) string { if len(s) <= n { return s } - return s[:n] + // Walk forward by runes; stop when the next rune would exceed n bytes. + pos := 0 + for pos < len(s) { + _, size := utf8.DecodeRuneInString(s[pos:]) + if pos+size > n { + break + } + pos += size + } + return s[:pos] } diff --git a/internal/store/store_test.go b/internal/store/store_test.go index 0f9b74494..c29f4366c 100644 --- a/internal/store/store_test.go +++ b/internal/store/store_test.go @@ -4,6 +4,7 @@ import ( "context" "path/filepath" "testing" + "unicode/utf8" "github.com/samcharles93/archie-core/internal/events" ) @@ -22,6 +23,44 @@ func openTest(t *testing.T) *Store { return s } +func TestClip(t *testing.T) { + tests := []struct { + name string + input string + n int + want string + }{ + {"ascii truncate", "hello", 3, "hel"}, + {"ascii below n", "hello", 10, "hello"}, + {"empty zero", "", 0, ""}, + {"empty positive", "", 5, ""}, + {"2-byte fits exactly", "café", 5, "café"}, + {"2-byte cut off", "café", 4, "caf"}, + {"clean ASCII boundary", "café", 3, "caf"}, + {"4-byte emoji fits", "🍕pizza", 4, "🍕"}, + {"4-byte emoji cut off", "🍕pizza", 3, ""}, + {"2-byte ñ fits", "niño", 5, "niño"}, + {"2-byte ñ cut off", "niño", 4, "niñ"}, + {"3-byte CJK fits", "日本語", 6, "日本"}, + {"3-byte CJK cut off", "日本語", 5, "日"}, + {"mixed multi-byte", "a🍕b", 5, "a🍕"}, + {"mixed multi-byte cut off", "a🍕b", 4, "a"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := clip(tt.input, tt.n) + if got != tt.want { + t.Errorf("clip(%q, %d) = %q, want %q", tt.input, tt.n, got, tt.want) + } + // Verify output is valid UTF-8. + if !utf8.ValidString(got) { + t.Errorf("clip(%q, %d) produced invalid UTF-8: %q", tt.input, tt.n, got) + } + }) + } +} + func TestClearTerminalTasks(t *testing.T) { s := openTest(t) ctx := context.Background() diff --git a/internal/worktree/worktree_test.go b/internal/worktree/worktree_test.go index 2c9168a44..f7b187584 100644 --- a/internal/worktree/worktree_test.go +++ b/internal/worktree/worktree_test.go @@ -201,7 +201,6 @@ func TestPreparePersistentReusesRepoCacheAndCleanupExpiresIt(t *testing.T) { } } -<<<<<<< HEAD func TestAskpassWrittenOnce(t *testing.T) { m := &Manager{WorkDir: t.TempDir()} @@ -242,7 +241,9 @@ func TestAskpassWrittenOnce(t *testing.T) { want := "#!/bin/sh\necho \"$ARCHIE_GIT_TOKEN\"\n" if string(b) != want { t.Fatalf("content = %q, want %q", string(b), want) -======= + } +} + func TestPrepareResumesAfterInterruptedClone(t *testing.T) { if _, err := exec.LookPath("git"); err != nil { t.Skip("git not installed") @@ -331,7 +332,6 @@ func TestPrepareResumesAfterInterruptedClone(t *testing.T) { } if _, err := os.Stat(dir3); !os.IsNotExist(err) { t.Fatal("cleanup left the worktree behind") ->>>>>>> origin/main } }