Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/improsocial/impro
An extensible Bluesky client for web impro.social
Something went wrong. Try again.
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899import { describe, it } from "node:test";import assert from "node:assert/strict";import { tokenizeRichText, isEmojiOnlyTokens } from "/js/richTextHelpers.js";
describe("tokenizeRichText", () => { it("returns a single text token for plain text", () => { assert.deepEqual(tokenizeRichText({ text: "hello", facets: [] }), [ { type: "text", value: "hello" }, ]); });
it("interleaves text and facet tokens without empty text tokens", () => { const text = "#tag in front"; const facets = [ { index: { byteStart: 0, byteEnd: 4 }, features: [{ $type: "app.bsky.richtext.facet#tag", tag: "tag" }], }, ]; const tokens = tokenizeRichText({ text, facets }); assert.deepEqual(tokens.length, 2); assert.deepEqual(tokens[0].type, "facet"); assert.deepEqual(tokens[0].text, "#tag"); assert.deepEqual(tokens[1], { type: "text", value: " in front" }); });
it("drops overlapping facets like the renderer does", () => { const text = "overlap here"; const facets = [ { index: { byteStart: 0, byteEnd: 7 }, features: [{ $type: "app.bsky.richtext.facet#tag", tag: "a" }], }, { index: { byteStart: 3, byteEnd: 12 }, features: [{ $type: "app.bsky.richtext.facet#tag", tag: "b" }], }, ]; const tokens = tokenizeRichText({ text, facets }); assert.deepEqual( tokens.filter((token) => token.type === "facet").length, 1, ); });});
describe("isEmojiOnlyTokens", () => { it("is true for a single emoji-only text token", () => { assert.deepEqual(isEmojiOnlyTokens([{ type: "text", value: "๐" }]), true); });
it("concatenates adjacent text tokens before testing", () => { assert.deepEqual( isEmojiOnlyTokens([ { type: "text", value: "๐" }, { type: "text", value: "๐" }, ]), true, ); });
it("is false when the concatenated text is not emoji-only", () => { assert.deepEqual( isEmojiOnlyTokens([ { type: "text", value: "๐" }, { type: "text", value: "a" }, ]), false, ); });
it("is false when any token is a facet", () => { assert.deepEqual( isEmojiOnlyTokens([ { type: "text", value: "๐" }, { type: "facet", facet: { index: {} }, text: "๐" }, ]), false, ); });
it("is false when any token is an inline or block node", () => { assert.deepEqual( isEmojiOnlyTokens([{ type: "inline", node: { tag: "img" } }]), false, ); assert.deepEqual( isEmojiOnlyTokens([{ type: "block", node: { tag: "pre" } }]), false, ); });
it("is false for an empty array and non-arrays", () => { assert.deepEqual(isEmojiOnlyTokens([]), false); assert.deepEqual(isEmojiOnlyTokens(null), false); assert.deepEqual(isEmojiOnlyTokens(undefined), false); });});