import { describe, expect, test } from "vitest"; import { decodePeachScript, encodePeachScript, isPeachScript, normalisePeachScript, } from "./peachScript"; describe("peachScript", () => { test("byte 0 maps to U+E000", () => { const out = encodePeachScript(new Uint8Array([0])); expect(out.codePointAt(0)).toBe(0xe000); }); test("byte 255 maps to U+E0FF", () => { const out = encodePeachScript(new Uint8Array([255])); expect(out.codePointAt(0)).toBe(0xe0ff); }); test("round-trip identity for all 256 byte values", () => { const bytes = new Uint8Array(256); for (let i = 0; i < 256; i++) bytes[i] = i; const encoded = encodePeachScript(bytes); const decoded = decodePeachScript(encoded); expect(decoded).toEqual(bytes); }); test("empty bytes round-trip to empty string and back", () => { expect(encodePeachScript(new Uint8Array())).toBe(""); expect(decodePeachScript("")).toEqual(new Uint8Array()); }); test("decode throws on character outside PUA range", () => { expect(() => decodePeachScript("hello")).toThrow("INVALID_PEACHSCRIPT_CHARACTER"); expect(() => decodePeachScript(String.fromCodePoint(0xe100))).toThrow( "INVALID_PEACHSCRIPT_CHARACTER" ); expect(() => decodePeachScript(String.fromCodePoint(0xdfff))).toThrow( "INVALID_PEACHSCRIPT_CHARACTER" ); }); test("random-payload round-trip preserves bytes", () => { const payload = crypto.getRandomValues(new Uint8Array(2048)); const decoded = decodePeachScript(encodePeachScript(payload)); expect(decoded).toEqual(payload); }); test("normalisePeachScript strips whitespace", () => { const encoded = encodePeachScript(new Uint8Array([1, 2, 3, 4])); const wrapped = encoded.slice(0, 2) + "\n " + encoded.slice(2); expect(normalisePeachScript(wrapped)).toBe(encoded); }); test("isPeachScript accepts only PUA strings", () => { expect(isPeachScript(encodePeachScript(new Uint8Array([1, 2, 3])))).toBe(true); expect(isPeachScript("")).toBe(false); expect(isPeachScript("hello")).toBe(false); const mixed = encodePeachScript(new Uint8Array([1])) + "x"; expect(isPeachScript(mixed)).toBe(false); }); });