Files
youtube-generator/tests/engine/description-builder.test.ts
T
poli0981andClaude Opus 4.7 edea7e80ff feat(v0.6): sponsor / key-provider credit toggle
Adds a new optional "🎁 Thanks to {sponsor} for providing the
{platform} key of this game." line to the generated description, for
creators who get review / press keys from publishers or developers.

- Editor: new SponsorCreditEditor (sponsor name + key platform) inside
  the Attribution accordion. Editor store v2 → v3 with a trivial
  migration that back-fills empty strings for old drafts.
- Settings: new `showSponsorCredit` toggle under Tag Settings. Off by
  default so existing users see no behaviour change. Settings store
  v3 → v4; healSettings back-fills the new default.
- Engine: `buildDescription` / `renderAll` take a `showSponsorCredit`
  option. Renders only when the toggle is on AND BOTH sponsor fields
  are non-empty — partial data is treated as "not ready, skip" rather
  than rendered with holes. Positioned above the music section and
  donate block so the thank-you is visually prominent.
- i18n: added sponsor-section UI labels/placeholders, the
  `settings.showSponsorCredit` toggle label, and
  `description.sections.sponsorCredit` template across all 6 locales
  (en, vi, ja, es, ko, zh) + schema.
- Tests: 7 new description-builder cases (render / toggle-off / blank
  name / blank platform / trim / ordering / vi localization) + 1
  healSettings case for the v3 → v4 back-fill.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 10:46:34 +07:00

