Docs/Skills and code/tests/

tests/channel-queues.test.js

Code185 lines13.1 KBtests/

import test from "node:test";
import assert from "node:assert/strict";
import { scoreChannelQueues, QUEUES } from "../src/analysis/channel-queues.js";

const now = new Date("2026-10-12T00:00:00.000Z");
const qualify = (classification = "core", confidence = 0.9, tags = ["technology education", "systems design"]) => ({
  classification, confidence, relevance_score: classification === "core" ? 95 : classification === "adjacent" ? 65 : 10,
  focus_specificity: 82, focus_summary: "Technology education and close readings of systems", topic_tags: tags,
  formats: ["video essays"], supporting_evidence: ["Description and recurring titles support this focus"],
  counter_evidence: [], uncertainty_flags: [], source: "llm", prompt_version: "ecosystem-qualification-v1.0"
});
const mkVideo = (id, daysAgo, views, precision = "exact_day") => ({
  video_id: id, title: `Systems analysis ${id}`, view_count: views,
  published_at_normalized: new Date(now.getTime() - daysAgo * 86400000).toISOString().slice(0, 10),
  published_at_precision: precision
});
function ratingFor(result, url, key) { return result.ratings.find(row => row.channel_url === url && row.queue.queue_key === key)?.queue; }

test("always emits five queue-specific ratings for every channel", () => {
  const result = scoreChannelQueues({ channels: [{ channel_url: "u", name: "X", videos: [] }], now });
  assert.equal(result.queues.length, 5);
  assert.deepEqual(result.queues.map(q => q.key), ["learn_from", "breaking_out", "durable_exemplars", "unexplored_niches", "reference_inspiration"]);
  assert.equal(result.ratings.length, 5);
});

test("unknown/low-confidence channels are qualification-pending rather than ranked", () => {
  const result = scoreChannelQueues({ channels: [{ channel_url: "u", name: "Unclear", ecosystem_classification: "core", videos: Array.from({ length: 10 }, (_, i) => mkVideo(String(i), 100 + i, 10000)) }], now });
  for (const { queue } of result.ratings.filter(row => row.queue.queue_key !== "reference_inspiration")) {
    assert.equal(queue.score, null);
    assert.equal(queue.status, "needs_qualification");
    assert.equal(queue.rank, null);
    assert.ok(queue.uncertainty.includes("qualification_confidence_below_threshold"));
  }
});

test("irrelevant channels cannot enter the ecosystem learning or growth rankings", () => {
  const videos = Array.from({ length: 20 }, (_, i) => mkVideo(String(i), 40 + i * 4, 100000));
  const result = scoreChannelQueues({ channels: [{ channel_url: "u", name: "Unrelated", qualification_assessment: qualify("irrelevant", 0.99), videos, subscriber_count: 1000, joined_date: "2022-01-01", video_count: 50 }], now });
  for (const key of ["learn_from", "breaking_out", "durable_exemplars", "unexplored_niches"]) {
    const row = ratingFor(result, "u", key);
    assert.equal(row.score, null);
    assert.equal(row.eligibility, "ineligible_irrelevant");
  }
});

test("core channels rank before adjacent channels regardless of raw score", () => {
  const videos = Array.from({ length: 10 }, (_, i) => mkVideo(String(i), 100 + i * 5, 20000));
  const result = scoreChannelQueues({ channels: [
    { channel_url: "adjacent", name: "Adjacent", qualification_assessment: qualify("adjacent", 0.95), videos, subscriber_count: 1000, joined_date: "2021-01-01", video_count: 80, priority: { specialist_score: 70, breakthrough_score: 70 } },
    { channel_url: "core", name: "Core", qualification_assessment: qualify("core", 0.8), videos, subscriber_count: 1000, joined_date: "2021-01-01", video_count: 80, priority: { specialist_score: 50, breakthrough_score: 50 } }
  ], now });
  const core = ratingFor(result, "core", "breaking_out");
  const adjacent = ratingFor(result, "adjacent", "breaking_out");
  assert.ok(core.score != null && adjacent.score != null);
  assert.ok(core.rank < adjacent.rank);
  assert.equal(core.eligibility, "eligible_core");
  assert.equal(adjacent.eligibility, "eligible_adjacent");
});

