Files
oh-my-pi/packages/coding-agent/test/truncate-to-width.test.ts
T
can1357 e1bcf492a1 refactor: restructured text module and simplified NAPI API surface
- Renamed `EllipsisKind` enum to `Ellipsis` across all packages for clearer API naming.
- Refactored Rust text module from 356 to 155 lines with simplified state tracking using u32 ColorVal instead of enum ColorCode and named attribute constants.
- Removed `Env` parameter from public NAPI functions `truncate_to_width` and `extract_segments` to simplify API surface.
- Changed return type of `truncate_to_width` from `JsString` to `Either<JsString, Utf16String>` to support zero-copy returns.
- Removed early return optimization in `truncateToWidth()` function.
- Reordered native module search paths to prioritize repository build artifacts over system paths.
2026-02-01 10:23:41 +01:00

79 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from "bun:test";
import { truncateToWidth, visibleWidth } from "@oh-my-pi/pi-tui";
/**
* Tests for truncateToWidth behavior with Unicode characters.
*
* These tests verify that truncateToWidth properly handles text with
* Unicode characters that have different byte vs display widths.
*/
describe("truncateToWidth", () => {
it("should truncate messages with Unicode characters correctly", () => {
// This message contains a checkmark (✔) which may have display width > 1 byte
const message = '✔ script to run › dev $ concurrently "vite" "node --import tsx ./';
const maxMsgWidth = visibleWidth(message) - 1;
const truncated = truncateToWidth(message, maxMsgWidth);
const truncatedWidth = visibleWidth(truncated);
expect(truncatedWidth).toBeLessThanOrEqual(maxMsgWidth);
});
it("should handle emoji characters", () => {
const message = "🎉 Celebration! 🚀 Launch 📦 Package ready for deployment now";
const maxMsgWidth = visibleWidth(message) - 2;
const truncated = truncateToWidth(message, maxMsgWidth);
const truncatedWidth = visibleWidth(truncated);
expect(truncatedWidth).toBeLessThanOrEqual(maxMsgWidth);
});
it("should handle mixed ASCII and wide characters", () => {
const message = "Hello 世界 Test 你好 More text here that is long";
const maxMsgWidth = visibleWidth(message) - 2;
const truncated = truncateToWidth(message, maxMsgWidth);
const truncatedWidth = visibleWidth(truncated);
expect(truncatedWidth).toBeLessThanOrEqual(maxMsgWidth);
});
it("should not truncate messages that fit", () => {
const message = "Short message";
const width = 50;
const maxMsgWidth = width - 2;
const truncated = truncateToWidth(message, maxMsgWidth);
expect(truncated).toBe(message);
expect(visibleWidth(truncated)).toBeLessThanOrEqual(maxMsgWidth);
});
it("should add ellipsis when truncating", () => {
const message = "This is a very long message that needs to be truncated";
const width = 30;
const maxMsgWidth = width - 2;
const truncated = truncateToWidth(message, maxMsgWidth);
expect(truncated).toContain("…");
expect(visibleWidth(truncated)).toBeLessThanOrEqual(maxMsgWidth);
});
it("should handle the exact crash case from issue report", () => {
// Terminal width is set to one column less than the full line
// The problematic text contained "✔" and "›" characters
const message = '✔ script to run › dev $ concurrently "vite" "node --import tsx ./server.ts"';
const cursorWidth = 2; // "› " or " "
const terminalWidth = visibleWidth(message) + cursorWidth - 1;
const maxMsgWidth = terminalWidth - cursorWidth;
const truncated = truncateToWidth(message, maxMsgWidth);
const finalWidth = visibleWidth(truncated);
// The final line (cursor + message) must not exceed terminal width
expect(finalWidth + cursorWidth).toBeLessThanOrEqual(terminalWidth);
});
});