commit 60cf82fe3888455fae5b5eec2c912440fcd2d18a
parent f3b337aab4180301802fd040374bfbe178f2cdc7
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Sat, 31 Oct 2026 23:59:42 -0400
umtool, common: a Twitch clip's archive link carries the record id v<digits> -- one siteVideoId(entry, platform) (common/lib/videoDirId.mjs: siteVideo first, v<n> only when the platform is Twitch, never because the id is digits), and one umtool archiveMomentUrl (report-to-video/site-links.mjs: the channel's platform from its config.json) behind the deck QR, the corner and rail QRs, the YouTube sources, lint's QR check, the review cite link and the moment lookup; links.ts momentHref takes the platform and reads twitch.tv/videos/<n> URLs
A clip with no citeUrl on a Twitch channel linked ?v=<channel>%2F<n>, a page
the archive does not serve (it keys v<n>). An X status id (digits) goes out
unchanged. The archive link now also honours a clip's siteChannel/siteVideo.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
15 files changed, 274 insertions(+), 30 deletions(-)
diff --git a/common/lib/report/draft/tasks/links.ts b/common/lib/report/draft/tasks/links.ts
@@ -12,19 +12,23 @@
// the run's corpus.
import { parseCitationHref } from "../../convertShared";
+import { siteVideoId } from "../../../videoDirId.mjs";
import { extractJsonObject } from "../../../parseStdoutJson";
import type { CorpusToolsLike } from "./types";
-export function momentHref(origin: string, slug: string, id: string, seconds: number): string {
- return `${origin}/?v=${encodeURIComponent(`${slug}/${id}`)}&t=${Math.max(0, Math.floor(seconds))}`;
+// `platform`, when the caller knows it, maps a Twitch VOD's directory id `<n>`
+// to the record id `v<n>` the archive serves (siteVideoId — never keyed on the
+// id being digits). An id from a tool result is already the archive's.
+export function momentHref(origin: string, slug: string, id: string, seconds: number, platform?: string | null): string {
+ return `${origin}/?v=${encodeURIComponent(`${slug}/${siteVideoId(id, platform)}`)}&t=${Math.max(0, Math.floor(seconds))}`;
}
export function postLinkHref(origin: string, slug: string, id: string): string {
return `${origin}/?v=${encodeURIComponent(`${slug}/${id}`)}&vm=post`;
}
-export const momentBaseOf = (origin: string, slug: string, id: string) =>
- `${origin}/?v=${encodeURIComponent(`${slug}/${id}`)}&t=`;
+export const momentBaseOf = (origin: string, slug: string, id: string, platform?: string | null) =>
+ `${origin}/?v=${encodeURIComponent(`${slug}/${siteVideoId(id, platform)}`)}&t=`;
// Which channel (slug) holds a recording: read off a moment link when there is
// one, else asked of the corpus (get_video_metadata over the run's channels).
@@ -108,9 +112,11 @@ export async function rewriteMomentBases(text: string, resolver: SlugResolver, o
const MD_LINK = /\]\((https?:\/\/[^)\s]+)\)/g;
-// A platform's watch URL: `?v=<id>` (no slash in it) or youtu.be/<id>, and
-// its `t` (`t=90`, `t=90s`).
-function platformVideo(href: string): { id: string; seconds: number } | null {
+// A platform's watch URL: `?v=<id>` (no slash in it) or youtu.be/<id>, or a
+// Twitch VOD's `twitch.tv/videos/<n>` (served by the archive as `v<n>`:
+// siteVideoId, on the platform the host names), and its `t` (`t=90`, `t=90s`,
+// `t=1h2m3s`).
+export function platformVideo(href: string): { id: string; seconds: number } | null {
let u: URL;
try {
u = new URL(href);
@@ -118,14 +124,17 @@ function platformVideo(href: string): { id: string; seconds: number } | null {
return null;
}
let id: string | null = null;
+ const twitch = /^\/videos\/(\d+)\/?$/.exec(u.pathname);
if (u.hostname === "youtu.be") id = u.pathname.slice(1).split("/")[0] || null;
+ else if (/(^|\.)twitch\.tv$/.test(u.hostname)) id = twitch ? siteVideoId(twitch[1]!, "twitch") : null;
else {
const v = u.searchParams.get("v");
if (v && !v.includes("/")) id = v;
}
if (!id) return null;
- const t = /^(\d+)s?$/.exec(u.searchParams.get("t") ?? "");
- return { id, seconds: t ? Number(t[1]) : 0 };
+ const t = /^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s?)?$/.exec(u.searchParams.get("t") ?? "");
+ const seconds = t ? Number(t[1] ?? 0) * 3600 + Number(t[2] ?? 0) * 60 + Number(t[3] ?? 0) : 0;
+ return { id, seconds };
}
// The backstop over finished markdown. Answers the text and how many links it
diff --git a/common/lib/report/draft/tasks/tasks.test.ts b/common/lib/report/draft/tasks/tasks.test.ts
@@ -8,7 +8,7 @@ import os from "node:os";
import path from "node:path";
import { ORIGIN, POSTS, cuesOf } from "../../../../testing/draft/corpus.mjs";
import { NARRATION_WRITING_RULES, rulesForModel, ruleForModel } from "../authoringRules";
-import { momentHref, rewriteLinks, rewriteMomentBases, slugResolver } from "./links";
+import { momentHref, platformVideo, rewriteLinks, rewriteMomentBases, slugResolver } from "./links";
import { chunksOf, lintClaim, locate, parseEnumerate, parseTranscripts, windowsOf } from "./sweep-pipeline";
import { pickCorpusBuild, siteOriginOf } from "./corpusSource";
import { checkQuotes, convertSweepText, dropFailedFindings } from "./quotes";
@@ -66,6 +66,22 @@ test("links: moment links are built on the origin; a platform URL and another or
assert.equal(asked, 2, "synth-other, then synth-kitchen — and cached for the second link");
});
+test("links: a Twitch VOD is linked as the archive's v<n> — by platform, never because the id is digits", async () => {
+ // A directory id with the platform known: the record id the archive serves.
+ assert.equal(momentHref(ORIGIN, "synth-twitch", "2468013579", 61.2, "twitch"), `${ORIGIN}/?v=synth-twitch%2Fv2468013579&t=61`);
+ assert.equal(momentHref(ORIGIN, "synth-twitch", "v2468013579", 61, "twitch"), `${ORIGIN}/?v=synth-twitch%2Fv2468013579&t=61`);
+ // An X status id is digits too: never a v.
+ assert.equal(momentHref(ORIGIN, "synth-social", "1850000000000000000", 4, "x"), `${ORIGIN}/?v=synth-social%2F1850000000000000000&t=4`);
+ assert.equal(momentHref(ORIGIN, "synth-social", "1850000000000000000", 4), `${ORIGIN}/?v=synth-social%2F1850000000000000000&t=4`);
+ // A twitch.tv/videos/<n> URL is the platform saying so; its t is h/m/s.
+ assert.deepEqual(platformVideo("https://www.twitch.tv/videos/2468013579?t=1h2m3s"), { id: "v2468013579", seconds: 3723 });
+ assert.equal(platformVideo("https://www.twitch.tv/somebody"), null);
+ const r = slugResolver(stubCorpus({ v2468013579: "synth-twitch" }), ["synth-twitch"]);
+ const out = await rewriteLinks("- [Stream @ 1:02:03](https://www.twitch.tv/videos/2468013579?t=1h2m3s)", r, ORIGIN);
+ assert.equal(out.fixed, 1);
+ assert.ok(out.md.includes(`(${ORIGIN}/?v=synth-twitch%2Fv2468013579&t=3723)`), out.md);
+});
+
test("links: get_transcripts' moment_base becomes the origin's, by the moment_base's own slug or by asking", async () => {
const text = [
"## Pie stream\n- video_id: pie-stream-02 | channel: Synth Kitchen\n- source: https://video.example/watch?v=pie-stream-02\n- moment_base: https://video.example/watch?v=pie-stream-02&t=\n[0:10|10] hello",
diff --git a/common/lib/videoDirId.mjs b/common/lib/videoDirId.mjs
@@ -50,6 +50,26 @@ export function twitchVodRecordId(dirId) {
}
/**
+ * The id the ARCHIVE serves a manifest entry's recording under — what a link
+ * to its page (`?v=<channel>/<id>`) must carry. An explicit `siteVideo` wins
+ * (a Rumble clip: its local dir is the URL slug, the archive keys the embed
+ * id). Else `video`, except that a TWITCH VOD's directory id `<n>` is served
+ * as its record id `v<n>` (twitchVodRecordId) — keyed on the PLATFORM, never
+ * on the id being all digits: an X status id is digits too, and is served as
+ * it is.
+ * @param {string | { video?: unknown, siteVideo?: unknown } | null | undefined} entry
+ * an entry (`{video, siteVideo?}`) or a bare id
+ * @param {string | null | undefined} platform the recording's (or its channel's) platform
+ * @returns {string}
+ */
+export function siteVideoId(entry, platform) {
+ const e = typeof entry === "string" ? { video: entry } : (entry ?? {});
+ if (typeof e.siteVideo === "string" && e.siteVideo) return e.siteVideo;
+ const id = e.video == null ? "" : String(e.video);
+ return platform === "twitch" ? (twitchVodRecordId(id) ?? id) : id;
+}
+
+/**
* The platform a channel's config names: `platform`, else its `url`'s host
* (ytdlp/channelArgs.ts channelPlatform's rule). Null for none.
* @param {{ platform?: unknown, url?: unknown } | null | undefined} config
diff --git a/common/lib/videoDirId.test.ts b/common/lib/videoDirId.test.ts
@@ -14,6 +14,7 @@ import {
channelConfigPlatform,
extractVideoId,
pickVideoDirId,
+ siteVideoId,
twitchVodDirId,
twitchVodRecordId,
} from "./videoId";
@@ -36,6 +37,16 @@ test("twitchVodDirId drops only a v before digits; twitchVodRecordId is its reve
assert.equal(twitchVodRecordId(twitchVodDirId(META.id)!), META.id);
});
+test("siteVideoId: the archive's id — v<n> for a Twitch directory id only, siteVideo first, X digits untouched", () => {
+ assert.equal(siteVideoId({ video: N }, "twitch"), `v${N}`);
+ assert.equal(siteVideoId(`v${N}`, "twitch"), `v${N}`);
+ assert.equal(siteVideoId({ video: N }, "x"), N);
+ assert.equal(siteVideoId({ video: N }, null), N);
+ assert.equal(siteVideoId({ video: N }, undefined), N);
+ assert.equal(siteVideoId({ video: "a-url-slug", siteVideo: "v2embedid" }, "rumble"), "v2embedid");
+ assert.equal(siteVideoId(null, "twitch"), "");
+});
+
test("channelConfigPlatform: the platform key, else the url's host", () => {
assert.equal(channelConfigPlatform({ platform: "twitch" }), "twitch");
assert.equal(channelConfigPlatform({ url: "https://www.twitch.tv/example" }), "twitch");
diff --git a/common/lib/videoId.ts b/common/lib/videoId.ts
@@ -20,6 +20,7 @@ import { isJwPlayerHost, isWaybackHost, jwPlayerMediaId, parseWaybackUrl } from
export {
channelConfigPlatform,
pickVideoDirId,
+ siteVideoId,
twitchVodDirId,
twitchVodRecordId,
} from "./videoDirId.mjs";
diff --git a/umtool/lib/projects/report.mjs b/umtool/lib/projects/report.mjs
@@ -20,6 +20,7 @@ import { resolveVideoDirSync } from "yt-dlp-transcript-common/lib/videoDir-serve
import { deliverablesState, finishCommand, outDirState, leftoversOf } from "../report/storage.mjs";
import { CHANNELS_DIR, inside, resolveInRoots } from "../paths.mjs";
import { channelName, cleanTitle } from "umtool-report-to-video/attribution";
+import { archiveMomentUrl } from "umtool-report-to-video/site-links";
import { deckOn, resolveDeck, teaserTitle } from "umtool-report-to-video/deck";
import { originQrsOf } from "umtool-report-to-video/lint";
import { DRAFTS_REVIEW_FILE, NARRATION_REVIEW_FILE } from "umtool-report-to-video/drafts-review";
@@ -188,7 +189,7 @@ export const channelFor = (m, e) => e.channel ?? m?.provenance?.channelSlug ?? n
* is not the id the site serves), else the viewer moment URL is derived.
*/
export const derivedCiteUrl = (m, e) =>
- `${m?.provenance?.siteOrigin ?? ""}/?v=${encodeURIComponent(`${channelFor(m, e)}/${e.video}`)}&t=${Math.floor(e.start ?? 0)}`;
+ archiveMomentUrl({ ...e, channel: channelFor(m, e) }, m?.provenance, e.start ?? 0, { origin: m?.provenance?.siteOrigin ?? "" });
export const citeUrlFor = (m, e) => e.citeUrl ?? derivedCiteUrl(m, e);
/**
diff --git a/umtool/lib/report/moments.mjs b/umtool/lib/report/moments.mjs
@@ -23,6 +23,7 @@ import { readdir, readFile, stat } from "node:fs/promises";
import path from "node:path";
import { DEFAULT_VARIANT } from "umtool-report-to-video/build-video";
import { channelFor } from "../projects/report.mjs";
+import { archiveMomentUrl } from "umtool-report-to-video/site-links";
const TAKE_RE = /^[a-z0-9][a-z0-9-]{0,63}$/;
const APPROX_TOLERANCE = 0.25;
@@ -132,7 +133,7 @@ export function resolveMoment({ schedule, manifest, variant = null, t, duration
out.video = String(e.video);
const origin = manifest?.provenance?.siteOrigin;
if (origin && channel) {
- out.url = `${origin}/?v=${encodeURIComponent(`${channel}/${e.video}`)}&t=${Math.floor(out.sourceT)}`;
+ out.url = archiveMomentUrl({ ...e, channel }, manifest?.provenance, out.sourceT, { origin });
}
}
}
diff --git a/umtool/report-to-video/build-video.mjs b/umtool/report-to-video/build-video.mjs
@@ -135,6 +135,7 @@ import {
cardWidth, contentWidth, reservedFooterHeight,
} from "./render-cards.mjs";
import { DEFAULT_CHANNELS_DIR, createCueSource, siteOriginFromManifest } from "./cues.mjs";
+import { archiveMomentUrl } from "./site-links.mjs";
import { cutArgs } from "yt-dlp-transcript-common/lib/evidenceClip.mjs";
// Where a clip's media is ALREADY on disk -- the build's raw cache, the
// editor's corpus windows, the saved source -- asked before anything fetches.
@@ -2271,11 +2272,7 @@ async function qrForEntry(entry, provenance, render, outDir) {
// A mirror's LOCAL slug is not the id the site serves, and a clip taken from a
// copy whose archived transcript is broken should point at the copy that reads —
// so an explicit per-clip citeUrl always wins over the derived one.
- const url =
- entry.citeUrl ??
- `${provenance.siteOrigin}/?v=${encodeURIComponent(
- `${entry.channel ?? provenance.channelSlug}/${entry.video}`,
- )}&t=${Math.floor(entry.start)}`;
+ const url = entry.citeUrl ?? archiveMomentUrl(entry, provenance, entry.start);
const png = path.join(outDir, "qr", `${entry.id}.png`);
await execFileP(QRENCODE, [
"-o", png,
diff --git a/umtool/report-to-video/deck.mjs b/umtool/report-to-video/deck.mjs
@@ -15,6 +15,7 @@
// No ffmpeg, no fs, no network. If a function here needs one, it belongs in
// build-video.mjs or compose-chrome.mjs instead.
import { createHash } from "node:crypto";
+import { archiveMomentUrl } from "./site-links.mjs";
import { playEdges, soundEdges as soundEdgesOf, soundOnSegment } from "./mute.mjs";
import { gappedLength, offsetIn, recordGaps, renderGaps } from "./gaps.mjs";
import { COMPOSITION_PARAMS } from "yt-dlp-transcript-common/lib/report/fxParams.mjs";
@@ -980,10 +981,7 @@ export function deckQrUrl(entry, provenance = {}, { links = DECK_DEFAULTS.qr.lin
// A clip from a file beside the manifest (`src`) has no page anywhere.
if (entry.src != null) return null;
const original = links === "original" ? originalUrlAt(meta?.webpageUrl, entry.start) : null;
- return original ??
- `${provenance.siteOrigin}/?v=${encodeURIComponent(
- `${entry.channel ?? provenance.channelSlug}/${entry.video}`,
- )}&t=${Math.floor(entry.start)}`;
+ return original ?? archiveMomentUrl(entry, provenance, entry.start);
}
if (entry.type === "image") return entry.citeUrl ?? null;
return null;
diff --git a/umtool/report-to-video/lint.mjs b/umtool/report-to-video/lint.mjs
@@ -95,6 +95,7 @@ import { validateTransitionStyle } from "./transition.mjs";
import { validateLook } from "./look.mjs";
import { roomWindowWarnings } from "./room.mjs";
import { qrUrlFor } from "./render-cards.mjs";
+import { archiveMomentUrl } from "./site-links.mjs";
import { cardNarrationSeconds, hasNarration, narrationsOf, validateNarration } from "./narration.mjs";
import { validatePodcast } from "./build-podcast.mjs";
import { draftsReviewFromNarration, draftsReviewProblems, readDraftsReview } from "./drafts-review.mjs";
@@ -199,8 +200,7 @@ export function qrsOf(manifest, { only = null, metaOf = null } = {}) {
let url = null;
if (e.type === "clip") {
if (e.src != null && !e.citeUrl) continue;
- url = e.citeUrl ??
- `${provenance.siteOrigin}/?v=${encodeURIComponent(`${e.channel ?? provenance.channelSlug}/${e.video}`)}&t=${Math.floor(e.start)}`;
+ url = e.citeUrl ?? archiveMomentUrl(e, provenance, e.start);
} else if (e.type === "image" && e.citeUrl) {
url = e.citeUrl;
}
diff --git a/umtool/report-to-video/package.json b/umtool/report-to-video/package.json
@@ -18,6 +18,7 @@
},
"exports": {
"./attribution": "./attribution.mjs",
+ "./site-links": "./site-links.mjs",
"./brand": "./brand.mjs",
"./brand-ids": "./brand-ids.mjs",
"./build-video": "./build-video.mjs",
diff --git a/umtool/report-to-video/render-cards.mjs b/umtool/report-to-video/render-cards.mjs
@@ -39,6 +39,7 @@ import { BRAND_CARD_STYLES, renderBrandCard } from "./brand-cards.mjs";
import { FIRA_SANS, textWidth } from "./svg-faces.mjs";
import { deckOn, resolveDeck, subtitleBoxes } from "./deck.mjs";
import { ensureWriteDir } from "../lib/report/storage.mjs";
+import { archiveMomentUrl } from "./site-links.mjs";
const execFileP = promisify(execFile);
@@ -738,12 +739,7 @@ export function qrUrlFor(entry, provenance) {
if (entry.type === "clip" && !momentless) {
// A mirror's LOCAL slug is not the id the site serves, so an explicit
// per-clip citeUrl always wins over the derived one.
- return (
- entry.citeUrl ??
- `${provenance.siteOrigin}/?v=${encodeURIComponent(
- `${entry.channel ?? provenance.channelSlug}/${entry.video}`,
- )}&t=${Math.floor(entry.cite ?? entry.start)}`
- );
+ return entry.citeUrl ?? archiveMomentUrl(entry, provenance, entry.cite ?? entry.start);
}
// A card is not a moment, so it gets the search rather than a timestamp.
//
diff --git a/umtool/report-to-video/site-links.mjs b/umtool/report-to-video/site-links.mjs
@@ -0,0 +1,98 @@
+// site-links — the ONE place a manifest entry's link to its moment on the
+// archive is built (`<origin>/?v=<channel>%2F<id>&t=<s>`): the corner QR, the
+// deck's QR, the YouTube description's sources, lint's QR check, the review
+// page's cite link, the moment lookup.
+//
+// The id is the one the ARCHIVE serves (siteVideoId, common/lib/videoDirId.mjs):
+// an entry's `siteVideo` when it pins one (a Rumble clip), else its `video` —
+// but a Twitch VOD's `video` is its directory id `<n>` while the archive keys
+// the record id `v<n>`. That mapping is decided by the recording's PLATFORM,
+// never by the id being all digits (an X status id is digits too):
+//
+// the entry's own `platform`, else its channel's (config.json `platform`,
+// else the host of its `url` — channelConfigPlatform, the rule every
+// resolveVideoDirId caller uses), read once per channel from CHANNELS_DIR.
+//
+// With no local channel config (a machine with no corpus) the platform is
+// unknown and the id goes out as written. The channel is `siteChannel`, else
+// `channel`, else the provenance's channelSlug.
+
+import { readFileSync } from "node:fs";
+import path from "node:path";
+import { channelConfigPlatform, siteVideoId } from "yt-dlp-transcript-common/lib/videoDirId.mjs";
+import { CHANNELS_DIR } from "../lib/paths.mjs";
+
+/** @type {Map<string, string | null>} */
+const PLATFORMS = new Map();
+
+/**
+ * A channel's platform from its config.json, cached per (dir, slug); null when
+ * there is no readable config.
+ * @param {string | null | undefined} slug
+ * @param {string} [channelsDir]
+ * @returns {string | null}
+ */
+export function channelPlatform(slug, channelsDir = CHANNELS_DIR) {
+ if (!slug) return null;
+ const key = `${channelsDir}\u0000${slug}`;
+ if (PLATFORMS.has(key)) return /** @type {string | null} */ (PLATFORMS.get(key));
+ let platform = null;
+ try {
+ platform = channelConfigPlatform(JSON.parse(readFileSync(path.join(/* turbopackIgnore: true */ channelsDir, slug, "config.json"), "utf8")));
+ } catch {
+ platform = null;
+ }
+ PLATFORMS.set(key, platform);
+ return platform;
+}
+
+/** Forget the cached platforms (tests). */
+export function clearChannelPlatforms() {
+ PLATFORMS.clear();
+}
+
+/**
+ * The recording's platform: the entry's own `platform`, else its LOCAL
+ * channel's (`channel`, else the provenance's slug — the directory the cues
+ * come from).
+ * @param {any} entry
+ * @param {any} [provenance]
+ * @param {{ channelsDir?: string }} [opts]
+ * @returns {string | null}
+ */
+export function entryPlatform(entry, provenance = {}, { channelsDir } = {}) {
+ if (typeof entry?.platform === "string" && entry.platform) return entry.platform;
+ return channelPlatform(entry?.channel ?? provenance?.channelSlug ?? null, channelsDir);
+}
+
+/**
+ * The `<channel>/<id>` the archive serves an entry's recording under.
+ * @param {any} entry
+ * @param {any} [provenance]
+ * @param {{ channelsDir?: string, platform?: string | null }} [opts]
+ * @returns {{ channel: string | undefined, video: string }}
+ */
+export function siteRefOf(entry, provenance = {}, opts = {}) {
+ // No channel stays `undefined`, as the builders this replaced wrote it (a
+ // schedule pinned byte-for-byte reads "undefined/…").
+ const channel = entry?.siteChannel ?? entry?.channel ?? provenance?.channelSlug;
+ const platform = opts.platform !== undefined ? opts.platform : entryPlatform(entry, provenance, opts);
+ return { channel, video: siteVideoId(entry, platform) };
+}
+
+/**
+ * An entry's moment on the archive at `seconds` (floored):
+ * `<origin>/?v=<channel>%2F<id>&t=<s>`. `origin` is the provenance's
+ * `siteOrigin` unless given; it is used as it is (a missing origin gives the
+ * "undefined/?v=…" the old builders gave — callers that care check first).
+ * @param {any} entry
+ * @param {any} provenance
+ * @param {number} seconds
+ * @param {{ origin?: string | null, channelsDir?: string, platform?: string | null }} [opts]
+ */
+export function archiveMomentUrl(entry, provenance, seconds, opts = {}) {
+ const origin = opts.origin !== undefined ? opts.origin : provenance?.siteOrigin;
+ const { channel, video } = siteRefOf(entry, provenance, opts);
+ const at = Number(seconds);
+ return `${origin}/?v=${encodeURIComponent(`${channel}/${video}`)}&t=${Math.floor(Number.isFinite(at) ? at : 0)}`;
+}
diff --git a/umtool/report-to-video/site-links.test.mjs b/umtool/report-to-video/site-links.test.mjs
@@ -0,0 +1,93 @@
+// The archive link an entry's QR, deck QR, YouTube source line, lint check,
+// review cite link and moment lookup carry (site-links.mjs): a Twitch VOD's
+// directory id `<n>` goes out as the record id `v<n>` the archive serves —
+// decided by the channel's PLATFORM, never by the id being digits (an X status
+// id is digits too and goes out as it is). A temp CHANNELS_DIR with synthetic
+// channels; never the corpus. Run with: pnpm test:scripts
+import assert from "node:assert/strict";
+import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import path from "node:path";
+import test from "node:test";
+
+const CHANNELS = mkdtempSync(path.join(tmpdir(), "site-links-"));
+for (const [slug, config] of [
+ ["twitchchan", { platform: "twitch" }],
+ ["twitchbyurl", { url: "https://www.twitch.tv/somebody" }],
+ ["xchan", { platform: "x" }],
+ ["ytchan", { url: "https://www.youtube.com/@somebody" }],
+]) {
+ mkdirSync(path.join(CHANNELS, slug), { recursive: true });
+ writeFileSync(path.join(CHANNELS, slug, "config.json"), JSON.stringify(config));
+}
+// CHANNELS_DIR is read when umtool/lib/paths.mjs loads: set it first.
+process.env.CHANNELS_DIR = CHANNELS;
+const { archiveMomentUrl, channelPlatform, siteRefOf } = await import("./site-links.mjs");
+const { deckQrUrl } = await import("./deck.mjs");
+const { momentLinkOf } = await import("./youtube.mjs");
+const { qrUrlFor } = await import("./render-cards.mjs");
+const { qrsOf } = await import("./lint.mjs");
+const { derivedCiteUrl, citeUrlFor } = await import("../lib/projects/report.mjs");
+const { resolveMoment } = await import("../lib/report/moments.mjs");
+const { siteVideoId } = await import("yt-dlp-transcript-common/lib/videoDirId.mjs");
+
+const ORIGIN = "https://example.pages.dev";
+const N = "2468013579";
+const XID = "1850000000000000000";
+const twitchClip = { type: "clip", id: "t01", channel: "twitchchan", video: N, start: 125.4, end: 140, cite: 130 };
+const xClip = { type: "clip", id: "x01", channel: "xchan", video: XID, start: 3.2, end: 9 };
+const prov = { siteOrigin: ORIGIN, channelSlug: "ytchan" };
+const twitchUrl = (t) => `${ORIGIN}/?v=twitchchan%2Fv${N}&t=${t}`;
+const xUrl = (t) => `${ORIGIN}/?v=xchan%2F${XID}&t=${t}`;
+
+test("siteVideoId: v<n> only for Twitch; siteVideo wins; X digits and every other platform untouched", () => {
+ assert.equal(siteVideoId({ video: N }, "twitch"), `v${N}`);
+ assert.equal(siteVideoId(N, "twitch"), `v${N}`);
+ assert.equal(siteVideoId({ video: `v${N}` }, "twitch"), `v${N}`, "already the record id");
+ assert.equal(siteVideoId({ video: XID }, "x"), XID);
+ assert.equal(siteVideoId({ video: XID }, null), XID, "unknown platform: as written, never guessed from digits");
+ assert.equal(siteVideoId({ video: "a-url-slug", siteVideo: "v2embed" }, "rumble"), "v2embed");
+ assert.equal(siteVideoId({ video: "dQw4w9WgXcQ" }, "youtube"), "dQw4w9WgXcQ");
+});
+
+test("channelPlatform reads config.json (platform, else the url's host); none for an unknown channel", () => {
+ assert.equal(channelPlatform("twitchchan"), "twitch");
+ assert.equal(channelPlatform("twitchbyurl"), "twitch");
+ assert.equal(channelPlatform("xchan"), "x");
+ assert.equal(channelPlatform("ytchan"), "youtube");
+ assert.equal(channelPlatform("nochan"), null);
+ assert.deepEqual(siteRefOf({ video: N, platform: "twitch", channel: "nochan" }), { channel: "nochan", video: `v${N}` }, "an entry's own platform wins");
+ assert.deepEqual(siteRefOf({ video: N, channel: "nochan" }), { channel: "nochan", video: N }, "no config: as written");
+ assert.deepEqual(siteRefOf({ video: "slug", siteVideo: "v9", siteChannel: "site-chan", channel: "local" }), { channel: "site-chan", video: "v9" });
+ assert.equal(archiveMomentUrl({ video: N }, { siteOrigin: ORIGIN, channelSlug: "twitchbyurl" }, 7.9), `${ORIGIN}/?v=twitchbyurl%2Fv${N}&t=7`, "the provenance's channel decides");
+});
+
+test("a Twitch clip with no citeUrl: every builder links v<digits>; an X clip's digits get no v", () => {
+ // the deck's QR (a clip's start)
+ assert.equal(deckQrUrl(twitchClip, prov), twitchUrl(125));
+ assert.equal(deckQrUrl(xClip, prov), xUrl(3));
+ // the YouTube description's moment link (the cited second)
+ assert.equal(momentLinkOf(twitchClip, prov), twitchUrl(130));
+ assert.equal(momentLinkOf(xClip, prov), xUrl(3));
+ // the rail / corner QR (render-cards)
+ assert.equal(qrUrlFor(twitchClip, prov), twitchUrl(130));
+ assert.equal(qrUrlFor(xClip, prov), xUrl(3));
+ // the review page's cite link
+ const m = { provenance: prov, timeline: [twitchClip, xClip] };
+ assert.equal(derivedCiteUrl(m, twitchClip), twitchUrl(125));
+ assert.equal(citeUrlFor(m, xClip), xUrl(3));
+ // a citeUrl still wins
+ assert.equal(deckQrUrl({ ...twitchClip, citeUrl: "https://elsewhere/x" }, prov), "https://elsewhere/x");
+});
+
+test("lint measures the same link the corner QR draws", () => {
+ const qrs = qrsOf({ provenance: prov, render: {}, timeline: [twitchClip, xClip] });
+ assert.deepEqual(qrs.map((q) => q.url), [twitchUrl(125), xUrl(3)]);
+});
+
+test("a moment on the cut resolves to the archive's v<digits> page", () => {
+ const schedule = { segments: [{ id: "t01", start: 0, duration: 14.6 }], total: 14.6, transition: 0 };
+ const got = resolveMoment({ schedule, manifest: { provenance: prov, timeline: [twitchClip] }, t: 2 });
+ assert.equal(got.video, N, "the local id stays the local id");
+ assert.equal(got.url, twitchUrl(127));
+});
diff --git a/umtool/report-to-video/youtube.mjs b/umtool/report-to-video/youtube.mjs
@@ -24,6 +24,8 @@
//
// PURE: no fs. build-video.mjs writes the file.
+import { archiveMomentUrl } from "./site-links.mjs";
+
/** YouTube's rules for a description's chapters. */
export const YOUTUBE_CHAPTERS = Object.freeze({ min: 3, minSeconds: 10 });
@@ -98,7 +100,7 @@ export function momentLinkOf(entry, provenance = {}) {
const channel = entry.channel ?? provenance?.channelSlug;
if (!origin || !channel || !entry.video) return null;
const at = Number.isFinite(Number(entry.cite)) ? Number(entry.cite) : Number(entry.start);
- return `${origin}/?v=${encodeURIComponent(`${channel}/${entry.video}`)}&t=${Math.floor(Number.isFinite(at) ? at : 0)}`;
+ return archiveMomentUrl(entry, provenance, at, { origin });
}
/** The cut's moment links, in cut order, each once. */