feat(video-export): add deterministic Quiz question-list scrolling (#1102)

* feat(video-export): add deterministic Quiz question-list scrolling

* fix(video-export): bound Quiz layout measurement
This commit is contained in:
Yizuki_Ame 2026-08-12 10:31:13 +08:00 committed by GitHub
parent 4ac70cceeb
commit d1eeb95e1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
60 changed files with 2642 additions and 133 deletions

View File

@ -38,7 +38,7 @@ import {
VIDEO_QUALITIES,
VIDEO_RESOLUTIONS,
type VideoResolution,
} from '@/lib/video-export-app/build-export-zip';
} from '@/lib/video-export-app/export-options';
const RESOLUTIONS = Object.keys(VIDEO_RESOLUTIONS) as VideoResolution[];

View File

@ -490,6 +490,47 @@ const eslintConfig = defineConfig([
],
},
},
// Hyperframes emitter boundary. This subtree stays pure and backend-text-only:
// it may reach other video-export modules through one-level relatives, and it
// has one intentional app-module dependency on the existing pure Quiz math
// renderer so classroom and exported formulas cannot drift. Every other
// two-level escape is rejected.
{
files: ['lib/video-export/emit-hyperframes/**/*.{ts,tsx,js,jsx,mjs,cjs}'],
rules: {
'no-restricted-syntax': [
'error',
{
selector:
'ImportDeclaration > Literal.source[value=/^(?!\\.\\/|\\.\\.\\/(?!\\.\\.\\/)|\\.\\.\\/\\.\\.\\/quiz\\/math-text$).+/]',
message:
'The Hyperframes emitter may import only in-module relatives (./… or one ../…) and the shared pure Quiz renderer ../../quiz/math-text.',
},
{
selector:
'ExportNamedDeclaration > Literal.source[value=/^(?!\\.\\/|\\.\\.\\/(?!\\.\\.\\/)|\\.\\.\\/\\.\\.\\/quiz\\/math-text$).+/]',
message:
'The Hyperframes emitter may re-export only in-module relatives (./… or one ../…) and ../../quiz/math-text.',
},
{
selector:
'ExportAllDeclaration > Literal.source[value=/^(?!\\.\\/|\\.\\.\\/(?!\\.\\.\\/)|\\.\\.\\/\\.\\.\\/quiz\\/math-text$).+/]',
message:
'The Hyperframes emitter may re-export only in-module relatives (./… or one ../…) and ../../quiz/math-text.',
},
{
selector: 'ImportExpression',
message:
'The Hyperframes emitter must not use dynamic import() — it bypasses the static import allowlist.',
},
{
selector: "CallExpression[callee.name='require']",
message:
'The Hyperframes emitter must not use require() — it bypasses the static import allowlist.',
},
],
},
},
// PBL v2 operations boundary: leaf/shared kernel operations may be used by
// composite runtime operations, but the kernel must never reach back into
// operations/runtime. Match the module string in every literal form so

View File

