package host import ( "sync" "testing" "time" "unicode/utf16" ) func TestIMECoalescerCJKAndEmojiSurrogates(t *testing.T) { type gotItem struct { vm string text string } var ( mu sync.Mutex got []gotItem done = make(chan struct{}, 4) ) c := NewIMECoalescer(25*time.Millisecond, func(target IMETarget, text string) { mu.Lock() got = append(got, gotItem{vm: target.VMName, text: text}) mu.Unlock() done <- struct{}{} }) defer c.Close() vmA := IMETarget{VMName: "mac-a", PID: 1001} vmB := IMETarget{VMName: "mac-b", PID: 1002} // Push Chinese + ASCII + Emoji (surrogate pair) rapidly -> single coalesced phrase. for _, u := range utf16.Encode([]rune("你好ABC🎉")) { c.Push(vmA, u) } select { case <-done: case <-time.After(time.Second): t.Fatal("timed out waiting for coalesced phrase") } // Switching target VM flushes immediately, and '\r' normalizes to '\n' + flushes. c.Push(vmA, 'x') c.Push(vmB, 'y') c.Push(vmB, '\r') for i := 0; i < 2; i++ { select { case <-done: case <-time.After(time.Second): t.Fatalf("timed out waiting for flush %d", i+2) } } mu.Lock() defer mu.Unlock() if len(got) != 3 { t.Fatalf("got %d flushes: %+v", len(got), got) } if got[0] != (gotItem{vm: "mac-a", text: "你好ABC🎉"}) { t.Fatalf("flush[0] = %+v", got[0]) } if got[1] != (gotItem{vm: "mac-a", text: "x"}) { t.Fatalf("flush[1] = %+v", got[1]) } if got[2] != (gotItem{vm: "mac-b", text: "y\n"}) { t.Fatalf("flush[2] = %+v", got[2]) } }