-
Notifications
You must be signed in to change notification settings - Fork 33
test: property-check display-width clustering and parseKey totality #598
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
2
commits into
main
Choose a base branch
from
claude/audit-79-display-width-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
2 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,187 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import * as fc from "fast-check"; | ||
| import { displayWidth, truncateToWidth } from "../../lib/ui/display-width.js"; | ||
| import { parseKey, type KeyAction } from "../../lib/ui/ansi.js"; | ||
|
|
||
| // Adversarial code-point alphabet: plain ASCII and CJK, plus every cluster | ||
| // mechanic the implementation special-cases — ZWJ, variation selector-16, | ||
| // keycap, combining marks, skin-tone modifiers, regional indicators, emoji. | ||
| const arbAdversarialText = fc | ||
| .array( | ||
| fc.constantFrom( | ||
| "a", | ||
| "Z", | ||
| "7", | ||
| " ", | ||
| "漢", | ||
| "한", | ||
| "🚀", | ||
| "👩", | ||
| "👨", | ||
| "☀", | ||
| "❤", | ||
| "", // ZWJ | ||
| "️", // variation selector-16 | ||
| "⃣", // combining enclosing keycap | ||
| "́", // combining acute accent | ||
| "\u{1f3fb}", // skin-tone modifier | ||
| "\u{1f1e6}", // regional indicator A | ||
| "\u{1f1fa}", // regional indicator U | ||
| ), | ||
| { minLength: 0, maxLength: 16 }, | ||
| ) | ||
| .map((chars) => chars.join("")); | ||
|
|
||
| // Plain alphabet with no joiners/modifiers: per-character widths are | ||
| // independent, giving an exact sum oracle (the table-formatter assumption). | ||
| const PLAIN_WIDTHS: ReadonlyArray<readonly [string, number]> = [ | ||
| ["a", 1], | ||
| ["B", 1], | ||
| ["7", 1], | ||
| [" ", 1], | ||
| ["-", 1], | ||
| ["é", 1], | ||
| ["漢", 2], | ||
| ["字", 2], | ||
| ["한", 2], | ||
| ["🚀", 2], | ||
| ]; | ||
|
|
||
| const arbPlainText = fc.array( | ||
| fc.constantFrom(...PLAIN_WIDTHS.map(([char]) => char)), | ||
| { minLength: 0, maxLength: 20 }, | ||
| ); | ||
|
|
||
| const KEY_ACTIONS: readonly KeyAction[] = [ | ||
| "up", | ||
| "down", | ||
| "home", | ||
| "end", | ||
| "enter", | ||
| "escape", | ||
| "escape-start", | ||
| null, | ||
| ]; | ||
|
|
||
| describe("display-width property invariants", () => { | ||
| it("displayWidth is total, non-negative, and bounded by two columns per code point", () => { | ||
| fc.assert( | ||
| fc.property(arbAdversarialText, (text) => { | ||
| const width = displayWidth(text); | ||
| expect(Number.isInteger(width)).toBe(true); | ||
| expect(width).toBeGreaterThanOrEqual(0); | ||
| expect(width).toBeLessThanOrEqual([...text].length * 2); | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it("plain text width equals the sum of per-character widths", () => { | ||
| fc.assert( | ||
| fc.property(arbPlainText, (chars) => { | ||
| const widthByChar = new Map(PLAIN_WIDTHS); | ||
| const expected = chars.reduce( | ||
| (sum, char) => sum + (widthByChar.get(char) ?? 0), | ||
| 0, | ||
| ); | ||
| expect(displayWidth(chars.join(""))).toBe(expected); | ||
| // With no joiners or modifiers in the alphabet, concatenation is | ||
| // exactly additive — the assumption the table formatter relies on. | ||
| const half = Math.floor(chars.length / 2); | ||
| const left = chars.slice(0, half).join(""); | ||
| const right = chars.slice(half).join(""); | ||
| expect(displayWidth(left) + displayWidth(right)).toBe(expected); | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it("truncateToWidth returns a self-consistent, in-budget prefix that is maximal", () => { | ||
| fc.assert( | ||
| fc.property( | ||
| arbAdversarialText, | ||
| fc.integer({ min: 0, max: 20 }), | ||
| (text, maxWidth) => { | ||
| const { text: kept, width } = truncateToWidth(text, maxWidth); | ||
| // Self-consistency: the reported width is the measurer's answer. | ||
| expect(displayWidth(kept)).toBe(width); | ||
| expect(width).toBeLessThanOrEqual(maxWidth); | ||
| expect(text.startsWith(kept)).toBe(true); | ||
| if (kept !== text) { | ||
| // Maximality: clusters are at most 2 columns wide, so a gap of | ||
| // 2+ columns means the next cluster would have fit — the only | ||
| // legal reason to stop early is a remaining gap of 0 or 1. | ||
| expect(maxWidth - width).toBeLessThanOrEqual(1); | ||
| } | ||
| // Idempotence: re-truncating the kept prefix changes nothing. | ||
| const again = truncateToWidth(kept, maxWidth); | ||
| expect(again.text).toBe(kept); | ||
| expect(again.width).toBe(width); | ||
| }, | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it("truncation prefixes grow monotonically with the width budget", () => { | ||
| fc.assert( | ||
| fc.property( | ||
| arbAdversarialText, | ||
| fc.integer({ min: 0, max: 18 }), | ||
| fc.integer({ min: 0, max: 6 }), | ||
| (text, smaller, delta) => { | ||
| const narrow = truncateToWidth(text, smaller); | ||
| const wide = truncateToWidth(text, smaller + delta); | ||
| expect(wide.text.startsWith(narrow.text)).toBe(true); | ||
| expect(wide.width).toBeGreaterThanOrEqual(narrow.width); | ||
| // Self-enforcing cluster granularity: one extra budget column can | ||
| // admit at most 2 more columns of content. This is exactly the | ||
| // "no cluster wider than 2" assumption the maximality bound in | ||
| // the truncation property relies on — if a wider cluster type is | ||
| // ever introduced, this step assertion fails first. | ||
| const step = truncateToWidth(text, smaller + 1); | ||
| expect(step.width - narrow.width).toBeLessThanOrEqual(2); | ||
| }, | ||
| ), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("parseKey property invariants", () => { | ||
| it("is total over arbitrary byte buffers and only ever returns known actions", () => { | ||
| fc.assert( | ||
| fc.property(fc.uint8Array({ maxLength: 12 }), (bytes) => { | ||
| const action = parseKey(Buffer.from(bytes)); | ||
| expect(KEY_ACTIONS.includes(action)).toBe(true); | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it("recognized sequences are stable and unrecognized ones map to null", () => { | ||
| const table: ReadonlyArray<readonly [string, KeyAction]> = [ | ||
| ["\x1b[A", "up"], | ||
| ["\x1bOA", "up"], | ||
| ["\x1b[B", "down"], | ||
| ["\x1bOB", "down"], | ||
| ["\x1b[H", "home"], | ||
| ["\x1bOH", "home"], | ||
| ["\x1b[1~", "home"], | ||
| ["\x1b[7~", "home"], | ||
| ["\x1b[F", "end"], | ||
| ["\x1bOF", "end"], | ||
| ["\x1b[4~", "end"], | ||
| ["\x1b[8~", "end"], | ||
| ["\r", "enter"], | ||
| ["\n", "enter"], | ||
| ["\x03", "escape"], | ||
| ["\x1b", "escape-start"], | ||
| ]; | ||
| const known = new Set(table.map(([sequence]) => sequence)); | ||
| for (const [sequence, action] of table) { | ||
| expect(parseKey(Buffer.from(sequence))).toBe(action); | ||
| } | ||
| fc.assert( | ||
| fc.property(fc.string({ maxLength: 6 }), (input) => { | ||
| fc.pre(!known.has(input)); | ||
| expect(parseKey(Buffer.from(input))).toBeNull(); | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
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.