@biunow/agent-biutalk 0.0.0-stage → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (227) hide show
  1. package/bin/server.js +355 -0
  2. package/engine/AGENTS.md +1414 -0
  3. package/engine/CLAUDE.md +1 -0
  4. package/engine/LICENSE +21 -0
  5. package/engine/README.md +216 -0
  6. package/engine/bin/explainroo.js +19 -0
  7. package/engine/dev/audio-lab.mjs +268 -0
  8. package/engine/docs/media/contact-sheet.jpg +0 -0
  9. package/engine/docs/media/dns-poster.jpg +0 -0
  10. package/engine/docs/media/intro-poster.jpg +0 -0
  11. package/engine/docs/media/logo-dark.png +0 -0
  12. package/engine/docs/media/logo-light.png +0 -0
  13. package/engine/docs/media/looks-dark.webp +0 -0
  14. package/engine/docs/media/looks-light.webp +0 -0
  15. package/engine/docs/media/product-demo.webp +0 -0
  16. package/engine/docs/media/rebase-poster.jpg +0 -0
  17. package/engine/docs/media/sizes-dark.webp +0 -0
  18. package/engine/docs/media/sizes-light.webp +0 -0
  19. package/engine/engine/audio/dsp.js +69 -0
  20. package/engine/engine/audio/instruments.js +147 -0
  21. package/engine/engine/audio/music.js +576 -0
  22. package/engine/engine/audio/pcm.js +93 -0
  23. package/engine/engine/audio/rng.js +40 -0
  24. package/engine/engine/audio/sfx.js +287 -0
  25. package/engine/engine/audio/soundtrack.js +201 -0
  26. package/engine/engine/audio/synth.js +721 -0
  27. package/engine/engine/audio/theory.js +105 -0
  28. package/engine/engine/audio/wav.js +47 -0
  29. package/engine/engine/captions.js +131 -0
  30. package/engine/engine/clips.js +107 -0
  31. package/engine/engine/code.js +179 -0
  32. package/engine/engine/director.js +475 -0
  33. package/engine/engine/frame.html +135 -0
  34. package/engine/engine/icons/LICENSE-lucide.txt +43 -0
  35. package/engine/engine/icons/lucide-tags.json +1 -0
  36. package/engine/engine/icons/lucide.json +1 -0
  37. package/engine/engine/layout.js +69 -0
  38. package/engine/engine/pathdata.js +234 -0
  39. package/engine/engine/pen.js +240 -0
  40. package/engine/engine/player.html +67 -0
  41. package/engine/engine/player.js +219 -0
  42. package/engine/engine/readability.js +9 -0
  43. package/engine/engine/runtime.js +690 -0
  44. package/engine/engine/stage.js +2247 -0
  45. package/engine/engine/studio.html +216 -0
  46. package/engine/engine/text.js +81 -0
  47. package/engine/engine/themes.js +360 -0
  48. package/engine/engine/transitions.js +161 -0
  49. package/engine/engine/ui.js +1042 -0
  50. package/engine/engine/upload.js +32 -0
  51. package/engine/engine/util.js +204 -0
  52. package/engine/examples/callbacks-to-async-await/code-versions.js +34 -0
  53. package/engine/examples/callbacks-to-async-await/scenes.js +106 -0
  54. package/engine/examples/callbacks-to-async-await/script.md +22 -0
  55. package/engine/examples/callbacks-to-async-await/video.json +16 -0
  56. package/engine/examples/circle-area/scenes.js +228 -0
  57. package/engine/examples/circle-area/script.md +37 -0
  58. package/engine/examples/circle-area/video.json +14 -0
  59. package/engine/examples/explainroo-intro/scenes.js +133 -0
  60. package/engine/examples/explainroo-intro/script.md +27 -0
  61. package/engine/examples/explainroo-intro/video.json +8 -0
  62. package/engine/examples/how-dns-works/scenes.js +130 -0
  63. package/engine/examples/how-dns-works/script.md +28 -0
  64. package/engine/examples/how-dns-works/video.json +8 -0
  65. package/engine/examples/leap-years/scenes.js +156 -0
  66. package/engine/examples/leap-years/script.md +25 -0
  67. package/engine/examples/leap-years/video.json +16 -0
  68. package/engine/examples/leap-years-ja/scenes.js +194 -0
  69. package/engine/examples/leap-years-ja/script.md +32 -0
  70. package/engine/examples/leap-years-ja/video.json +17 -0
  71. package/engine/examples/merge-vs-rebase/scenes.js +129 -0
  72. package/engine/examples/merge-vs-rebase/script.md +22 -0
  73. package/engine/examples/merge-vs-rebase/video.json +8 -0
  74. package/engine/examples/password-storage-zh/scenes.js +185 -0
  75. package/engine/examples/password-storage-zh/script.md +25 -0
  76. package/engine/examples/password-storage-zh/video.json +19 -0
  77. package/engine/examples/sqlite-demo/README.md +19 -0
  78. package/engine/examples/sqlite-demo/scenes.js +423 -0
  79. package/engine/examples/sqlite-demo/script.md +35 -0
  80. package/engine/examples/sqlite-demo/video.json +17 -0
  81. package/engine/examples/unspar-demo/assets/fonts/Figtree-400.woff2 +0 -0
  82. package/engine/examples/unspar-demo/assets/fonts/Figtree-500.woff2 +0 -0
  83. package/engine/examples/unspar-demo/assets/fonts/Figtree-600.woff2 +0 -0
  84. package/engine/examples/unspar-demo/assets/fonts/Figtree-700.woff2 +0 -0
  85. package/engine/examples/unspar-demo/assets/fonts/Figtree-OFL.txt +93 -0
  86. package/engine/examples/unspar-demo/assets/unspar-logo.svg +23 -0
  87. package/engine/examples/unspar-demo/scenes.js +367 -0
  88. package/engine/examples/unspar-demo/script.md +25 -0
  89. package/engine/examples/unspar-demo/video.json +21 -0
  90. package/engine/examples/why-is-the-sky-blue-es/scenes.js +160 -0
  91. package/engine/examples/why-is-the-sky-blue-es/script.md +22 -0
  92. package/engine/examples/why-is-the-sky-blue-es/video.json +15 -0
  93. package/engine/fonts/ArchitectsDaughter-400-latin-ext.woff2 +0 -0
  94. package/engine/fonts/ArchitectsDaughter-400-latin.woff2 +0 -0
  95. package/engine/fonts/CabinSketch-400-latin.woff2 +0 -0
  96. package/engine/fonts/CabinSketch-700-latin.woff2 +0 -0
  97. package/engine/fonts/InstrumentSerif-400-italic-latin-ext.woff2 +0 -0
  98. package/engine/fonts/InstrumentSerif-400-italic-latin.woff2 +0 -0
  99. package/engine/fonts/InstrumentSerif-400-normal-latin-ext.woff2 +0 -0
  100. package/engine/fonts/InstrumentSerif-400-normal-latin.woff2 +0 -0
  101. package/engine/fonts/Inter-400-latin-ext.woff2 +0 -0
  102. package/engine/fonts/Inter-400-latin.woff2 +0 -0
  103. package/engine/fonts/JetBrainsMono-400-latin-ext.woff2 +0 -0
  104. package/engine/fonts/JetBrainsMono-400-latin.woff2 +0 -0
  105. package/engine/fonts/Kalam-400-latin-ext.woff2 +0 -0
  106. package/engine/fonts/Kalam-400-latin.woff2 +0 -0
  107. package/engine/fonts/Kalam-700-latin-ext.woff2 +0 -0
  108. package/engine/fonts/Kalam-700-latin.woff2 +0 -0
  109. package/engine/fonts/NotoSansDevanagari-400.ttf +0 -0
  110. package/engine/fonts/NotoSansDevanagari-700.ttf +0 -0
  111. package/engine/fonts/PatrickHand-400-latin-ext.woff2 +0 -0
  112. package/engine/fonts/PatrickHand-400-latin.woff2 +0 -0
  113. package/engine/fonts/PlusJakartaSans-600-latin-ext.woff2 +0 -0
  114. package/engine/fonts/PlusJakartaSans-600-latin.woff2 +0 -0
  115. package/engine/fonts/SpaceGrotesk-500-latin-ext.woff2 +0 -0
  116. package/engine/fonts/SpaceGrotesk-500-latin.woff2 +0 -0
  117. package/engine/fonts/fonts.json +216 -0
  118. package/engine/fonts/licenses/ArchitectsDaughter-OFL.txt +93 -0
  119. package/engine/fonts/licenses/CabinSketch-OFL.txt +93 -0
  120. package/engine/fonts/licenses/InstrumentSerif-OFL.txt +93 -0
  121. package/engine/fonts/licenses/Inter-OFL.txt +93 -0
  122. package/engine/fonts/licenses/JetBrainsMono-OFL.txt +93 -0
  123. package/engine/fonts/licenses/Kalam-OFL.txt +93 -0
  124. package/engine/fonts/licenses/NotoSansDevanagari-OFL.txt +118 -0
  125. package/engine/fonts/licenses/PatrickHand-OFL.txt +93 -0
  126. package/engine/fonts/licenses/PlusJakartaSans-OFL.txt +93 -0
  127. package/engine/fonts/licenses/SpaceGrotesk-OFL.txt +93 -0
  128. package/engine/package-lock.json +1201 -0
  129. package/engine/package.json +67 -0
  130. package/engine/scripts/build-icons.mjs +74 -0
  131. package/engine/scripts/fetch-fonts.mjs +68 -0
  132. package/engine/skills/explainroo/SKILL.md +51 -0
  133. package/engine/src/align.js +357 -0
  134. package/engine/src/browser.js +121 -0
  135. package/engine/src/capture.js +315 -0
  136. package/engine/src/chapters.js +32 -0
  137. package/engine/src/cli.js +421 -0
  138. package/engine/src/clips.js +19 -0
  139. package/engine/src/elevenlabs.js +90 -0
  140. package/engine/src/env.js +16 -0
  141. package/engine/src/export.js +187 -0
  142. package/engine/src/ffmpeg.js +103 -0
  143. package/engine/src/fonts.js +86 -0
  144. package/engine/src/g2p.py +76 -0
  145. package/engine/src/images.js +151 -0
  146. package/engine/src/licenses/misaki-Apache-2.0.txt +201 -0
  147. package/engine/src/math.js +83 -0
  148. package/engine/src/models.js +161 -0
  149. package/engine/src/phonemes.js +162 -0
  150. package/engine/src/pipeline.js +63 -0
  151. package/engine/src/project.js +331 -0
  152. package/engine/src/qa.js +186 -0
  153. package/engine/src/recordings.js +89 -0
  154. package/engine/src/render.js +156 -0
  155. package/engine/src/script.js +266 -0
  156. package/engine/src/server.js +288 -0
  157. package/engine/src/subtitles.js +78 -0
  158. package/engine/src/timeline.js +55 -0
  159. package/engine/src/voice.js +240 -0
  160. package/engine/src/wav.js +98 -0
  161. package/engine/templates/starter/scenes.js +31 -0
  162. package/engine/templates/starter/script.md +14 -0
  163. package/engine/test/align.test.js +108 -0
  164. package/engine/test/brand.test.js +41 -0
  165. package/engine/test/browser.test.js +89 -0
  166. package/engine/test/camera-browser.test.js +225 -0
  167. package/engine/test/caption-phrases.test.js +35 -0
  168. package/engine/test/capture-browser.test.js +90 -0
  169. package/engine/test/capture-fixture.js +46 -0
  170. package/engine/test/capture.test.js +62 -0
  171. package/engine/test/chapters.test.js +41 -0
  172. package/engine/test/cjk-browser.test.js +101 -0
  173. package/engine/test/cjk.test.js +232 -0
  174. package/engine/test/cli-exit.test.js +25 -0
  175. package/engine/test/cli.test.js +116 -0
  176. package/engine/test/clips-browser.test.js +125 -0
  177. package/engine/test/clips.test.js +67 -0
  178. package/engine/test/code-morph-browser.test.js +99 -0
  179. package/engine/test/code-morph.test.js +110 -0
  180. package/engine/test/contrast-browser.test.js +63 -0
  181. package/engine/test/contrast.test.js +80 -0
  182. package/engine/test/director.test.js +316 -0
  183. package/engine/test/engine-pure.test.js +29 -0
  184. package/engine/test/export-browser.test.js +224 -0
  185. package/engine/test/export.test.js +96 -0
  186. package/engine/test/fixtures/capture.html +55 -0
  187. package/engine/test/formats-pace.test.js +104 -0
  188. package/engine/test/languages-browser.test.js +78 -0
  189. package/engine/test/languages.test.js +140 -0
  190. package/engine/test/looks-browser.test.js +109 -0
  191. package/engine/test/looks-preview.test.js +57 -0
  192. package/engine/test/looks.test.js +130 -0
  193. package/engine/test/math-browser.test.js +114 -0
  194. package/engine/test/math.test.js +54 -0
  195. package/engine/test/multi-size-browser.test.js +133 -0
  196. package/engine/test/multi-size.test.js +116 -0
  197. package/engine/test/phonemes.test.js +92 -0
  198. package/engine/test/readability-browser.test.js +36 -0
  199. package/engine/test/readability.test.js +20 -0
  200. package/engine/test/render-options.test.js +52 -0
  201. package/engine/test/script.test.js +67 -0
  202. package/engine/test/server.test.js +256 -0
  203. package/engine/test/stills-browser.test.js +70 -0
  204. package/engine/test/subtitles.test.js +83 -0
  205. package/engine/test/timeline.test.js +31 -0
  206. package/engine/test/transparent-browser.test.js +123 -0
  207. package/engine/test/ui-kit.test.js +96 -0
  208. package/engine/test/upload-browser.test.js +55 -0
  209. package/engine/test/voice-providers.test.js +338 -0
  210. package/lib/host.mjs +480 -0
  211. package/manifest.json +54 -0
  212. package/package.json +8 -4
  213. package/skills/biutalk/SKILL.md +21 -0
  214. package/studio/app.js +209 -0
  215. package/studio/assert-panel-permit.mjs +67 -0
  216. package/studio/composer.css +354 -0
  217. package/studio/composer.js +169 -0
  218. package/studio/index.html +82 -0
  219. package/studio/looks/blueprint.svg +21 -0
  220. package/studio/looks/chalk.svg +15 -0
  221. package/studio/looks/clean.svg +20 -0
  222. package/studio/looks/editorial.svg +18 -0
  223. package/studio/looks/midnight.svg +17 -0
  224. package/studio/looks/paper.svg +11 -0
  225. package/studio/select-menu.js +268 -0
  226. package/studio/styles.css +4 -0
  227. package/README.md +0 -3
