From b5c2954ad67f0280732df043d5d9702f3d202819 Mon Sep 17 00:00:00 2001 From: Tahsin Arafat <146520753+TahsinArafat@users.noreply.github.com> Date: Thu, 8 Oct 2026 22:26:25 +0600 Subject: [PATCH] fix(domain): order canonical paths by code point, not UTF-16 unit (#1034) compareCodePoints documented a Unicode code-point comparison but used operators, which compare UTF-16 code units. For astral code points (above U+FFFF) the lead surrogate 0xD800-0xDBFF is numerically below the BMP tail U+E000-U+FFFF, so those characters sorted before it. Delegate to the existing compareUnicodeCodePoints, which the domain already uses elsewhere, so the two canonical comparators agree. All call sites use only the sign, so sort and '< 0' checks are unaffected. Adds the missing regression coverage for canonicalOrdering. Co-authored-by: TahsinArafat Co-authored-by: N0zoM1z0 <1705004398@qq.com> --- src/domain/canonicalOrdering.test.ts | 47 ++++++++++++++++++++++++++++ src/domain/canonicalOrdering.ts | 15 ++++++--- 2 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/domain/canonicalOrdering.test.ts diff --git a/src/domain/canonicalOrdering.test.ts b/src/domain/canonicalOrdering.test.ts new file mode 100644 index 000000000..712c7d3e2 --- /dev/null +++ b/src/domain/canonicalOrdering.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, it } from "vitest"; + +import { compareCodePoints, uniqueSorted } from "./canonicalOrdering.js"; + +const REPLACEMENT = "\uFFFD"; +const GRINNING = "\u{1F600}"; + +describe("compareCodePoints", () => { + it("orders by Unicode code point, not UTF-16 code unit", () => { + // U+FFFD < U+1F600, but the astral character's lead surrogate 0xD83D is + // numerically below U+FFFD, so a code-unit comparison inverts this pair. + expect(compareCodePoints(REPLACEMENT, GRINNING)).toBeLessThan(0); + expect(compareCodePoints(GRINNING, REPLACEMENT)).toBeGreaterThan(0); + }); + + it("reports equality only for equal strings", () => { + expect(compareCodePoints("", "")).toBe(0); + expect(compareCodePoints(GRINNING, GRINNING)).toBe(0); + expect(compareCodePoints("a", "a")).toBe(0); + }); + + it("keeps a prefix before its own extension", () => { + expect(compareCodePoints("ab", "abc")).toBe(-1); + expect(compareCodePoints("abc", "ab")).toBe(1); + }); + + it("orders astral characters by code point against the BMP tail", () => { + const ordered = ["\uFFFD", "\uFFFF", "\u{1F600}"]; + for (const [index, left] of ordered.entries()) + for (const right of ordered.slice(index + 1)) + expect(compareCodePoints(left, right)).toBeLessThan(0); + }); +}); + +describe("uniqueSorted", () => { + it("deduplicates and orders astral values by code point", () => { + expect(uniqueSorted([GRINNING, REPLACEMENT, GRINNING])).toEqual([ + REPLACEMENT, + GRINNING, + ]); + }); + + it("preserves the inferred literal type", () => { + const values: ReadonlyArray<"a" | "b"> = ["b", "a", "b"]; + expect(uniqueSorted(values)).toEqual(["a", "b"]); + }); +}); diff --git a/src/domain/canonicalOrdering.ts b/src/domain/canonicalOrdering.ts index 104efe2af..0675c081e 100644 --- a/src/domain/canonicalOrdering.ts +++ b/src/domain/canonicalOrdering.ts @@ -1,13 +1,18 @@ +import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js"; + /** * Canonical code-point ordering shared across domain and application layers. * - * Compares strings by Unicode code point (`<`/`>`) so graph ordering, - * fingerprints, and deterministic tie-breaks stay stable regardless of - * locale. Prefer this over `String.prototype.localeCompare`, which is - * locale-dependent. + * Compares strings by Unicode code point so graph ordering, fingerprints, and + * deterministic tie-breaks stay stable regardless of locale. Prefer this over + * `String.prototype.localeCompare`, which is locale-dependent. + * + * Ordering by code point differs from ordering by UTF-16 code unit for astral + * code points (above U+FFFF), whose lead surrogate is numerically below the + * BMP tail U+E000-U+FFFF. */ export const compareCodePoints = (left: string, right: string): number => - left < right ? -1 : left > right ? 1 : 0; + compareUnicodeCodePoints(left, right); /** Deduplicate and sort strings with canonical code-point ordering. */ export const uniqueSorted = (