|
1 | 1 | import { describe, it } from "node:test"; |
2 | 2 | import assert from "node:assert"; |
| 3 | + |
| 4 | +// Import polyfills first |
| 5 | +import "./polyfills.js"; |
| 6 | + |
3 | 7 | import { toArray, escapeRegex, validateDomain } from "./index.js"; |
4 | 8 |
|
5 | 9 | // ============================================================================ |
@@ -358,3 +362,77 @@ describe("IP response parsing", () => { |
358 | 362 | assert.strictEqual(response.registrar.name, "ARIN"); |
359 | 363 | }); |
360 | 364 | }); |
| 365 | + |
| 366 | + |
| 367 | +// ============================================================================ |
| 368 | +// String.prototype.toWellFormed polyfill tests (Node.js 18 compatibility) |
| 369 | +// ============================================================================ |
| 370 | + |
| 371 | +describe("String.prototype.toWellFormed polyfill", () => { |
| 372 | + it("toWellFormed is available as a function", () => { |
| 373 | + assert.strictEqual(typeof "".toWellFormed, "function"); |
| 374 | + }); |
| 375 | + |
| 376 | + it("returns same string for well-formed input", () => { |
| 377 | + const str = "Hello, World!"; |
| 378 | + assert.strictEqual(str.toWellFormed(), str); |
| 379 | + }); |
| 380 | + |
| 381 | + it("handles empty string", () => { |
| 382 | + assert.strictEqual("".toWellFormed(), ""); |
| 383 | + }); |
| 384 | + |
| 385 | + it("handles valid surrogate pairs (emoji)", () => { |
| 386 | + const emoji = "😀"; // U+1F600 = \uD83D\uDE00 |
| 387 | + assert.strictEqual(emoji.toWellFormed(), emoji); |
| 388 | + }); |
| 389 | + |
| 390 | + it("replaces lone high surrogate with U+FFFD", () => { |
| 391 | + const loneHigh = "abc\uD800def"; |
| 392 | + assert.strictEqual(loneHigh.toWellFormed(), "abc\uFFFDdef"); |
| 393 | + }); |
| 394 | + |
| 395 | + it("replaces lone low surrogate with U+FFFD", () => { |
| 396 | + const loneLow = "abc\uDC00def"; |
| 397 | + assert.strictEqual(loneLow.toWellFormed(), "abc\uFFFDdef"); |
| 398 | + }); |
| 399 | + |
| 400 | + it("replaces lone high surrogate at end", () => { |
| 401 | + const str = "test\uD800"; |
| 402 | + assert.strictEqual(str.toWellFormed(), "test\uFFFD"); |
| 403 | + }); |
| 404 | + |
| 405 | + it("handles multiple lone surrogates", () => { |
| 406 | + const str = "\uD800\uD800"; |
| 407 | + assert.strictEqual(str.toWellFormed(), "\uFFFD\uFFFD"); |
| 408 | + }); |
| 409 | + |
| 410 | + it("preserves valid surrogate pairs among lone surrogates", () => { |
| 411 | + const str = "\uD800\uD83D\uDE00\uDC00"; // lone high, valid pair, lone low |
| 412 | + assert.strictEqual(str.toWellFormed(), "\uFFFD😀\uFFFD"); |
| 413 | + }); |
| 414 | +}); |
| 415 | + |
| 416 | +describe("String.prototype.isWellFormed polyfill", () => { |
| 417 | + it("isWellFormed is available as a function", () => { |
| 418 | + assert.strictEqual(typeof "".isWellFormed, "function"); |
| 419 | + }); |
| 420 | + |
| 421 | + it("returns true for well-formed strings", () => { |
| 422 | + assert.strictEqual("Hello".isWellFormed(), true); |
| 423 | + assert.strictEqual("".isWellFormed(), true); |
| 424 | + assert.strictEqual("😀".isWellFormed(), true); |
| 425 | + }); |
| 426 | + |
| 427 | + it("returns false for lone high surrogate", () => { |
| 428 | + assert.strictEqual("test\uD800".isWellFormed(), false); |
| 429 | + }); |
| 430 | + |
| 431 | + it("returns false for lone low surrogate", () => { |
| 432 | + assert.strictEqual("test\uDC00".isWellFormed(), false); |
| 433 | + }); |
| 434 | + |
| 435 | + it("returns true for valid surrogate pair", () => { |
| 436 | + assert.strictEqual("\uD83D\uDE00".isWellFormed(), true); |
| 437 | + }); |
| 438 | +}); |
0 commit comments