Docs/Skills and code/tests/

tests/within-channel-performance.test.js

Code84 lines4.8 KBtests/

import test from "node:test";
import assert from "node:assert/strict";
import { analyzeWithinChannelCorpus } from "../src/analysis/within-channel-performance.js";
import { ageCohort, median, medianAbsoluteDeviation, quantile, reliablePublicationDate, sampleAssessment } from "../src/analysis/within-channel-statistics.js";

const exactVideo = (video_id, view_count, published_at, extra = {}) => ({
  video_id,
  title: `Video ${video_id}`,
  url: `https://www.youtube.com/watch?v=${video_id}`,
  view_count,
  view_count_precision: "exact",
  view_count_source: "test",
  view_observed_at: "2026-10-15T00:00:00.000Z",
  published_at,
  published_at_normalized: published_at,
  published_at_precision: "exact_day",
  published_at_normalization_method: "absolute_source_value",
  ...extra
});

test("median, quantile, and MAD handle odd/even samples", () => {
  assert.equal(median([1, 3, 2]), 2);
  assert.equal(median([1, 2, 3, 4]), 2.5);
  assert.equal(quantile([1, 2, 3, 4], 0.25), 1.75);
  assert.equal(medianAbsoluteDeviation([1, 2, 3]), 1);
});

test("sample assessment follows the explicit engineering bands", () => {
  assert.equal(sampleAssessment(0), "insufficient_evidence");
  assert.equal(sampleAssessment(4), "insufficient_evidence");
  assert.equal(sampleAssessment(5), "preliminary_distribution");
  assert.equal(sampleAssessment(10), "initial_outlier_analysis");
  assert.equal(sampleAssessment(20), "distribution_analysis");
  assert.equal(sampleAssessment(50), "subgroup_analysis_possible");
});

test("age cohorts are explicit and boundary-aware", () => {
  assert.equal(ageCohort(7), "0_7d");
  assert.equal(ageCohort(8), "8_30d");
  assert.equal(ageCohort(31), "31_90d");
  assert.equal(ageCohort(91), "91_365d");
  assert.equal(ageCohort(366), "1_3y");
  assert.equal(ageCohort(1096), "3y_plus");
});

test("relative or estimated publication dates are not accepted for age adjustment", () => {
  assert.equal(reliablePublicationDate(exactVideo("a", 100, "2026-01-01T00:00:00.000Z")), Date.parse("2026-01-01T00:00:00.000Z"));
  assert.equal(reliablePublicationDate(exactVideo("b", 100, "2026-01-01", { published_at_precision: "estimated_day" })), null);
  assert.equal(reliablePublicationDate(exactVideo("c", 100, "2026-01-01", { published_at_normalization_method: "relative_to_discovered_at" })), null);
});

test("analysis returns every channel, deduplicates repeated video IDs, and separates lifetime from age-adjusted outliers", () => {
  const channels = [
    { channel_url: "channel-a", name: "A", resolved_name: "A", resolved_subscriber_count: 10000, resolved_video_count: 20 },
    { channel_url: "channel-empty", name: "Empty", resolved_name: "Empty", resolved_subscriber_count: null, resolved_video_count: null }
  ];
  const videos = Array.from({ length: 12 }, (_, i) => exactVideo(`a${i}`, i === 11 ? 500000 : 1000 + i * 100, `2026-0${Math.max(1, Math.min(9, i + 1))}-01T00:00:00.000Z`));
  videos.push(exactVideo("a-missing", null, "2026-09-01T00:00:00.000Z"));
  const videosByChannel = new Map([
    ["channel-a", [...videos, videos[0]]],
    ["channel-empty", []]
  ]);
  const result = analyzeWithinChannelCorpus({ channels, videosByChannel, nowMs: Date.parse("2026-10-17T00:00:00.000Z"), config: { minAgeCohortSize: 2 } });
  assert.equal(result.profiles.length, 2);
  const profile = result.profiles.find(row => row.channel_url === "channel-a");
  assert.equal(profile.linked_unique_video_count, 13);
  assert.equal(profile.eligible_view_count, 12);
  assert.equal(profile.sample_assessment, "initial_outlier_analysis");
  assert.ok(profile.top_1_view_share <= profile.top_5_view_share);
  assert.ok(profile.top_5_view_share <= profile.top_10_view_share);
  assert.equal(profile.reliable_dated_view_count, 12);
  assert.equal(result.outliers.length, 13);
  assert.equal(result.outliers.find(row => row.video_id === "a-missing").lifetime_outlier_label, "unavailable_no_view_count");
  assert.equal(result.outliers.find(row => row.video_id === "a-missing").age_adjusted_outlier_label, "unavailable_no_view_count");
  assert.ok(result.outliers.some(row => row.lifetime_outlier_label.startsWith("strong_outlier") || row.lifetime_outlier_label.startsWith("potential_outlier")));
  assert.ok(result.outliers.some(row => row.age_adjusted_outlier_label !== "unavailable_unreliable_publication_date"));
  const empty = result.profiles.find(row => row.channel_url === "channel-empty");
  assert.equal(empty.eligible_view_count, 0);
  assert.equal(empty.sample_assessment, "insufficient_evidence");
  assert.equal(result.queueEvidence.length, 10);
  assert.ok(result.queueEvidence.every(row => row.integration_status === "observational_only" && row.score_effect === 0));
  assert.equal(result.queueEvidence.find(row => row.queue_name === "unexplored_niches").candidate_features_json.includes('"niche_rarity_measured":false'), true);
});

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.