Docs/Skills and code/tests/
tests/video-normalization.test.js
import test from "node:test";
import { execFileSync } from "node:child_process";
import assert from "node:assert/strict";
import {
countPrecision, durationPrecision, normalizeChannelVideoNode, normalizeSearchVideo,
parseAbsoluteDate, parseCount, parseDurationSeconds
} from "../src/video-normalization.js";
test("parses exact and abbreviated counts without losing the source precision", () => {
assert.equal(parseCount("63,347,092 views"), 63347092);
assert.equal(parseCount("179K"), 179000);
assert.equal(countPrecision("179K"), "abbreviated");
assert.equal(countPrecision(179080), "exact");
assert.equal(countPrecision("63,347,092 views"), "exact_displayed");
assert.equal(parseCount(null), null);
});
test("keeps relative dates as display-only rather than inventing a date", () => {
assert.deepEqual(parseAbsoluteDate("1mo ago"), { value: null, precision: "relative" });
assert.deepEqual(parseAbsoluteDate("Sep 1, 2026"), { value: "2026-09-01", precision: "date" });
assert.deepEqual(parseAbsoluteDate("Feb 20, 2017"), { value: "2017-02-20", precision: "date" });
assert.deepEqual(parseAbsoluteDate("2024-02-29"), { value: "2024-02-29", precision: "date" });
assert.deepEqual(parseAbsoluteDate("Feb 29, 2025"), { value: null, precision: "display_only" });
});
test("parses duration display formats", () => {
assert.equal(parseDurationSeconds("27 minutes"), 1620);
assert.equal(parseDurationSeconds("2 minutes, 57 seconds"), 177);
assert.equal(parseDurationSeconds("1:02:03"), 3723);
assert.equal(durationPrecision("27 minutes"), "rounded_display");
assert.equal(durationPrecision("2 minutes, 57 seconds"), "displayed_seconds");
assert.equal(durationPrecision(1648), "exact");
});
test("extracts nested LockupView thumbnails, view text and relative date", () => {
const node = {
type: "LockupView", content_type: "VIDEO", content_id: "abc123",
metadata: {
title: { text: "A systems design explained" },
metadata: { metadata_rows: [{ metadata_parts: [
{ text: { text: "179K" } }, { text: { text: "1mo ago" } }
] }] }
},
content_image: { image: [
{ url: "https://example.com/small.jpg", width: 120, height: 68 },
{ url: "https://example.com/large.jpg", width: 720, height: 404 }
] },
renderer_context: { accessibility_context: { label: "A systems design explained 27 minutes" } }
};
const video = normalizeChannelVideoNode(node, "Example Channel");
assert.equal(video.video_id, "abc123");
assert.equal(video.title, "A systems design explained");
assert.equal(video.thumbnail_url, "https://example.com/large.jpg");
assert.equal(video.view_count, 179000);
assert.equal(video.view_count_text, "179K");
assert.equal(video.view_count_precision, "abbreviated");
assert.equal(video.published_at_text, "1mo ago");
assert.equal(video.published_at, null);
assert.equal(video.published_at_precision, "relative");
assert.equal(video.duration_seconds, 1620);
});
test("search normalization retains raw fields and does not replace relative dates with exact dates", () => {
const video = normalizeSearchVideo({
id: "search1", title: "Test", url: "https://youtube.com/watch?v=search1",
author: { name: "Example Channel", url: "https://youtube.com/@examplechannel" },
thumbnail: { url: "https://example.com/thumb.jpg" },
viewCount: "2.4K views", publishedAt: "3 weeks ago", duration: "4:20", customField: "kept"
});
assert.equal(video.view_count, 2400);
assert.equal(video.view_count_precision, "abbreviated");
assert.equal(video.published_at, null);
assert.equal(video.published_at_precision, "relative");
assert.equal(video.duration_seconds, 260);
assert.match(video.raw_metadata_json, /customField/);
});
test("absolute calendar dates are timezone-independent", () => {
const moduleUrl = new URL("../src/video-normalization.js", import.meta.url).href;
const script = `import { parseAbsoluteDate } from ${JSON.stringify(moduleUrl)}; console.log(JSON.stringify(parseAbsoluteDate("Sep 1, 2026")));`;
const output = execFileSync(process.execPath, ["--input-type=module", "-e", script], {
encoding: "utf8", env: { ...process.env, TZ: "Asia/Kolkata" }
}).trim();
assert.deepEqual(JSON.parse(output), { value: "2026-09-01", precision: "date" });
});
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.