@ -22,13 +22,12 @@ import { toast } from 'sonner';
import { createLogger } from '@/lib/logger';
import { runPolledTask } from '@/lib/media/polled-task';
import {
buildExportZip,
NoScenesError,
sanitizeFilename,
type VideoFps,
type VideoQuality,
type VideoResolution,
} from '@/lib/video-export-app/build-export-zip';
} from '@/lib/video-export-app/export-options';
import type { Locale } from '@/lib/i18n';
const log = createLogger('VideoRenderStore');
@ -133,6 +132,7 @@ export const useVideoRenderStore = create<VideoRenderState>()((set, get) => ({
let missingCount = 0;
let errorCount = 0;
try {
const { buildExportZip } = await import('@/lib/video-export-app/build-export-zip');
const built = await buildExportZip({ resolution, burnInSubtitles, locale });
({ zipBlob, stageName, missingCount, errorCount } = built);
} catch (error) {

View File

@ -21,24 +21,20 @@ import { accessDocument } from '@/lib/document-store';
import { createVideoTimelineDeps } from './timeline-deps';
import { collectVideoAssets } from './collect';
import { getVideoExportCoverLabels, resolveVideoExportCta } from './cover-config';
import { NoScenesError, VIDEO_RESOLUTIONS, type VideoResolution } from './export-options';
import { createQuizLayoutProbe } from './quiz-layout';
import { packageVideoZip } from './package-zip';
/** Selectable render resolutions (16:9). Width drives slide-snapshot render width too. */
export const VIDEO_RESOLUTIONS = {
'720p': { width: 1280, height: 720 },
'1080p': { width: 1920, height: 1080 },
'4k': { width: 3840, height: 2160 },
} as const;
export type VideoResolution = keyof typeof VIDEO_RESOLUTIONS;
/** Selectable frame rates for MP4 rendering. */
export const VIDEO_FPS = [24, 30, 60] as const;
export type VideoFps = (typeof VIDEO_FPS)[number];
/** Producer quality presets (speed vs fidelity). */
export const VIDEO_QUALITIES = ['draft', 'standard', 'high'] as const;
export type VideoQuality = (typeof VIDEO_QUALITIES)[number];
export {
NoScenesError,
sanitizeFilename,
VIDEO_FPS,
VIDEO_QUALITIES,
VIDEO_RESOLUTIONS,
type VideoFps,
type VideoQuality,
type VideoResolution,
} from './export-options';
export interface BuildExportZipResult {
zipBlob: Blob;
@ -49,8 +45,6 @@ export interface BuildExportZipResult {
errorCount: number;
}
export class NoScenesError extends Error {}
let warnedInvalidVideoExportCta = false;
/** Resolve the build-time public setting at the app boundary, warning once. */
@ -77,9 +71,13 @@ function configuredVideoExportCta() {
* build and the subtitles-only path go through here so their timing/assets/
* geometry wiring can never drift.
*/
async function compileStageIr(
options: { skipGeometry?: boolean; skipInteractiveHtml?: boolean } = {},
): Promise<{
async function compileStageIr(options: {
resolution: VideoResolution;
locale: Locale;
labels: ReturnType<typeof getVideoExportCoverLabels>;
skipGeometry?: boolean;
skipInteractiveHtml?: boolean;
}): Promise<{
ir: ReturnType<typeof compileVideoTimeline>;
stageName: string;
scenes: ReturnType<typeof useStageStore.getState>['scenes'];
@ -92,13 +90,23 @@ async function compileStageIr(
const latest = await accessDocument(stage.id).catch(() => undefined);
const stageName = latest?.document?.stage.name || stage.name || 'classroom';
const { width, height } = VIDEO_RESOLUTIONS[options.resolution];
const deps = await createVideoTimelineDeps({
stage: { id: stage.id },
scenes,
skipGeometry: options.skipGeometry,
skipInteractiveHtml: options.skipInteractiveHtml,
});
const [deps, quizLayout] = await Promise.all([
createVideoTimelineDeps({
stage: { id: stage.id },
scenes,
skipGeometry: options.skipGeometry,
skipInteractiveHtml: options.skipInteractiveHtml,
}),
createQuizLayoutProbe({
scenes,
width,
height,
locale: options.locale,
labels: options.labels,
}),
]);
const ir = compileVideoTimeline(
{ stage: { id: stage.id, name: stageName }, scenes },
{
@ -106,6 +114,7 @@ async function compileStageIr(
assets: deps.assets,
geometry: deps.geometry,
interactive: deps.interactive,
quizLayout,
},
);
@ -139,7 +148,11 @@ export async function buildExportZip(
const cta = configuredVideoExportCta();
// 1. DI deps (Dexie durations + asset presence + measured geometry) → 2. pure compile.
const { ir, stageName, scenes, deps } = await compileStageIr();
const { ir, stageName, scenes, deps } = await compileStageIr({
resolution,
locale,
labels,
});
// 3. emit the Hyperframes project text.
const project = emitHyperframes(ir, {
@ -168,10 +181,6 @@ export async function buildExportZip(
};
}
export function sanitizeFilename(name: string): string {
return name.replace(/[\\/:*?"<>|]/g, '_') || 'classroom';
}
export interface CompiledSubtitles {
srt: string;
vtt: string;
@ -192,8 +201,20 @@ export interface CompiledSubtitles {
* subtitles need only the timeline, and audio/video *duration* probes still run
* so these cues match the ones the burned-in video would carry.
*/
export async function compileSubtitles(): Promise<CompiledSubtitles> {
export interface CompileSubtitlesOptions {
resolution: VideoResolution;
locale: Locale;
}
export async function compileSubtitles(
options: CompileSubtitlesOptions,
): Promise<CompiledSubtitles> {
// Pin the same localized chrome before the first await that a full export at
// this resolution uses; Quiz measurement and timing therefore cannot drift.
const labels = getVideoExportCoverLabels(options.locale);
const { ir, stageName } = await compileStageIr({
...options,
labels,
skipGeometry: true,
skipInteractiveHtml: true,
});

View File

@ -189,6 +189,10 @@ export function getVideoExportCoverLabels(locale: Locale): VideoExportLabels {
quiz: at('quiz.title'),
questions: at('quiz.questionsCount'),
points: at('quiz.pointsSuffix'),
singleChoice: at('quiz.singleChoice'),
multipleChoice: at('quiz.multipleChoice'),
shortAnswer: at('quiz.shortAnswer'),
answerPlaceholder: at('quiz.inputPlaceholder'),
pbl: at('pbl.v2.hero.title'),
stages: at('pbl.v2.hero.stage'),
tasks: at('pbl.v2.hero.task'),

View File

@ -0,0 +1,24 @@
/** Lightweight video-export options shared by always-loaded classroom UI. */
/** Selectable render resolutions (16:9). Width drives slide snapshots and Quiz layout. */
export const VIDEO_RESOLUTIONS = {
'720p': { width: 1280, height: 720 },
'1080p': { width: 1920, height: 1080 },
'4k': { width: 3840, height: 2160 },
} as const;
export type VideoResolution = keyof typeof VIDEO_RESOLUTIONS;
/** Selectable frame rates for MP4 rendering. */
export const VIDEO_FPS = [24, 30, 60] as const;
export type VideoFps = (typeof VIDEO_FPS)[number];
/** Producer quality presets (speed vs fidelity). */
export const VIDEO_QUALITIES = ['draft', 'standard', 'high'] as const;
export type VideoQuality = (typeof VIDEO_QUALITIES)[number];
export class NoScenesError extends Error {}
export function sanitizeFilename(name: string): string {
return name.replace(/[\\/:*?"<>|]/g, '_') || 'classroom';
}

View File

@ -25,6 +25,8 @@ export interface PackageOptions {
* fetch (e.g. in tests).
*/
gsapSource?: string;
/** Load one app-local vendored binary. Injectable for deterministic unit tests. */
loadVendorAsset?: (sourceUrl: string) => Promise<Blob>;
onProgress?: (message: string) => void;
}
@ -36,6 +38,12 @@ async function loadGsapSource(): Promise<string> {
return res.text();
}
async function loadVendorAsset(sourceUrl: string): Promise<Blob> {
const res = await fetch(sourceUrl);
if (!res.ok) throw new Error(`Failed to load vendored export asset ${sourceUrl} (${res.status})`);
return res.blob();
}
/**
* Build the export ZIP blob from an emitted project and its collected assets.
* Text files land at the project root; asset blobs land under `assets/<planPath>`
@ -62,6 +70,19 @@ export async function packageVideoZip(
zip.file(assetUrl(planPath), await blob.arrayBuffer());
}
// Quiz fonts are project-declared: cover-only/no-Quiz exports never fetch or
// parse their bytes, while Quiz projects keep every font local to the ZIP.
const vendorAssetLoader = options.loadVendorAsset ?? loadVendorAsset;
const vendorAssets = await Promise.all(
project.vendorAssets.map(async (asset) => ({
path: asset.path,
bytes: await (await vendorAssetLoader(asset.sourceUrl)).arrayBuffer(),
})),
);
for (const asset of vendorAssets) {
zip.file(asset.path, asset.bytes);
}
// Vendored GSAP at the path the HTML loads it from.
options.onProgress?.('Bundling GSAP runtime');
const gsapSource = options.gsapSource ?? (await loadGsapSource());

View File

@ -0,0 +1,204 @@
'use client';
/** Browser-side premeasurement for deterministic Quiz question-list export. */
import type { Locale } from '@/lib/i18n';
import {
prepareQuizQuestionList,
quizQuestionListCss,
renderQuizQuestionListSurface,
type CompilerScene,
type CoverCardLabels,
type QuizLayoutMeasurement,
type QuizLayoutProbe,
type QuizQuestionListContent,
} from '@/lib/video-export';
import { INTER_FONT_FACE_CSS } from '@/lib/video-export/emit-hyperframes/inter-font';
import { KATEX_MEASUREMENT_CSS } from '@/lib/video-export/emit-hyperframes/katex-assets';
import { NOTO_CJK_MEASUREMENT_CSS } from '@/lib/video-export/emit-hyperframes/noto-cjk-assets';
const RTL_LANGUAGES = new Set(['ar', 'fa', 'he', 'ur', 'ps', 'sd', 'ug', 'yi']);
function direction(locale: string): 'ltr' | 'rtl' {
return RTL_LANGUAGES.has(locale.split('-')[0].toLowerCase()) ? 'rtl' : 'ltr';
}
export interface MeasureQuizQuestionListInput {
content: QuizQuestionListContent;
width: number;
height: number;
locale: Locale;
labels: CoverCardLabels;
}
type QuizQuestionListMeasurer = (
input: MeasureQuizQuestionListInput,
) => Promise<QuizLayoutMeasurement | null>;
/** Fully prepared static surface consumed by the browser-only layout core. */
export interface QuizQuestionListMeasurementSurface {
html: string;
css: string;
width: number;
height: number;
}
/** Prepare the exact emitted question markup, CSS, and embedded font faces. */
export function createQuizQuestionListMeasurementSurface(
input: MeasureQuizQuestionListInput,
): QuizQuestionListMeasurementSurface {
return {
html: renderQuizQuestionListSurface(
input.content,
input.labels,
direction(input.locale),
'quiz-layout-measurement-content',
),
css: [
INTER_FONT_FACE_CSS,
NOTO_CJK_MEASUREMENT_CSS,
KATEX_MEASUREMENT_CSS,
quizQuestionListCss(input.width),
].join('\n'),
width: input.width,
height: input.height,
};
}
/**
* Mount one fully prepared surface off-screen and wait for fonts plus two
* identical layout reads. This function deliberately closes over no module
* state, so the Chromium guardrail can execute the production DOM lifecycle
* itself instead of reimplementing it in the test.
*/
export async function measureQuizQuestionList(
input: QuizQuestionListMeasurementSurface,
timeoutMs = 5_000,
): Promise<QuizLayoutMeasurement | null> {
const host = document.createElement('div');
let active = true;
let deadlineTimer: ReturnType<typeof setTimeout> | undefined;
const budgetMs = Number.isFinite(timeoutMs) ? Math.max(0, timeoutMs) : 5_000;
const deadline = new Promise<boolean>((resolve) => {
deadlineTimer = setTimeout(() => {
active = false;
resolve(false);
}, budgetMs);
});
const waitBeforeDeadline = (work: PromiseLike<unknown>): Promise<boolean> =>
Promise.race([
Promise.resolve(work).then(
() => active,
() => false,
),
deadline,
]);
try {
host.setAttribute('aria-hidden', 'true');
Object.assign(host.style, {
position: 'fixed',
left: '-100000px',
top: '0',
width: `${input.width}px`,
height: `${input.height}px`,
visibility: 'hidden',
pointerEvents: 'none',
overflow: 'hidden',
});
const style = document.createElement('style');
style.textContent = [
input.css,
`#quiz-layout-measurement, #quiz-layout-measurement * { box-sizing:border-box; }`,
`#quiz-layout-measurement { position:absolute;inset:0;display:flex;align-items:center;justify-content:center;overflow:hidden; }`,
].join('\n');
host.id = 'quiz-layout-measurement';
host.className = 'cover-card quiz-question-list';
host.append(style);
host.insertAdjacentHTML('beforeend', input.html);
document.body.append(host);
if (!(await waitBeforeDeadline(document.fonts?.ready ?? Promise.resolve()))) return null;
let previous: QuizLayoutMeasurement | null = null;
let stableReads = 0;
for (let attempt = 0; attempt < 8; attempt += 1) {
const frame = new Promise<void>((resolve) => {
requestAnimationFrame(() => {
if (active) resolve();
});
});
if (!(await waitBeforeDeadline(frame))) return null;
const viewport = host.querySelector<HTMLElement>('.quiz-list-viewport');
const content = host.querySelector<HTMLElement>('.quiz-list-content');
if (!viewport || !content) return null;
const current = {
contentHeightPx: Math.ceil(content.scrollHeight),
viewportHeightPx: Math.ceil(viewport.clientHeight),
frameHeightPx: input.height,
};
if (current.contentHeightPx <= 0 || current.viewportHeightPx <= 0) return null;
if (
previous &&
previous.contentHeightPx === current.contentHeightPx &&
previous.viewportHeightPx === current.viewportHeightPx
) {
stableReads += 1;
if (stableReads >= 2) return current;
} else {
stableReads = 0;
}
previous = current;
}
return null;
} catch {
return null;
} finally {
active = false;
if (deadlineTimer !== undefined) clearTimeout(deadlineTimer);
host.remove();
}
}
async function measureQuizQuestionListContent(
input: MeasureQuizQuestionListInput,
): Promise<QuizLayoutMeasurement | null> {
return measureQuizQuestionList(createQuizQuestionListMeasurementSurface(input));
}
export interface CreateQuizLayoutProbeInput {
scenes: readonly CompilerScene[];
width: number;
height: number;
locale: Locale;
labels: CoverCardLabels;
}
/** Premeasure all non-empty Quiz scenes and return the compiler's sync probe. */
export async function createQuizLayoutProbe(
input: CreateQuizLayoutProbeInput,
measure: QuizQuestionListMeasurer = measureQuizQuestionListContent,
): Promise<QuizLayoutProbe> {
const bySceneId = new Map<string, QuizLayoutMeasurement>();
for (const scene of input.scenes) {
if (scene.type !== 'quiz') continue;
const questions = prepareQuizQuestionList(scene);
if (questions.length === 0) continue;
try {
const result = await measure({
content: { title: scene.title, questions },
width: input.width,
height: input.height,
locale: input.locale,
labels: input.labels,
});
if (result) bySceneId.set(scene.id, result);
} catch {
// The compiler receives a miss and emits the cover-only fallback warning.
}
}
return {
measureQuestionList(scene) {
return bySceneId.get(scene.id) ?? null;
},
};
}

View File

@ -17,8 +17,9 @@ import { saveAs } from 'file-saver';
import { toast } from 'sonner';
import { useI18n } from '@/lib/hooks/use-i18n';
import { createLogger } from '@/lib/logger';
import { useVideoRenderStore } from '@/lib/store/video-render';
import { acquireExport, releaseExport } from './export-in-flight';
import { compileSubtitles, NoScenesError, sanitizeFilename } from './build-export-zip';
import { NoScenesError, sanitizeFilename } from './export-options';
const log = createLogger('DownloadSubtitles');
@ -26,7 +27,8 @@ export type SubtitleFormat = 'srt' | 'vtt';
export function useDownloadSubtitles() {
const [downloading, setDownloading] = useState(false);
const { t } = useI18n();
const { t, locale } = useI18n();
const resolution = useVideoRenderStore((state) => state.options.resolution);
const downloadSubtitles = useCallback(
async (format: SubtitleFormat = 'srt') => {
@ -37,7 +39,11 @@ export function useDownloadSubtitles() {
setDownloading(true);
const toastId = toast.loading(t('export.subtitlesCompiling'));
try {
const { srt, vtt, stageName, cueCount } = await compileSubtitles();
const { compileSubtitles } = await import('./build-export-zip');
const { srt, vtt, stageName, cueCount } = await compileSubtitles({
resolution,
locale,
});
if (cueCount === 0) {
toast.error(t('export.subtitlesEmpty'), { id: toastId });
return;
@ -59,7 +65,7 @@ export function useDownloadSubtitles() {
setDownloading(false);
}
},
[downloading, t],
[downloading, t, locale, resolution],
);
return { downloading, downloadSubtitles };

View File

@ -19,12 +19,11 @@ import { useI18n } from '@/lib/hooks/use-i18n';
import { createLogger } from '@/lib/logger';
import { acquireExport, releaseExport } from './export-in-flight';
import {
buildExportZip,
NoScenesError,
sanitizeFilename,
VIDEO_RESOLUTIONS,
type VideoResolution,
} from './build-export-zip';
} from './export-options';
const log = createLogger('ExportVideo');
@ -47,6 +46,7 @@ export function useExportVideo() {
try {
toast.loading(t('export.videoRendering'), { id: toastId });
const { buildExportZip } = await import('./build-export-zip');
const { zipBlob, stageName, missingCount, errorCount } = await buildExportZip({
resolution,
burnInSubtitles,

View File

@ -3,7 +3,7 @@
*
* Composes the ordered passes into the `VideoTimeline` IR:
*
* normalize → probe → timeline → visuals → interactive → geometry → assets → (unsupported) → assemble
* normalize → probe → timeline → visuals → interactive → reflow → geometry → assets → (unsupported) → assemble
*
* Every pass is a pure function; live app state enters only through the injected
* {@link TimingProbe} / {@link AssetSource} (issue #864 DI boundary), so the whole
@ -19,7 +19,14 @@
* Pure: no IO beyond the injected dependencies.
*/
import type { SceneType, PlayVideoAction, Action } from '@openmaic/dsl';
import type { AssetSource, CompileConfig, CompilerScene, GeometryProbe, TimingProbe } from './deps';
import type {
AssetSource,
CompileConfig,
CompilerScene,
GeometryProbe,
QuizLayoutProbe,
TimingProbe,
} from './deps';
import {
CANVAS,
VIDEO_TIMELINE_COMPILER,
@ -34,6 +41,7 @@ import { buildTimelineOptions } from './passes/probe';
import { buildTimeline } from './passes/timeline';
import { applyVisuals } from './passes/visuals';
import { applyInteractiveHtml } from './passes/interactive';
import { reflowQuizTimelines } from './passes/reflow';
import { applyGeometry } from './passes/geometry';
import { planAssets } from './passes/assets';
@ -56,6 +64,8 @@ export interface CompileDeps {
geometry?: GeometryProbe;
/** Optional prepared-HTML adapter; omitted callers degrade interactive scenes. */
interactive?: import('./deps').InteractiveHtmlSource;
/** Optional app-premeasured Quiz question-list layouts. */
quizLayout?: QuizLayoutProbe;
config?: CompileConfig;
}
@ -156,19 +166,28 @@ export function compileVideoTimeline(input: CompileInput, deps: CompileDeps): Vi
const timeline = buildTimeline(normalized.scenes, opts);
// 4. visuals — turn Quiz/PBL authored data into whole-scene static covers.
const visuals = applyVisuals(timeline.scenes, normalized.scenes);
const visuals = applyVisuals(timeline.scenes, normalized.scenes, deps.quizLayout);
// 5. interactive HTML — promote successfully prepared pages to a first-class base.
const interactive = applyInteractiveHtml(visuals.scenes, normalized.scenes, deps.interactive);
// 6. geometry — resolve effect + video element placement (degrade on miss).
// Prefers measured content-box geometry when a GeometryProbe is supplied.
const geometry = applyGeometry(interactive.scenes, normalized.scenes, deps.geometry);
// 6. reflow — compiler-added Quiz tails shift every later absolute timestamp,
// including first-class interactive bases prepared by the preceding pass.
const reflow = reflowQuizTimelines(
interactive.scenes,
timeline.subtitles,
timeline.totalDurationMs,
visuals.extensionsMs,
);
// 7. assets — dedup + naming plan; stamp asset refs onto segments.
// 7. geometry — resolve effect + video element placement (degrade on miss).
// Prefers measured content-box geometry when a GeometryProbe is supplied.
const geometry = applyGeometry(reflow.scenes, normalized.scenes, deps.geometry);
// 8. assets — dedup + naming plan; stamp asset refs onto segments.
const assets = planAssets(normalized.scenes, geometry.scenes, deps.assets);
// 8. remaining unsupported scene families → markers + diagnostics.
// 9. remaining unsupported scene families → markers + diagnostics.
const unsupportedDiagnostics: Diagnostic[] = [];
const scenes = markUnsupported(assets.scenes, unsupportedDiagnostics);
@ -193,9 +212,9 @@ export function compileVideoTimeline(input: CompileInput, deps: CompileDeps): Vi
ttsEnabled: timeline.ttsEnabled,
whiteboardInitiallyOpen: config.whiteboardInitiallyOpen ?? false,
},
totalDurationMs: timeline.totalDurationMs,
totalDurationMs: reflow.totalDurationMs,
scenes,
subtitles: timeline.subtitles,
subtitles: reflow.subtitles,
assets: assets.plan,
diagnostics,
};

View File

@ -148,6 +148,24 @@ export interface GeometryProbe {
contentGeometry(elementId: string, scene: SceneCore): PercentageGeometry | null;
}
/** Stable app-side layout facts consumed by the pure Quiz timing planner. */
export interface QuizLayoutMeasurement {
/** Full static question-list content height at the target resolution. */
contentHeightPx: number;
/** Visible list viewport height at the target resolution. */
viewportHeightPx: number;
/** Target video frame height, used to scale the 720p scroll-speed baseline. */
frameHeightPx: number;
}
/**
* Synchronous Quiz layout dependency. The app measures all Quiz scenes before
* compile and exposes the resulting table through this pure lookup boundary.
*/
export interface QuizLayoutProbe {
measureQuestionList(scene: SceneCore): QuizLayoutMeasurement | null;
}
/** Compiler configuration — the determinism inputs recorded into the IR's `config`. */
export interface CompileConfig {
/** Playback speed multiplier applied to speech dwell. Default 1. */

View File

@ -24,6 +24,7 @@
import type {
PblCoverVisual,
QuizCoverVisual,
QuizQuestionListVisual,
VideoTimeline,
VideoTimelineScene,
VisualSegment,
@ -35,6 +36,18 @@ import { toSrt, toVtt } from '../subtitles';
import { EASE_DEFS, emitEffect } from './effects';
import { escapeHtml, sec } from './format';
import { INTER_FONT_FACE_CSS, INTER_OFL_LICENSE } from './inter-font';
import { KATEX_EXPORT_CSS, KATEX_FONT_ASSETS, KATEX_MIT_LICENSE } from './katex-assets';
import {
NOTO_CJK_EXPORT_CSS,
NOTO_CJK_FONT_ASSETS,
NOTO_SANS_KR_OFL_LICENSE,
NOTO_SANS_SC_OFL_LICENSE,
} from './noto-cjk-assets';
import {
quizQuestionListCss,
renderQuizQuestionListSurface,
type QuizQuestionListLabels,
} from './quiz-question-list';
/** A file in the emitted project: a relative path and its text content. */
export interface EmittedFile {
@ -42,6 +55,14 @@ export interface EmittedFile {
content: string;
}
/** Binary file copied from the app's vendored public assets into the export ZIP. */
export interface EmittedVendorAsset {
/** Project-relative path referenced by emitted HTML/CSS. */
path: string;
/** App-local URL used by the packaging boundary to load the committed bytes. */
sourceUrl: string;
}
/** Optional informational destination displayed on exported Quiz/PBL covers. */
export interface VideoExportCta {
/** Normalized display destination without a URL scheme or trailing slash. */
@ -68,13 +89,21 @@ export interface InteractiveFallbackLabels {
* active locale so an exported video reads like the lesson it came from. Kept as
* injected strings (not an i18n import) so the emitter stays pure.
*/
export interface VideoExportLabels {
export interface VideoExportLabels extends QuizQuestionListLabels {
/** `quiz.title` — the Quiz card's eyebrow. */
quiz: string;
/** `quiz.questionsCount` — unit after the question count. */
questions: string;
/** `quiz.pointsSuffix` — unit after the total points. */
points: string;
/** `quiz.singleChoice` — static question type label. */
singleChoice: string;
/** `quiz.multipleChoice` — static question type label. */
multipleChoice: string;
/** `quiz.shortAnswer` — static question type label. */
shortAnswer: string;
/** `quiz.inputPlaceholder` — text shown inside the visual-only answer box. */
answerPlaceholder: string;
/** `pbl.v2.hero.title` — the PBL card's eyebrow. */
pbl: string;
/** `pbl.v2.hero.stage` — unit after the stage count. */
@ -101,6 +130,9 @@ export interface VideoExportLabels {
interactive: InteractiveFallbackLabels;
}
/** Backward-compatible name for app-side cover and Quiz measurement labels. */
export type CoverCardLabels = VideoExportLabels;
type VideoExportLabelOverrides = Partial<Omit<VideoExportLabels, 'interactive'>> & {
interactive?: Partial<InteractiveFallbackLabels>;
};
@ -141,6 +173,8 @@ export interface EmitHyperframesOptions {
export interface EmittedProject {
files: EmittedFile[];
/** Font/runtime bytes required by this project; empty for exports without a Quiz list. */
vendorAssets: EmittedVendorAsset[];
width: number;
height: number;
compositionId: string;
@ -165,6 +199,10 @@ const DEFAULT_VIDEO_EXPORT_LABELS: VideoExportLabels = {
quiz: 'Quiz',
questions: 'questions',
points: 'pts',
singleChoice: 'Single',
multipleChoice: 'Multiple',
shortAnswer: 'Short answer',
answerPlaceholder: 'Type your answer here...',
pbl: 'Project-Based Learning',
stages: 'Stages',
tasks: 'Tasks',
@ -370,6 +408,7 @@ function visualClip(
visual: VisualSegment,
index: number,
className: string,
trackIndex = 0,
): string {
return [
`id="scene-${scene.index + 1}-visual-${index + 1}"`,
@ -377,7 +416,7 @@ function visualClip(
`data-visual-kind="${visual.kind}"`,
`data-start="${sec(visual.startMs)}"`,
`data-duration="${sec(visual.durationMs)}"`,
`data-track-index="0"`,
`data-track-index="${trackIndex}"`,
].join(' ');
}
@ -408,6 +447,54 @@ function renderQuizCover(
.join('\n');
}
function renderQuizQuestionList(
scene: VideoTimelineScene,
visual: QuizQuestionListVisual,
index: number,
labels: CoverCardLabels,
direction: 'ltr' | 'rtl',
): string {
// A crossfade is intentionally an overlap. Hyperframes rejects overlapping
// clips on one track, so the list owns track 3 (0=base/cover, 1=video,
// 2=audio) while GSAP performs the resolved 600ms transition.
const clip = visualClip(scene, visual, index, 'quiz-question-list', 3);
const contentId = `scene-${scene.index + 1}-visual-${index + 1}-content`;
return [
`<div ${clip}>`,
renderQuizQuestionListSurface(visual, labels, direction, contentId),
`</div>`,
].join('\n');
}
function quizQuestionListStatements(
scene: VideoTimelineScene,
visual: QuizQuestionListVisual,
index: number,
): string[] {
const id = `scene-${scene.index + 1}-visual-${index + 1}`;
const coverIndex = scene.visuals.findIndex((candidate) => candidate.kind === 'quiz-cover');
const coverId = `scene-${scene.index + 1}-visual-${coverIndex + 1}`;
const start = sec(visual.startMs);
const transition = sec(visual.transitionDurationMs);
const statements = [
`tl.fromTo('#${id}',{autoAlpha:0},{autoAlpha:1,duration:${transition},ease:'none'},${start});`,
];
if (coverIndex >= 0) {
statements.push(
`tl.to('#${coverId}',{autoAlpha:0,duration:${transition},ease:'none'},${start});`,
);
}
if (visual.scrollDistancePx > 0 && visual.scrollDurationMs > 0) {
const scrollStart = sec(
visual.startMs + visual.transitionDurationMs + visual.topHoldDurationMs,
);
statements.push(
`tl.to('#${id}-content',{y:-${visual.scrollDistancePx},duration:${sec(visual.scrollDurationMs)},ease:'none'},${scrollStart});`,
);
}
return statements;
}
function renderPerson(
label: string,
name: string,
@ -495,20 +582,32 @@ function renderVisuals(
frame: { width: number; height: number; burnInSubtitles: boolean },
cta: VideoExportCta | null,
direction: 'ltr' | 'rtl',
): string[] {
return scene.visuals.map((visual, index) =>
visual.kind === 'quiz-cover'
? renderQuizCover(scene, visual, index, labels, cta, direction)
: renderPblCover(
scene,
visual,
index,
labels,
planPblCover(visual, labels, { ...frame, cta }),
cta,
direction,
),
);
): { html: string[]; statements: string[] } {
const html: string[] = [];
const statements: string[] = [];
scene.visuals.forEach((visual, index) => {
if (visual.kind === 'quiz-cover') {
html.push(renderQuizCover(scene, visual, index, labels, cta, direction));
return;
}
if (visual.kind === 'quiz-question-list') {
html.push(renderQuizQuestionList(scene, visual, index, labels, direction));
statements.push(...quizQuestionListStatements(scene, visual, index));
return;
}
html.push(
renderPblCover(
scene,
visual,
index,
labels,
planPblCover(visual, labels, { ...frame, cta }),
cta,
direction,
),
);
});
return { html, statements };
}
/** A `play_video` clip, positioned at the target element's geometry (0–100 space). */
@ -985,6 +1084,7 @@ function renderReadme(project: {
burnInSubtitles: boolean;
labels: VideoExportLabels;
cta: VideoExportCta | null;
hasQuizQuestionList: boolean;
}): string {
const seconds = (project.totalDurationMs / 1000).toFixed(1);
const effectiveLabels = JSON.stringify(project.labels, null, 2);
@ -1023,6 +1123,11 @@ folder — no network access, no CDN.
- \`assets/frames\`, \`assets/audio\`, \`assets/media\`, \`assets/interactive\` — slide snapshots, narration audio, embedded video clips, frozen interactive pages.
- ${optionValue(project.gsapVendorPath)} — vendored GSAP (determinism: no CDN at render time).
- \`LICENSES/Inter-OFL-1.1.txt\` — license for the font embedded in \`index.html\`.
${
project.hasQuizQuestionList
? '- `assets/fonts` — 20 KaTeX faces plus deterministic Han/Kana and Hangul WOFF2 assets.\n- `LICENSES/KaTeX-MIT.txt` — license for the KaTeX renderer and math-font faces.\n- `LICENSES/Noto-Sans-SC-OFL-1.1.txt` — license for the bundled deterministic Han/Kana face.\n- `LICENSES/Noto-Sans-KR-OFL-1.1.txt` — license for the bundled deterministic Hangul face.'
: ''
}
## Render
@ -1036,7 +1141,11 @@ Duration: ~${seconds}s at ${project.width}×${project.height}.
## Emitted with
The same manifest, emitter implementation, and complete effective options produce byte-identical HTML.
Local renders on different hosts do not guarantee identical non-Latin pixels because system fonts may differ.
${
project.hasQuizQuestionList
? 'Quiz CJK (Han/Kana/Hangul), Latin, and math rendering is host-independent because the project bundles those exact faces.'
: 'Local renders on different hosts do not guarantee identical non-Latin pixels because system fonts may differ.'
}
| Option | Value |
| --- | --- |
@ -1089,6 +1198,9 @@ export function emitHyperframes(
const locale = options.locale ?? DEFAULT_LOCALE;
const totalSec = sec(ir.totalDurationMs);
const hasInteractiveHtml = ir.scenes.some((scene) => scene.base.kind === 'interactive-html');
const hasQuizQuestionList = ir.scenes.some((scene) =>
scene.visuals.some((visual) => visual.kind === 'quiz-question-list'),
);
const sceneHtml: string[] = [];
const effectHtml: string[] = [];
@ -1097,19 +1209,19 @@ export function emitHyperframes(
for (const scene of ir.scenes) {
sceneHtml.push(`<!-- scene ${scene.index + 1}: ${escapeHtml(scene.title)} -->`);
sceneHtml.push(renderBase(scene, labels));
sceneHtml.push(
...renderVisuals(
scene,
labels,
{
width,
height,
burnInSubtitles: options.burnInSubtitles === true && ir.subtitles.length > 0,
},
cta,
isRtl(locale) ? 'rtl' : 'ltr',
),
const visuals = renderVisuals(
scene,
labels,
{
width,
height,
burnInSubtitles: options.burnInSubtitles === true && ir.subtitles.length > 0,
},
cta,
isRtl(locale) ? 'rtl' : 'ltr',
);
sceneHtml.push(...visuals.html);
statements.push(...visuals.statements);
sceneHtml.push(...renderVideo(scene));
sceneHtml.push(...renderNarration(scene));
@ -1141,11 +1253,13 @@ export function emitHyperframes(
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>${escapeHtml(ir.stage.name)} — OpenMAIC video</title>
<style>
${INTER_FONT_FACE_CSS}
${INTER_FONT_FACE_CSS}${
hasQuizQuestionList ? `\n ${NOTO_CJK_EXPORT_CSS}\n ${KATEX_EXPORT_CSS}` : ''
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: #000; }
#${compositionId} { font-family:Inter,system-ui,sans-serif; }
${coverCardCss(width)}
${coverCardCss(width)}${hasQuizQuestionList ? `\n${quizQuestionListCss(width)}` : ''}
</style>
</head>
<body>
@ -1179,6 +1293,19 @@ window.__openmaicInteractiveReady.then(function () {
const files: EmittedFile[] = [
{ path: 'index.html', content: html },
{ path: 'LICENSES/Inter-OFL-1.1.txt', content: INTER_OFL_LICENSE },
...(hasQuizQuestionList
? [
{ path: 'LICENSES/KaTeX-MIT.txt', content: KATEX_MIT_LICENSE },
{
path: 'LICENSES/Noto-Sans-SC-OFL-1.1.txt',
content: NOTO_SANS_SC_OFL_LICENSE,
},
{
path: 'LICENSES/Noto-Sans-KR-OFL-1.1.txt',
content: NOTO_SANS_KR_OFL_LICENSE,
},
]
: []),
{ path: manifestPath, content: emitManifestJson(ir) },
{ path: 'subtitles.srt', content: toSrt(ir.subtitles) },
{ path: 'subtitles.vtt', content: toVtt(ir.subtitles) },
@ -1196,12 +1323,14 @@ window.__openmaicInteractiveReady.then(function () {
burnInSubtitles: options.burnInSubtitles === true,
labels,
cta,
hasQuizQuestionList,
}),
},
];
return {
files,
vendorAssets: hasQuizQuestionList ? [...NOTO_CJK_FONT_ASSETS, ...KATEX_FONT_ASSETS] : [],
width,
height,
compositionId,

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,31 @@
/**
* GENERATED FILE — do not edit by hand.
* Sources: @fontsource/noto-sans-sc and @fontsource/noto-sans-kr 400 WOFF2.
* Regenerate with: pnpm gen:video-export-noto-cjk
*/
const NOTO_CJK_CSS_TEMPLATE =
'@font-face{font-family:"OpenMAIC Noto Sans SC";font-style:normal;font-weight:400;font-display:block;src:url("__OPENMAIC_QUIZ_FONT_BASE__/noto-sans-sc-chinese-simplified-400-normal.woff2") format("woff2")}\n@font-face{font-family:"OpenMAIC Noto Sans KR";font-style:normal;font-weight:400;font-display:block;src:url("__OPENMAIC_QUIZ_FONT_BASE__/noto-sans-kr-korean-400-normal.woff2") format("woff2")}';
export const NOTO_CJK_MEASUREMENT_CSS = NOTO_CJK_CSS_TEMPLATE.replaceAll(
'__OPENMAIC_QUIZ_FONT_BASE__',
'/vendor/video-export/fonts',
);
export const NOTO_CJK_EXPORT_CSS = NOTO_CJK_CSS_TEMPLATE.replaceAll(
'__OPENMAIC_QUIZ_FONT_BASE__',
'assets/fonts',
);
export const NOTO_CJK_FONT_ASSETS = [
{
path: 'assets/fonts/noto-sans-sc-chinese-simplified-400-normal.woff2',
sourceUrl: '/vendor/video-export/fonts/noto-sans-sc-chinese-simplified-400-normal.woff2',
},
{
path: 'assets/fonts/noto-sans-kr-korean-400-normal.woff2',
sourceUrl: '/vendor/video-export/fonts/noto-sans-kr-korean-400-normal.woff2',
},
] as const;
export const NOTO_SANS_SC_OFL_LICENSE =
'Google Inc.\n\nThis Font Software is licensed under the SIL Open Font License, Version 1.1.\nThis license is copied below, and is also available with a FAQ at:\nhttp://scripts.sil.org/OFL\n\n\n-----------------------------------------------------------\nSIL OPEN FONT LICENSE Version 1.1 - 26 February 2007\n-----------------------------------------------------------\n\nPREAMBLE\nThe goals of the Open Font License (OFL) are to stimulate worldwide\ndevelopment of collaborative font projects, to support the font creation\nefforts of academic and linguistic communities, and to provide a free and\nopen framework in which fonts may be shared and improved in partnership\nwith others.\n\nThe OFL allows the licensed fonts to be used, studied, modified and\nredistributed freely as long as they are not sold by themselves. The\nfonts, including any derivative works, can be bundled, embedded,\nredistributed and/or sold with any software provided that any reserved\nnames are not used by derivative works. The fonts and derivatives,\nhowever, cannot be released under any other type of license. The\nrequirement for fonts to remain under this license does not apply\nto any document created using the fonts or their derivatives.\n\nDEFINITIONS\n"Font Software" refers to the set of files released by the Copyright\nHolder(s) under this license and clearly marked as such. This may\ninclude source files, build scripts and documentation.\n\n"Reserved Font Name" refers to any names specified as such after the\ncopyright statement(s).\n\n"Original Version" refers to the collection of Font Software components as\ndistributed by the Copyright Holder(s).\n\n"Modified Version" refers to any derivative made by adding to, deleting,\nor substituting -- in part or in whole -- any of the components of the\nOriginal Version, by changing formats or by porting the Font Software to a\nnew environment.\n\n"Author" refers to any designer, engineer, programmer, technical\nwriter or other person who contributed to the Font Software.\n\nPERMISSION & CONDITIONS\nPermission is hereby granted, free of charge, to any person obtaining\na copy of the Font Software, to use, study, copy, merge, embed, modify,\nredistribute, and sell modified and unmodified copies of the Font\nSoftware, subject to the following conditions:\n\n1) Neither the Font Software nor any of its individual components,\nin Original or Modified Versions, may be sold by itself.\n\n2) Original or Modified Versions of the Font Software may be bundled,\nredistributed and/or sold with any software, provided that each copy\ncontains the above copyright notice and this license. These can be\nincluded either as stand-alone text files, human-readable headers or\nin the appropriate machine-readable metadata fields within text or\nbinary files as long as those fields can be easily viewed by the user.\n\n3) No Modified Version of the Font Software may use the Reserved Font\nName(s) unless explicit written permission is granted by the corresponding\nCopyright Holder. This restriction only applies to the primary font name as\npresented to the users.\n\n4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font\nSoftware shall not be used to promote, endorse or advertise any\nModified Version, except to acknowledge the contribution(s) of the\nCopyright Holder(s) and the Author(s) or with their explicit written\npermission.\n\n5) The Font Software, modified or unmodified, in part or in whole,\nmust be distributed entirely under this license, and must not be\ndistributed under any other license. The requirement for fonts to\nremain under this license does not apply to any document created\nusing the Font Software.\n\nTERMINATION\nThis license becomes null and void if any of the above conditions are\nnot met.\n\nDISCLAIMER\nTHE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT\nOF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE\nCOPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\nINCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL\nDAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM\nOTHER DEALINGS IN THE FONT SOFTWARE.\n';
export const NOTO_SANS_KR_OFL_LICENSE =
'Google Inc.\n\nThis Font Software is licensed under the SIL Open Font License, Version 1.1.\nThis license is copied below, and is also available with a FAQ at:\nhttp://scripts.sil.org/OFL\n\n\n-----------------------------------------------------------\nSIL OPEN FONT LICENSE Version 1.1 - 26 February 2007\n-----------------------------------------------------------\n\nPREAMBLE\nThe goals of the Open Font License (OFL) are to stimulate worldwide\ndevelopment of collaborative font projects, to support the font creation\nefforts of academic and linguistic communities, and to provide a free and\nopen framework in which fonts may be shared and improved in partnership\nwith others.\n\nThe OFL allows the licensed fonts to be used, studied, modified and\nredistributed freely as long as they are not sold by themselves. The\nfonts, including any derivative works, can be bundled, embedded,\nredistributed and/or sold with any software provided that any reserved\nnames are not used by derivative works. The fonts and derivatives,\nhowever, cannot be released under any other type of license. The\nrequirement for fonts to remain under this license does not apply\nto any document created using the fonts or their derivatives.\n\nDEFINITIONS\n"Font Software" refers to the set of files released by the Copyright\nHolder(s) under this license and clearly marked as such. This may\ninclude source files, build scripts and documentation.\n\n"Reserved Font Name" refers to any names specified as such after the\ncopyright statement(s).\n\n"Original Version" refers to the collection of Font Software components as\ndistributed by the Copyright Holder(s).\n\n"Modified Version" refers to any derivative made by adding to, deleting,\nor substituting -- in part or in whole -- any of the components of the\nOriginal Version, by changing formats or by porting the Font Software to a\nnew environment.\n\n"Author" refers to any designer, engineer, programmer, technical\nwriter or other person who contributed to the Font Software.\n\nPERMISSION & CONDITIONS\nPermission is hereby granted, free of charge, to any person obtaining\na copy of the Font Software, to use, study, copy, merge, embed, modify,\nredistribute, and sell modified and unmodified copies of the Font\nSoftware, subject to the following conditions:\n\n1) Neither the Font Software nor any of its individual components,\nin Original or Modified Versions, may be sold by itself.\n\n2) Original or Modified Versions of the Font Software may be bundled,\nredistributed and/or sold with any software, provided that each copy\ncontains the above copyright notice and this license. These can be\nincluded either as stand-alone text files, human-readable headers or\nin the appropriate machine-readable metadata fields within text or\nbinary files as long as those fields can be easily viewed by the user.\n\n3) No Modified Version of the Font Software may use the Reserved Font\nName(s) unless explicit written permission is granted by the corresponding\nCopyright Holder. This restriction only applies to the primary font name as\npresented to the users.\n\n4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font\nSoftware shall not be used to promote, endorse or advertise any\nModified Version, except to acknowledge the contribution(s) of the\nCopyright Holder(s) and the Author(s) or with their explicit written\npermission.\n\n5) The Font Software, modified or unmodified, in part or in whole,\nmust be distributed entirely under this license, and must not be\ndistributed under any other license. The requirement for fonts to\nremain under this license does not apply to any document created\nusing the Font Software.\n\nTERMINATION\nThis license becomes null and void if any of the above conditions are\nnot met.\n\nDISCLAIMER\nTHE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT\nOF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE\nCOPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\nINCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL\nDAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM\nOTHER DEALINGS IN THE FONT SOFTWARE.\n';

View File

@ -0,0 +1,119 @@
/** Shared deterministic markup/CSS for Quiz measurement and final emission. */
import type { QuizQuestionListQuestion, QuizQuestionListVisual } from '../ir';
import { renderQuizMathText } from '../../quiz/math-text';
import { escapeHtml } from './format';
export interface QuizQuestionListLabels {
singleChoice: string;
multipleChoice: string;
shortAnswer: string;
answerPlaceholder: string;
}
export interface QuizQuestionListContent {
title: string;
questions: QuizQuestionListQuestion[];
}
function mathText(value: string, allowDisplayMode = false): string {
return renderQuizMathText(value)
.map((segment) => {
if (segment.type === 'text') return escapeHtml(segment.value);
const className =
allowDisplayMode && segment.displayMode
? 'quiz-list-math quiz-list-math-display'
: 'quiz-list-math';
return `<span class="${className}">${segment.html}</span>`;
})
.join('');
}
function typeLabel(question: QuizQuestionListQuestion, labels: QuizQuestionListLabels): string {
if (question.type === 'single') return labels.singleChoice;
if (question.type === 'multiple') return labels.multipleChoice;
return labels.shortAnswer;
}
function renderChoiceOptions(question: QuizQuestionListQuestion): string {
return (question.options ?? [])
.map(
(option) =>
`<div class="quiz-list-option"><span class="quiz-list-option-control" aria-hidden="true"></span><span class="quiz-list-option-key">${escapeHtml(option.value)}</span><span class="quiz-list-option-label" dir="auto">${mathText(option.label)}</span></div>`,
)
.join('\n');
}
function renderQuestion(
question: QuizQuestionListQuestion,
index: number,
labels: QuizQuestionListLabels,
): string {
const body =
question.type === 'short_answer'
? `<div class="quiz-list-answer-box"><span>${escapeHtml(labels.answerPlaceholder)}</span></div>`
: `<div class="quiz-list-options">${renderChoiceOptions(question)}</div>`;
return [
`<article class="quiz-list-question" data-question-type="${question.type}">`,
` <div class="quiz-list-question-header">`,
` <span class="quiz-list-number">${index + 1}</span>`,
` <div class="quiz-list-question-copy">`,
` <div class="quiz-list-prompt" dir="auto">${mathText(question.question, true)}</div>`,
` <div class="quiz-list-type">${escapeHtml(typeLabel(question, labels))}</div>`,
` </div>`,
` </div>`,
` ${body}`,
`</article>`,
].join('\n');
}
/** Markup used verbatim by the app-side measurement surface and emitted clip. */
export function renderQuizQuestionListSurface(
content: QuizQuestionListContent | QuizQuestionListVisual,
labels: QuizQuestionListLabels,
direction: 'ltr' | 'rtl',
contentId?: string,
): string {
return [
`<div class="quiz-list-shell" dir="${direction}">`,
` <header class="quiz-list-header"><span class="quiz-list-header-icon">?</span><h1 dir="auto">${escapeHtml(content.title)}</h1></header>`,
` <div class="quiz-list-viewport">`,
` <div${contentId ? ` id="${escapeHtml(contentId)}"` : ''} class="quiz-list-content">`,
content.questions.map((question, index) => renderQuestion(question, index, labels)).join('\n'),
` </div>`,
` </div>`,
`</div>`,
].join('\n');
}
/** Resolution-scaled final CSS shared with the off-screen measurement surface. */
export function quizQuestionListCss(width: number): string {
const scale = width / 1280;
const px = (value: number): string => `${Math.max(1, Math.round(value * scale))}px`;
return [
`.quiz-question-list { padding:5.2%;background:linear-gradient(145deg,#071426 0%,#102a43 56%,#133f4b 100%);font-family:Inter,"OpenMAIC Noto Sans SC","OpenMAIC Noto Sans KR",sans-serif; }`,
`.quiz-question-list * { overflow-wrap:anywhere; }`,
`.quiz-list-shell { display:flex;width:min(88%,${px(1040)});height:100%;min-height:0;flex-direction:column;padding:${px(28)} ${px(34)};overflow:hidden;border:${px(1)} solid rgba(255,255,255,.13);border-radius:${px(28)};background:rgba(11,18,33,.9);box-shadow:0 ${px(30)} ${px(80)} rgba(0,0,0,.42);color:#f8fafc; }`,
`.quiz-list-header { display:flex;flex:0 0 auto;align-items:center;gap:${px(12)};margin-bottom:${px(20)}; }`,
`.quiz-list-header-icon { display:grid;width:${px(34)};height:${px(34)};flex:0 0 auto;place-items:center;border:${px(1)} solid rgba(111,220,255,.26);border-radius:999px;background:rgba(86,197,234,.14);color:#8fdcf5;font-size:${px(17)};font-weight:800; }`,
`.quiz-list-header h1 { min-width:0;margin:0;overflow:hidden;color:#f8fafc;font-size:${px(28)};line-height:1.15;text-overflow:ellipsis;white-space:nowrap; }`,
`.quiz-list-viewport { min-height:0;flex:1 1 auto;overflow:hidden; }`,
`.quiz-list-content { display:flex;flex-direction:column;gap:${px(16)};will-change:transform; }`,
`.quiz-list-question { position:relative;flex:0 0 auto;padding:${px(18)} ${px(20)};overflow:hidden;border:${px(1)} solid rgba(255,255,255,.12);border-radius:${px(18)};background:rgba(255,255,255,.07);box-shadow:0 ${px(10)} ${px(28)} rgba(0,0,0,.15); }`,
`.quiz-list-question::before { position:absolute;inset:0 auto 0 0;width:${px(4)};background:#8b5cf6;content:""; }`,
`.quiz-list-question-header { display:flex;align-items:flex-start;gap:${px(12)}; }`,
`.quiz-list-number { display:grid;width:${px(28)};height:${px(28)};flex:0 0 auto;place-items:center;border-radius:${px(8)};background:rgba(167,139,250,.2);color:#c4b5fd;font-size:${px(12)};font-weight:800; }`,
`.quiz-list-question-copy { min-width:0;flex:1 1 auto; }`,
`.quiz-list-prompt { color:#f8fafc;font-size:${px(17)};font-weight:650;line-height:1.5;overflow-wrap:anywhere; }`,
`.quiz-list-type { margin-top:${px(4)};color:#94a3b8;font-size:${px(11)};font-weight:650;letter-spacing:.04em;text-transform:uppercase; }`,
`.quiz-list-options { display:grid;gap:${px(8)};margin-top:${px(14)}; }`,
`.quiz-list-option { display:flex;align-items:flex-start;gap:${px(9)};padding:${px(10)} ${px(12)};border:${px(1)} solid rgba(255,255,255,.09);border-radius:${px(11)};background:rgba(2,6,23,.3);color:#e2e8f0;font-size:${px(14)};line-height:1.4; }`,
`.quiz-list-option-control { width:${px(14)};height:${px(14)};flex:0 0 auto;margin-top:${px(2)};border:${px(2)} solid #64748b;border-radius:999px; }`,
`.quiz-list-question[data-question-type="multiple"] .quiz-list-option-control { border-radius:${px(3)}; }`,
`.quiz-list-option-key { flex:0 0 auto;color:#a78bfa;font-weight:800; }`,
`.quiz-list-option-label { min-width:0;overflow-wrap:anywhere; }`,
`.quiz-list-answer-box { min-height:${px(70)};margin-top:${px(14)};padding:${px(12)};border:${px(1)} dashed #64748b;border-radius:${px(11)};background:rgba(2,6,23,.24);color:#64748b;font-size:${px(13)}; }`,
`.quiz-list-math { display:inline-block;vertical-align:baseline; }`,
`.quiz-list-math-display { display:block;margin:${px(4)} 0;overflow:visible; }`,
`.quiz-list-math .katex-display { margin:0; }`,
].join('\n');
}

View File

@ -28,8 +28,9 @@ export { compileVideoTimeline, type CompileInput, type CompileDeps } from './com
export { normalizeScenes, type NormalizeResult } from './passes/normalize';
export { buildTimelineOptions } from './passes/probe';
export { buildTimeline, type TimelineResult } from './passes/timeline';
export { applyVisuals, type VisualsResult } from './passes/visuals';
export { applyVisuals, prepareQuizQuestionList, type VisualsResult } from './passes/visuals';
export { applyInteractiveHtml, type InteractiveResult } from './passes/interactive';
export { reflowQuizTimelines, type ReflowResult } from './passes/reflow';
export {
applyGeometry,
resolveEffectGeometry,
@ -54,5 +55,13 @@ export {
type EmittedFile,
type VideoExportLabels,
type InteractiveFallbackLabels,
type EmittedVendorAsset,
type CoverCardLabels,
type VideoExportCta,
} from './emit-hyperframes';
export {
quizQuestionListCss,
renderQuizQuestionListSurface,
type QuizQuestionListContent,
type QuizQuestionListLabels,
} from './emit-hyperframes/quiz-question-list';

View File

@ -32,7 +32,7 @@ import { SCENE_TYPES } from '@openmaic/dsl';
export const VIDEO_TIMELINE_SCHEMA = 'openmaic.videoTimeline';
/** IR/manifest version. Bump on any breaking shape change. */
export const VIDEO_TIMELINE_VERSION = 3;
export const VIDEO_TIMELINE_VERSION = 4;
/** Compiler identity stamped into the manifest for provenance. */
export const VIDEO_TIMELINE_COMPILER = 'openmaic-video-timeline';
@ -68,6 +68,8 @@ export const DiagnosticSeveritySchema = z.enum(['info', 'warn', 'error']);
* - `unsupported-scene` — a remaining runtime-only scene family is not rendered,
* represented by markers instead.
* - `cover-card` — a Quiz/PBL scene is rendered as a deterministic static cover.
* - `quiz-layout-unavailable` — a Quiz question list could not be measured and
* safely remained on the cover-only path.
* - `unknown-action` — an action with an unrecognized `type` was dropped.
* - `invalid-action` — an action missing a required field was dropped.
* - `interactive-static-html` — an interactive scene uses packaged frozen HTML.
@ -82,6 +84,7 @@ export const DiagnosticCodeSchema = z.enum([
'skipped-media',
'unsupported-scene',
'cover-card',
'quiz-layout-unavailable',
'unknown-action',
'invalid-action',
'interactive-static-html',
@ -147,6 +150,38 @@ export const QuizCoverVisualSchema = TimedVisualSegmentSchema.extend({
totalPoints: z.number(),
});
/** One learner-visible option on the exported static Quiz question list. */
export const QuizQuestionListOptionSchema = z.object({
value: z.string(),
label: z.string(),
});
/**
* Safe, display-only Quiz projection. Correct answers, analysis, grading
* prompts, points and learner runtime state are deliberately not representable.
*/
export const QuizQuestionListQuestionSchema = z.object({
id: z.string(),
type: z.enum(['single', 'multiple', 'short_answer']),
question: z.string(),
options: z.array(QuizQuestionListOptionSchema).optional(),
});
/** Measured and fully timed static Quiz question-list visual. */
export const QuizQuestionListVisualSchema = TimedVisualSegmentSchema.extend({
kind: z.literal('quiz-question-list'),
title: z.string(),
questions: z.array(QuizQuestionListQuestionSchema),
contentHeightPx: z.number(),
viewportHeightPx: z.number(),
scrollDistancePx: z.number(),
pixelsPerSecond: z.number(),
transitionDurationMs: z.number(),
topHoldDurationMs: z.number(),
scrollDurationMs: z.number(),
bottomHoldDurationMs: z.number(),
});
/** Static PBL Hero-style card built only from authored, learner-visible design fields. */
export const PblCoverVisualSchema = TimedVisualSegmentSchema.extend({
kind: z.literal('pbl-cover'),
@ -166,6 +201,7 @@ export const PblCoverVisualSchema = TimedVisualSegmentSchema.extend({
*/
export const VisualSegmentSchema = z.discriminatedUnion('kind', [
QuizCoverVisualSchema,
QuizQuestionListVisualSchema,
PblCoverVisualSchema,
]);
@ -359,6 +395,9 @@ export type Diagnostic = z.infer<typeof DiagnosticSchema>;
export type DurationSource = z.infer<typeof DurationSourceSchema>;
export type BaseSegment = z.infer<typeof BaseSegmentSchema>;
export type QuizCoverVisual = z.infer<typeof QuizCoverVisualSchema>;
export type QuizQuestionListOption = z.infer<typeof QuizQuestionListOptionSchema>;
export type QuizQuestionListQuestion = z.infer<typeof QuizQuestionListQuestionSchema>;
export type QuizQuestionListVisual = z.infer<typeof QuizQuestionListVisualSchema>;
export type PblCoverVisual = z.infer<typeof PblCoverVisualSchema>;
export type VisualSegment = z.infer<typeof VisualSegmentSchema>;
export type NarrationSegment = z.infer<typeof NarrationSegmentSchema>;

View File

@ -0,0 +1,65 @@
/**
* Pure absolute-time reflow for compiler-added Quiz question-list tails.
* Authored action timing inside each scene remains unchanged; each inserted tail
* extends its owning scene and shifts every later absolute timestamp.
*/
import type { SubtitleCue, VideoTimelineScene } from '../ir';
export interface ReflowResult {
scenes: VideoTimelineScene[];
subtitles: SubtitleCue[];
totalDurationMs: number;
}
function shiftScene(scene: VideoTimelineScene, offsetMs: number, extensionMs: number) {
const shiftTimed = <T extends { startMs: number }>(value: T): T => ({
...value,
startMs: value.startMs + offsetMs,
});
return {
...scene,
startMs: scene.startMs + offsetMs,
durationMs: scene.durationMs + extensionMs,
visuals: scene.visuals.map(shiftTimed),
narration: scene.narration.map(shiftTimed),
effects: scene.effects.map(shiftTimed),
videos: scene.videos.map(shiftTimed),
markers: scene.markers.map(shiftTimed),
};
}
export function reflowQuizTimelines(
scenes: readonly VideoTimelineScene[],
subtitles: readonly SubtitleCue[],
totalDurationMs: number,
extensionsMs: readonly number[],
): ReflowResult {
let cumulativeOffsetMs = 0;
const offsets = scenes.map((scene, index) => {
const offsetMs = cumulativeOffsetMs;
const extensionMs = Math.max(0, extensionsMs[index] ?? 0);
cumulativeOffsetMs += extensionMs;
return { scene, offsetMs, extensionMs };
});
const shiftedScenes = offsets.map(({ scene, offsetMs, extensionMs }) =>
shiftScene(scene, offsetMs, extensionMs),
);
const shiftedSubtitles = subtitles.map((cue) => {
const owner = offsets.find(
({ scene }) =>
scene.id === cue.sceneId &&
cue.startMs >= scene.startMs &&
cue.startMs < scene.startMs + scene.durationMs,
);
const offsetMs =
owner?.offsetMs ?? offsets.find(({ scene }) => scene.id === cue.sceneId)?.offsetMs ?? 0;
return { ...cue, startMs: cue.startMs + offsetMs, endMs: cue.endMs + offsetMs };
});
return {
scenes: shiftedScenes,
subtitles: shiftedSubtitles,
totalDurationMs: totalDurationMs + cumulativeOffsetMs,
};
}

View File

@ -8,8 +8,15 @@
*
* Pure: no IO, clock, randomness, DOM, React, or app-layer imports.
*/
import type { CompilerScene } from '../deps';
import type { Diagnostic, PblCoverVisual, VideoTimelineScene, VisualSegment } from '../ir';
import type { CompilerScene, QuizLayoutMeasurement, QuizLayoutProbe } from '../deps';
import type {
Diagnostic,
PblCoverVisual,
QuizQuestionListQuestion,
QuizQuestionListVisual,
VideoTimelineScene,
VisualSegment,
} from '../ir';
import {
isRunnablePblV2CoverProject,
isUsableLegacyCoverConfig,
@ -19,6 +26,8 @@ import {
export interface VisualsResult {
scenes: VideoTimelineScene[];
diagnostics: Diagnostic[];
/** Per-scene duration inserted after the original authored scene timeline. */
extensionsMs: number[];
}
type UnknownRecord = Record<string, unknown>;
@ -37,6 +46,98 @@ function text(value: unknown): string | undefined {
return trimmed || undefined;
}
function quizOption(value: unknown, index: number): { value: string; label: string } | null {
if (typeof value === 'string') {
const label = text(value);
return label ? { value: String.fromCharCode(65 + index), label } : null;
}
if (!isRecord(value)) return null;
const optionValue = text(value.value);
const label = text(value.label);
return optionValue && label ? { value: optionValue, label } : null;
}
/**
* Project a Quiz scene onto the complete learner-visible field whitelist used
* by export. This is the only path from authored Quiz data into question-list
* IR, so answer keys and runtime state cannot leak into the emitter.
*/
export function prepareQuizQuestionList(scene: CompilerScene): QuizQuestionListQuestion[] {
const raw = Array.isArray(scene.content?.questions) ? scene.content.questions : [];
const questions: QuizQuestionListQuestion[] = [];
for (const value of raw) {
if (!isRecord(value)) continue;
const id = text(value.id);
const question = text(value.question);
const type = value.type;
if (!id || !question || (type !== 'single' && type !== 'multiple' && type !== 'short_answer')) {
continue;
}
if (type === 'short_answer') {
questions.push({ id, type, question });
continue;
}
const options = Array.isArray(value.options)
? value.options
.map((option, index) => quizOption(option, index))
.filter((option): option is { value: string; label: string } => option !== null)
: [];
questions.push({ id, type, question, options });
}
return questions;
}
const QUIZ_TRANSITION_MS = 600;
const QUIZ_TOP_HOLD_MS = 1200;
const QUIZ_BOTTOM_HOLD_MS = 1200;
const QUIZ_SCROLL_PX_PER_SECOND_720P = 96;
const QUIZ_MIN_SCROLL_MS = 4000;
const QUIZ_MAX_SCROLL_MS = 24_000;
function validMeasurement(value: QuizLayoutMeasurement | null): value is QuizLayoutMeasurement {
return (
value !== null &&
Number.isFinite(value.contentHeightPx) &&
value.contentHeightPx > 0 &&
Number.isFinite(value.viewportHeightPx) &&
value.viewportHeightPx > 0 &&
Number.isFinite(value.frameHeightPx) &&
value.frameHeightPx > 0
);
}
function quizQuestionListVisual(
source: CompilerScene,
timeline: VideoTimelineScene,
questions: QuizQuestionListQuestion[],
measurement: QuizLayoutMeasurement,
): QuizQuestionListVisual {
const scrollDistancePx = Math.max(0, measurement.contentHeightPx - measurement.viewportHeightPx);
const targetPixelsPerSecond = QUIZ_SCROLL_PX_PER_SECOND_720P * (measurement.frameHeightPx / 720);
const unclampedScrollMs = Math.round((scrollDistancePx / targetPixelsPerSecond) * 1000);
const scrollDurationMs =
scrollDistancePx === 0
? 0
: Math.min(QUIZ_MAX_SCROLL_MS, Math.max(QUIZ_MIN_SCROLL_MS, unclampedScrollMs));
const pixelsPerSecond = scrollDurationMs === 0 ? 0 : scrollDistancePx / (scrollDurationMs / 1000);
const durationMs = QUIZ_TRANSITION_MS + QUIZ_TOP_HOLD_MS + scrollDurationMs + QUIZ_BOTTOM_HOLD_MS;
return {
kind: 'quiz-question-list',
startMs: timeline.startMs + timeline.durationMs,
durationMs,
title: source.title,
questions,
contentHeightPx: measurement.contentHeightPx,
viewportHeightPx: measurement.viewportHeightPx,
scrollDistancePx,
pixelsPerSecond,
transitionDurationMs: QUIZ_TRANSITION_MS,
topHoldDurationMs: QUIZ_TOP_HOLD_MS,
scrollDurationMs,
bottomHoldDurationMs: QUIZ_BOTTOM_HOLD_MS,
};
}
function quizCover(scene: CompilerScene, timeline: VideoTimelineScene): VisualSegment {
const questions = Array.isArray(scene.content?.questions) ? scene.content.questions : [];
const totalPoints = questions.reduce((sum, question) => {
@ -114,26 +215,60 @@ function pblCover(scene: CompilerScene, timeline: VideoTimelineScene): PblCoverV
export function applyVisuals(
timelineScenes: readonly VideoTimelineScene[],
sourceScenes: readonly CompilerScene[],
quizLayout?: QuizLayoutProbe,
): VisualsResult {
const diagnostics: Diagnostic[] = [];
const extensionsMs = timelineScenes.map(() => 0);
const scenes = timelineScenes.map((timeline, index): VideoTimelineScene => {
const source = sourceScenes[index];
if (!source || (source.type !== 'quiz' && source.type !== 'pbl')) return timeline;
const visual =
source.type === 'quiz' ? quizCover(source, timeline) : pblCover(source, timeline);
let visuals: VisualSegment[] = [visual];
if (source.type === 'quiz') {
const questions = prepareQuizQuestionList(source);
if (questions.length > 0) {
let measurement: QuizLayoutMeasurement | null = null;
if (quizLayout) {
try {
measurement = quizLayout.measureQuestionList(source);
} catch {
measurement = null;
}
}
if (validMeasurement(measurement)) {
const list = quizQuestionListVisual(source, timeline, questions, measurement);
extensionsMs[index] = list.durationMs;
visuals = [
{ ...visual, durationMs: timeline.durationMs + list.transitionDurationMs },
list,
];
} else {
diagnostics.push({
severity: 'warn',
code: 'quiz-layout-unavailable',
sceneId: timeline.id,
message: `Quiz question-list layout was unavailable for scene "${timeline.title}"; using the deterministic cover-only fallback.`,
});
}
}
}
diagnostics.push({
severity: 'info',
code: 'cover-card',
sceneId: timeline.id,
message: `Scene "${timeline.title}" (${timeline.type}) is rendered as a deterministic static cover card.`,
message:
timeline.type === 'quiz' && visuals.some((item) => item.kind === 'quiz-question-list')
? `Scene "${timeline.title}" (quiz) is rendered as a deterministic cover-to-question-list sequence.`
: `Scene "${timeline.title}" (${timeline.type}) is rendered as a deterministic static cover card.`,
});
return {
...timeline,
supported: true,
base: { kind: 'visual-segments' },
visuals: [visual],
visuals,
};
});
return { scenes, diagnostics };
return { scenes, diagnostics, extensionsMs };
}

View File

@ -9,6 +9,8 @@
"scripts": {
"postinstall": "cd packages/mathml2omml && npm run build && cd ../pptxgenjs && npm run build && cd ../@openmaic/dsl && pnpm run build && cd ../generation && pnpm run build && cd ../storage && pnpm run build && cd ../importer && pnpm run build && cd ../renderer && pnpm run build && cd ../editor && pnpm run build && cd ../../.. && node scripts/sync-maic-importer.mjs",
"sync:maic-importer": "node scripts/sync-maic-importer.mjs",
"gen:video-export-katex": "node scripts/generate-video-export-katex.mjs",
"gen:video-export-noto-cjk": "node scripts/generate-video-export-noto-cjk.mjs",
"dev": "next dev",
"build": "node scripts/assert-vendor-maic-importer.mjs && next build",
"start": "next start",
@ -54,6 +56,7 @@
"@fontsource/lxgw-wenkai": "^5.2.5",
"@fontsource/merriweather": "^5.2.11",
"@fontsource/montserrat": "^5.2.8",
"@fontsource/noto-sans-kr": "^5.2.9",
"@fontsource/noto-sans-sc": "^5.2.9",
"@fontsource/noto-serif-sc": "^5.2.8",
"@fontsource/open-sans": "^5.2.7",

8
pnpm-lock.yaml generated
View File

@ -83,6 +83,9 @@ importers:
'@fontsource/montserrat':
specifier: ^5.2.8
version: 5.2.8
'@fontsource/noto-sans-kr':
specifier: ^5.2.9
version: 5.3.0
'@fontsource/noto-sans-sc':
specifier: ^5.2.9
version: 5.2.9
@ -2829,6 +2832,9 @@ packages:
'@fontsource/montserrat@5.2.8':
resolution: {integrity: sha512-xTjLxSbSfCycDB0pwmNsfNvdfWPaDaRQ2LC6yt/ZI7SdvXG52zHnzNYC/09mzuAuWNJyShkteutfCoDgym56hQ==}
'@fontsource/noto-sans-kr@5.3.0':
resolution: {integrity: sha512-/JnpTjaCOXW7xUoqOyCVYSr05VOkDy5Yla9O+WAEiS7u+yYLsmHoqj6v1W0bf91G8G3XA2+NnRS3CX2Tf/FVZg==}
'@fontsource/noto-sans-sc@5.2.9':
resolution: {integrity: sha512-bTUIWGBgJDpwi5qAr+x0/lcgv80IHTB9vl6s2f6EymZEa7qYV99yNRBZuKFT+SYDKVunZrjCEhWtpxqmbXWl5Q==}
@ -16641,6 +16647,8 @@ snapshots:
'@fontsource/montserrat@5.2.8': {}
'@fontsource/noto-sans-kr@5.3.0': {}
'@fontsource/noto-sans-sc@5.2.9': {}
'@fontsource/noto-serif-sc@5.2.8': {}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,66 @@
/** Generate the offline KaTeX CSS/license module used by Quiz video export. */
import { copyFileSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import prettier from 'prettier';
const require = createRequire(import.meta.url);
const here = path.dirname(fileURLToPath(import.meta.url));
const root = path.join(here, '..');
const katexDir = path.dirname(require.resolve('katex/package.json'));
const katexVersion = require('katex/package.json').version;
const sourceCss = readFileSync(path.join(katexDir, 'dist', 'katex.min.css'), 'utf8');
const license = readFileSync(path.join(katexDir, 'LICENSE'), 'utf8');
const publicFontDir = path.join(root, 'public', 'vendor', 'video-export', 'fonts');
const publicFontBase = '/vendor/video-export/fonts';
const exportFontBase = 'assets/fonts';
mkdirSync(publicFontDir, { recursive: true });
const faces = [];
const assets = [];
for (const [, body] of sourceCss.matchAll(/@font-face\{([^}]*)\}/g)) {
const woff2 = body.match(/url\(fonts\/([^)]+\.woff2)\)/);
if (!woff2) continue;
const filename = woff2[1];
copyFileSync(path.join(katexDir, 'dist', 'fonts', filename), path.join(publicFontDir, filename));
const declaration = (pattern) => body.match(pattern)?.[0] ?? '';
faces.push(
`@font-face{${declaration(/font-family:[^;]+/)};${declaration(/font-style:[^;]+/)};${declaration(/font-weight:[^;]+/)};font-display:block;src:url("__OPENMAIC_QUIZ_FONT_BASE__/${filename}") format("woff2")}`,
);
assets.push({
path: `${exportFontBase}/${filename}`,
sourceUrl: `${publicFontBase}/${filename}`,
});
}
if (faces.length !== 20) {
throw new Error(`Expected 20 KaTeX WOFF2 faces, found ${faces.length}`);
}
const rules = sourceCss.replace(/@font-face\{[^}]*\}/g, '');
const cssTemplate = `${faces.join('')}\n${rules}`;
const contents = `/**
* GENERATED FILE — do not edit by hand.
* Complete KaTeX ${katexVersion} CSS with all 20 WOFF2 fonts emitted as
* deterministic project-relative binary assets.
* Regenerate with \`pnpm gen:video-export-katex\`.
*/
const KATEX_CSS_TEMPLATE = ${JSON.stringify(cssTemplate)};
export const KATEX_MEASUREMENT_CSS = KATEX_CSS_TEMPLATE.replaceAll(
'__OPENMAIC_QUIZ_FONT_BASE__',
${JSON.stringify(publicFontBase)},
);
export const KATEX_EXPORT_CSS = KATEX_CSS_TEMPLATE.replaceAll(
'__OPENMAIC_QUIZ_FONT_BASE__',
${JSON.stringify(exportFontBase)},
);
export const KATEX_FONT_ASSETS = ${JSON.stringify(assets)} as const;
export const KATEX_MIT_LICENSE = ${JSON.stringify(license)};
`;
const outFile = path.join(root, 'lib', 'video-export', 'emit-hyperframes', 'katex-assets.ts');
const config = await prettier.resolveConfig(outFile);
writeFileSync(outFile, await prettier.format(contents, { ...config, parser: 'typescript' }));
console.log(
`[video-export-katex] emitted ${faces.length} KaTeX ${katexVersion} WOFF2 assets and complete CSS`,
);

View File

@ -0,0 +1,86 @@
import { copyFileSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import prettier from 'prettier';
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const faces = [
{
package: 'noto-sans-sc',
subset: 'chinese-simplified',
family: 'OpenMAIC Noto Sans SC',
licenseExport: 'NOTO_SANS_SC_OFL_LICENSE',
filename: 'noto-sans-sc-chinese-simplified-400-normal.woff2',
},
{
package: 'noto-sans-kr',
subset: 'korean',
family: 'OpenMAIC Noto Sans KR',
licenseExport: 'NOTO_SANS_KR_OFL_LICENSE',
filename: 'noto-sans-kr-korean-400-normal.woff2',
},
];
const outputPath = path.join(root, 'lib', 'video-export', 'emit-hyperframes', 'noto-cjk-assets.ts');
const publicFontDir = path.join(root, 'public', 'vendor', 'video-export', 'fonts');
const publicFontBase = '/vendor/video-export/fonts';
const exportFontBase = 'assets/fonts';
mkdirSync(publicFontDir, { recursive: true });
const singleQuoted = (value) =>
`'${value
.replaceAll('\\', '\\\\')
.replaceAll("'", "\\'")
.replaceAll('\r', '\\r')
.replaceAll('\n', '\\n')
.replaceAll('\u2028', '\\u2028')
.replaceAll('\u2029', '\\u2029')}'`;
let totalBytes = 0;
const css = [];
const licenses = [];
const assets = [];
for (const face of faces) {
const packageDir = path.join(root, 'node_modules', '@fontsource', face.package);
const fontPath = path.join(
packageDir,
'files',
`${face.package}-${face.subset}-400-normal.woff2`,
);
const font = readFileSync(fontPath);
totalBytes += font.byteLength;
copyFileSync(fontPath, path.join(publicFontDir, face.filename));
css.push(
`@font-face{font-family:"${face.family}";font-style:normal;font-weight:400;font-display:block;src:url("__OPENMAIC_QUIZ_FONT_BASE__/${face.filename}") format("woff2")}`,
);
assets.push({
path: `${exportFontBase}/${face.filename}`,
sourceUrl: `${publicFontBase}/${face.filename}`,
});
licenses.push(
`export const ${face.licenseExport} =\n ${singleQuoted(readFileSync(path.join(packageDir, 'LICENSE'), 'utf8'))};`,
);
}
const output = `/**
* GENERATED FILE — do not edit by hand.
* Sources: @fontsource/noto-sans-sc and @fontsource/noto-sans-kr 400 WOFF2.
* Regenerate with: pnpm gen:video-export-noto-cjk
*/
const NOTO_CJK_CSS_TEMPLATE = ${singleQuoted(css.join('\n'))};
export const NOTO_CJK_MEASUREMENT_CSS = NOTO_CJK_CSS_TEMPLATE.replaceAll(
'__OPENMAIC_QUIZ_FONT_BASE__',
${singleQuoted(publicFontBase)},
);
export const NOTO_CJK_EXPORT_CSS = NOTO_CJK_CSS_TEMPLATE.replaceAll(
'__OPENMAIC_QUIZ_FONT_BASE__',
${singleQuoted(exportFontBase)},
);
export const NOTO_CJK_FONT_ASSETS = ${JSON.stringify(assets)} as const;
${licenses.join('\n\n')}
`;
const config = await prettier.resolveConfig(outputPath);
writeFileSync(outputPath, await prettier.format(output, { ...config, parser: 'typescript' }));
console.log(
`[video-export] emitted Pan-CJK font assets (${totalBytes} bytes) → ${path.relative(root, outputPath)}`,
);

View File

@ -5,16 +5,19 @@ const mocks = vi.hoisted(() => ({
collectVideoAssets: vi.fn(),
compileVideoTimeline: vi.fn(),
createVideoTimelineDeps: vi.fn(),
createQuizLayoutProbe: vi.fn(),
emitHyperframes: vi.fn(),
packageVideoZip: vi.fn(),
stageState: vi.fn(),
toSrt: vi.fn(),
toVtt: vi.fn(),
}));
vi.mock('@/lib/video-export', () => ({
compileVideoTimeline: mocks.compileVideoTimeline,
emitHyperframes: mocks.emitHyperframes,
toSrt: vi.fn(),
toVtt: vi.fn(),
toSrt: mocks.toSrt,
toVtt: mocks.toVtt,
}));
vi.mock('@/lib/store', () => ({
useStageStore: { getState: mocks.stageState },
@ -25,6 +28,9 @@ vi.mock('@/lib/document-store', () => ({
vi.mock('@/lib/video-export-app/timeline-deps', () => ({
createVideoTimelineDeps: mocks.createVideoTimelineDeps,
}));
vi.mock('@/lib/video-export-app/quiz-layout', () => ({
createQuizLayoutProbe: mocks.createQuizLayoutProbe,
}));
vi.mock('@/lib/video-export-app/collect', () => ({
collectVideoAssets: mocks.collectVideoAssets,
}));
@ -32,7 +38,8 @@ vi.mock('@/lib/video-export-app/package-zip', () => ({
packageVideoZip: mocks.packageVideoZip,
}));
import { buildExportZip } from '@/lib/video-export-app/build-export-zip';
import { buildExportZip, compileSubtitles } from '@/lib/video-export-app/build-export-zip';
import { getVideoExportCoverLabels } from '@/lib/video-export-app/cover-config';
const ENV_KEY = 'NEXT_PUBLIC_VIDEO_EXPORT_CTA_DESTINATION';
const originalEnv = process.env[ENV_KEY];
@ -48,12 +55,35 @@ beforeEach(() => {
assets: {},
records: {},
});
mocks.createQuizLayoutProbe.mockResolvedValue({ measureQuestionList: vi.fn() });
mocks.compileVideoTimeline.mockReturnValue({ diagnostics: [] });
mocks.emitHyperframes.mockReturnValue({ files: [] });
mocks.collectVideoAssets.mockResolvedValue({ blobs: new Map(), missing: [] });
mocks.packageVideoZip.mockResolvedValue(new Blob(['zip']));
});
describe('compileSubtitles Quiz timing', () => {
it('uses the selected resolution/locale Quiz probe while skipping slide geometry only', async () => {
mocks.accessDocument.mockResolvedValue(undefined);
const quizLayout = { measureQuestionList: vi.fn() };
mocks.createQuizLayoutProbe.mockResolvedValue(quizLayout);
mocks.compileVideoTimeline.mockReturnValue({ diagnostics: [], subtitles: [] });
await compileSubtitles({ resolution: '1080p', locale: 'en-US' });
expect(mocks.createVideoTimelineDeps).toHaveBeenCalledWith(
expect.objectContaining({ skipGeometry: true }),
);
expect(mocks.createQuizLayoutProbe).toHaveBeenCalledWith(
expect.objectContaining({ width: 1920, height: 1080, locale: 'en-US' }),
);
expect(mocks.compileVideoTimeline).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ quizLayout }),
);
});
});
afterAll(() => {
if (originalEnv === undefined) delete process.env[ENV_KEY];
else process.env[ENV_KEY] = originalEnv;
@ -67,6 +97,30 @@ describe('buildExportZip CTA boundary', () => {
expect(result).not.toHaveProperty('runtimeDiagnosticsPath');
});
});
describe('buildExportZip app boundary', () => {
it('premeasures Quiz layout with the selected resolution/locale and injects the sync probe', async () => {
mocks.accessDocument.mockResolvedValue(undefined);
const quizLayout = { measureQuestionList: vi.fn() };
mocks.createQuizLayoutProbe.mockResolvedValue(quizLayout);
await buildExportZip({ resolution: '4k', locale: 'zh-CN' });
expect(mocks.createQuizLayoutProbe).toHaveBeenCalledWith(
expect.objectContaining({
scenes: [{ id: 'scene-1' }],
width: 3840,
height: 2160,
locale: 'zh-CN',
labels: getVideoExportCoverLabels('zh-CN'),
}),
);
expect(mocks.compileVideoTimeline).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ quizLayout }),
);
});
it('freezes the configured CTA and complete locale labels before its first await', async () => {
let releaseDocument!: (value: unknown) => void;

View File

@ -36,7 +36,7 @@ describe('compileVideoTimeline — end-to-end golden', () => {
it('stamps the envelope + config', () => {
expect(ir).toMatchObject({
schema: 'openmaic.videoTimeline',
version: 3,
version: 4,
compiler: 'openmaic-video-timeline',
stage: { id: 'stg', name: 'Demo' },
config: { playbackSpeed: 1, ttsEnabled: true, whiteboardInitiallyOpen: false },

View File

@ -11,15 +11,25 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { chromium, type Browser, type Page, type Request } from '@playwright/test';
import { compileVideoTimeline, emitHyperframes } from '@/lib/video-export';
import type { CompilerScene, VideoExportCta } from '@/lib/video-export';
import { compileVideoTimeline, emitHyperframes, prepareQuizQuestionList } from '@/lib/video-export';
import type { CompilerScene, QuizLayoutProbe, VideoExportCta } from '@/lib/video-export';
import { PBL_PANEL_DESIGN_BOX } from '@/lib/video-export/emit-hyperframes';
import {
getVideoExportCoverLabels,
resolveVideoExportCta,
} from '@/lib/video-export-app/cover-config';
import {
createQuizQuestionListMeasurementSurface,
measureQuizQuestionList,
} from '@/lib/video-export-app/quiz-layout';
import type { Locale } from '@/lib/i18n';
import { NO_ASSETS, NO_PROBE, speech } from './helpers';
import {
QUIZ_SCROLL_LAYOUT_720P,
QUIZ_SCROLL_QUESTIONS,
QUIZ_SCROLL_TITLE,
quizScrollScene,
} from './quiz-scroll-fixture';
const REQUIRED = process.env.COVER_LAYOUT_BROWSER === '1';
@ -28,6 +38,12 @@ const FRAMES = {
'480p': { width: 854, height: 480 },
} as const;
const QUIZ_LIST_FRAMES = {
'720p': { width: 1280, height: 720 },
'1080p': { width: 1920, height: 1080 },
'4k': { width: 3840, height: 2160 },
} as const;
interface CoverContent {
title: string;
description: string;
@ -190,6 +206,62 @@ function coverHtml(
}).files.find((file) => file.path === 'index.html')!.content;
}
const LONG_QUIZ_QUESTIONS = QUIZ_SCROLL_QUESTIONS;
function quizListHtml(
frame: { width: number; height: number },
questions: readonly unknown[],
measurement: { contentHeightPx: number; viewportHeightPx: number },
): {
html: string;
scrollDistancePx: number;
timing: {
transitionStartMs: number;
transitionEndMs: number;
topHoldEndMs: number;
scrollEndMs: number;
bottomHoldEndMs: number;
};
} {
const source = {
id: 'quiz-list',
stageId: 'stage',
title: QUIZ_SCROLL_TITLE,
order: 0,
type: 'quiz',
content: { type: 'quiz', questions },
actions: [speech('quiz-list-narration', 'Question-list narration')],
} as CompilerScene;
const probe: QuizLayoutProbe = {
measureQuestionList: () => ({ ...measurement, frameHeightPx: frame.height }),
};
const ir = compileVideoTimeline(
{ stage: { id: 'stage', name: 'quiz-list-layout' }, scenes: [source] },
{ timing: NO_PROBE, assets: NO_ASSETS, quizLayout: probe },
);
const visual = ir.scenes[0].visuals.find((item) => item.kind === 'quiz-question-list');
if (!visual || visual.kind !== 'quiz-question-list') throw new Error('Quiz list not planned');
return {
html: emitHyperframes(ir, {
...frame,
locale: 'en-US',
labels: getVideoExportCoverLabels('en-US'),
}).files.find((file) => file.path === 'index.html')!.content,
scrollDistancePx: visual.scrollDistancePx,
timing: {
transitionStartMs: visual.startMs,
transitionEndMs: visual.startMs + visual.transitionDurationMs,
topHoldEndMs: visual.startMs + visual.transitionDurationMs + visual.topHoldDurationMs,
scrollEndMs:
visual.startMs +
visual.transitionDurationMs +
visual.topHoldDurationMs +
visual.scrollDurationMs,
bottomHoldEndMs: visual.startMs + visual.durationMs,
},
};
}
const GSAP = readFileSync(join(process.cwd(), 'public/vendor/gsap.min.js'), 'utf8');
const HARNESS_URL = 'http://cover-layout.test/index.html';
@ -580,6 +652,13 @@ describe('Quiz/PBL cover cards in a real browser', () => {
await page.route('**/gsap.min.js', (route) =>
route.fulfill({ body: GSAP, contentType: 'text/javascript' }),
);
await page.route('**/*.woff2', (route) => {
const filename = new URL(route.request().url()).pathname.split('/').at(-1)!;
route.fulfill({
body: readFileSync(join(process.cwd(), 'public/vendor/video-export/fonts', filename)),
contentType: 'font/woff2',
});
});
} catch (error) {
if (REQUIRED) throw error;
}
@ -727,4 +806,297 @@ describe('Quiz/PBL cover cards in a real browser', () => {
`${labels.ctaVisit} ${DEFAULT_CTA.destination}`,
]);
});
for (const [name, frame] of Object.entries(QUIZ_LIST_FRAMES)) {
check(
`measures and reaches the real Quiz list bottom consistently at ${name}`,
async (page) => {
const initial = quizListHtml(frame, LONG_QUIZ_QUESTIONS, {
contentHeightPx: frame.height * 2,
viewportHeightPx: frame.height / 2,
});
await page.setViewportSize(frame);
await loadEmittedHtml(page, initial.html);
const reads = await page.evaluate(async () => {
const values: Array<{ contentHeightPx: number; viewportHeightPx: number }> = [];
for (let index = 0; index < 3; index += 1) {
await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
const content = document.querySelector<HTMLElement>('.quiz-list-content')!;
const viewport = document.querySelector<HTMLElement>('.quiz-list-viewport')!;
values.push({
contentHeightPx: Math.ceil(content.scrollHeight),
viewportHeightPx: Math.ceil(viewport.clientHeight),
});
}
return values;
});
expect(reads[1]).toEqual(reads[0]);
expect(reads[2]).toEqual(reads[0]);
expect(reads[0].contentHeightPx).toBeGreaterThan(reads[0].viewportHeightPx);
const resolved = quizListHtml(frame, LONG_QUIZ_QUESTIONS, reads[0]);
await loadEmittedHtml(page, resolved.html);
const geometry = await page.evaluate((distance) => {
const viewport = document.querySelector<HTMLElement>('.quiz-list-viewport')!;
const content = document.querySelector<HTMLElement>('.quiz-list-content')!;
const first = document.querySelector<HTMLElement>('.quiz-list-question')!;
const cards = document.querySelectorAll<HTMLElement>('.quiz-list-question');
const last = cards[cards.length - 1]!;
const viewportBox = viewport.getBoundingClientRect();
const firstTop = first.getBoundingClientRect().top;
const fontReady = document.fonts.check('16px KaTeX_Math');
const math = document.querySelector<HTMLElement>('.katex .mathnormal');
content.style.transform = `translateY(-${distance}px)`;
const lastBottom = last.getBoundingClientRect().bottom;
return {
firstOffsetPx: firstTop - viewportBox.top,
lastOffsetPx: lastBottom - viewportBox.bottom,
fontReady,
mathFontFamily: math ? getComputedStyle(math).fontFamily : '',
externalFontUrls: Array.from(document.styleSheets)
.flatMap((sheet) => {
try {
return Array.from(sheet.cssRules, (rule) => rule.cssText);
} catch {
return [];
}
})
.filter((rule) => /url\(["']?https?:\/\//.test(rule)),
};
}, resolved.scrollDistancePx);
expect(geometry.firstOffsetPx).toBeCloseTo(0, 0);
expect(Math.abs(geometry.lastOffsetPx)).toBeLessThanOrEqual(1);
expect(geometry.fontReady).toBe(true);
expect(geometry.mathFontFamily).toContain('KaTeX_Math');
expect(geometry.externalFontUrls).toEqual([]);
},
);
}
for (const blockedStep of ['fonts', 'animation-frame'] as const) {
check(`bounds Quiz measurement when ${blockedStep} never settles`, async (page) => {
const frame = QUIZ_LIST_FRAMES['720p'];
const fixture = quizScrollScene();
const surface = createQuizQuestionListMeasurementSurface({
content: {
title: fixture.title,
questions: prepareQuizQuestionList(fixture),
},
width: frame.width,
height: frame.height,
locale: 'en-US',
labels: getVideoExportCoverLabels('en-US'),
});
await page.setViewportSize(frame);
await page.goto(HARNESS_URL, { waitUntil: 'load' });
const result = await page.evaluate(
async ({ source, input, blocked }) => {
const measure = (0, eval)(`(${source})`) as (
value: typeof input,
timeoutMs?: number,
) => Promise<unknown>;
const originalRaf = window.requestAnimationFrame;
if (blocked === 'fonts') {
Object.defineProperty(document, 'fonts', {
configurable: true,
value: { ready: new Promise<void>(() => {}) },
});
} else {
window.requestAnimationFrame = () => 0;
}
const outcome = await Promise.race([
measure(input, 40).then((value) => ({ settled: true, value })),
new Promise<{ settled: false; value: null }>((resolve) =>
setTimeout(() => resolve({ settled: false, value: null }), 250),
),
]);
const remainingHosts = document.querySelectorAll('#quiz-layout-measurement').length;
window.requestAnimationFrame = originalRaf;
if (blocked === 'fonts') {
delete (document as unknown as { fonts?: FontFaceSet }).fonts;
}
document.querySelector('#quiz-layout-measurement')?.remove();
return { ...outcome, remainingHosts };
},
{ source: measureQuizQuestionList.toString(), input: surface, blocked: blockedStep },
);
expect(result).toEqual({ settled: true, value: null, remainingHosts: 0 });
const degraded = compileVideoTimeline(
{ stage: { id: 'stage', name: 'deadline fallback' }, scenes: [fixture] },
{
timing: NO_PROBE,
assets: NO_ASSETS,
quizLayout: { measureQuestionList: () => result.value as null },
},
);
expect(degraded.scenes[0].visuals).not.toContainEqual(
expect.objectContaining({ kind: 'quiz-question-list' }),
);
expect(degraded.diagnostics).toContainEqual(
expect.objectContaining({ code: 'quiz-layout-unavailable', sceneId: fixture.id }),
);
});
}
check('runs the production Quiz measurer and emitted timeline end to end', async (page) => {
const frame = QUIZ_LIST_FRAMES['720p'];
const initial = quizListHtml(frame, LONG_QUIZ_QUESTIONS, {
contentHeightPx: frame.height * 2,
viewportHeightPx: frame.height / 2,
});
await page.setViewportSize(frame);
await loadEmittedHtml(page, initial.html);
const fixture = quizScrollScene();
const surface = createQuizQuestionListMeasurementSurface({
content: {
title: fixture.title,
questions: prepareQuizQuestionList(fixture),
},
width: frame.width,
height: frame.height,
locale: 'en-US',
labels: getVideoExportCoverLabels('en-US'),
});
const measureSource = measureQuizQuestionList.toString();
const measured = await page.evaluate(
async ({ source, input }) => {
try {
const measure = (0, eval)(`(${source})`) as (value: typeof input) => Promise<{
contentHeightPx: number;
viewportHeightPx: number;
frameHeightPx: number;
} | null>;
return {
value: await measure(input),
error: null,
remainingHosts: document.querySelectorAll('#quiz-layout-measurement').length,
};
} catch (error) {
return {
value: null,
error: error instanceof Error ? error.message : String(error),
remainingHosts: document.querySelectorAll('#quiz-layout-measurement').length,
};
}
},
{ source: measureSource, input: surface },
);
expect(measured.error).toBeNull();
expect(measured.remainingHosts).toBe(0);
expect(measured.value).toEqual(QUIZ_SCROLL_LAYOUT_720P);
expect(surface.css).not.toMatch(/url\(["']?https?:\/\//);
expect(
await page.evaluate(() => document.fonts.check('16px "OpenMAIC Noto Sans SC"', '中文')),
).toBe(true);
const cjkCoverage = await page.evaluate(async () => {
await Promise.all([
document.fonts.load('40px "OpenMAIC Noto Sans SC"', '漢字あア'),
document.fonts.load('40px "OpenMAIC Noto Sans KR"', '한글'),
]);
const signature = (text: string, font: string): number => {
const canvas = document.createElement('canvas');
canvas.width = 256;
canvas.height = 64;
const context = canvas.getContext('2d')!;
context.font = `40px ${font}`;
context.fillText(text, 0, 44);
let hash = 2_166_136_261;
for (const value of context.getImageData(0, 0, 256, 64).data) {
hash ^= value;
hash = Math.imul(hash, 16_777_619);
}
return hash >>> 0;
};
const covered = (text: string, family: string): boolean =>
signature(text, `"${family}", monospace`) !== signature(text, 'monospace');
return {
han: covered('漢字', 'OpenMAIC Noto Sans SC'),
kana: covered('あア', 'OpenMAIC Noto Sans SC'),
hangul: covered('한글', 'OpenMAIC Noto Sans KR'),
};
});
expect(cjkCoverage).toEqual({ han: true, kana: true, hangul: true });
const resolved = quizListHtml(frame, LONG_QUIZ_QUESTIONS, measured.value!);
await loadEmittedHtml(page, resolved.html);
const checkpoints = await page.evaluate((timing) => {
const timeline = (
window as typeof window & {
__timelines: Record<string, { time(value: number, suppressEvents?: boolean): unknown }>;
}
).__timelines.openmaic;
const cover = document.querySelector<HTMLElement>('#scene-1-visual-1')!;
const list = document.querySelector<HTMLElement>('#scene-1-visual-2')!;
const content = document.querySelector<HTMLElement>('.quiz-list-content')!;
const viewport = document.querySelector<HTMLElement>('.quiz-list-viewport')!;
const first = document.querySelector<HTMLElement>('.quiz-list-question')!;
const cards = document.querySelectorAll<HTMLElement>('.quiz-list-question');
const last = cards[cards.length - 1]!;
const read = (timeMs: number) => {
timeline.time(timeMs / 1000, false);
const viewportBox = viewport.getBoundingClientRect();
return {
coverOpacity: Number(getComputedStyle(cover).opacity),
listOpacity: Number(getComputedStyle(list).opacity),
firstOffsetPx: first.getBoundingClientRect().top - viewportBox.top,
lastOffsetPx: last.getBoundingClientRect().bottom - viewportBox.bottom,
transform: getComputedStyle(content).transform,
};
};
return {
transitionMid: read((timing.transitionStartMs + timing.transitionEndMs) / 2),
topHoldEnd: read(timing.topHoldEndMs),
scrollQuarter: read(timing.topHoldEndMs + (timing.scrollEndMs - timing.topHoldEndMs) / 4),
scrollEnd: read(timing.scrollEndMs),
bottomHoldEnd: read(timing.bottomHoldEndMs),
};
}, resolved.timing);
expect(checkpoints.transitionMid.coverOpacity).toBeCloseTo(0.5, 1);
expect(checkpoints.transitionMid.listOpacity).toBeCloseTo(0.5, 1);
expect(checkpoints.topHoldEnd.firstOffsetPx).toBeCloseTo(0, 0);
expect(checkpoints.scrollQuarter.firstOffsetPx).toBeCloseTo(-resolved.scrollDistancePx / 4, 0);
expect(Math.abs(checkpoints.scrollEnd.lastOffsetPx)).toBeLessThanOrEqual(1);
expect(checkpoints.bottomHoldEnd.lastOffsetPx).toBeCloseTo(
checkpoints.scrollEnd.lastOffsetPx,
1,
);
expect(checkpoints.bottomHoldEnd.transform).toBe(checkpoints.scrollEnd.transform);
});
check('keeps a short Quiz list stationary when it fits', async (page) => {
const frame = QUIZ_LIST_FRAMES['720p'];
const shortQuestions = [
{
id: 'short',
type: 'short_answer',
question: 'Explain $x=2$.',
},
];
const first = quizListHtml(frame, shortQuestions, {
contentHeightPx: 100,
viewportHeightPx: 100,
});
await page.setViewportSize(frame);
await loadEmittedHtml(page, first.html);
const measured = await page.evaluate(() => {
const content = document.querySelector<HTMLElement>('.quiz-list-content')!;
const viewport = document.querySelector<HTMLElement>('.quiz-list-viewport')!;
return {
contentHeightPx: Math.ceil(content.scrollHeight),
viewportHeightPx: Math.ceil(viewport.clientHeight),
};
});
const resolved = quizListHtml(frame, shortQuestions, measured);
expect(resolved.scrollDistancePx).toBe(0);
expect(resolved.html).not.toMatch(/tl\.to\('#scene-1-visual-2-content'/);
});
});

View File

@ -19,6 +19,7 @@ import {
resolveVideoExportCta,
} from '@/lib/video-export-app/cover-config';
import { NO_ASSETS, NO_PROBE, interactive, slide, speech } from './helpers';
import { QUIZ_SCROLL_LAYOUT_720P, quizScrollScene } from './quiz-scroll-fixture';
/**
* Materializes a fully-emitted Hyperframes project (index.html + manifest +
@ -39,29 +40,6 @@ const PNG_1x1 = Buffer.from(
'base64',
);
function quizCard(order = 0): CompilerScene {
return {
id: `quiz-${order}`,
stageId: 'stage',
title: 'Quiz:确定性导出与超长标题排版验证 SupercalifragilisticexpialidociousWithoutBreaks',
order,
type: 'quiz',
content: {
type: 'quiz',
questions: [
{ id: 'q1', type: 'single', question: 'Hidden', points: 3 },
{ id: 'q2', type: 'short_answer', question: 'Hidden' },
],
},
actions: [
speech(
`quiz-narration-${order}`,
'字幕与 Quiz 封面同时显示,用于验证长文本卡片不会遮挡底部字幕。',
),
],
} as CompilerScene;
}
function pblV2Card(order = 0): CompilerScene {
return {
id: `pbl-v2-${order}`,
@ -232,6 +210,7 @@ interface MaterializedSample {
height?: number;
locale?: Locale;
marker: string;
quizQuestionList?: boolean;
}
const DEFAULT_SAMPLE_LOCALE: Locale = 'en-US';
@ -268,11 +247,19 @@ const COMPLETE_PROJECT_FILES = [
'subtitles.vtt',
].sort();
const QUIZ_PROJECT_FILES = [
...COMPLETE_PROJECT_FILES,
'LICENSES/KaTeX-MIT.txt',
'LICENSES/Noto-Sans-KR-OFL-1.1.txt',
'LICENSES/Noto-Sans-SC-OFL-1.1.txt',
].sort();
const samples: MaterializedSample[] = [
{
name: 'quiz',
scenes: [quizCard()],
scenes: [quizScrollScene()],
marker: 'Quiz:确定性导出与超长标题排版验证',
quizQuestionList: true,
},
{
name: 'pbl-v2',
@ -299,7 +286,7 @@ const samples: MaterializedSample[] = [
order: 0,
elements: [],
}),
quizCard(1),
quizScrollScene(1),
pblV2Card(2),
],
marker: 'Mixed direction: English ثم العربية',
@ -334,6 +321,7 @@ describe('Hyperframes materialized sample contract', () => {
const byName = new Map(samples.map((sample) => [sample.name, sample]));
const legacyContent = byName.get('pbl-legacy')!.scenes[0]!.content as Record<string, unknown>;
expect(JSON.stringify(byName.get('quiz')!.scenes)).toContain('a^2+b^2=c^2');
expect(legacyContent).toHaveProperty('projectConfig');
expect(legacyContent).not.toHaveProperty('projectV2');
expect(byName.get('pbl-dense')).toMatchObject({ width: 854, height: 480 });
@ -359,6 +347,16 @@ describe.skipIf(!OUT_DIR)('materialize a Hyperframes project for real-CLI E2E',
timing: NO_PROBE,
assets: NO_ASSETS,
interactive: preparedInteractive,
...(sample.quizQuestionList
? {
quizLayout: {
// This exact shared fixture is asserted against Chromium in
// cover-card-layout.browser.test.ts. Production exports use
// createQuizLayoutProbe instead of this pinned E2E geometry.
measureQuestionList: () => QUIZ_SCROLL_LAYOUT_720P,
},
}
: {}),
},
);
const project = emitHyperframes(ir, effectiveSampleOptions(sample));
@ -386,16 +384,33 @@ describe.skipIf(!OUT_DIR)('materialize a Hyperframes project for real-CLI E2E',
);
}
// Vendored Quiz fonts use the same committed public bytes as the app-side
// measurement surface, then land at the project-relative CSS paths.
for (const asset of project.vendorAssets) {
const source = join(process.cwd(), 'public', asset.sourceUrl.replace(/^\//, ''));
const target = join(dir, asset.path);
mkdirSync(dirname(target), { recursive: true });
cpSync(source, target);
}
// Vendored GSAP from the committed public copy.
const gsapSrc = join(process.cwd(), 'public/vendor/gsap.min.js');
const gsapDst = join(dir, project.gsapVendorPath);
mkdirSync(dirname(gsapDst), { recursive: true });
cpSync(gsapSrc, gsapDst);
expect([...project.files.map((file) => file.path), project.gsapVendorPath].sort()).toEqual(
COMPLETE_PROJECT_FILES,
);
for (const path of COMPLETE_PROJECT_FILES) {
const expectedFiles = [
...(sample.quizQuestionList ? QUIZ_PROJECT_FILES : COMPLETE_PROJECT_FILES),
...project.vendorAssets.map((asset) => asset.path),
].sort();
expect(
[
...project.files.map((file) => file.path),
...project.vendorAssets.map((asset) => asset.path),
project.gsapVendorPath,
].sort(),
).toEqual(expectedFiles);
for (const path of expectedFiles) {
expect(existsSync(join(dir, path)), `${sample.name}/${path}`).toBe(true);
}
expect(
@ -405,11 +420,14 @@ describe.skipIf(!OUT_DIR)('materialize a Hyperframes project for real-CLI E2E',
}
expect(readdirSync(root).sort()).toEqual([...EXPECTED_SAMPLE_NAMES].sort());
const mixedHtml = readFileSync(join(root, 'mixed/index.html'), 'utf8');
const quizHtml = readFileSync(join(root, 'quiz/index.html'), 'utf8');
const arabicHtml = readFileSync(join(root, 'arabic/index.html'), 'utf8');
const mixedLabels = getVideoExportCoverLabels('en-US');
const arabicLabels = getVideoExportCoverLabels('ar-SA');
expect(DEFAULT_SAMPLE_CTA).toEqual({ destination: 'open.maic.chat' });
expect(quizHtml).toContain('data-visual-kind="quiz-question-list"');
expect(quizHtml).not.toContain('SECRET_SAMPLE_ANALYSIS');
for (const expected of [
mixedLabels.quizCtaPrompt,
mixedLabels.pblCtaPrompt,

View File

@ -0,0 +1,43 @@
import { ESLint } from 'eslint';
import { describe, expect, it } from 'vitest';
const BOUNDARY_RULE = 'no-restricted-syntax';
const eslint = new ESLint({ cwd: process.cwd() });
async function boundaryErrors(code: string): Promise<string[]> {
const filePath = 'lib/video-export/emit-hyperframes/probe.ts';
if (await eslint.isPathIgnored(filePath)) return ['<path is eslint-ignored>'];
const [result] = await eslint.lintText(code, { filePath, warnIgnored: false });
return (result?.messages ?? [])
.filter((message) => message.severity === 2 && message.ruleId === BOUNDARY_RULE)
.map((message) => message.ruleId ?? 'unknown');
}
describe('Hyperframes emitter lint boundary', () => {
it('allows only in-module relatives and the shared pure Quiz math renderer', async () => {
const errors = await boundaryErrors(`
import type { VideoTimeline } from '../ir';
import { renderQuizMathText } from '../../quiz/math-text';
import { escapeHtml } from './format';
export type Timeline = VideoTimeline;
export const rendered = renderQuizMathText(escapeHtml('x'));
`);
expect(errors).toEqual([]);
});
it.each([
['a host-app alias', "import value from '@/lib/store'; export default value;"],
['another app module', "import value from '../../store'; export default value;"],
[
'a named re-export from another app module',
"export { default as value } from '../../store';",
],
['a star re-export from another app module', "export * from '../../store';"],
['React', "import React from 'react'; export default React;"],
['a dynamic import', "export async function load() { return import('./format'); }"],
['require', "const format = require('./format'); export default format;"],
])('rejects %s', async (_label, code) => {
expect(await boundaryErrors(code)).toContain(BOUNDARY_RULE);
});
});

View File

@ -0,0 +1,36 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
const ROOT = process.cwd();
function source(path: string): string {
return readFileSync(join(ROOT, path), 'utf8');
}
describe('video-export client loading boundary', () => {
it('keeps the Quiz export implementation behind user-triggered dynamic imports', () => {
const clientEntries = [
'components/stage/video-export-dialog.tsx',
'lib/store/video-render.ts',
'lib/video-export-app/use-export-video.ts',
'lib/video-export-app/use-download-subtitles.ts',
];
for (const path of clientEntries) {
expect(source(path), `${path} must not eagerly import the export compiler`).not.toMatch(
/from\s+['"][^'"]*build-export-zip['"]/,
);
}
expect(source('lib/store/video-render.ts')).toContain(
"await import('@/lib/video-export-app/build-export-zip')",
);
expect(source('lib/video-export-app/use-export-video.ts')).toContain(
"await import('./build-export-zip')",
);
expect(source('lib/video-export-app/use-download-subtitles.ts')).toContain(
"await import('./build-export-zip')",
);
});
});

View File

@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import { compileVideoTimeline, VideoTimelineSchema } from '@/lib/video-export';
import { NO_ASSETS, NO_PROBE, interactive, stubInteractiveHtml } from './helpers';
import { NO_ASSETS, NO_PROBE, interactive, speech, stubInteractiveHtml } from './helpers';
import { quizScrollScene } from './quiz-scroll-fixture';
describe('compileVideoTimeline — static interactive HTML', () => {
it('promotes prepared HTML to a supported base and plans its asset', () => {
@ -94,4 +95,60 @@ describe('compileVideoTimeline — static interactive HTML', () => {
expect.objectContaining({ code: 'unresolved-interactive-resource', sceneId: 'remote' }),
);
});
it('keeps static interactive first-class while a preceding Quiz shifts all later timing', () => {
const quiz = quizScrollScene(0);
const widget = interactive(
'widget-after-quiz',
'<!doctype html><h1>Widget after Quiz</h1>',
[speech('widget-narration', 'The interactive scene follows the extended Quiz.')],
1,
);
const interactiveSource = stubInteractiveHtml({
'widget-after-quiz': {
id: 'interactive:widget-after-quiz',
present: true,
contentHash: 'b'.repeat(64),
},
});
const baseline = compileVideoTimeline(
{ stage: { id: 'stage', name: 'Integrated export' }, scenes: [quiz, widget] },
{ timing: NO_PROBE, assets: NO_ASSETS, interactive: interactiveSource },
);
const ir = compileVideoTimeline(
{ stage: { id: 'stage', name: 'Integrated export' }, scenes: [quiz, widget] },
{
timing: NO_PROBE,
assets: NO_ASSETS,
interactive: interactiveSource,
quizLayout: {
measureQuestionList: () => ({
contentHeightPx: 1_200,
viewportHeightPx: 480,
frameHeightPx: 720,
}),
},
},
);
const list = ir.scenes[0].visuals.find((visual) => visual.kind === 'quiz-question-list');
if (!list || list.kind !== 'quiz-question-list') throw new Error('Quiz list not planned');
expect(ir.version).toBe(4);
expect(() => VideoTimelineSchema.parse(ir)).not.toThrow();
expect(ir.scenes[1]).toMatchObject({
startMs: baseline.scenes[1].startMs + list.durationMs,
supported: true,
base: {
kind: 'interactive-html',
assetRef: 'interactive/002-widget-after-quiz.html',
},
});
expect(ir.scenes[1].narration[0].startMs).toBe(
baseline.scenes[1].narration[0].startMs + list.durationMs,
);
expect(ir.subtitles.find((cue) => cue.sceneId === widget.id)?.startMs).toBe(
baseline.subtitles.find((cue) => cue.sceneId === widget.id)!.startMs + list.durationMs,
);
expect(ir.totalDurationMs).toBe(baseline.totalDurationMs + list.durationMs);
});
});

View File

@ -1,17 +1,26 @@
import { describe, expect, it } from 'vitest';
import JSZip from 'jszip';
import { describe, expect, it, vi } from 'vitest';
import type { EmittedProject } from '@/lib/video-export';
import { packageVideoZip } from '@/lib/video-export-app/package-zip';
function project(vendorAssets: EmittedProject['vendorAssets']): EmittedProject {
return {
files: [{ path: 'index.html', content: '<!doctype html>' }],
width: 1280,
height: 720,
compositionId: 'openmaic',
totalDurationMs: 1000,
gsapVendorPath: 'assets/vendor/gsap.min.js',
vendorAssets,
};
}
describe('packageVideoZip — interactive HTML', () => {
it('writes the prepared page under assets/interactive in the self-contained ZIP', async () => {
const blob = await packageVideoZip(
{
...project([]),
files: [{ path: 'index.html', content: '<!doctype html><main>composition</main>' }],
width: 1280,
height: 720,
compositionId: 'openmaic',
totalDurationMs: 1000,
gsapVendorPath: 'assets/vendor/gsap.min.js',
},
new Map([
[
@ -30,3 +39,40 @@ describe('packageVideoZip — interactive HTML', () => {
expect(await zip.file('assets/vendor/gsap.min.js')?.async('string')).toContain('window.gsap');
});
});
describe('video-export ZIP vendor assets', () => {
it('fetches and packages only the binary assets declared by the emitted project', async () => {
const loadVendorAsset = vi.fn(async (sourceUrl: string) => {
expect(sourceUrl).toBe('/vendor/video-export/fonts/quiz-font.woff2');
return new Blob([new Uint8Array([1, 2, 3])], { type: 'font/woff2' });
});
const blob = await packageVideoZip(
project([
{
path: 'assets/fonts/quiz-font.woff2',
sourceUrl: '/vendor/video-export/fonts/quiz-font.woff2',
},
]),
new Map(),
{ gsapSource: '/* gsap */', loadVendorAsset },
);
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
expect(loadVendorAsset).toHaveBeenCalledTimes(1);
expect(await zip.file('assets/fonts/quiz-font.woff2')!.async('uint8array')).toEqual(
new Uint8Array([1, 2, 3]),
);
});
it('does not fetch font assets for a project that declares none', async () => {
const loadVendorAsset = vi.fn();
await packageVideoZip(project([]), new Map(), {
gsapSource: '/* gsap */',
loadVendorAsset,
});
expect(loadVendorAsset).not.toHaveBeenCalled();
});
});

View File

@ -0,0 +1,76 @@
import { describe, expect, it, vi } from 'vitest';
import type { CompilerScene, QuizLayoutMeasurement } from '@/lib/video-export';
import { createQuizLayoutProbe } from '@/lib/video-export-app/quiz-layout';
import { getVideoExportCoverLabels } from '@/lib/video-export-app/cover-config';
function quiz(id: string, questions: readonly unknown[]): CompilerScene {
return {
id,
stageId: 'stage',
title: id,
order: 0,
type: 'quiz',
content: { type: 'quiz', questions },
actions: [],
} as CompilerScene;
}
describe('app-side Quiz layout probe', () => {
it('premeasures non-empty Quiz scenes at the target frame and exposes sync lookups', async () => {
const measurement: QuizLayoutMeasurement = {
contentHeightPx: 900,
viewportHeightPx: 500,
frameHeightPx: 1080,
};
const measure = vi.fn().mockResolvedValue(measurement);
const nonEmpty = quiz('non-empty', [{ id: 'q', type: 'single', question: 'Q' }]);
const empty = quiz('empty', []);
const nonQuiz = {
...quiz('slide', []),
type: 'slide',
content: { type: 'slide', canvas: { elements: [] } },
} as CompilerScene;
const probe = await createQuizLayoutProbe(
{
scenes: [nonEmpty, empty, nonQuiz],
width: 1920,
height: 1080,
locale: 'en-US',
labels: getVideoExportCoverLabels('en-US'),
},
measure,
);
expect(measure).toHaveBeenCalledTimes(1);
expect(measure).toHaveBeenCalledWith(
expect.objectContaining({
content: {
title: 'non-empty',
questions: [{ id: 'q', type: 'single', question: 'Q', options: [] }],
},
width: 1920,
height: 1080,
locale: 'en-US',
}),
);
expect(probe.measureQuestionList(nonEmpty)).toEqual(measurement);
expect(probe.measureQuestionList(empty)).toBeNull();
expect(probe.measureQuestionList(nonQuiz)).toBeNull();
});
it('turns measurement rejection into a null lookup for compiler fallback', async () => {
const source = quiz('failed', [{ id: 'q', type: 'short_answer', question: 'Q' }]);
const probe = await createQuizLayoutProbe(
{
scenes: [source],
width: 1280,
height: 720,
locale: 'zh-CN',
labels: getVideoExportCoverLabels('zh-CN'),
},
vi.fn().mockRejectedValue(new Error('font timeout')),
);
expect(probe.measureQuestionList(source)).toBeNull();
});
});

View File

@ -0,0 +1,178 @@
import { createHash } from 'node:crypto';
import { readdirSync } from 'node:fs';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
import type { Action } from '@openmaic/dsl';
import {
compileVideoTimeline,
emitHyperframes,
type CompilerScene,
type QuizLayoutProbe,
} from '@/lib/video-export';
import {
KATEX_EXPORT_CSS,
KATEX_MIT_LICENSE,
} from '@/lib/video-export/emit-hyperframes/katex-assets';
import { NO_ASSETS, stubProbe } from './helpers';
const layout = (contentHeightPx: number, viewportHeightPx: number): QuizLayoutProbe => ({
measureQuestionList: () => ({ contentHeightPx, viewportHeightPx, frameHeightPx: 720 }),
});
function projectDigest(value: unknown): string {
return createHash('sha256').update(JSON.stringify(value)).digest('hex');
}
function compile(layoutProbe?: QuizLayoutProbe) {
const quiz = {
id: 'quiz',
stageId: 'stage',
title: 'Checkpoint <safe>',
order: 0,
type: 'quiz',
content: {
type: 'quiz',
questions: [
{
id: 'single',
type: 'single',
question: 'Which <script>bad()</script> value solves $x^2=4$?',
options: [
{ value: 'A', label: '<img src=x onerror=bad()>' },
{ value: 'B', label: '$2$' },
],
answer: ['B'],
analysis: 'SECRET_ANALYSIS',
},
{
id: 'multiple',
type: 'multiple',
question: '选择所有满足 $a^2+b^2=c^2$ 的选项',
options: [
{ value: 'A', label: '三、四、五' },
{ value: 'B', label: '5, 12, 13' },
],
submission: 'SECRET_SUBMISSION',
},
{
id: 'short',
type: 'short_answer',
question: 'Explain why $\\sqrt{4}=2$.',
commentPrompt: 'SECRET_RUBRIC',
},
],
},
actions: [{ id: 'speech', type: 'speech', text: 'Quiz narration' } as Action],
} as CompilerScene;
return compileVideoTimeline(
{ stage: { id: 'stage', name: 'Quiz list' }, scenes: [quiz] },
{
timing: stubProbe({ speech: 2400 }),
assets: NO_ASSETS,
...(layoutProbe ? { quizLayout: layoutProbe } : {}),
},
);
}
const labels = {
singleChoice: 'Single choice',
multipleChoice: 'Multiple choice',
shortAnswer: 'Short answer',
answerPlaceholder: 'Write your answer',
};
describe('Quiz question-list KaTeX bundle', () => {
it('contains complete offline KaTeX CSS, exactly 20 project-local WOFF2 faces, and the MIT license', () => {
expect(KATEX_EXPORT_CSS).toContain('.katex');
expect(KATEX_EXPORT_CSS.match(/url\("assets\/fonts\//g)).toHaveLength(20);
expect(KATEX_EXPORT_CSS).not.toContain('data:font/woff2;base64,');
expect(KATEX_MIT_LICENSE).toContain('The MIT License (MIT)');
expect(KATEX_MIT_LICENSE).toContain('Copyright (c) 2013-2020 Khan Academy');
});
});
describe('Quiz question-list Hyperframes emission', () => {
it('declares no Quiz font assets when layout falls back to the cover-only path', () => {
const project = emitHyperframes(compile(), { width: 1280, height: 720, labels });
const html = project.files.find((file) => file.path === 'index.html')!.content;
expect(project.vendorAssets).toEqual([]);
expect(html).not.toContain('assets/fonts/');
expect(project.files.map((file) => file.path)).not.toContain('LICENSES/KaTeX-MIT.txt');
});
it('renders escaped static questions with shared math parsing and no learner controls/state', () => {
const project = emitHyperframes(compile(layout(1001, 401)), {
width: 1280,
height: 720,
labels,
});
const html = project.files.find((file) => file.path === 'index.html')!.content;
expect(html).toContain('data-visual-kind="quiz-question-list"');
expect(html).toContain('Which &lt;script&gt;bad()&lt;/script&gt; value solves');
expect(html).not.toContain('<script>bad()</script>');
expect(html).toContain('&lt;img src=x onerror=bad()&gt;');
expect(html).toContain('class="katex"');
expect(html).toContain('Single choice');
expect(html).toContain('Multiple choice');
expect(html).toContain('Short answer');
expect(html).toContain('Write your answer');
expect(html).not.toMatch(/<(?:input|textarea|button)\b/);
expect(html).not.toMatch(/SECRET_(?:ANALYSIS|SUBMISSION|RUBRIC)/);
expect(html).not.toMatch(/(?:scrollHeight|clientHeight|requestAnimationFrame)/);
expect(html).toContain('url("assets/fonts/noto-sans-sc-chinese-simplified-400-normal.woff2")');
expect(html).toContain('url("assets/fonts/noto-sans-kr-korean-400-normal.woff2")');
expect(html).toContain(
'font-family:Inter,"OpenMAIC Noto Sans SC","OpenMAIC Noto Sans KR",sans-serif',
);
});
it('keeps emitted CSS, declared vendor assets, and committed font files closed and offline', () => {
const project = emitHyperframes(compile(layout(1001, 401)), {
width: 1280,
height: 720,
labels,
});
const html = project.files.find((file) => file.path === 'index.html')!.content;
const cssPaths = Array.from(
html.matchAll(/url\("(assets\/fonts\/[^"?]+\.woff2)"\)/g),
(match) => match[1],
);
const declaredPaths = project.vendorAssets.map((asset) => asset.path);
const publicPaths = readdirSync(join(process.cwd(), 'public/vendor/video-export/fonts'))
.filter((name) => name.endsWith('.woff2'))
.map((name) => `assets/fonts/${name}`);
expect(cssPaths).toHaveLength(new Set(cssPaths).size);
expect(declaredPaths).toHaveLength(new Set(declaredPaths).size);
expect(new Set(cssPaths)).toEqual(new Set(declaredPaths));
expect(new Set(publicPaths)).toEqual(new Set(declaredPaths));
expect(
project.vendorAssets.every(
({ path, sourceUrl }) =>
path === sourceUrl.replace('/vendor/video-export/fonts/', 'assets/fonts/'),
),
).toBe(true);
expect(html).not.toMatch(/url\(["']?https?:\/\//);
expect(project.files.map((file) => file.path)).toEqual(
expect.arrayContaining([
'LICENSES/KaTeX-MIT.txt',
'LICENSES/Noto-Sans-SC-OFL-1.1.txt',
'LICENSES/Noto-Sans-KR-OFL-1.1.txt',
]),
);
});
it('emits an identical complete project for repeated Quiz compilation', () => {
const options = {
width: 1280,
height: 720,
labels,
};
expect(projectDigest(emitHyperframes(compile(layout(1001, 401)), options))).toBe(
projectDigest(emitHyperframes(compile(layout(1001, 401)), options)),
);
});
});

View File

@ -0,0 +1,264 @@
import { describe, expect, it } from 'vitest';
import type { Action } from '@openmaic/dsl';
import {
compileVideoTimeline,
prepareQuizQuestionList,
type CompilerScene,
type QuizLayoutProbe,
} from '@/lib/video-export';
import { NO_ASSETS, stubProbe } from './helpers';
const speech = (id: string): Action =>
({ id, type: 'speech', text: `Narration for ${id}` }) as Action;
function scene(questions: readonly unknown[], order = 0): CompilerScene {
return {
id: `quiz-${order}`,
stageId: 'stage',
title: `Quiz ${order}`,
order,
type: 'quiz',
content: { type: 'quiz', questions },
actions: [speech(`speech-${order}`)],
} as CompilerScene;
}
const measured: QuizLayoutProbe = {
measureQuestionList: () => ({
contentHeightPx: 1001,
viewportHeightPx: 401,
frameHeightPx: 720,
}),
};
describe('Quiz question-list preparation', () => {
it('whitelists only learner-visible fields across all three question types', () => {
const questions = prepareQuizQuestionList(
scene([
{
id: 'single',
type: 'single',
question: 'Choose <one>',
options: [
{ value: 'A', label: 'Alpha' },
{ value: 'B', label: 'Beta', correct: true },
],
answer: ['B'],
analysis: 'SECRET_ANALYSIS',
points: 99,
attempt: { answer: 'B' },
},
{
id: 'multiple',
type: 'multiple',
question: 'Choose several',
options: ['Legacy option', { value: 'Z', label: 'Last' }],
submission: ['SECRET_SUBMISSION'],
},
{
id: 'short',
type: 'short_answer',
question: 'Explain $x^2$',
options: [{ value: 'LEAK', label: 'SECRET_OPTION' }],
commentPrompt: 'SECRET_RUBRIC',
review: 'SECRET_REVIEW',
},
]),
);
expect(questions).toEqual([
{
id: 'single',
type: 'single',
question: 'Choose <one>',
options: [
{ value: 'A', label: 'Alpha' },
{ value: 'B', label: 'Beta' },
],
},
{
id: 'multiple',
type: 'multiple',
question: 'Choose several',
options: [
{ value: 'A', label: 'Legacy option' },
{ value: 'Z', label: 'Last' },
],
},
{ id: 'short', type: 'short_answer', question: 'Explain $x^2$' },
]);
expect(JSON.stringify(questions)).not.toMatch(
/SECRET_(ANALYSIS|SUBMISSION|OPTION|RUBRIC|REVIEW)/,
);
});
it('skips malformed questions and safely normalizes damaged option data', () => {
const questions = prepareQuizQuestionList(
scene([
null,
{ id: '', type: 'single', question: 'missing id' },
{ id: 'bad-type', type: 'essay', question: 'unsupported' },
{ id: 'bad-question', type: 'single', question: 42 },
{
id: 'ok',
type: 'single',
question: 'Still renderable',
options: [null, 7, { value: 'A' }, { label: 'No value' }, ' Visible '],
},
]),
);
expect(questions).toMatchObject([
{
id: 'ok',
type: 'single',
question: 'Still renderable',
options: [{ label: 'Visible' }],
},
]);
});
});
describe('Quiz question-list timeline planning', () => {
it('adds a measured list, preserves narration, and extends the Quiz deterministically', () => {
const ir = compileVideoTimeline(
{
stage: { id: 'stage', name: 'Measured Quiz' },
scenes: [scene([{ id: 'q', type: 'single', question: 'Question' }])],
},
{
timing: stubProbe({ 'speech-0': 2400 }),
assets: NO_ASSETS,
quizLayout: measured,
},
);
expect(ir.totalDurationMs).toBe(11_650);
expect(ir.scenes[0]).toMatchObject({
startMs: 0,
durationMs: 11_650,
narration: [{ startMs: 0, durationMs: 2400 }],
visuals: [
{ kind: 'quiz-cover', startMs: 0, durationMs: 3000 },
{
kind: 'quiz-question-list',
startMs: 2400,
durationMs: 9250,
contentHeightPx: 1001,
viewportHeightPx: 401,
scrollDistancePx: 600,
pixelsPerSecond: 96,
transitionDurationMs: 600,
topHoldDurationMs: 1200,
scrollDurationMs: 6250,
bottomHoldDurationMs: 1200,
},
],
});
expect(ir.diagnostics).toContainEqual(
expect.objectContaining({
code: 'cover-card',
sceneId: 'quiz-0',
message: expect.stringContaining('cover-to-question-list sequence'),
}),
);
});
it('uses a static 2.4s hold when the measured list fits the viewport', () => {
const fitProbe: QuizLayoutProbe = {
measureQuestionList: () => ({
contentHeightPx: 400,
viewportHeightPx: 400,
frameHeightPx: 1080,
}),
};
const ir = compileVideoTimeline(
{
stage: { id: 'stage', name: 'Fit Quiz' },
scenes: [scene([{ id: 'q', type: 'short_answer', question: 'Question' }])],
},
{ timing: stubProbe({ 'speech-0': 2400 }), assets: NO_ASSETS, quizLayout: fitProbe },
);
const list = ir.scenes[0].visuals[1];
expect(list).toMatchObject({
kind: 'quiz-question-list',
scrollDistancePx: 0,
pixelsPerSecond: 0,
scrollDurationMs: 0,
durationMs: 3000,
});
expect(ir.totalDurationMs).toBe(5400);
});
it('scales the 96px/s baseline with frame height at an equivalent visual duration', () => {
const compileAt = (frameHeightPx: number, scrollDistancePx: number) => {
const viewportHeightPx = 400;
const ir = compileVideoTimeline(
{
stage: { id: 'stage', name: `${frameHeightPx}p Quiz` },
scenes: [scene([{ id: 'q', type: 'single', question: 'Question' }])],
},
{
timing: stubProbe({ 'speech-0': 1000 }),
assets: NO_ASSETS,
quizLayout: {
measureQuestionList: () => ({
contentHeightPx: viewportHeightPx + scrollDistancePx,
viewportHeightPx,
frameHeightPx,
}),
},
},
);
return ir.scenes[0].visuals[1];
};
expect(compileAt(720, 960)).toMatchObject({
pixelsPerSecond: 96,
scrollDurationMs: 10_000,
});
expect(compileAt(1080, 1440)).toMatchObject({
pixelsPerSecond: 144,
scrollDurationMs: 10_000,
});
});
it('keeps empty, missing-probe, and failed measurements on the existing cover-only path', () => {
const failed: QuizLayoutProbe = { measureQuestionList: () => null };
const empty = compileVideoTimeline(
{ stage: { id: 'stage', name: 'Empty' }, scenes: [scene([])] },
{ timing: stubProbe({ 'speech-0': 2400 }), assets: NO_ASSETS, quizLayout: measured },
);
const missing = compileVideoTimeline(
{
stage: { id: 'stage', name: 'Missing' },
scenes: [scene([{ id: 'q', type: 'single', question: 'Q' }])],
},
{ timing: stubProbe({ 'speech-0': 2400 }), assets: NO_ASSETS },
);
const unavailable = compileVideoTimeline(
{
stage: { id: 'stage', name: 'Failed' },
scenes: [scene([{ id: 'q', type: 'single', question: 'Q' }])],
},
{ timing: stubProbe({ 'speech-0': 2400 }), assets: NO_ASSETS, quizLayout: failed },
);
expect(empty.totalDurationMs).toBe(2400);
expect(empty.scenes[0].visuals).toHaveLength(1);
expect(missing.totalDurationMs).toBe(2400);
expect(missing.scenes[0].visuals).toHaveLength(1);
expect(unavailable.totalDurationMs).toBe(2400);
expect(unavailable.scenes[0].visuals).toHaveLength(1);
expect(empty.diagnostics).not.toContainEqual(
expect.objectContaining({ code: 'quiz-layout-unavailable' }),
);
expect(missing.diagnostics).toContainEqual(
expect.objectContaining({ severity: 'warn', code: 'quiz-layout-unavailable' }),
);
expect(unavailable.diagnostics).toContainEqual(
expect.objectContaining({ severity: 'warn', code: 'quiz-layout-unavailable' }),
);
});
});

View File

@ -0,0 +1,171 @@
import { describe, expect, it } from 'vitest';
import type { Action } from '@openmaic/dsl';
import {
compileVideoTimeline,
reflowQuizTimelines,
type CompilerScene,
type QuizLayoutProbe,
type SubtitleCue,
type VideoTimelineScene,
} from '@/lib/video-export';
import { NO_ASSETS, stubProbe } from './helpers';
function timedScene(id: string, index: number, startMs: number, durationMs: number) {
return {
id,
index,
title: id,
type: index === 0 ? 'quiz' : 'slide',
startMs,
durationMs,
supported: true,
base: { kind: 'visual-segments' },
visuals: [
{
kind: 'quiz-cover',
startMs,
durationMs,
title: id,
questionCount: 1,
totalPoints: 1,
},
],
narration: [
{
actionIndex: 0,
startMs: startMs + 10,
durationMs: 100,
text: id,
audio: { durationMs: 100, source: 'stored', present: false },
},
],
effects: [
{
actionIndex: 1,
type: 'spotlight',
descriptorId: 'spotlight.v1',
startMs: startMs + 20,
durationMs: 100,
elementId: 'element',
geometry: null,
params: {},
degraded: true,
},
],
videos: [
{
actionIndex: 2,
startMs: startMs + 30,
durationMs: 100,
elementId: 'element',
geometry: null,
rotate: 0,
present: false,
degraded: true,
durationSource: 'skipped',
},
],
markers: [{ actionIndex: 3, kind: 'beat', startMs: startMs + 40, durationMs: 100 }],
} as VideoTimelineScene;
}
describe('pure Quiz absolute-time reflow', () => {
it('extends the owner and shifts every absolute field and subtitle in later scenes', () => {
const scenes = [timedScene('quiz', 0, 0, 1000), timedScene('slide', 1, 1000, 2000)];
const subtitles: SubtitleCue[] = [
{ index: 0, sceneId: 'quiz', startMs: 10, endMs: 110, text: 'quiz' },
{ index: 1, sceneId: 'slide', startMs: 1010, endMs: 1110, text: 'slide' },
];
const result = reflowQuizTimelines(scenes, subtitles, 3000, [3000, 0]);
expect(result.totalDurationMs).toBe(6000);
expect(result.scenes[0]).toMatchObject({
startMs: 0,
durationMs: 4000,
narration: [{ startMs: 10 }],
effects: [{ startMs: 20 }],
videos: [{ startMs: 30 }],
markers: [{ startMs: 40 }],
});
expect(result.scenes[1]).toMatchObject({
startMs: 4000,
durationMs: 2000,
visuals: [{ startMs: 4000 }],
narration: [{ startMs: 4010 }],
effects: [{ startMs: 4020 }],
videos: [{ startMs: 4030 }],
markers: [{ startMs: 4040 }],
});
expect(result.subtitles).toEqual([
{ index: 0, sceneId: 'quiz', startMs: 10, endMs: 110, text: 'quiz' },
{ index: 1, sceneId: 'slide', startMs: 4010, endMs: 4110, text: 'slide' },
]);
});
it('accumulates extensions from consecutive Quiz scenes without changing relative action timing', () => {
const scenes = [
timedScene('quiz-1', 0, 0, 1000),
timedScene('quiz-2', 1, 1000, 1000),
timedScene('slide', 2, 2000, 1000),
];
const result = reflowQuizTimelines(scenes, [], 3000, [3000, 7000, 0]);
expect(result.scenes.map((scene) => [scene.startMs, scene.durationMs])).toEqual([
[0, 4000],
[4000, 8000],
[12_000, 1000],
]);
expect(result.scenes[2].narration[0].startMs - result.scenes[2].startMs).toBe(10);
expect(result.totalDurationMs).toBe(13_000);
});
});
function quiz(id: string, order: number): CompilerScene {
return {
id,
stageId: 'stage',
title: id,
order,
type: 'quiz',
content: { type: 'quiz', questions: [{ id: `${id}-q`, type: 'single', question: 'Q' }] },
actions: [{ id: `${id}-speech`, type: 'speech', text: id } as Action],
} as CompilerScene;
}
describe('Quiz scroll-duration bounds and cumulative compiler reflow', () => {
it('clamps one-pixel overflow to 4s and extreme overflow to 24s', () => {
const probe: QuizLayoutProbe = {
measureQuestionList: (scene) =>
scene.id === 'min'
? { contentHeightPx: 401, viewportHeightPx: 400, frameHeightPx: 720 }
: { contentHeightPx: 100_000, viewportHeightPx: 400, frameHeightPx: 720 },
};
const ir = compileVideoTimeline(
{ stage: { id: 'stage', name: 'Bounds' }, scenes: [quiz('min', 0), quiz('max', 1)] },
{
timing: stubProbe({ 'min-speech': 1000, 'max-speech': 1000 }),
assets: NO_ASSETS,
quizLayout: probe,
},
);
const lists = ir.scenes.map((scene) => scene.visuals[1]);
expect(lists[0]).toMatchObject({
scrollDistancePx: 1,
scrollDurationMs: 4000,
pixelsPerSecond: 0.25,
});
expect(lists[1]).toMatchObject({
scrollDistancePx: 99_600,
scrollDurationMs: 24_000,
pixelsPerSecond: 4150,
});
expect(ir.scenes.map((scene) => [scene.startMs, scene.durationMs])).toEqual([
[0, 8000],
[8000, 28_000],
]);
expect(ir.subtitles.map((cue) => cue.startMs)).toEqual([0, 8000]);
expect(ir.totalDurationMs).toBe(36_000);
});
});

View File

@ -0,0 +1,46 @@
import type { Action } from '@openmaic/dsl';
import type { CompilerScene, QuizLayoutMeasurement } from '@/lib/video-export';
export const QUIZ_SCROLL_TITLE =
'Quiz:确定性导出与超长标题排版验证 SupercalifragilisticexpialidociousWithoutBreaks';
export const QUIZ_SCROLL_QUESTIONS: readonly unknown[] = Array.from({ length: 9 }, (_, index) => ({
id: `long-${index + 1}`,
type: index % 3 === 0 ? 'short_answer' : index % 2 === 0 ? 'multiple' : 'single',
question:
index % 2 === 0
? `第 ${index + 1} 题:请结合公式 $a^2+b^2=c^2$ 解释这个较长的中文问题,并注意公式字体和多行排版保持稳定。`
: `Question ${index + 1}: choose the statement that correctly explains $\\sqrt{x^2}=|x|$ across a deliberately long Latin prompt.`,
options: [
{ value: 'A', label: 'A concise option with $x^2$.' },
{ value: 'B', label: '一个包含 $\\frac{a}{b}$ 的较长中文选项,用于验证真实换行。' },
{ value: 'C', label: 'Supercalifragilisticexpialidocious wraps without escaping.' },
],
answer: ['A'],
analysis: 'SECRET_SAMPLE_ANALYSIS',
}));
/** Chromium-pinned geometry for this exact shared fixture at 1280×720. */
export const QUIZ_SCROLL_LAYOUT_720P: QuizLayoutMeasurement = {
contentHeightPx: 2170,
viewportHeightPx: 475,
frameHeightPx: 720,
};
export function quizScrollScene(order = 0): CompilerScene {
return {
id: `quiz-${order}`,
stageId: 'stage',
title: QUIZ_SCROLL_TITLE,
order,
type: 'quiz',
content: { type: 'quiz', questions: QUIZ_SCROLL_QUESTIONS },
actions: [
{
id: `quiz-narration-${order}`,
type: 'speech',
text: '字幕与 Quiz 封面同时显示,用于验证长文本卡片不会遮挡底部字幕。',
} as Action,
],
} as CompilerScene;
}

View File

@ -111,7 +111,7 @@ describe('video-export cover visual pass', () => {
}),
);
expect(ir.version).toBe(3);
expect(ir.version).toBe(4);
expect(ir.scenes[0]).toMatchObject({
supported: true,
base: { kind: 'visual-segments' },