diff --git a/internal/storage/storage_test.go b/internal/storage/storage_test.go index e30316bce..83a943e56 100644 --- a/internal/storage/storage_test.go +++ b/internal/storage/storage_test.go @@ -3,6 +3,8 @@ package storage import ( "context" "encoding/json" + "fmt" + "go/format" "io" "net/http" "net/http/httptest" @@ -581,6 +583,48 @@ func dockerTestClient(t *testing.T, roundTrip func(*http.Request) (*http.Respons return cli } +// ── gofmt compliance ───────────────────────────────────────────────── + +func TestStorageDotGoIsGofmtClean(t *testing.T) { + // storage.go must be gofmt-clean so that CI checks (gofmt -l) pass. + // This test reads the source and feeds it through go/format.Source; + // if the output differs, the file is not canonical Go formatting. + src, err := os.ReadFile("storage.go") + if err != nil { + t.Fatal(err) + } + formatted, err := format.Source(src) + if err != nil { + t.Fatalf("storage.go is not valid Go: %v", err) + } + if string(formatted) != string(src) { + t.Errorf("storage.go is not gofmt-clean. diff:\n%s", diffBytes(src, formatted)) + } +} + +func diffBytes(a, b []byte) string { + linesA := strings.Split(string(a), "\n") + linesB := strings.Split(string(b), "\n") + var out strings.Builder + maxLine := len(linesA) + if len(linesB) > maxLine { + maxLine = len(linesB) + } + for i := 0; i < maxLine; i++ { + var lineA, lineB string + if i < len(linesA) { + lineA = linesA[i] + } + if i < len(linesB) { + lineB = linesB[i] + } + if lineA != lineB { + out.WriteString(fmt.Sprintf("-%s\n+%s\n", lineA, lineB)) + } + } + return out.String() +} + func dockerAPIPath(path string) string { if strings.HasPrefix(path, "/v1.55/") { return strings.TrimPrefix(path, "/v1.55") 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()