@@ -0,0 +1,36 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import fs from 'node:fs';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import { findChrome } from '../src/browser.js';
7
+ import { loadProject } from '../src/project.js';
8
+ import { withEngine } from '../src/pipeline.js';
9
+
10
+ let chrome;
11
+ try { chrome = findChrome(); } catch {}
12
+ test('playback check uses camera zoom and ignores the half-size QA canvas', { skip: !chrome }, async () => {
13
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'explainroo-readability-'));
14
+ try {
15
+ fs.writeFileSync(path.join(dir, 'video.json'), JSON.stringify({ theme: 'clean', captions: false, music: false, sfx: false }));
16
+ fs.writeFileSync(path.join(dir, 'script.md'), '## demo\nA query.');
17
+ fs.writeFileSync(path.join(dir, 'scenes.js'), `export default { demo(s) {
18
+ s.camera([{ at: 1, x: 960, y: 540, zoom: 2, dur: 0.1 }]);
19
+ s.ui.text('SELECT item FROM orders;', 800, 500, {size:24});
20
+ } };`);
21
+ const project = loadProject(dir);
22
+ const timeline = { pace: 1, duration: 2, frames: 60, fps: 30, width: 1920, height: 1080, transitionSeconds: 0,
23
+ scenes: [{ id: 'demo', index: 0, start: 0, dur: 2, words: [], marks: {}, voice: null, lead: 0, transition: 'none' }] };
24
+ for (const scale of [0.5, 1]) {
25
+ await withEngine(project, timeline, { scale }, async page => {
26
+ const issues = await page.evaluate(() => window.explainroo.check(0.25, 854));
27
+ const warnings = issues.filter(i => i.message.includes('player width'));
28
+ assert.equal(warnings.length, 1);
29
+ assert.equal(warnings[0].t, 0);
30
+ assert.match(warnings[0].message, /10.7px/);
31
+ const normal = await page.evaluate(() => window.explainroo.check(0.25));
32
+ assert.equal(normal.some(i => i.message.includes('player width')), false);
33
+ });
34
+ }
35
+ } finally { fs.rmSync(dir, { recursive: true, force: true }); }
36
+ });
@@ -0,0 +1,20 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { playbackTextIssue } from '../engine/readability.js';
4
+
5
+ const text = { text: 'SELECT item FROM orders;', size: 24, alpha: 1, x0: 100, y0: 200, x1: 800, y1: 230 };
6
+ test('full-HD terminal text needs enlarging for a 480p player', () => {
7
+ assert.equal(playbackTextIssue(text, 1920, 1080, 1920), null);
8
+ assert.match(playbackTextIssue(text, 1920, 1080, 854), /10.7px/);
9
+ assert.equal(playbackTextIssue({ ...text, size: 40 }, 1920, 1080, 854), null);
10
+ assert.equal(playbackTextIssue({ ...text, size: 24 * 2 }, 1920, 1080, 854), null);
11
+ });
12
+ test('fades and text outside the shot do not trigger playback warnings', () => {
13
+ assert.equal(playbackTextIssue({ ...text, alpha: 0.5 }, 1920, 1080, 854), null);
14
+ assert.equal(playbackTextIssue({ ...text, x0: 2000, x1: 2500 }, 1920, 1080, 854), null);
15
+ assert.match(playbackTextIssue({ ...text, x0: -100 }, 1920, 1080, 854), /enlarge/);
16
+ });
17
+ test('upscaling a player does not make small source text readable', () => {
18
+ assert.match(playbackTextIssue({ ...text, size: 12 }, 1920, 1080, 3840), /12.0px/);
19
+ for (const w of [0, -1, Infinity, NaN]) assert.throws(() => playbackTextIssue(text, 1920, 1080, w), /positive/);
20
+ });
@@ -0,0 +1,52 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import path from 'node:path';
4
+ import { renderFormat, renderOutput } from '../src/render.js';
5
+ import { normalizeConfig } from '../src/project.js';
6
+ import { resolveTimes, verify } from '../src/qa.js';
7
+
8
+ test('render accepts mp4, gif, webm and mov with matching output extensions', () => {
9
+ assert.equal(renderFormat(), 'mp4');
10
+ assert.equal(renderFormat({ out: 'x/y.mp4' }), 'mp4');
11
+ assert.equal(renderFormat({ format: 'gif', out: 'x/y.gif' }), 'gif');
12
+ assert.equal(renderFormat({ format: 'mp4', out: 'x/y.MP4' }), 'mp4');
13
+ for (const format of ['webm', 'mov']) {
14
+ assert.equal(renderFormat({ format, out: `x/y.${format}` }), format);
15
+ assert.equal(renderFormat({ format, transparent: true }), format);
16
+ assert.throws(() => renderFormat({ format, out: 'x/y.mp4' }), new RegExp(`\\.${format}`));
17
+ }
18
+ assert.throws(() => renderFormat({ format: 'avi' }), /mp4, gif, webm or mov/);
19
+ assert.throws(() => renderFormat({ out: 'x/y.gif' }), /\.mp4/);
20
+ assert.throws(() => renderFormat({ format: 'gif', out: 'x/y.mp4' }), /\.gif/);
21
+ assert.throws(() => renderFormat({ out: 'x/y' }), /\.mp4/);
22
+ });
23
+
24
+ test('transparent export needs an alpha format', () => {
25
+ for (const format of [undefined, 'mp4', 'gif']) {
26
+ assert.throws(() => renderFormat({ format, transparent: true }), /--format webm or --format mov/);
27
+ }
28
+ });
29
+
30
+ test('render output names follow format, draft and --out', () => {
31
+ for (const format of ['mp4', 'gif', 'webm', 'mov']) {
32
+ assert.equal(renderOutput({ paths: { out: '/tmp/out' } }, { format }), path.resolve(`/tmp/out/video.${format}`));
33
+ assert.equal(renderOutput({ paths: { out: '/tmp/out' } }, { format, draft: true }), path.resolve(`/tmp/out/draft.${format}`));
34
+ assert.equal(renderOutput({ paths: { out: '/tmp/out' } }, { format, draft: true, out: `/tmp/custom.${format}` }), path.resolve(`/tmp/custom.${format}`));
35
+ }
36
+ assert.equal(renderOutput({ paths: { out: '/tmp/out' } }), path.resolve('/tmp/out/video.mp4'));
37
+ });
38
+
39
+ test('cover validates its type and syntax while allowing still specs', () => {
40
+ assert.equal(normalizeConfig({}).cover, 'auto');
41
+ for (const cover of [0, 12.5, '12.5', 'intro', 'intro@2.4', 'intro@end', 'intro@start']) assert.equal(normalizeConfig({ cover }).cover, cover);
42
+ for (const cover of [false, null, -1, Infinity, {}, '', 'intro@bad', 'intro@2@3']) assert.throws(() => normalizeConfig({ cover }), /cover must be/);
43
+ const timeline = { duration: 8, fps: 30, scenes: [{ id: 'intro', start: 0, dur: 3 }, { id: 'next', start: 3, dur: 5 }] };
44
+ assert.equal(resolveTimes(timeline, ['intro@end'])[0].T, 2.7);
45
+ assert.equal(resolveTimes(timeline, ['next@2.4'])[0].T, 5.4);
46
+ assert.equal(resolveTimes(timeline, [1e21])[0].T, 8 - 1 / 30);
47
+ assert.throws(() => resolveTimes(timeline, ['missing']), /no scene "missing".*intro, next/);
48
+ });
49
+
50
+ test('verify explains that GIF is unsupported', async () => {
51
+ await assert.rejects(verify({ paths: { out: '/tmp' } }, { file: '/tmp/video.gif' }), /verify checks MP4, WebM and MOV files/);
52
+ });
@@ -0,0 +1,67 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { parseScript, speechChunks, resolveMarks, tokenizeNarration, ScriptError } from '../src/script.js';
4
+
5
+ test('parses title, scenes, attributes and narration', () => {
6
+ const r = parseScript('# My video\n\n## intro {hold=1.5 transition=cut}\nHello there.\n\n## next\nBye.');
7
+ assert.equal(r.title, 'My video');
8
+ assert.deepEqual(r.scenes.map((s) => s.id), ['intro', 'next']);
9
+ assert.deepEqual(r.scenes[0].attrs, { hold: 1.5, transition: 'cut' });
10
+ assert.equal(r.scenes[0].text, 'Hello there.');
11
+ });
12
+
13
+ test('quoted attributes keep spaces and commas with either kind of quote', () => {
14
+ const r = parseScript('## a {hold="1.5", chapter="How the resolver finds it" transition=cut}\nHi.\n## b {chapter=\'More, with spaces\' min:3}\nBye.');
15
+ assert.deepEqual(r.scenes[0].attrs, { hold: 1.5, chapter: 'How the resolver finds it', transition: 'cut' });
16
+ assert.deepEqual(r.scenes[1].attrs, { chapter: 'More, with spaces', min: 3 });
17
+ assert.deepEqual(parseScript('## old {hold=1.5,min:3 lead=0 transition=fade}\nHi.').scenes[0].attrs, { hold: 1.5, min: 3, lead: 0, transition: 'fade' });
18
+ assert.equal(parseScript("## old {note=don't}\nHi.").scenes[0].attrs.note, "don't");
19
+ });
20
+
21
+ test('chapter titles must have 1 to 60 characters', () => {
22
+ for (const title of ['', ' ', 'x'.repeat(61)]) assert.throws(() => parseScript(`## a {chapter="${title}"}\nHi.`), /chapter title must be 1 to 60 characters/);
23
+ assert.equal(parseScript(`## a {chapter="${'x'.repeat(60)}"}\nHi.`).scenes[0].attrs.chapter.length, 60);
24
+ });
25
+
26
+ test('markers, pauses and pronunciation overrides', () => {
27
+ const units = tokenizeNarration('The {DNS|D N S} lookup, [pause 0.4] takes [#fast] {20ms|twenty milliseconds}.');
28
+ const words = units.filter((u) => u.type === 'word');
29
+ assert.deepEqual(words.map((w) => w.display), ['The', 'DNS', 'lookup,', 'takes', '20ms.']);
30
+ assert.deepEqual(words.map((w) => w.spoken), ['The', 'D N S', 'lookup,', 'takes', 'twenty milliseconds.']);
31
+ assert.ok(units.some((u) => u.type === 'pause' && u.sec === 0.4));
32
+ assert.ok(units.some((u) => u.type === 'mark' && u.name === 'fast'));
33
+ });
34
+
35
+ test('comments and director notes are ignored', () => {
36
+ const r = parseScript('## a\n> note for me\nSpoken <!-- not spoken --> words.\n<!--\nblock\n-->\nMore.');
37
+ assert.equal(r.scenes[0].text, 'Spoken words. More.');
38
+ });
39
+
40
+ test('scenes without narration are allowed', () => {
41
+ const r = parseScript('## title {min=3}\n\n## body\nText.');
42
+ assert.equal(r.scenes[0].units.length, 0);
43
+ assert.equal(r.scenes[0].attrs.min, 3);
44
+ });
45
+
46
+ test('errors carry line numbers', () => {
47
+ assert.throws(() => parseScript('Hello\n## a\nx'), (e) => e instanceof ScriptError && /line 1/.test(e.message));
48
+ assert.throws(() => parseScript('## a\nx\n## a\ny'), /used twice/);
49
+ assert.throws(() => parseScript('## a\nBad {override} here'), /\{shown\|spoken\}/);
50
+ assert.throws(() => parseScript('## a b\nx'), /scene headings/);
51
+ });
52
+
53
+ test('speech chunks split on sentences and pauses', () => {
54
+ const { scenes } = parseScript('## a\nOne two. Three four! [pause 1] Five,\n\nSix.');
55
+ const chunks = speechChunks(scenes[0].units, { sentenceGap: 0.3, paragraphGap: 0.55 });
56
+ assert.deepEqual(chunks.map((c) => c.text), ['One two.', 'Three four!', 'Five,', 'Six.']);
57
+ assert.deepEqual(chunks.map((c) => Math.round(c.gapBefore * 100) / 100), [0, 0.3, 1.3, 0.55]);
58
+ });
59
+
60
+ test('marks resolve to the next word, or the last word end', () => {
61
+ const units = tokenizeNarration('[#start] Hello [#mid] world. [#end]');
62
+ const times = [];
63
+ units.forEach((u, i) => {
64
+ if (u.type === 'word') times[i] = u.display === 'Hello' ? { start: 0.1, end: 0.5 } : { start: 0.6, end: 1.1 };
65
+ });
66
+ assert.deepEqual(resolveMarks(units, times), { start: 0.1, mid: 0.6, end: 1.1 });
67
+ });
@@ -0,0 +1,256 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import fs from 'node:fs';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import http from 'node:http';
7
+ import { startServer } from '../src/server.js';
8
+ import { mathHash, MAX_TEX_LENGTH } from '../src/math.js';
9
+
10
+ function request(port, { method = 'GET', path: p, host = `127.0.0.1:${port}`, origin, body }) {
11
+ return new Promise((resolve, reject) => {
12
+ const headers = { host };
13
+ if (origin) headers.origin = origin;
14
+ const req = http.request({ host: '127.0.0.1', port, method, path: p, headers }, (res) => {
15
+ let data = '';
16
+ res.on('data', (c) => (data += c));
17
+ res.on('end', () => resolve({ status: res.statusCode, body: data }));
18
+ });
19
+ req.on('error', reject);
20
+ req.end(body);
21
+ });
22
+ }
23
+
24
+ test('math only accepts bounded GET input and keeps the cache inside build/math', async t => {
25
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'explainroo-math-server-'));
26
+ const outside = fs.mkdtempSync(path.join(os.tmpdir(), 'explainroo-math-outside-'));
27
+ t.after(() => { fs.rmSync(dir, { recursive: true, force: true }); fs.rmSync(outside, { recursive: true, force: true }); });
28
+ const server = await startServer({ projectDir: dir, getState: () => ({}) });
29
+ t.after(() => server.close());
30
+ const get = (tex, extra = {}) => request(server.port, { path: `/__math?tex=${encodeURIComponent(tex)}`, ...extra });
31
+ const result = await get(String.raw`\frac{1}{2}`);
32
+ assert.equal(result.status, 200);
33
+ assert.equal(JSON.parse(result.body).paths.length, 3);
34
+ const file = path.join(dir, 'build/math', `${mathHash(String.raw`\frac{1}{2}`)}.json`);
35
+ assert.equal(fs.readFileSync(file, 'utf8'), result.body);
36
+ assert.equal((await get('x'.repeat(MAX_TEX_LENGTH))).status, 200);
37
+ assert.equal((await get('x'.repeat(MAX_TEX_LENGTH + 1))).status, 400);
38
+ assert.equal((await request(server.port, { path: '/__math' })).status, 400);
39
+ assert.equal((await request(server.port, { path: '/__math?tex=x&display=false' })).status, 400);
40
+ for (const method of ['POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS']) assert.equal((await get('x', { method })).status, 405);
41
+ assert.equal((await get('x', { host: 'evil.example:80' })).status, 403);
42
+ assert.equal((await get('x', { origin: 'https://evil.example' })).status, 403);
43
+ const bad = await get(String.raw`\unknown`);
44
+ assert.equal(bad.status, 400);
45
+ assert.match(JSON.parse(bad.body).error, /Undefined control sequence/);
46
+ assert.equal((await request(server.port, { path: '/project/build/math/' + path.basename(file) })).status, 404);
47
+ fs.symlinkSync(path.join(outside, 'cache.json'), path.join(dir, 'build/math', `${mathHash('x')}.json`));
48
+ assert.equal((await get('x')).status, 400);
49
+ assert.deepEqual(fs.readdirSync(outside), []);
50
+ fs.rmSync(path.join(dir, 'build/math'), { recursive: true });
51
+ fs.symlinkSync(outside, path.join(dir, 'build/math'));
52
+ assert.equal((await get('x')).status, 400);
53
+ assert.deepEqual(fs.readdirSync(outside), []);
54
+ });
55
+
56
+ test('chunked uploads are assembled in order and appear only when complete', async () => {
57
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'explainroo-chunks-'));
58
+ fs.writeFileSync(path.join(dir, 'scenes.js'), 'export default {};');
59
+ const server = await startServer({ projectDir: dir, getState: () => ({}) });
60
+ try {
61
+ const port = server.port;
62
+ const put = (p, query, body) => request(port, { method: 'PUT', path: `/__upload?path=${encodeURIComponent(p)}${query}`, body });
63
+ const piece = (p, id, offset, body, last = false) => put(p, `&upload=${id}&offset=${offset}${last ? '&last=1' : ''}`, body);
64
+ const A = 'a'.repeat(32);
65
+ const B = 'b'.repeat(32);
66
+ const build = path.join(dir, 'build');
67
+ const mix = path.join(build, 'mix.wav');
68
+ const parts = () => fs.readdirSync(build).filter((n) => n.endsWith('.part'));
69
+ fs.mkdirSync(build, { recursive: true });
70
+ fs.writeFileSync(mix, 'old');
71
+
72
+ // pieces in order; the existing file is replaced only by the last piece
73
+ assert.equal((await piece('build/mix.wav', A, 0, 'aaa')).status, 200);
74
+ assert.equal((await piece('build/mix.wav', A, 3, 'bbb')).status, 200);
75
+ assert.equal(fs.readFileSync(mix, 'utf8'), 'old');
76
+ // a piece at the wrong offset is refused and changes nothing
77
+ assert.equal((await piece('build/mix.wav', A, 99, 'zzz')).status, 409);
78
+ assert.equal((await piece('build/mix.wav', A, 6, 'cc', true)).status, 200);
79
+ assert.equal(fs.readFileSync(mix, 'utf8'), 'aaabbbcc');
80
+ assert.deepEqual(parts(), []);
81
+
82
+ // uploading the same file again replaces the completed destination
83
+ assert.equal((await piece('build/mix.wav', B, 0, 'second', true)).status, 200);
84
+ assert.equal(fs.readFileSync(mix, 'utf8'), 'second');
85
+
86
+ // an interrupted upload leaves only its part file; a new upload starts clean
87
+ assert.equal((await piece('build/mix.wav', A, 0, 'xxxx')).status, 200);
88
+ assert.equal((await piece('build/mix.wav', A, 4, 'yyyy')).status, 200);
89
+ assert.equal(fs.readFileSync(mix, 'utf8'), 'second');
90
+ assert.equal((await piece('build/mix.wav', B, 0, 'fresh')).status, 200);
91
+ assert.equal((await piece('build/mix.wav', B, 5, '!', true)).status, 200);
92
+ assert.equal(fs.readFileSync(mix, 'utf8'), 'fresh!');
93
+ assert.deepEqual(parts(), []);
94
+
95
+ // a competing upload never mixes bytes: the older one fails, the newer one wins
96
+ assert.equal((await piece('build/mix.wav', A, 0, '1111')).status, 200);
97
+ assert.equal((await piece('build/mix.wav', B, 0, '2222')).status, 200);
98
+ assert.equal((await piece('build/mix.wav', A, 4, '1111', true)).status, 409);
99
+ assert.equal(fs.readFileSync(mix, 'utf8'), 'fresh!');
100
+ assert.equal((await piece('build/mix.wav', B, 4, '2222', true)).status, 200);
101
+ assert.equal(fs.readFileSync(mix, 'utf8'), '22222222');
102
+
103
+ // a continuation without a started upload is refused
104
+ assert.equal((await piece('build/other.wav', A, 5, 'x', true)).status, 409);
105
+ assert.ok(!fs.existsSync(path.join(build, 'other.wav')));
106
+ // malformed offsets and upload ids are refused
107
+ for (const bad of ['-1', '1.5', 'abc', '']) assert.equal((await put('build/mix.wav', `&upload=${A}&offset=${bad}`, 'x')).status, 400);
108
+ for (const bad of ['', 'short', '../../scenes', 'A'.repeat(32), 'g'.repeat(32)]) assert.equal((await put('build/mix.wav', `&upload=${bad}&offset=0`, 'x')).status, 400);
109
+ // an empty upload is one last piece at offset 0
110
+ assert.equal((await piece('out/empty.bin', A, 0, '', true)).status, 200);
111
+ assert.equal(fs.statSync(path.join(dir, 'out', 'empty.bin')).size, 0);
112
+ // the single-request form keeps working, under the same path rules
113
+ assert.equal((await put('out/single.txt', '', 'one')).status, 200);
114
+ assert.equal(fs.readFileSync(path.join(dir, 'out', 'single.txt'), 'utf8'), 'one');
115
+
116
+ // chunked uploads obey the same path rules, and the part file cannot be a symlink
117
+ assert.equal((await piece('build/../scenes.js', A, 0, 'x', true)).status, 400);
118
+ assert.equal((await piece('assets/x.png', A, 0, 'x', true)).status, 400);
119
+ assert.equal(fs.readFileSync(path.join(dir, 'scenes.js'), 'utf8'), 'export default {};');
120
+ fs.symlinkSync(path.join(dir, 'scenes.js'), path.join(build, `trap.wav.${A}.part`));
121
+ assert.equal((await piece('build/trap.wav', A, 0, 'x', true)).status, 400);
122
+ assert.equal(fs.readFileSync(path.join(dir, 'scenes.js'), 'utf8'), 'export default {};');
123
+ // removing a stale part never follows a symlink to its target
124
+ assert.equal((await piece('build/trap.wav', B, 0, 'x', true)).status, 200);
125
+ assert.equal(fs.readFileSync(path.join(dir, 'scenes.js'), 'utf8'), 'export default {};');
126
+ } finally {
127
+ await server.close();
128
+ fs.rmSync(dir, { recursive: true, force: true });
129
+ }
130
+ });
131
+
132
+ test('the preview server keeps secrets and sources safe', async () => {
133
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'explainroo-server-'));
134
+ fs.writeFileSync(path.join(dir, '.env'), 'OPENROUTER_API_KEY=secret');
135
+ fs.writeFileSync(path.join(dir, 'notes.md'), 'private');
136
+ fs.writeFileSync(path.join(dir, 'scenes.js'), 'export default {};');
137
+ fs.mkdirSync(path.join(dir, 'assets'));
138
+ fs.writeFileSync(path.join(dir, 'assets', 'logo.png'), 'png');
139
+ fs.mkdirSync(path.join(dir, 'assets', 'fonts'));
140
+ fs.writeFileSync(path.join(dir, 'assets', 'fonts', 'Brand-600.woff2'), 'font');
141
+ fs.writeFileSync(path.join(dir, 'assets', 'fonts', 'OFL.txt'), 'license');
142
+ const server = await startServer({ projectDir: dir, getState: () => ({}) });
143
+ try {
144
+ const port = server.port;
145
+ assert.equal((await request(port, { path: '/project/scenes.js' })).status, 200);
146
+ assert.equal((await request(port, { path: '/project/assets/logo.png' })).status, 200);
147
+ assert.equal((await request(port, { path: '/project/assets/fonts/Brand-600.woff2' })).status, 200);
148
+ assert.equal((await request(port, { path: '/project/assets/fonts/OFL.txt' })).status, 404);
149
+ assert.equal((await request(port, { path: '/project/.env' })).status, 404);
150
+ assert.equal((await request(port, { path: '/project/notes.md' })).status, 404);
151
+ assert.equal((await request(port, { path: '/project/assets/../.env' })).status, 404);
152
+ assert.equal((await request(port, { path: '/project/scenes.js', host: 'evil.example:80' })).status, 403);
153
+ assert.equal((await request(port, { path: '/project/scenes.js', origin: 'http://evil.example' })).status, 403);
154
+ const put = (p) => request(port, { method: 'PUT', path: `/__upload?path=${encodeURIComponent(p)}`, body: 'x' });
155
+ assert.equal((await put('build/../scenes.js')).status, 400);
156
+ assert.equal(fs.readFileSync(path.join(dir, 'scenes.js'), 'utf8'), 'export default {};');
157
+ assert.equal((await put('out/stills/a.png')).status, 200);
158
+ assert.ok(fs.existsSync(path.join(dir, 'out', 'stills', 'a.png')));
159
+
160
+ // Symlinks do not get around the rules.
161
+ const outside = fs.mkdtempSync(path.join(os.tmpdir(), 'explainroo-outside-'));
162
+ fs.writeFileSync(path.join(outside, 'secret.png'), 'outside');
163
+ fs.symlinkSync(path.join(dir, '.env'), path.join(dir, 'assets', 'alias.png'));
164
+ fs.symlinkSync(path.join(outside, 'secret.png'), path.join(dir, 'assets', 'away.png'));
165
+ assert.equal((await request(port, { path: '/project/assets/alias.png' })).status, 404);
166
+ assert.equal((await request(port, { path: '/project/assets/away.png' })).status, 404);
167
+ fs.mkdirSync(path.join(dir, 'build'), { recursive: true });
168
+ fs.symlinkSync(path.join(dir, 'scenes.js'), path.join(dir, 'build', 'source-link.js'));
169
+ assert.equal((await put('build/source-link.js')).status, 400);
170
+ assert.equal(fs.readFileSync(path.join(dir, 'scenes.js'), 'utf8'), 'export default {};');
171
+ fs.symlinkSync(path.join(outside, 'new.png'), path.join(dir, 'build', 'dangling.png'));
172
+ assert.equal((await put('build/dangling.png')).status, 400);
173
+ assert.ok(!fs.existsSync(path.join(outside, 'new.png')));
174
+ fs.symlinkSync(outside, path.join(dir, 'out', 'elsewhere'));
175
+ assert.equal((await put('out/elsewhere/x.png')).status, 400);
176
+ assert.ok(!fs.existsSync(path.join(outside, 'x.png')));
177
+ fs.rmSync(outside, { recursive: true, force: true });
178
+ } finally {
179
+ await server.close();
180
+ fs.rmSync(dir, { recursive: true, force: true });
181
+ }
182
+ });
183
+
184
+ test('clip endpoints only read video assets and keep the cache inside build', async () => {
185
+ const { ffmpeg } = await import('../src/ffmpeg.js');
186
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'explainroo-clip-server-'));
187
+ const outside = fs.mkdtempSync(path.join(os.tmpdir(), 'explainroo-clip-outside-'));
188
+ fs.mkdirSync(path.join(dir, 'assets'));
189
+ const video = path.join(dir, 'assets/ok.mp4');
190
+ await ffmpeg(['-loglevel', 'error', '-f', 'lavfi', '-i', 'color=red:s=80x60:r=30:d=0.2', '-f', 'lavfi', '-i', 'sine=frequency=440:duration=0.2', '-shortest', '-c:v', 'libx264', '-threads', '1', '-c:a', 'aac', video]);
191
+ fs.copyFileSync(video, path.join(outside, 'away.mp4'));
192
+ fs.copyFileSync(video, path.join(dir, 'root.mp4'));
193
+ fs.copyFileSync(video, path.join(dir, 'assets/.private.mp4'));
194
+ fs.mkdirSync(path.join(dir, 'assets/.hidden'));
195
+ fs.copyFileSync(video, path.join(dir, 'assets/.hidden/ok.mp4'));
196
+ fs.writeFileSync(path.join(dir, 'assets/notes.md'), 'secret');
197
+ fs.symlinkSync(path.join(outside, 'away.mp4'), path.join(dir, 'assets/away.mp4'));
198
+ fs.symlinkSync(path.join(dir, 'root.mp4'), path.join(dir, 'assets/root-link.mp4'));
199
+ fs.symlinkSync(path.join(dir, 'assets/.private.mp4'), path.join(dir, 'assets/hidden-link.mp4'));
200
+ fs.symlinkSync(path.join(dir, 'assets/notes.md'), path.join(dir, 'assets/notes.mp4'));
201
+ const server = await startServer({ projectDir: dir, getState: () => ({ config: { fps: 30 } }) });
202
+ try {
203
+ for (const endpoint of ['info', 'frame', 'audio']) {
204
+ const url = (p) => `/__clip/${endpoint}?path=${encodeURIComponent(p)}`;
205
+ for (const p of ['root.mp4', 'assets/../root.mp4', 'assets/missing.mp4', 'assets/notes.md', 'assets/ok.avi', 'assets/.private.mp4', 'assets/.hidden/ok.mp4', 'assets/away.mp4', 'assets/root-link.mp4', 'assets/hidden-link.mp4', 'assets/notes.mp4', '../away.mp4', video]) {
206
+ assert.equal((await request(server.port, { path: url(p) })).status, 404, `${endpoint}: ${p}`);
207
+ }
208
+ assert.equal((await request(server.port, { path: url('assets/ok.mp4'), host: 'evil.example' })).status, 403);
209
+ assert.equal((await request(server.port, { path: url('assets/ok.mp4'), origin: 'http://evil.example' })).status, 403);
210
+ assert.equal((await request(server.port, { path: url('assets/ok.mp4'), method: 'POST' })).status, 405);
211
+ }
212
+ for (const [ext, mime] of [['mp4', 'video/mp4'], ['mov', 'video/quicktime'], ['webm', 'video/webm'], ['m4v', 'video/mp4']]) {
213
+ fs.copyFileSync(video, path.join(dir, `assets/type.${ext}`));
214
+ const response = await fetch(`${server.url}/project/assets/type.${ext}`);
215
+ assert.equal(response.status, 200);
216
+ assert.equal(response.headers.get('content-type'), mime);
217
+ await response.arrayBuffer();
218
+ }
219
+ const responses = await Promise.all(Array.from({ length: 4 }, () => request(server.port, { path: '/__clip/info?path=assets/ok.mp4' })));
220
+ assert.ok(responses.every((r) => r.status === 200));
221
+ const info = JSON.parse(responses[0].body);
222
+ assert.deepEqual(info, { frames: 6, fps: 30, width: 80, height: 60, duration: 0.2, hasAudio: true });
223
+ assert.equal(fs.readdirSync(path.join(dir, 'build/clips')).length, 1);
224
+ const frame = await fetch(`${server.url}/__clip/frame?path=assets/ok.mp4&frame=999`);
225
+ assert.equal(frame.headers.get('content-type'), 'image/jpeg');
226
+ const last = await fetch(`${server.url}/__clip/frame?path=assets/ok.mp4&frame=5`);
227
+ assert.deepEqual(Buffer.from(await frame.arrayBuffer()), Buffer.from(await last.arrayBuffer()));
228
+ const audio = await fetch(`${server.url}/__clip/audio?path=assets/ok.mp4`);
229
+ assert.equal(audio.headers.get('content-type'), 'audio/wav');
230
+ assert.equal(Buffer.from(await audio.arrayBuffer()).toString('ascii', 0, 4), 'RIFF');
231
+ for (const frame of ['-1', '1.2', 'foo', '9007199254740992']) {
232
+ assert.equal((await request(server.port, { path: `/__clip/frame?path=assets/ok.mp4&frame=${frame}` })).status, 400);
233
+ }
234
+ const silent = path.join(dir, 'assets/silent.mp4');
235
+ await ffmpeg(['-loglevel', 'error', '-i', video, '-an', '-c:v', 'copy', silent]);
236
+ const silentInfo = await request(server.port, { path: '/__clip/info?path=assets/silent.mp4' });
237
+ assert.equal(JSON.parse(silentInfo.body).hasAudio, false);
238
+ assert.equal((await request(server.port, { path: '/__clip/audio?path=assets/silent.mp4' })).status, 404);
239
+ const hash = fs.readdirSync(path.join(dir, 'build/clips')).find((hash) => fs.existsSync(path.join(dir, 'build/clips', hash, 'audio.wav')));
240
+ const cached = path.join(dir, 'build/clips', hash, '00000000.jpg');
241
+ fs.unlinkSync(cached);
242
+ fs.symlinkSync(path.join(outside, 'away.mp4'), cached);
243
+ assert.equal((await request(server.port, { path: '/__clip/frame?path=assets/ok.mp4' })).status, 404);
244
+ fs.rmSync(path.join(dir, 'build/clips'), { recursive: true });
245
+ fs.symlinkSync(outside, path.join(dir, 'build/clips'));
246
+ const other = await startServer({ projectDir: dir, getState: () => ({ config: { fps: 30 } }) });
247
+ try {
248
+ assert.equal((await request(other.port, { path: '/__clip/info?path=assets/ok.mp4' })).status, 500);
249
+ assert.deepEqual(fs.readdirSync(outside), ['away.mp4']);
250
+ } finally { await other.close(); }
251
+ } finally {
252
+ await server.close();
253
+ fs.rmSync(dir, { recursive: true, force: true });
254
+ fs.rmSync(outside, { recursive: true, force: true });
255
+ }
256
+ });
@@ -0,0 +1,70 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import fs from 'node:fs';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import { findChrome } from '../src/browser.js';
7
+ import { loadProject } from '../src/project.js';
8
+ import { withEngine } from '../src/pipeline.js';
9
+
10
+ let chrome;
11
+ try { chrome = findChrome(); } catch {}
12
+ test('stills preserve PNG by default and support JPEG covers', { skip: !chrome }, async () => {
13
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'explainroo-stills-'));
14
+ try {
15
+ fs.writeFileSync(path.join(dir, 'video.json'), JSON.stringify({ theme: 'clean', size: '320x180', captions: false }));
16
+ fs.writeFileSync(path.join(dir, 'script.md'), '## intro\n');
17
+ fs.writeFileSync(path.join(dir, 'scenes.js'), 'export default { intro(s) { s.bg("#245cff"); } };');
18
+ const project = loadProject(dir);
19
+ const timeline = { duration: 1, frames: 30, fps: 30, width: 320, height: 180, transitionSeconds: 0,
20
+ scenes: [{ id: 'intro', index: 0, start: 0, dur: 1, words: [], marks: {}, voice: null, transition: 'none' }] };
21
+ await withEngine(project, timeline, {}, async (page) => {
22
+ await page.evaluate(() => window.explainroo.stills([{ T: 0.5, path: 'out/still.png' }]));
23
+ await page.evaluate(() => window.explainroo.stills([{ T: 0.5, path: 'build/cover.jpg' }], { type: 'image/jpeg', quality: 0.9 }));
24
+ const dimensions = await page.evaluate(async () => {
25
+ const img = new Image();
26
+ img.src = '/project/build/cover.jpg';
27
+ await img.decode();
28
+ return [img.naturalWidth, img.naturalHeight];
29
+ });
30
+ assert.deepEqual(dimensions, [320, 180]);
31
+ });
32
+ assert.equal(fs.readFileSync(path.join(dir, 'out/still.png')).subarray(1, 4).toString(), 'PNG');
33
+ assert.deepEqual([...fs.readFileSync(path.join(dir, 'build/cover.jpg')).subarray(0, 3)], [0xff, 0xd8, 0xff]);
34
+ } finally { fs.rmSync(dir, { recursive: true, force: true }); }
35
+ });
36
+
37
+ test('covers leave out the burned-in captions', { skip: !chrome }, async () => {
38
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'explainroo-cover-captions-'));
39
+ try {
40
+ fs.writeFileSync(path.join(dir, 'video.json'), JSON.stringify({ theme: 'clean', size: '640x360', captions: true, watermark: false }));
41
+ fs.writeFileSync(path.join(dir, 'script.md'), '## intro\nHello there.\n');
42
+ fs.writeFileSync(path.join(dir, 'scenes.js'), 'export default { intro(s) { s.bg("#ffffff"); } };');
43
+ const project = loadProject(dir);
44
+ const timeline = { duration: 2, frames: 60, fps: 30, width: 640, height: 360, transitionSeconds: 0,
45
+ scenes: [{ id: 'intro', index: 0, start: 0, dur: 2, marks: {}, voice: null, transition: 'none',
46
+ words: [{ text: 'Hello', start: 0.2, end: 0.6 }, { text: 'there.', start: 0.7, end: 1.1 }] }] };
47
+ await withEngine(project, timeline, {}, async (page) => {
48
+ const dark = await page.evaluate(async () => {
49
+ const count = async (options) => {
50
+ await window.explainroo.stills([{ T: 0.9, path: 'out/c.png' }], options);
51
+ const img = new Image();
52
+ img.src = '/project/out/c.png?' + Math.random();
53
+ await img.decode();
54
+ const c = document.createElement('canvas');
55
+ c.width = img.naturalWidth;
56
+ c.height = img.naturalHeight;
57
+ const g = c.getContext('2d');
58
+ g.drawImage(img, 0, 0);
59
+ const d = g.getImageData(0, 0, c.width, c.height).data;
60
+ let n = 0;
61
+ for (let i = 0; i < d.length; i += 4) if (d[i] < 128) n++;
62
+ return n;
63
+ };
64
+ return { with: await count({}), without: await count({ captions: false }) };
65
+ });
66
+ assert.ok(dark.with > 100, `captions should draw dark pixels, got ${dark.with}`);
67
+ assert.equal(dark.without, 0);
68
+ });
69
+ } finally { fs.rmSync(dir, { recursive: true, force: true }); }
70
+ });
@@ -0,0 +1,83 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { subtitleCues, toSrt, toVtt } from '../src/subtitles.js';
4
+ import { parseScript } from '../src/script.js';
5
+ import { buildTimeline } from '../src/timeline.js';
6
+ import { normalizeConfig } from '../src/project.js';
7
+
8
+ function scene(text, { start = 0, step = 0.3, dur = 100 } = {}) {
9
+ return { start, dur, words: text.split(' ').map((text, i) => ({ text, start: i * step, end: i * step + 0.2 })) };
10
+ }
11
+
12
+ test('cues keep every word inside two 42-character lines and six seconds', () => {
13
+ for (const step of [0.15, 0.7]) {
14
+ const sc = scene('This longer explanation contains several ordinary words and keeps going until both lines fill up with enough text to need another cue before the sentence ends.', { step });
15
+ const cues = subtitleCues({ scenes: [sc] });
16
+ assert.ok(cues.length > 1);
17
+ assert.equal(cues.map((c) => c.text.replaceAll('\n', ' ')).join(' '), sc.words.map((w) => w.text).join(' '));
18
+ for (const c of cues) {
19
+ assert.ok(c.end - c.start <= 6);
20
+ assert.ok(c.text.split('\n').length <= 2);
21
+ assert.ok(c.text.split('\n').every((line) => line.length <= 42));
22
+ }
23
+ }
24
+ });
25
+
26
+ test('sentence endings start new cues and padding stops at the next cue', () => {
27
+ const cues = subtitleCues({ scenes: [scene('Hello there. Another sentence! Last one?')] });
28
+ assert.deepEqual(cues.map((c) => c.text), ['Hello there.', 'Another sentence!', 'Last one?']);
29
+ assert.equal(cues[0].end, cues[1].start);
30
+ assert.equal(cues[2].end, 1.95);
31
+ });
32
+
33
+ test('line and cue breaks prefer commas and other punctuation', () => {
34
+ const line = subtitleCues({ scenes: [scene('Here is a useful clause, followed by more words for the second line')] });
35
+ assert.equal(line[0].text, 'Here is a useful clause,\nfollowed by more words for the second line');
36
+ const cues = subtitleCues({ scenes: [scene('Here is a useful clause, followed by several words that keep going until the subtitle has no room left for more words')] });
37
+ assert.equal(cues[0].text, 'Here is a useful clause,');
38
+ const orphan = subtitleCues({ scenes: [scene("Computers don't find each other by name, though.")] });
39
+ assert.equal(orphan[0].text, "Computers don't find each\nother by name, though.");
40
+ });
41
+
42
+ test('pauses over 0.6 seconds and scene boundaries start new cues', () => {
43
+ const sc = { start: 2, dur: 4, words: [
44
+ { text: 'One', start: 0, end: 0.2 },
45
+ { text: 'two', start: 0.8, end: 1 },
46
+ { text: 'three', start: 1.61, end: 1.9 },
47
+ ] };
48
+ const cues = subtitleCues({ scenes: [sc, scene('Next', { start: 6, dur: 0.3 })] });
49
+ assert.deepEqual(cues.map((c) => c.text), ['One two', 'three', 'Next']);
50
+ assert.equal(cues[0].start, 2);
51
+ assert.equal(cues[0].end, 3.25);
52
+ assert.equal(cues.at(-1).end, 6.3);
53
+ });
54
+
55
+ test('partial ranges keep overlapping cues, clip times and start at clip zero', () => {
56
+ const timeline = { scenes: [scene('First. Second. Third.', { step: 1 })] };
57
+ assert.deepEqual(subtitleCues(timeline, { from: 0.2, to: 1.2 }), [
58
+ { start: 0, end: 0.25, text: 'First.' },
59
+ { start: 0.8, end: 1, text: 'Second.' },
60
+ ]);
61
+ assert.deepEqual(subtitleCues(timeline, { from: 3 }), []);
62
+ assert.deepEqual(subtitleCues({ scenes: [{ start: 0, words: [] }] }), []);
63
+ });
64
+
65
+ test('subtitles use the shown form from pronunciation overrides', () => {
66
+ const script = parseScript('## intro\nThe {DNS|D N S} answer.');
67
+ const words = script.scenes[0].units.map((u, i) => ({ text: u.display, start: i * 0.3, end: i * 0.3 + 0.2 }));
68
+ const timeline = buildTimeline({ script, config: normalizeConfig({}) }, { intro: { duration: 1, words } });
69
+ assert.equal(subtitleCues(timeline)[0].text, 'The DNS answer.');
70
+ });
71
+
72
+ test('an indivisible word over 42 characters gives a useful error', () => {
73
+ assert.throws(() => subtitleCues({ scenes: [scene('x'.repeat(43))] }), /Shorten its shown text/);
74
+ });
75
+
76
+ test('SRT and VTT have millisecond timestamps, UTF-8 text and LF endings', () => {
77
+ const cues = [{ start: 1.234, end: 3.5, text: 'Café\nline two' }, { start: 3661.9996, end: 3663, text: 'End.' }];
78
+ assert.equal(toSrt(cues), '1\n00:00:01,234 --> 00:00:03,500\nCafé\nline two\n\n2\n01:01:02,000 --> 01:01:03,000\nEnd.\n\n');
79
+ assert.equal(toVtt(cues), 'WEBVTT\n\n00:00:01.234 --> 00:00:03.500\nCafé\nline two\n\n01:01:02.000 --> 01:01:03.000\nEnd.\n\n');
80
+ assert.equal(toSrt([]), '');
81
+ assert.equal(toVtt([]), 'WEBVTT\n\n');
82
+ assert.match(toVtt([{ start: 0, end: 1, text: '<tag> & text' }]), /&lt;tag&gt; &amp; text/);
83
+ });
@@ -0,0 +1,31 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { parseScript } from '../src/script.js';
4
+ import { normalizeConfig } from '../src/project.js';
5
+ import { buildTimeline } from '../src/timeline.js';
6
+
7
+ function project(src, cfg = {}) {
8
+ return { config: normalizeConfig(cfg), script: parseScript(src) };
9
+ }
10
+
11
+ test('scene length is lead + voice + hold, snapped to frames', () => {
12
+ const p = project('## a\nHi.\n\n## b {min=4}\n', { fps: 30, lead: 0.3, hold: 0.5, end: 1 });
13
+ const tl = buildTimeline(p, { a: { duration: 1.234, words: [{ text: 'Hi.', start: 0, end: 1.2 }], marks: {}, chunks: [{ start: 0, end: 1.234 }] } });
14
+ assert.equal(tl.scenes[0].start, 0);
15
+ assert.equal(tl.scenes[0].dur, Math.round((0.3 + 1.234 + 0.5) * 30) / 30);
16
+ assert.equal(tl.scenes[1].start, tl.scenes[0].dur);
17
+ assert.equal(tl.scenes[1].dur, 5);
18
+ assert.equal(tl.scenes[0].words[0].start, 0.3);
19
+ assert.equal(tl.frames, Math.round(tl.duration * 30));
20
+ assert.equal(tl.voiceSpans.length, 1);
21
+ });
22
+
23
+ test('config validation gives useful errors', () => {
24
+ assert.throws(() => normalizeConfig({ theme: 'neon' }), /theme must be one of/);
25
+ assert.throws(() => normalizeConfig({ voice: 'nobody' }), /explainroo voices/);
26
+ assert.throws(() => normalizeConfig({ colour: 'red' }), /unknown setting/);
27
+ assert.equal(normalizeConfig({ size: '9:16' }).captions, true);
28
+ assert.equal(normalizeConfig({}).captions, false);
29
+ assert.equal(normalizeConfig({ theme: 'chalk' }).music.style, 'calm');
30
+ assert.equal(normalizeConfig({ music: false }).music, null);
31
+ });