Docs/Skills and code/tests/

tests/transcript-sampling.test.js

Code45 lines2.4 KBtests/

import test from "node:test";
import assert from "node:assert/strict";
import { channelFolderName, rankAndBucket, seededRandom } from "../src/transcript-sampling.js";

const NOW = Date.parse("2026-10-19T00:00:00Z");
const daysAgo = d => new Date(NOW - d * 86400000).toISOString();
const makeVideos = n => Array.from({ length: n }, (_, i) => ({
  video_id: `v${i}`, view_count: (i + 1) * 1000, published_at_normalized: daysAgo(100)
}));

test("best is highest views/day, worst is lowest, median comes from the middle band", () => {
  const { best, worst, median, stats } = rankAndBucket(makeVideos(20), { now: NOW, rng: seededRandom(1) });
  assert.equal(stats.eligible, 20);
  assert.deepEqual(best.slice(0, 2).map(v => v.video_id), ["v19", "v18"]);
  assert.deepEqual(worst.slice(0, 2).map(v => v.video_id), ["v0", "v1"]);
  assert.ok(median.slice(0, 2).every(v => v.rank >= 7 && v.rank <= 14), "median picks sit near the middle");
  assert.equal(best[0].rank, 1);
  assert.equal(best[0].percentile_from_top, 0);
});

test("videos too young to judge are excluded, and views/day floors age so new hits aren't over-ranked", () => {
  const videos = [...makeVideos(12), { video_id: "new", view_count: 1, published_at_normalized: daysAgo(2) }];
  const { best, stats } = rankAndBucket(videos, { now: NOW });
  assert.ok(!best.some(v => v.video_id === "new"));
  assert.equal(stats.excluded, 1);
  const hit = [...makeVideos(12), { video_id: "hit", view_count: 30000, published_at_normalized: daysAgo(15) }];
  assert.equal(rankAndBucket(hit, { now: NOW }).best.find(v => v.video_id === "hit").views_per_day, 1000);
});

test("videos without a view count or date are ignored", () => {
  const videos = [...makeVideos(10), { video_id: "nodate", view_count: 5 }, { video_id: "noviews", published_at_normalized: daysAgo(90) }];
  assert.equal(rankAndBucket(videos, { now: NOW }).stats.eligible, 10);
});

test("median selection is reproducible for a seed", () => {
  const ids = seed => rankAndBucket(makeVideos(30), { now: NOW, rng: seededRandom(seed) }).median.slice(0, 2).map(v => v.video_id);
  assert.deepEqual(ids(7), ids(7));
});

test("channelFolderName is Windows safe and keeps non-Latin names", () => {
  assert.equal(channelFolderName('Who? <am> "I"/x:y*'), "Who am Ixy");
  assert.equal(channelFolderName("हिंदी चैनल. "), "हिंदी चैनल");
  assert.equal(channelFolderName("///"), "channel");
});

Shown as it is in the repository at build time (2026-11-03); the home directory appears as <home>. If this page and the file differ, the file is right. Back to the catalogue.