- Introduced `repairText` and `containsSuspiciousEncoding` utilities for handling corrupted text encodings. - Added unit tests (`encoding-repair.spec.js`) to validate encoding repair and encoding marker detection. - Integrated encoding check into unit test workflows (`text:check-encoding` and `text:fix-encoding` scripts). - Replaced corrupted strings across multiple components and locales with proper UTF-8 encodings.
42 lines
1.4 KiB
JavaScript
42 lines
1.4 KiB
JavaScript
import { describe, expect, it } from "vitest";
|
|
import { containsSuspiciousEncoding, repairText } from "../../scripts/text-encoding.mjs";
|
|
|
|
function fromCharCodes(codes) {
|
|
return String.fromCharCode(...codes);
|
|
}
|
|
|
|
function buildSingleEncoded(inputPrefix, inputSuffix) {
|
|
return `${inputPrefix}${fromCharCodes([0xc3, 0xb8])}${inputSuffix}`;
|
|
}
|
|
|
|
function buildDoubleEncoded(inputPrefix, inputSuffix) {
|
|
return `${inputPrefix}${fromCharCodes([0xc3, 0x83, 0xc2, 0xb8])}${inputSuffix}`;
|
|
}
|
|
|
|
describe("encoding repair utilities", () => {
|
|
it("repairs a single-encoded Danish string", () => {
|
|
const input = buildSingleEncoded("Gennemf", "r");
|
|
expect(repairText(input)).toBe("Gennemfør");
|
|
});
|
|
|
|
it("repairs a second single-encoded Danish string", () => {
|
|
const input = buildSingleEncoded("Tilf", "j");
|
|
expect(repairText(input)).toBe("Tilføj");
|
|
});
|
|
|
|
it("repairs a double-encoded Danish string", () => {
|
|
const input = buildDoubleEncoded("Gennemf", "r");
|
|
expect(repairText(input)).toBe("Gennemfør");
|
|
});
|
|
|
|
it("does not mutate already-correct UTF-8 text", () => {
|
|
expect(repairText("Gennemfør")).toBe("Gennemfør");
|
|
expect(repairText("Tilføj")).toBe("Tilføj");
|
|
});
|
|
|
|
it("detects suspicious encoding markers only in corrupted strings", () => {
|
|
expect(containsSuspiciousEncoding(buildSingleEncoded("Tilf", "j"))).toBe(true);
|
|
expect(containsSuspiciousEncoding("Tilføj")).toBe(false);
|
|
});
|
|
});
|