|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + decodeCustomIdComponent, |
| 4 | + encodeCustomIdComponent, |
| 5 | + escapeCustomIdFieldValue, |
| 6 | + needsCustomIdFieldEscaping, |
| 7 | + unescapeCustomIdFieldValue, |
| 8 | +} from "./custom-id-codec.js"; |
| 9 | + |
| 10 | +const URI_ROUND_TRIP_VALUES = [ |
| 11 | + "plain", |
| 12 | + "with space", |
| 13 | + "semi;colon", |
| 14 | + "percent%value", |
| 15 | + "env|prod", |
| 16 | + "unicode-ünïcødé-🎛️", |
| 17 | + "a=b&c=d;e=f", |
| 18 | + "", |
| 19 | +]; |
| 20 | + |
| 21 | +describe("custom-id URI component codec", () => { |
| 22 | + it("round-trips values through encode/decode", () => { |
| 23 | + for (const value of URI_ROUND_TRIP_VALUES) { |
| 24 | + expect(decodeCustomIdComponent(encodeCustomIdComponent(value))).toBe(value); |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + it("never emits the ; field separator or raw %", () => { |
| 29 | + for (const value of URI_ROUND_TRIP_VALUES) { |
| 30 | + const encoded = encodeCustomIdComponent(value); |
| 31 | + expect(encoded).not.toContain(";"); |
| 32 | + expect(encoded).not.toMatch(/%(?![0-9A-Fa-f]{2})/); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + // Discord redelivers component ids from old messages indefinitely; values |
| 37 | + // that predate strict encoding must pass through unchanged. |
| 38 | + it("falls back to the raw value on malformed percent input", () => { |
| 39 | + expect(decodeCustomIdComponent("100%")).toBe("100%"); |
| 40 | + expect(decodeCustomIdComponent("a%zzb")).toBe("a%zzb"); |
| 41 | + expect(decodeCustomIdComponent("trailing%2")).toBe("trailing%2"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("decodes historical unguarded-encoded values", () => { |
| 45 | + expect(decodeCustomIdComponent("a%20b")).toBe("a b"); |
| 46 | + expect(decodeCustomIdComponent("env%7Cprod")).toBe("env|prod"); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +describe("custom-id field escape (versioned occomp/ocmodal grammar)", () => { |
| 51 | + it("round-trips only % and the ; separator", () => { |
| 52 | + for (const value of URI_ROUND_TRIP_VALUES) { |
| 53 | + expect(unescapeCustomIdFieldValue(escapeCustomIdFieldValue(value))).toBe(value); |
| 54 | + } |
| 55 | + expect(escapeCustomIdFieldValue("a;b%c")).toBe("a%3Bb%25c"); |
| 56 | + expect(escapeCustomIdFieldValue("unicode-ü 🎛️")).toBe("unicode-ü 🎛️"); |
| 57 | + }); |
| 58 | + |
| 59 | + it("detects values that require escaping", () => { |
| 60 | + expect(needsCustomIdFieldEscaping("plain value")).toBe(false); |
| 61 | + expect(needsCustomIdFieldEscaping("has;separator")).toBe(true); |
| 62 | + expect(needsCustomIdFieldEscaping("has%percent")).toBe(true); |
| 63 | + }); |
| 64 | + |
| 65 | + // Wire compat: ids escaped by the pre-consolidation copies must keep |
| 66 | + // decoding byte-exactly (e=1 payloads live on old Discord messages). |
| 67 | + it("decodes historical escaped payloads case-insensitively", () => { |
| 68 | + expect(unescapeCustomIdFieldValue("a%3Bb%25c")).toBe("a;b%c"); |
| 69 | + expect(unescapeCustomIdFieldValue("a%3bb%25c")).toBe("a;b%c"); |
| 70 | + }); |
| 71 | +}); |
0 commit comments