|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | +import vm from "node:vm"; |
| 3 | +import { Window } from "happy-dom"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | + |
| 6 | +const LATIN = ["bright", "camera", "moment", "simple", "render", "motion"]; |
| 7 | +const KOREAN = ["오늘", "영상", "처음", "공개", "끝까지", "감사"]; |
| 8 | +const CYRILLIC = ["сегодня", "невероятно", "показываем", "результат", "спасибо", "друзья"]; |
| 9 | +const LATIN_LONG = ["segodnya", "neveroyatn", "pokazyvaem", "rezultatt", "spasibo", "druzyaa"]; |
| 10 | + |
| 11 | +function source(name: string): string { |
| 12 | + const url = new URL(`../../../../registry/components/${name}/${name}.html`, import.meta.url); |
| 13 | + return readFileSync(url, "utf8"); |
| 14 | +} |
| 15 | + |
| 16 | +function withTranscript(html: string, words: string[], step = 0.5): string { |
| 17 | + const list = words.map( |
| 18 | + (text, i) => `{ text: "${text}", start: ${i * step}, end: ${i * step + 0.2} }`, |
| 19 | + ); |
| 20 | + return html.replace(/var TRANSCRIPT = \[[\s\S]*?\];/, `var TRANSCRIPT = [${list.join(",")}];`); |
| 21 | +} |
| 22 | + |
| 23 | +function withKeywords(html: string, words: string[]): string { |
| 24 | + return html.replace( |
| 25 | + /var KEYWORDS = new Set\(\[[^\]]*\]\);/, |
| 26 | + `var KEYWORDS = new Set(${JSON.stringify(words)});`, |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +/** Runs the component's inline script; returns its document and how many tweens it scheduled. */ |
| 31 | +function run(html: string): { document: Window["document"]; tweens: number } { |
| 32 | + const window = new Window({ |
| 33 | + settings: { disableJavaScriptEvaluation: true, disableJavaScriptFileLoading: true }, |
| 34 | + }); |
| 35 | + window.document.write(html); |
| 36 | + const scripts = [...window.document.querySelectorAll("script")]; |
| 37 | + const script = scripts.filter((el) => !el.hasAttribute("src")).at(-1)!.textContent; |
| 38 | + for (const el of scripts) el.remove(); |
| 39 | + const canvasContext = { font: "", measureText: () => ({ width: 0 }) }; |
| 40 | + window.HTMLCanvasElement.prototype.getContext = (() => |
| 41 | + canvasContext) as unknown as typeof window.HTMLCanvasElement.prototype.getContext; |
| 42 | + let tweens = 0; |
| 43 | + const tl: Record<string, () => unknown> = {}; |
| 44 | + for (const method of ["set", "seek", "add", "call"]) tl[method] = () => tl; |
| 45 | + for (const method of ["to", "from", "fromTo"]) { |
| 46 | + tl[method] = () => { |
| 47 | + tweens += 1; |
| 48 | + return tl; |
| 49 | + }; |
| 50 | + } |
| 51 | + const gsap = { timeline: () => tl, set: () => undefined, to: () => undefined }; |
| 52 | + vm.runInNewContext(script, { window, document: window.document, gsap, console }); |
| 53 | + return { document: window.document, tweens }; |
| 54 | +} |
| 55 | + |
| 56 | +function wordColors(html: string): string[] { |
| 57 | + const words = [...run(html).document.querySelectorAll(".caption-word")]; |
| 58 | + return words.map((el) => (el as unknown as { style: { color: string } }).style.color); |
| 59 | +} |
| 60 | + |
| 61 | +describe("caption components with a transcript that is not in Latin letters", () => { |
| 62 | + it("caption-emoji-pop shows the emoji mapped to a Korean word", () => { |
| 63 | + const html = withTranscript(source("caption-emoji-pop"), KOREAN).replace( |
| 64 | + /var WORD_EMOJI = \{[\s\S]*?\};/, |
| 65 | + 'var WORD_EMOJI = { 영상: "📹" };', |
| 66 | + ); |
| 67 | + const emoji = [...run(html).document.querySelectorAll(".caption-emoji")]; |
| 68 | + expect(emoji.map((el) => el.textContent)).toEqual(["📹"]); |
| 69 | + }); |
| 70 | + |
| 71 | + it.each(["caption-emoji-pop", "caption-neon-accent"])( |
| 72 | + "%s accents Korean keywords the way it accents Latin ones", |
| 73 | + (name) => { |
| 74 | + const latin = wordColors(withKeywords(withTranscript(source(name), LATIN), LATIN)); |
| 75 | + const korean = wordColors(withKeywords(withTranscript(source(name), KOREAN), KOREAN)); |
| 76 | + expect(latin).toHaveLength(LATIN.length); |
| 77 | + expect(korean).toEqual(latin); |
| 78 | + }, |
| 79 | + ); |
| 80 | + |
| 81 | + it("caption-glitch-rgb gives a long Cyrillic word the aftershock a long Latin word gets", () => { |
| 82 | + const tweens = (words: string[]) => |
| 83 | + run(withTranscript(source("caption-glitch-rgb"), words, 0.47)).tweens; |
| 84 | + const [short, latin, cyrillic] = [tweens(LATIN), tweens(LATIN_LONG), tweens(CYRILLIC)]; |
| 85 | + expect(latin).toBeGreaterThan(short); |
| 86 | + expect(cyrillic).toBe(latin); |
| 87 | + }); |
| 88 | +}); |
0 commit comments