Files
vue-tui/packages/runtime-tests/integration/layout/flex.test.tsx
T
Yunfei He b6dc9a0919 fix(runtime): a 0-width box wraps its text onto its own line, not drops it (Ink parity) (#103)
A 0-width text container (flexBasis=0, width=0, width="0%", a negative parsed
percent) DROPPED its text in vue where Ink wraps it onto its own row. Ink's wrapText
has no width<=0 guard: wrapAnsi("A", 0, {hard:true, trim:false}) = "\nA" (height 2),
so the glyph occupies a second row and a row-sibling renders "B\nA". vue collapsed it
to height 1 (wrapText's `width <= 0 -> [""]` guard), then the paint clamp re-collapsed
the wrap, so the sibling overwrote the text -> "B".

Fixes, all confined to the width<=0 branch:
- text-measure.ts: drop the `width <= 0 -> [""]` guard. A styled string can't go
  through wrapAnsi at width 0 (wrap-ansi@10 byte-splits SGR codes -> garbage like
  "B\n["), so the wrap/hard branch routes through a new wrapZeroWidthAnsi that
  derives its line STRUCTURE from wrapAnsi on the PLAIN (stripped) text — which is
  correct for zero-width graphemes (ZWSP/ZWNJ/ZWJ/combining/VS16/BOM, interior and
  trailing) — then re-applies SGR per grapheme via slice-ansi's slot model, keeping
  wide glyphs whole. Input is NFC-normalized first so combining sequences compose to
  match wrap-ansi (and vue's own normal-width path), not the decomposed source bytes.
- paint.ts: pad the bg to the TRUE wrap width (0), not a >=1-clamped width — a 0-width
  box pads nothing (Ink getMaxWidth=0); clamping bg-padded the empty leading wrap line
  into a stray cell that collided with a row-sibling.

A comparison-battery test locks wrapZeroWidthAnsi's plain output to wrapAnsi's
width-0 layout for ~22 inputs (zero-width, wide, emoji, ZWJ, combining decomposed +
composed, multiline). The full layout suite is byte-unchanged for all width>=1 cases.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-31 23:54:12 +08:00

311 lines
10 KiB
TypeScript

import { defineComponent } from "vue";
import { expect, test } from "vite-plus/test";
import { render } from "@vue-tui/testing";
import { Box, Text } from "@vue-tui/runtime";
test("grow equally", async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box flexDirection="row" width={6}>
<Box flexGrow={1}>
<Text>A</Text>
</Box>
<Box flexGrow={1}>
<Text>B</Text>
</Box>
</Box>
)),
{ columns: 100 },
);
expect(lastFrame({ trimLines: true })).toBe("A B");
});
test("grow one element", async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box flexDirection="row" width={6}>
<Box flexGrow={1}>
<Text>A</Text>
</Box>
<Text>B</Text>
</Box>
)),
{ columns: 100 },
);
expect(lastFrame({ trimLines: true })).toBe("A B");
});
test("do not shrink", async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box flexDirection="row" width={16}>
<Box flexShrink={0} width={6}>
<Text>A</Text>
</Box>
<Box flexShrink={0} width={6}>
<Text>B</Text>
</Box>
<Box width={6}>
<Text>C</Text>
</Box>
</Box>
)),
{ columns: 100 },
);
expect(lastFrame({ trimLines: true })).toBe("A B C");
});
test("shrink equally", async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box flexDirection="row" width={10}>
<Box flexShrink={1} width={6}>
<Text>A</Text>
</Box>
<Box flexShrink={1} width={6}>
<Text>B</Text>
</Box>
<Text>C</Text>
</Box>
)),
{ columns: 100 },
);
expect(lastFrame({ trimLines: true })).toBe("A B C");
});
test('set flex basis with flexDirection="row" container', async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box flexDirection="row" width={6}>
<Box flexBasis={3}>
<Text>A</Text>
</Box>
<Text>B</Text>
</Box>
)),
{ columns: 100 },
);
expect(lastFrame({ trimLines: true })).toBe("A B");
});
test('set flex basis in percent with flexDirection="row" container', async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box flexDirection="row" width={6}>
<Box flexBasis="50%">
<Text>A</Text>
</Box>
<Text>B</Text>
</Box>
)),
{ columns: 100 },
);
expect(lastFrame({ trimLines: true })).toBe("A B");
});
test('set flex basis with flexDirection="column" container', async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box height={6} flexDirection="column">
<Box flexBasis={3}>
<Text>A</Text>
</Box>
<Text>B</Text>
</Box>
)),
{ columns: 100 },
);
expect(lastFrame({ trimLines: true })).toBe("A\n\n\nB\n\n");
});
test('set flex basis in percent with flexDirection="column" container', async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box height={6} flexDirection="column">
<Box flexBasis="50%">
<Text>A</Text>
</Box>
<Text>B</Text>
</Box>
)),
{ columns: 100 },
);
expect(lastFrame({ trimLines: true })).toBe("A\n\n\nB\n\n");
});
// Ink coerces ANY string flexBasis to a percent (styles.ts:547-555): a bare
// numeric string like "3" is parsed as 3% of the container, NOT 3 absolute
// cells. At width 6, "3" → 3% → 0 cells, so box A collapses and B takes the row.
test("bare numeric-string flexBasis is a percent (Ink parity), not absolute", async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box flexDirection="row" width={6}>
<Box flexBasis="3">
<Text>A</Text>
</Box>
<Text>B</Text>
</Box>
)),
{ columns: 100 },
);
// 3% of 6 = 0 cells → A width 0 → only B is rendered.
expect(lastFrame({ trimLines: true })).toBe("B");
});
// Guard: a "50%" string still resolves to 50% (3 cells of 6) → "A B".
test('percent-string flexBasis "50%" still resolves as percent', async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box flexDirection="row" width={6}>
<Box flexBasis="50%">
<Text>A</Text>
</Box>
<Text>B</Text>
</Box>
)),
{ columns: 100 },
);
expect(lastFrame({ trimLines: true })).toBe("A B");
});
// Guard: a numeric flexBasis stays absolute (3 cells) → "A B".
test("numeric flexBasis stays absolute", async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box flexDirection="row" width={6}>
<Box flexBasis={3}>
<Text>A</Text>
</Box>
<Text>B</Text>
</Box>
)),
{ columns: 100 },
);
expect(lastFrame({ trimLines: true })).toBe("A B");
});
// Ink's flexBasis branch (styles.ts:547-555) is number→setFlexBasis,
// string→setFlexBasisPercent, ELSE→setFlexBasisAuto. A non-number/non-string
// runtime value (Vue's [Number,String] prop validation only WARNS — it still
// forwards the value) must fall back to auto, not throw. Ink renders "AB"
// (flexBasis ignored → box shrinks to content). flexBasis={false} is the
// canonical case; the cast bypasses the compile-time prop type to exercise the
// real runtime branch a mis-typed app would hit.
test("non-number/non-string flexBasis falls back to auto (Ink parity), does not throw", async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box flexDirection="row" width={6}>
<Box flexBasis={false as unknown as number}>
<Text>A</Text>
</Box>
<Text>B</Text>
</Box>
)),
{ columns: 100 },
);
// Auto fallback → box shrinks to content → A and B adjacent, matching Ink.
expect(lastFrame({ trimLines: true })).toBe("AB");
});
// A zero/negative parsed percent ("0"→0%, "-5"→-5%, "0x10"→parseInt=0→0%) produces a
// 0-width inner box. Ink renders "B\nA" (B on the row, A wraps onto the next line). The
// 0-width text measures via wrapAnsi("A", 0, {hard:true, trim:false}) = "\nA" → height 2,
// so A occupies a second row. vue previously dropped the text ("B") because wrapText's
// `width <= 0 → [""]` guard collapsed the measure to height 1. Verified against Ink v7.0.4
// (@40b3a75): all four of flexBasis=0/"0%" and width=0/"0%" render "B\nA".
test("zero/negative flexBasis% wraps the sibling in Ink (downstream divergence)", async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box flexDirection="row" width={6}>
<Box flexBasis="0">
<Text>A</Text>
</Box>
<Text>B</Text>
</Box>
)),
{ columns: 100 },
);
// Ink v7.0.4 renders "B\nA".
expect(lastFrame({ trimLines: true })).toBe("B\nA");
});
test("zero-width Box wraps its text onto its own line (width={0})", async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box width={6}>
<Box width={0}>
<Text>A</Text>
</Box>
<Text>B</Text>
</Box>
)),
{ columns: 100 },
);
// Ink v7.0.4 renders "B\nA": the 0-width text measures height 2 via
// wrapAnsi("A", 0, {hard:true}) = "\nA", so A wraps below sibling B.
expect(lastFrame({ trimLines: true })).toBe("B\nA");
});
test('zero-percent-width Box wraps its text onto its own line (width="0%")', async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box width={6}>
<Box width="0%">
<Text>A</Text>
</Box>
<Text>B</Text>
</Box>
)),
{ columns: 100 },
);
// Ink v7.0.4 renders "B\nA" — same as width={0}; a 0% resolved width is also 0px.
expect(lastFrame({ trimLines: true })).toBe("B\nA");
});
test("zero-width Box with EMPTY text adds no spurious row", async () => {
const { lastFrame } = await render(
defineComponent(() => () => (
<Box width={6}>
<Box width={0}>
<Text>{""}</Text>
</Box>
<Text>B</Text>
</Box>
)),
{ columns: 100 },
);
// Ink v7.0.4 renders "B": empty text measures width 0 (≤ 0), so it never wraps and
// never gains a second row. The 0-width fix must NOT add a blank row here.
expect(lastFrame({ trimLines: true })).toBe("B");
});
test("zero-width Box with backgroundColor wraps cleanly, keeping the bg glyph (Ink parity)", async () => {
// Regression guard for the wrap-ansi width<=0 byte-split: at width 0 the 0-width Box's
// text wraps onto its own row, but vue bakes the bg color INTO the string before wrapping,
// and wrap-ansi@10 byte-splits the SGR escapes of a STYLED string at width<=0
// (wrapAnsi("\x1b[41mA\x1b[49m", 0) = "\x1b\n[\n4\n1\nm\nA\n…"). That scattered the escape
// bytes across rows and rendered a garbage "B\n[" (the 2nd byte of "\x1b[41m"). wrapText
// now routes width<=0 styled text through an ANSI-aware per-grapheme split, matching Ink,
// which wraps PLAIN text and colorizes per line afterwards.
const { lastFrame } = await render(
defineComponent(() => () => (
<Box flexDirection="row" width={6}>
<Box width={0} backgroundColor="red">
<Text>A</Text>
</Box>
<Text>B</Text>
</Box>
)),
{ columns: 100 },
);
// RAW-byte parity target captured from Ink v7.0.4 (@40b3a75) with chalk level 3:
// "B\n\x1b[41mA\x1b[49m\n" — row 2 keeps the FULL bg-colored glyph (overflow:visible).
// vue trims trailing whitespace/newlines per frame line, so the equivalent raw frame is
// "B\n\x1b[41mA\x1b[49m" (no trailing newline). The bg glyph must survive intact.
expect(lastFrame({ raw: true })).toBe("B\n\x1b[41mA\x1b[49m");
// And the stripped visible layout is "B\nA" (sanity check on the wrap position).
// eslint-disable-next-line no-control-regex -- strip ANSI to assert the visible layout
const visible = lastFrame({ trimLines: true })!.replace(/\x1b\[[0-9;]*m/g, "");
expect(visible).toBe("B\nA");
});