test("growth scoring combines absolute reach and repeatability, smoothing tiny denominators", () => {
  const tinyVideos = Array.from({ length: 51 }, (_, i) => mkVideo(`tiny${i}`, 60 + i * 2, i < 45 ? 46 : 279));
  const strongVideos = Array.from({ length: 20 }, (_, i) => mkVideo(`strong${i}`, 40 + i * 5, i < 16 ? 160000 : 265000));
  const result = scoreChannelQueues({ channels: [
    { channel_url: "tiny", name: "Tiny", qualification_assessment: qualify("core", 0.9), videos: tinyVideos, subscriber_count: 27, subscriber_count_precision: "exact", joined_date: "2026-08-01", video_count: 51 },
    { channel_url: "strong", name: "Strong Systems", qualification_assessment: qualify("core", 0.9), videos: strongVideos, subscriber_count: 24000, subscriber_count_precision: "abbreviated", joined_date: "2025-01-01", video_count: 50 }
  ], now });
  const tiny = ratingFor(result, "tiny", "breaking_out");
  const strong = ratingFor(result, "strong", "breaking_out");
  assert.ok(tiny.score != null && strong.score != null);
  assert.ok(tiny.score < strong.score, `tiny=${tiny.score}, strong=${strong.score}`);
  assert.equal(tiny.evidence.snapshot_dependent, false);
  assert.ok(tiny.reason_codes.includes("smoothed_small_channel_denominator"));
});

test("snapshot rows do not affect static growth scoring", () => {
  const videos = Array.from({ length: 8 }, (_, i) => ({ ...mkVideo(String(i), 60 + i * 10, 5000 + i * 100), metric_snapshots: [{ view_count: 999999999 }] }));
  const channel = { channel_url: "u", name: "Static Systems", qualification_assessment: qualify(), videos, subscriber_count: 4000, joined_date: "2024-01-01", video_count: 30 };
  const a = ratingFor(scoreChannelQueues({ channels: [channel], now }), "u", "breaking_out");
  const b = ratingFor(scoreChannelQueues({ channels: [{ ...channel, videos: videos.map(({ metric_snapshots, ...v }) => v) }], now }), "u", "breaking_out");
  assert.equal(a.score, b.score);
  assert.equal(a.evidence.snapshot_dependent, false);
});

test("explicit reference seed is preserved without automatically receiving a perfect score", () => {
  const url = "https://www.youtube.com/@SystemsExplained";
  const result = scoreChannelQueues({ channels: [{ channel_url: url, name: "Systems Explained", handle: "@SystemsExplained", qualification_assessment: qualify(), description: "Technology education", videos: [] }],
    referenceSeeds: [{ handle: "SystemsExplained", name: "Systems Explained" }], now });
  const ref = ratingFor(result, url, "reference_inspiration");
  assert.equal(ref.status, "explicit_reference_provisional");
  assert.equal(ref.eligibility, "explicit_reference");
  assert.ok(ref.score < 100);
  assert.ok(ref.reason_codes.includes("seed_membership_separate_from_score"));
});

test("durability cannot be established by age and scale alone without video evidence", () => {
  const result = scoreChannelQueues({ channels: [{ channel_url: "u", name: "Large Old Systems", qualification_assessment: qualify(), joined_date: "2000-01-01", total_view_count: 500000000, video_count: 10000, videos: [] }], now });
  const durable = ratingFor(result, "u", "durable_exemplars");
  assert.equal(durable.score, null);
  assert.equal(durable.status, "insufficient_evidence");
});

test("missing evidence remains null and does not get a fake score", () => {
  const result = scoreChannelQueues({ channels: [{ channel_url: "u", name: "Empty", videos: [] }], now });
  for (const { queue } of result.ratings) assert.equal(queue.score, null);
  assert.equal(QUEUES.length, 5);
});


test("inferred reference candidates must be qualified and irrelevant channels cannot rank", () => {
  const richVideos = Array.from({ length: 20 }, (_, i) => mkVideo(`ref${i}`, 60 + i * 5, 50000 + i * 1000));
  const result = scoreChannelQueues({ channels: [
    { channel_url: "irrelevant-ref", name: "News Channel", qualification_assessment: qualify("irrelevant", 0.99, ["news"]), description: "News", subscriber_count: 10000, video_count: 200, total_view_count: 5000000, joined_date: "2018-01-01", handle: "news", channel_id: "n1", metadata_source: "test", videos: richVideos },
    { channel_url: "unknown-ref", name: "Unclear Channel", qualification_assessment: qualify("unknown", 0.99, ["unclear"]), description: "Unknown", subscriber_count: 10000, video_count: 200, total_view_count: 5000000, joined_date: "2018-01-01", handle: "unclear", channel_id: "n2", metadata_source: "test", videos: richVideos }
  ], now });
  for (const url of ["irrelevant-ref", "unknown-ref"]) {
    const row = ratingFor(result, url, "reference_inspiration");
    assert.equal(row.score, null);
    assert.equal(row.rank, null);
  }
  assert.equal(ratingFor(result, "irrelevant-ref", "reference_inspiration").eligibility, "ineligible_irrelevant");
  assert.equal(ratingFor(result, "unknown-ref", "reference_inspiration").eligibility, "qualification_pending");
});

