import { test } from "node:test"; import assert from "node:assert/strict"; import { xTrendingMaxVideoBytes, coerceXTrendingConfig, isXTrendingChannel, parseXTrendingUrl, xTrendingLimits, } from "./config"; import { parseChannelConfig } from "../channelConfig"; test("a trending page URL is its id; anything else is refused", () => { assert.deepEqual(parseXTrendingUrl("https://x.com/i/trending/1900000000000000001"), { trendId: "1900000000000000001", url: "https://x.com/i/trending/1900000000000000001", }); // twitter.com, a trailing slash, a query and a fragment all name the same page. assert.equal( parseXTrendingUrl(" https://twitter.com/i/trending/1900000000000000001/?src=trend_click#top ")?.url, "https://x.com/i/trending/1900000000000000001", ); for (const bad of [ "", "x.com/i/trending/1", "https://x.com/i/trending/", "https://x.com/i/trending/abc", "https://x.com/i/trending/1/more", "https://x.com/example/status/1", "https://x.com/search?q=1", "https://x.com.example.test/i/trending/1", "ftp://x.com/i/trending/1", null, 42, ]) { assert.equal(parseXTrendingUrl(bad), null, String(bad)); } }); test("the marker is an object; its caps are kept only in range", () => { assert.deepEqual(coerceXTrendingConfig({}), {}); assert.deepEqual(coerceXTrendingConfig({ maxScrolls: 4, maxPosts: 30, other: 1 }), { maxScrolls: 4, maxPosts: 30 }); assert.deepEqual(coerceXTrendingConfig({ maxScrolls: 41, maxPosts: 0 }), {}); assert.deepEqual(coerceXTrendingConfig({ maxScrolls: 2.5, maxPosts: "9" }), {}); for (const bad of [undefined, null, true, "yes", 1, []]) assert.equal(coerceXTrendingConfig(bad), undefined); }); test("a library is a channel carrying the marker — the marker alone — and its config survives a parse", () => { const config = parseChannelConfig({ handling: "transcribe", sourceKind: "social", platform: "twitter", name: "Trending", xTrending: { maxPosts: 40 }, }); assert.ok(config); assert.deepEqual(config!.xTrending, { maxPosts: 40 }); assert.equal(isXTrendingChannel(config), true); assert.equal(isXTrendingChannel({ sourceKind: "social", platform: "twitter" } as never), false); // THE MARKER ALONE DECIDES: a library whose kind or platform was changed is // still a library (and still private). assert.equal(isXTrendingChannel({ sourceKind: "video", platform: "twitter", xTrending: {} } as never), true); assert.equal(isXTrendingChannel({ sourceKind: "social", platform: "bluesky", xTrending: {} } as never), true); assert.equal(isXTrendingChannel({ xTrending: { maxPosts: 3 } }), true); for (const none of [undefined, null, false, true, "yes", 1, []]) assert.equal(isXTrendingChannel({ xTrending: none }), false); assert.equal(isXTrendingChannel(null), false); }); test("a read's caps: the ask, else the channel's, else 20 scrolls and 200 posts; out of range is refused", () => { assert.deepEqual(xTrendingLimits(undefined), { ok: true, limits: { scrolls: 20, posts: 200 } }); assert.deepEqual(xTrendingLimits({ maxScrolls: 3 }), { ok: true, limits: { scrolls: 3, posts: 200 } }); assert.deepEqual(xTrendingLimits({ maxScrolls: 3 }, { scrolls: 20, posts: 200 }), { ok: true, limits: { scrolls: 20, posts: 200 }, }); assert.deepEqual(xTrendingLimits({}, { scrolls: 41 }), { ok: false, error: '"scrolls" is a whole number, 1–40' }); assert.deepEqual(xTrendingLimits({}, { posts: 401 }), { ok: false, error: '"posts" is a whole number, 1–400' }); assert.deepEqual(xTrendingLimits({}, { posts: 0 }), { ok: false, error: '"posts" is a whole number, 1–400' }); assert.deepEqual(xTrendingLimits({}, { scrolls: "8" }), { ok: false, error: '"scrolls" is a whole number, 1–40' }); }); test("the per-file media cap: 100 MB unless the library says otherwise, kept only in range", () => { assert.equal(xTrendingMaxVideoBytes(undefined), 100 * 1024 * 1024); assert.equal(xTrendingMaxVideoBytes({ maxVideoMb: 20 }), 20 * 1024 * 1024); assert.deepEqual(coerceXTrendingConfig({ maxVideoMb: 250 }), { maxVideoMb: 250 }); assert.deepEqual(coerceXTrendingConfig({ maxVideoMb: 0 }), {}); assert.deepEqual(coerceXTrendingConfig({ maxVideoMb: 2001 }), {}); });