Merge pull request 'clip() can truncate strings mid-rune, producing invalid UTF-8 (archie)' (#74) from feat/47-clip-can-truncate-strings-mid-rune-producing-invalid-utf-8 into main

Reviewed-on: sam/archie-core#74
This commit is contained in:
sam committed 2026-07-25 01:28:47 +00:00
commit fd0b60072e
3 files changed
+53 -4

No files matched your search

+11 -1
View File
@@ -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]
}
+39
View File
@@ -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()
+3 -3
View File
@@ -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
}
}