mirror of
https://github.com/block/buzz.git
synced 2026-11-26 03:01:38 +00:00
Signed-off-by: Tyler Longwell <tlongwell@block.xyz> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Max <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Mari <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Quinn <96f056ad5f2305c8ddf637dc65d048aa4c12d7daeb8867690e34fca46b0ef64c@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Sami <f4a42a97e594b77bdbd8ee35191c8b28a94a4cb871d96f32921558275421fb68@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Perci <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Tyler Longwell <tlongwell@block.xyz>
88 lines
2.8 KiB
JavaScript
88 lines
2.8 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import {
|
|
buildAnimatedAvatarUrl,
|
|
getAvatarSnapshotUrl,
|
|
parseAnimatedAvatarUrl,
|
|
} from "./animatedAvatar.ts";
|
|
|
|
const POSTER = "https://relay.example.com/media/aa.png";
|
|
const GIF = "https://relay.example.com/media/bb.gif";
|
|
|
|
test("build + parse round-trips poster and gif URLs", () => {
|
|
const url = buildAnimatedAvatarUrl(POSTER, GIF);
|
|
const parsed = parseAnimatedAvatarUrl(url);
|
|
assert.deepEqual(parsed, { posterUrl: POSTER, animationUrl: GIF });
|
|
});
|
|
|
|
test("build encodes the gif URL so the fragment has no reserved chars", () => {
|
|
const gif = "https://relay.example.com/media/bb.gif?x=1&y=2#frag";
|
|
const url = buildAnimatedAvatarUrl(POSTER, gif);
|
|
const fragment = url.slice(url.indexOf("#buzz-anim=") + "#buzz-anim=".length);
|
|
assert.ok(!fragment.includes("#"), "fragment should not contain raw #");
|
|
assert.ok(!fragment.includes("&"), "fragment should not contain raw &");
|
|
assert.equal(parseAnimatedAvatarUrl(url)?.animationUrl, gif);
|
|
});
|
|
|
|
test("parse returns null for plain image URLs", () => {
|
|
assert.equal(parseAnimatedAvatarUrl(POSTER), null);
|
|
});
|
|
|
|
test("parse returns null for null/undefined/empty", () => {
|
|
assert.equal(parseAnimatedAvatarUrl(null), null);
|
|
assert.equal(parseAnimatedAvatarUrl(undefined), null);
|
|
assert.equal(parseAnimatedAvatarUrl(""), null);
|
|
});
|
|
|
|
test("parse returns null for emoji data-url avatars", () => {
|
|
assert.equal(
|
|
parseAnimatedAvatarUrl("data:image/svg+xml,%3Csvg%3E%3C/svg%3E"),
|
|
null,
|
|
);
|
|
});
|
|
|
|
test("parse returns null when fragment marker has no poster prefix", () => {
|
|
assert.equal(parseAnimatedAvatarUrl(`#buzz-anim=${GIF}`), null);
|
|
});
|
|
|
|
test("parse returns null when gif part is empty", () => {
|
|
assert.equal(parseAnimatedAvatarUrl(`${POSTER}#buzz-anim=`), null);
|
|
});
|
|
|
|
test("parse returns null when gif part is not an http(s) URL", () => {
|
|
assert.equal(
|
|
parseAnimatedAvatarUrl(`${POSTER}#buzz-anim=javascript%3Aalert(1)`),
|
|
null,
|
|
);
|
|
});
|
|
|
|
test("parse returns null when poster part is not an http(s) URL", () => {
|
|
assert.equal(
|
|
parseAnimatedAvatarUrl(
|
|
`data:image/png;base64,xx#buzz-anim=${encodeURIComponent(GIF)}`,
|
|
),
|
|
null,
|
|
);
|
|
});
|
|
|
|
test("parse returns null on malformed percent-encoding", () => {
|
|
assert.equal(parseAnimatedAvatarUrl(`${POSTER}#buzz-anim=%E0%A4%A`), null);
|
|
});
|
|
|
|
test("getAvatarSnapshotUrl strips animated avatars to their poster URL", () => {
|
|
assert.equal(
|
|
getAvatarSnapshotUrl(buildAnimatedAvatarUrl(POSTER, GIF)),
|
|
POSTER,
|
|
);
|
|
});
|
|
|
|
test("getAvatarSnapshotUrl preserves plain avatar URLs", () => {
|
|
assert.equal(getAvatarSnapshotUrl(POSTER), POSTER);
|
|
});
|
|
|
|
test("getAvatarSnapshotUrl returns null for nullish avatar URLs", () => {
|
|
assert.equal(getAvatarSnapshotUrl(null), null);
|
|
assert.equal(getAvatarSnapshotUrl(undefined), null);
|
|
});
|