-
Notifications
You must be signed in to change notification settings - Fork 34
test: property-check table formatter alignment under unicode content #595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ndycode
wants to merge
3
commits into
main
Choose a base branch
from
claude/audit-76-table-formatter-property
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import * as fc from "fast-check"; | ||
| import { | ||
| buildTable, | ||
| buildTableRow, | ||
| type TableColumn, | ||
| type TableOptions, | ||
| } from "../../lib/table-formatter.js"; | ||
| import { displayWidth } from "../../lib/ui/display-width.js"; | ||
|
|
||
| // Mix single-column ASCII with double-column CJK and emoji so padding and | ||
| // truncation are exercised in display columns, not UTF-16 code units (ui-02). | ||
| const arbCellText = fc | ||
| .array(fc.constantFrom("a", "B", "7", " ", "-", "é", "漢", "字", "🚀", "⚡"), { | ||
| minLength: 0, | ||
| maxLength: 12, | ||
| }) | ||
| .map((chars) => chars.join("")); | ||
|
|
||
| const arbColumn: fc.Arbitrary<TableColumn> = fc.record({ | ||
| header: arbCellText, | ||
| width: fc.integer({ min: 0, max: 10 }), | ||
| // undefined exercises formatCell's default-left branch. | ||
| align: fc.option(fc.constantFrom<"left" | "right">("left", "right"), { | ||
| nil: undefined, | ||
| }), | ||
| }); | ||
|
|
||
| const arbOptions: fc.Arbitrary<TableOptions> = fc | ||
| .array(arbColumn, { minLength: 1, maxLength: 5 }) | ||
| .map((columns) => ({ columns })); | ||
|
|
||
| function expectedLineWidth(options: TableOptions): number { | ||
| const widths = options.columns.map((column) => Math.max(0, column.width)); | ||
| return widths.reduce((sum, width) => sum + width, 0) + (options.columns.length - 1); | ||
| } | ||
|
|
||
| describe("table formatter property invariants", () => { | ||
| it("every line of any table has the exact same display width as the layout", () => { | ||
| fc.assert( | ||
| fc.property( | ||
| arbOptions, | ||
| fc.array(fc.array(arbCellText, { minLength: 0, maxLength: 6 }), { | ||
| minLength: 0, | ||
| maxLength: 8, | ||
| }), | ||
| (options, rows) => { | ||
| const lines = buildTable(rows, options); | ||
| expect(lines).toHaveLength(rows.length + 2); | ||
| const layoutWidth = expectedLineWidth(options); | ||
| for (const line of lines) { | ||
| // Header, separator, and every data row stay in lockstep no | ||
| // matter what content (incl. CJK/emoji, missing cells, or | ||
| // extra cells beyond the column count) lands in the rows. | ||
| expect(displayWidth(line)).toBe(layoutWidth); | ||
| } | ||
| }, | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it("content that fits is preserved verbatim with padding on the declared side", () => { | ||
| fc.assert( | ||
| fc.property( | ||
| arbCellText, | ||
| fc.integer({ min: 1, max: 14 }), | ||
| fc.option(fc.constantFrom<"left" | "right">("left", "right"), { | ||
| nil: undefined, | ||
| }), | ||
| (value, width, align) => { | ||
| fc.pre(displayWidth(value) <= width); | ||
| const row = buildTableRow([value], { | ||
| columns: [{ header: "h", width, align }], | ||
| }); | ||
| const pad = " ".repeat(width - displayWidth(value)); | ||
| // undefined align defaults to left. | ||
| expect(row).toBe(align === "right" ? pad + value : value + pad); | ||
| }, | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it("content that overflows is truncated to a prefix plus ellipsis, never overflowing", () => { | ||
| fc.assert( | ||
| fc.property( | ||
| arbCellText, | ||
| fc.integer({ min: 1, max: 6 }), | ||
| fc.constantFrom<"left" | "right">("left", "right"), | ||
| (value, width, align) => { | ||
| fc.pre(displayWidth(value) > width); | ||
| const row = buildTableRow([value], { | ||
| columns: [{ header: "h", width, align }], | ||
| }); | ||
| expect(displayWidth(row)).toBe(width); | ||
| // The ellipsis terminates the visible content for either | ||
| // alignment (content can never follow it, so trimming trailing | ||
| // padding cannot eat content). | ||
| expect(row.trimEnd().endsWith("…")).toBe(true); | ||
| // Prefix fidelity is validated on the LEFT-aligned rendering, | ||
| // where the cell starts at column 0: stripping leading spaces on | ||
| // a right-aligned row could eat spaces that belong to the | ||
| // truncated content itself, not just alignment padding. | ||
| const leftRow = buildTableRow([value], { | ||
| columns: [{ header: "h", width, align: "left" }], | ||
| }); | ||
| const visible = leftRow.replace(/ +$/, ""); | ||
| expect(visible.endsWith("…")).toBe(true); | ||
| expect(value.startsWith(visible.slice(0, -1))).toBe(true); | ||
| expect(displayWidth(visible)).toBeLessThanOrEqual(width); | ||
| }, | ||
| ), | ||
| ); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| it("zero-width columns render empty and never leak an ellipsis into the layout", () => { | ||
| fc.assert( | ||
| fc.property(arbCellText, (value) => { | ||
| const row = buildTableRow([value], { | ||
| columns: [{ header: "h", width: 0 }], | ||
| }); | ||
| expect(row).toBe(""); | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.