448 lines
15 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { buildDescription, checkDescriptionWarning } from "@engine/description-builder";
import { createMockT } from "../helpers/mock-t";
import type { GeneratorInput } from "@engine/types";
function makeInput(overrides: Partial<GeneratorInput> = {}): GeneratorInput {
return {
videoType: "full",
language: "en",
genres: ["action"],
gameName: "Elden Ring",
channelName: "TestChannel",
platform: "steam",
spoilerWarning: false,
matureWarning: false,
storeLinks: {},
social: {},
rig: {},
...overrides,
};
}
describe("buildDescription", () => {
it("generates minimal description with only required fields", () => {
const t = createMockT("en");
const result = buildDescription(makeInput(), t);
expect(result).toContain("full gameplay of Elden Ring on TestChannel");
expect(result).toContain("No Commentary");
expect(result).toContain("Like | 🔔 Subscribe");
expect(result).toContain("#EldenRing");
});
it("sanitises hashtags for game names with special characters", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ gameName: "S.T.A.L.K.E.R. 2" }),
t,
);
expect(result).toContain("#STALKER2");
expect(result).not.toMatch(/#\s?S\./);
});
it("sanitises hashtags for snake_case genre ids (fixes #visual_novel bug)", () => {
const t = createMockT("en");
const result = buildDescription(makeInput({ genres: ["visual_novel"] }), t);
expect(result).toContain("#visualnovel");
expect(result).not.toContain("#visual_novel");
});
it("uses the first genre for the hashtag when multiple are selected", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ genres: ["hack_slash", "action", "soulslike"] }),
t,
);
expect(result).toContain("#hackslash");
expect(result).not.toContain("#action");
expect(result).not.toContain("#soulslike");
});
it("includes timestamps when provided", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ timestamps: "0:00 Intro\n5:30 Boss" }),
t,
);
expect(result).toContain("TIMESTAMPS");
expect(result).toContain("0:00 Intro");
expect(result).toContain("5:30 Boss");
});
it("includes store links when provided (paid default → Buy heading)", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ storeLinks: { Steam: "https://store.steampowered.com/app/123" } }),
t,
);
expect(result).toContain("GET THE GAME (if you want to buy)");
expect(result).toContain("Steam: https://store.steampowered.com/app/123");
expect(result).not.toContain("DOWNLOAD THE GAME");
});
it("switches to Download heading when majority of links are free/demo", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({
storeLinks: {
steam: "https://store.steampowered.com/app/1",
itchio: "https://dev.itch.io/demo",
epic: "https://store.epicgames.com/freegame",
},
storeLinkTypes: {
steam: "paid",
itchio: "demo",
epic: "free",
},
}),
t,
);
expect(result).toContain("DOWNLOAD THE GAME (if you want to play)");
expect(result).not.toContain("GET THE GAME");
});
it("ties favour the Buy heading", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({
storeLinks: {
steam: "https://store.steampowered.com/app/1",
itchio: "https://dev.itch.io/game",
},
storeLinkTypes: { steam: "paid", itchio: "free" },
}),
t,
);
// 1 paid, 1 free → nonPaid (1) is NOT > entries.length/2 (1) → Buy
expect(result).toContain("GET THE GAME (if you want to buy)");
});
it("adds (free demo) suffix to demo links", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({
storeLinks: { itchio: "https://dev.itch.io/demo" },
storeLinkTypes: { itchio: "demo" },
}),
t,
);
expect(result).toContain("(free demo)");
});
it("adds (free) suffix to free links but not paid ones", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({
storeLinks: {
steam: "https://store.steampowered.com/app/1",
itchio: "https://dev.itch.io/game",
},
storeLinkTypes: { steam: "paid", itchio: "free" },
}),
t,
);
// Find the itchio line and make sure it has (free), Steam line does not
const lines = result.split("\n");
const itchLine = lines.find((l) => l.includes("itch.io"));
const steamLine = lines.find((l) => l.includes("steampowered"));
expect(itchLine).toContain("(free)");
expect(steamLine).not.toContain("(free)");
});
it("defaults unspecified link types to paid", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({
storeLinks: { steam: "https://store.steampowered.com/app/1" },
// storeLinkTypes omitted entirely
}),
t,
);
expect(result).toContain("GET THE GAME (if you want to buy)");
expect(result).not.toContain("(free)");
});
it("includes video settings", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ resolution: "4K", fps: "60", graphicsPreset: "Ultra" }),
t,
);
expect(result).toContain("VIDEO SETTINGS");
expect(result).toContain("4K | 60 FPS | Ultra");
});
it("includes rig info when provided", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ rig: { CPU: "i9-14900K", GPU: "RTX 4090" } }),
t,
);
expect(result).toContain("MY RIG");
expect(result).toContain("CPU: i9-14900K");
expect(result).toContain("GPU: RTX 4090");
});
it("humanises snake_case rig keys (e.g. video_editor → VIDEO EDITOR)", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ rig: { video_editor: "davinci_resolve_studio|19.1" } }),
t,
);
expect(result).toContain("VIDEO EDITOR: DaVinci Resolve Studio 19.1");
expect(result).not.toContain("VIDEO_EDITOR");
});
it("includes spoiler warning when enabled", () => {
const t = createMockT("en");
const result = buildDescription(makeInput({ spoilerWarning: true }), t);
expect(result).toContain("SPOILER WARNING");
});
it("includes music attribution section when provided", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ musicAttribution: "Music by Karl Casey @ White Bat Audio" }),
t,
);
expect(result).toContain("🎵 MUSIC / SOUND");
expect(result).toContain("Music by Karl Casey @ White Bat Audio");
});
it("places music section before donate/social sections", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({
musicAttribution: "Music credit here",
social: { twitter: "https://x.com/me", kofi: "https://ko-fi.com/me" },
}),
t,
);
const musicIdx = result.indexOf("🎵 MUSIC / SOUND");
const donateIdx = result.indexOf("SUPPORT THE CHANNEL");
expect(musicIdx).toBeGreaterThan(-1);
expect(donateIdx).toBeGreaterThan(-1);
expect(musicIdx).toBeLessThan(donateIdx);
});
it("renders the sponsor credit line when toggle is on and both sponsor fields are filled", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ sponsorName: "Ubisoft", sponsorPlatform: "Steam" }),
t,
{ showSponsorCredit: true },
);
expect(result).toContain("🎁 Thanks to Ubisoft for providing the Steam key of this game.");
});
it("skips the sponsor credit when the toggle is off", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ sponsorName: "Ubisoft", sponsorPlatform: "Steam" }),
t,
{ showSponsorCredit: false },
);
expect(result).not.toContain("🎁 Thanks to Ubisoft");
});
it("skips the sponsor credit when sponsorName is blank even if toggle is on", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ sponsorName: "", sponsorPlatform: "Steam" }),
t,
{ showSponsorCredit: true },
);
expect(result).not.toContain("🎁");
expect(result).not.toContain("Steam key of this game");
});
it("skips the sponsor credit when sponsorPlatform is blank even if toggle is on", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ sponsorName: "Ubisoft", sponsorPlatform: "" }),
t,
{ showSponsorCredit: true },
);
expect(result).not.toContain("🎁");
expect(result).not.toContain("Thanks to Ubisoft");
});
it("trims whitespace around sponsor fields before rendering", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ sponsorName: " Ubisoft ", sponsorPlatform: " Steam " }),
t,
{ showSponsorCredit: true },
);
expect(result).toContain("Thanks to Ubisoft for providing the Steam key");
expect(result).not.toContain(" Ubisoft ");
});
it("places the sponsor credit above the music section and donate block", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({
sponsorName: "Ubisoft",
sponsorPlatform: "Steam",
musicAttribution: "Music credit",
social: { kofi: "https://ko-fi.com/me" },
}),
t,
{ showSponsorCredit: true },
);
const sponsorIdx = result.indexOf("🎁");
const musicIdx = result.indexOf("🎵 MUSIC / SOUND");
const donateIdx = result.indexOf("SUPPORT THE CHANNEL");
expect(sponsorIdx).toBeGreaterThan(-1);
expect(musicIdx).toBeGreaterThan(sponsorIdx);
expect(donateIdx).toBeGreaterThan(musicIdx);
});
it("renders sponsor credit in Vietnamese with the localized template", () => {
const t = createMockT("vi");
const result = buildDescription(
makeInput({
language: "vi",
sponsorName: "Ubisoft",
sponsorPlatform: "Steam",
}),
t,
{ showSponsorCredit: true },
);
expect(result).toContain("🎁 Cảm ơn Ubisoft đã tặng key Steam của game này.");
});
it("omits the music section when attribution is blank or whitespace", () => {
const t = createMockT("en");
const blank = buildDescription(makeInput({ musicAttribution: "" }), t);
const whitespace = buildDescription(makeInput({ musicAttribution: " \n " }), t);
expect(blank).not.toContain("🎵 MUSIC / SOUND");
expect(whitespace).not.toContain("🎵 MUSIC / SOUND");
});
it("renders copyright line when showCopyright is true and channelName is set", () => {
const t = createMockT("en");
const result = buildDescription(makeInput(), t, { showCopyright: true });
const year = new Date().getFullYear();
expect(result).toContain(`© ${year} TestChannel. All rights reserved.`);
});
it("skips copyright when channelName is blank even if toggle is on", () => {
const t = createMockT("en");
const result = buildDescription(makeInput({ channelName: "" }), t, {
showCopyright: true,
});
expect(result).not.toContain("All rights reserved");
});
it("skips copyright when the toggle is off", () => {
const t = createMockT("en");
const result = buildDescription(makeInput(), t, { showCopyright: false });
expect(result).not.toContain("All rights reserved");
});
it("renders the usage policy block when showUsagePolicy is true", () => {
const t = createMockT("en");
const result = buildDescription(makeInput(), t, { showUsagePolicy: true });
expect(result).toContain("📋 USAGE POLICY");
expect(result).toContain("Always credit");
expect(result).toContain("Compilation, review, commentary");
});
it("skips the usage policy when the toggle is off", () => {
const t = createMockT("en");
const result = buildDescription(makeInput(), t, { showUsagePolicy: false });
expect(result).not.toContain("📋 USAGE POLICY");
});
it("places copyright and usage policy between CTA and hashtags", () => {
const t = createMockT("en");
const result = buildDescription(makeInput(), t, {
showCopyright: true,
showUsagePolicy: true,
});
const ctaIdx = result.indexOf("Like | 🔔 Subscribe");
const copyrightIdx = result.indexOf("All rights reserved");
const policyIdx = result.indexOf("📋 USAGE POLICY");
const hashtagsIdx = result.indexOf("#EldenRing");
expect(ctaIdx).toBeGreaterThan(-1);
expect(copyrightIdx).toBeGreaterThan(ctaIdx);
expect(policyIdx).toBeGreaterThan(copyrightIdx);
expect(hashtagsIdx).toBeGreaterThan(policyIdx);
});
it("includes mature warning when enabled", () => {
const t = createMockT("en");
const result = buildDescription(makeInput({ matureWarning: true }), t);
expect(result).toContain("MATURE CONTENT");
});
it("includes social links when provided", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ social: { twitter: "https://x.com/test", discord: "https://discord.gg/test" } }),
t,
);
expect(result).toContain("FOLLOW ME");
expect(result).toContain("Twitter / X: https://x.com/test");
});
it("includes playlist link when provided", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ playlistLink: "https://youtube.com/playlist?list=abc" }),
t,
);
expect(result).toContain("Watch the full series: https://youtube.com/playlist?list=abc");
});
it("includes contact email when provided", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ contactEmail: "test@example.com" }),
t,
);
expect(result).toContain("Business inquiries: test@example.com");
});
it("generates part description correctly", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ videoType: "part", partNumber: "5" }),
t,
);
expect(result).toContain("Part 5 of Elden Ring");
});
it("generates boss description correctly", () => {
const t = createMockT("en");
const result = buildDescription(
makeInput({ videoType: "boss", bossName: "Margit" }),
t,
);
expect(result).toContain("Margit boss fight");
});
it("generates description in Vietnamese", () => {
const t = createMockT("vi");
const result = buildDescription(makeInput({ language: "vi" }), t);
expect(result).toContain("full gameplay của Elden Ring");
expect(result).toContain("Không bình luận");
});
});
describe("checkDescriptionWarning", () => {
it("returns null for description under limit", () => {
expect(checkDescriptionWarning("Short description")).toBeNull();
});
it("returns warning for description exceeding 5000 chars", () => {
const longDesc = "A".repeat(5001);
const warning = checkDescriptionWarning(longDesc);
expect(warning).not.toBeNull();
expect(warning!.field).toBe("description");
expect(warning!.limit).toBe(5000);
});
});