package str_utils

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestForceValidUTF8(t *testing.T) {
	tests := []struct {
		Input string
		Want  string
	}{
		{
			"hello",
			"hello",
		},
		{
			"こんにちは",
			"こんにちは",
		},
		{
			"baﬄe", // NOTE: "ﬄ" is a single-character ligature
			"baﬄe", // ligature is preserved exactly
		},
		{
			"wé́́é́́é́́!", // NOTE: These "e" have multiple combining diacritics
			"wé́́é́́é́́!", // diacritics are preserved exactly
		},
		{
			"😸😾", // Astral-plane characters
			"😸😾", // preserved exactly
		},
		{
			"\xff\xff",     // neither byte is valid UTF-8
			"\ufffd\ufffd", // both are replaced by replacement character
		},
		{
			"\xff\xff\xff\xff\xff",           // more than three invalid bytes
			"\ufffd\ufffd\ufffd\ufffd\ufffd", // still expanded even though it exceeds our initial slice capacity in the implementation
		},
		{
			"t\xffe\xffst",     // invalid bytes interleaved with other content
			"t\ufffde\ufffdst", // the valid content is preserved
		},
		{
			"\xff你好\xff世界",     // invalid bytes interacting with multibyte sequences
			"\ufffd你好\ufffd世界", // the valid content is preserved
		},
	}

	for _, test := range tests {
		got := ForceValidUTF8(test.Input)
		assert.Equal(t, got, test.Want)
	}
}
