describe("resolveCompactionInstructions", () => {
describe("null / undefined handling", () => {
it("returns DEFAULT when both args are undefined", () => {
expect(resolveCompactionInstructions(undefined, undefined)).toBe(
DEFAULT_COMPACTION_INSTRUCTIONS,
);
});
it("returns DEFAULT when both args are explicitly null (untyped JS caller)", () => {
expect(
resolveCompactionInstructions(null as unknown as undefined, null as unknown as undefined),
).toBe(DEFAULT_COMPACTION_INSTRUCTIONS);
});
});
describe("empty and whitespace normalization", () => {
it("treats empty-string event as absent -- runtime wins", () => { const result = resolveCompactionInstructions("", "runtime value");
expect(result).toBe("runtime value");
});
it("treats tab/newline-only event as absent -- runtime wins", () => { const result = resolveCompactionInstructions("\t\n\r", "runtime value");
expect(result).toBe("runtime value");
});
it("treats empty-string runtime as absent -- DEFAULT wins", () => { const result = resolveCompactionInstructions(undefined, "");
expect(result).toBe(DEFAULT_COMPACTION_INSTRUCTIONS);
});
it("treats whitespace-only runtime as absent -- DEFAULT wins", () => { const result = resolveCompactionInstructions(undefined, " ");
expect(result).toBe(DEFAULT_COMPACTION_INSTRUCTIONS);
});
it("falls through to DEFAULT when both are empty strings", () => {
expect(resolveCompactionInstructions("", "")).toBe(DEFAULT_COMPACTION_INSTRUCTIONS);
});
it("falls through to DEFAULT when both are whitespace-only", () => {
expect(resolveCompactionInstructions(" ", "\t\n")).toBe(DEFAULT_COMPACTION_INSTRUCTIONS);
});
it("non-breaking space (\\u00A0) IS trimmed by ES2015+ trim() -- falls through", () => { const nbsp = "\u00A0"; const result = resolveCompactionInstructions(nbsp, "runtime");
expect(result).toBe("runtime");
});
it("KNOWN_EDGE: zero-width space (\\u200B) survives normalization -- invisible string used as instructions", () => { const zws = "\u200B"; const result = resolveCompactionInstructions(zws, "runtime");
expect(result).toBe(zws);
});
});
describe("precedence", () => {
it("event wins over runtime when both are non-empty", () => { const result = resolveCompactionInstructions("event value", "runtime value");
expect(result).toBe("event value");
});
it("runtime wins when event is undefined", () => { const result = resolveCompactionInstructions(undefined, "runtime value");
expect(result).toBe("runtime value");
});
it("event is trimmed before use", () => { const result = resolveCompactionInstructions(" event ", "runtime");
expect(result).toBe("event");
});
it("runtime is trimmed before use", () => { const result = resolveCompactionInstructions(undefined, " runtime ");
expect(result).toBe("runtime");
});
});
describe("truncation at 800 chars", () => {
it("does NOT truncate string of exactly 800 chars", () => { const exact800 = "A".repeat(800); const result = resolveCompactionInstructions(exact800, undefined);
expect(result).toHaveLength(800);
expect(result).toBe(exact800);
});
it("truncates string of 801 chars to 800", () => { const over = "B".repeat(801); const result = resolveCompactionInstructions(over, undefined);
expect(result).toHaveLength(800);
expect(result).toBe("B".repeat(800));
});
it("truncates very long string to exactly 800", () => { const huge = "C".repeat(5000); const result = resolveCompactionInstructions(huge, undefined);
expect(result).toHaveLength(800);
});
it("truncation applies AFTER trimming -- 810 raw chars with 10 leading spaces yields 800", () => { const padded = " ".repeat(10) + "D".repeat(800); const result = resolveCompactionInstructions(padded, undefined);
expect(result).toHaveLength(800);
expect(result).toBe("D".repeat(800));
});
it("truncation applies to runtime fallback as well", () => { const longRuntime = "R".repeat(1000); const result = resolveCompactionInstructions(undefined, longRuntime);
expect(result).toHaveLength(800);
});
it("truncates by code points, not code units (emoji safe)", () => { const emojis801 = "\u{1F600}".repeat(801); const result = resolveCompactionInstructions(emojis801, undefined);
expect(Array.from(result)).toHaveLength(800);
});
it("does not split surrogate pair when cut lands inside a pair", () => { const input = "X" + "\u{1F600}".repeat(800); const result = resolveCompactionInstructions(input, undefined); const codePoints = Array.from(result);
expect(codePoints).toHaveLength(800);
expect(codePoints[0]).toBe("X"); // Every code point in the truncated result must be a complete character (no lone surrogates) for (const cp of codePoints) { const code = cp.codePointAt(0)!; const isLoneSurrogate = code >= 0xd800 && code <= 0xdfff;
expect(isLoneSurrogate).toBe(false);
}
});
});
describe("return type", () => {
it("always returns a string, never undefined or null", () => { const cases: [string | undefined, string | undefined][] = [
[undefined, undefined],
["", ""],
[" ", " "],
[null as unknown as undefined, null as unknown as undefined],
["valid", undefined],
[undefined, "valid"],
];
for (const [event, runtime] of cases) { const result = resolveCompactionInstructions(event, runtime);
expect(typeof result).toBe("string");
expect(result.length).toBeGreaterThan(0);
}
});
});
});
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.