test("irrelevant explicit seeds are preserved as conflicts but cannot receive an inspiration rank", () => {
  const url = "https://www.youtube.com/@unrelated";
  const result = scoreChannelQueues({ channels: [{ channel_url: url, name: "Not Relevant", handle: "notrelevant", qualification_assessment: qualify("irrelevant", 0.99, ["news"]), videos: [] }],
    referenceSeeds: [{ handle: "notrelevant", name: "Not Relevant" }], now });
  const ref = ratingFor(result, url, "reference_inspiration");
  assert.equal(ref.eligibility, "explicit_reference_conflict");
  assert.equal(ref.status, "explicit_reference_conflict");
  assert.equal(ref.score, null);
  assert.equal(ref.rank, null);
  assert.ok(ref.reason_codes.includes("seed_ecosystem_conflict"));
});

test("reference score no longer saturates and missing components are visible and penalized", () => {
  const videos = Array.from({ length: 20 }, (_, i) => mkVideo(`reference${i}`, 60 + i * 4, 80000 + i * 1000));
  const q = qualify("core", 0.95, ["systems design", "distributed systems", "technology education"]);
  const full = { channel_url: "full", name: "Full evidence", handle: "full", channel_id: "id-full", description: "Technology education", subscriber_count: 10000, video_count: 100, total_view_count: 10000000, joined_date: "2015-01-01", metadata_source: "test", qualification_assessment: q, videos };
  const partial = { channel_url: "partial", name: "Partial evidence", qualification_assessment: q, videos };
  const result = scoreChannelQueues({ channels: [full, partial], now });
  const fullRef = ratingFor(result, "full", "reference_inspiration");
  const partialRef = ratingFor(result, "partial", "reference_inspiration");
  assert.ok(fullRef.score < 100);
  assert.ok(partialRef.score < fullRef.score, `partial=${partialRef.score}, full=${fullRef.score}`);
  assert.ok(partialRef.coverage < fullRef.coverage, `partial coverage=${partialRef.coverage}, full coverage=${fullRef.coverage}`);
  assert.ok(fullRef.components.find(component => component.name === "raw_metadata_topic_evidence").score < 100);
  assert.ok(partialRef.components.some(component => component.name === "metadata_completeness" && component.missing));
  assert.ok(partialRef.reason_codes.includes("score_shrunk_for_missing_components"));
});

test("breakout score now includes relative reach and age-adjusted evidence components", () => {
  const videos = Array.from({ length: 20 }, (_, i) => mkVideo(`growth${i}`, 45 + i * 15, 30000 + i * 3000));
  const result = scoreChannelQueues({ channels: [{ channel_url: "growth", name: "Growth candidate", qualification_assessment: qualify(), videos, subscriber_count: 10000, subscriber_count_precision: "exact", joined_date: "2022-01-01", video_count: 100 }], now });
  const row = ratingFor(result, "growth", "breaking_out");
  assert.ok(row.score != null);
  assert.ok(row.components.some(component => component.name === "smoothed_reach_relative_to_channel_size" && !component.missing));
  assert.ok(row.components.some(component => component.name === "lifetime_views_per_day_proxy" && !component.missing));
});

test("queue ratings are invariant to channel-profile, vocabulary, and legacy priority outputs", () => {
  const videos = Array.from({ length: 18 }, (_, i) => mkVideo(`raw${i}`, 50 + i * 7, 12000 + i * 1300));
  const base = {
    channel_url: "raw-only", name: "Raw Only Systems", qualification_assessment: qualify(),
    description: "Technology education and systems analysis", keywords: ["technology education", "systems design"],
    subscriber_count: 15000, video_count: 80, total_view_count: 5000000,
    joined_date: "2020-01-01", handle: "rawonly", channel_id: "UCrawonly",
    metadata_source: "test", videos
  };
  const plain = scoreChannelQueues({ channels: [base], now });
  const contaminated = scoreChannelQueues({ channels: [{
    ...base,
    profile: { channel_summary: "Completely different profile", vocabulary_proposals: {
      themes: [{ name: "theme X" }, { name: "theme Y" }], entities: [{ name: "entity Z" }], patterns: [{ name: "pattern Q" }]
    } },
    priority: { specialist_score: 100, breakthrough_score: 0, novelty_score: 100, exploration_score: 100, dominant_cluster_no: 1 }
  }], now });
  const compact = result => result.ratings.map(row => ({
    queue: row.queue.queue_key, score: row.queue.score, rank: row.queue.rank,
    confidence: row.queue.confidence, coverage: row.queue.coverage, status: row.queue.status,
    components: row.queue.components, reason_codes: row.queue.reason_codes
  }));
  assert.deepEqual(compact(contaminated), compact(plain));
});

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.