返回 CodeWhale
media-manifest.test.ts
根目录 / web / lib / media-manifest.test.ts
1 /**
2 * Media-manifest contracts for the real-session media surface.
3 *
4 * Two enforceable states:
5 * pending — intent only: no asset fields, and no files may exist under
6 * web/public/media/. The component must render the visible
7 * "recording pending release candidate" state instead of any
8 * imagery. This is what keeps the site honest until the v0.9.2
9 * dogfood recording (#4906) exists.
10 * published — complete or nothing: poster, video, per-locale captions,
11 * transcript, and GIF fallback all present. The suite checks
12 * file presence/bytes and PNG dimensions; video dimensions and
13 * duration are declared metadata whose physical verification
14 * remains in the recording checklist.
15 *
16 * The reduced-motion contract is structural: no autoplay anywhere, poster is
17 * the static default, and the GIF is offered only as a link. The test asserts
18 * the component source carries that contract.
19 */
20 import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
21 import { describe, expect, it } from "vitest";
22 import {
23 getMediaAsset,
24 MEDIA_ASSETS,
25 MEDIA_BUDGETS,
26 MEDIA_PUBLIC_DIR,
27 REDUCED_MOTION_POLICY,
28 type MediaAsset,
29 } from "./media-manifest";
30 import { ALL_LOCALES } from "./i18n/config";
31
32 // Captions are required for locales that ship a complete pack, not for every
33 // routed locale: `locales` also includes `partial` locales, which route with an
34 // English-fallback pack. Promising a caption track per routed locale would be a
35 // claim the recording cannot meet, and the manifest exists to keep those claims
36 // honest.
37 const shippedLocales = ALL_LOCALES.filter((l) => l.status === "shipped").map((l) => l.code);
38
39 const webRoot = new URL("../", import.meta.url);
40 const repoRoot = new URL("../../", import.meta.url);
41
42 function publicFileBytes(src: string): number {
43 return statSync(new URL(`public/${src}`, webRoot)).size;
44 }
45
46 function pngDimensions(src: string): [number, number] {
47 const image = readFileSync(new URL(`public/${src}`, webRoot));
48 expect(image.subarray(1, 4).toString("ascii")).toBe("PNG");
49 return [image.readUInt32BE(16), image.readUInt32BE(20)];
50 }
51
52 describe("media manifest integrity", () => {
53 it("has unique asset ids and complete localized copy", () => {
54 const ids = MEDIA_ASSETS.map((a) => a.id);
55 expect(new Set(ids).size).toBe(ids.length);
56 for (const asset of MEDIA_ASSETS) {
57 for (const pair of [asset.title, asset.description, asset.pendingLabel]) {
58 expect(pair.en.trim().length, `${asset.id} en`).toBeGreaterThan(0);
59 expect(pair.zh.trim().length, `${asset.id} zh`).toBeGreaterThan(0);
60 }
61 expect(asset.id).toMatch(/^[a-z0-9-]+$/);
62 }
63 });
64
65 it("keeps pending entries asset-free on disk and in the manifest", () => {
66 const mediaDir = new URL(`public/${MEDIA_PUBLIC_DIR}/`, webRoot);
67 const onDisk = existsSync(mediaDir) ? readdirSync(mediaDir) : [];
68
69 for (const asset of MEDIA_ASSETS.filter((a) => a.status === "pending")) {
70 expect(asset.poster, asset.id).toBeUndefined();
71 expect(asset.video, asset.id).toBeUndefined();
72 expect(asset.captions, asset.id).toBeUndefined();
73 expect(asset.gifFallback, asset.id).toBeUndefined();
74 expect(asset.transcript, asset.id).toBeUndefined();
75 // No staged or fabricated file may exist for a pending asset.
76 for (const file of onDisk) {
77 expect(file.startsWith(asset.id), `${asset.id}: unexpected file ${file}`).toBe(false);
78 }
79 // The pending state must say why, in both locales.
80 expect(asset.pendingLabel.en).toContain("pending release candidate");
81 expect(asset.pendingLabel.zh.length).toBeGreaterThan(0);
82 }
83 });
84
85 it("requires published entries to be complete and satisfy enforceable budgets", () => {
86 for (const asset of MEDIA_ASSETS.filter((a) => a.status === "published")) {
87 assertPublishedAsset(asset);
88 }
89 });
90
91 it("keeps budgets aligned with the canonical screenshot contract", () => {
92 // The site screenshot contract pins 1280×720 under 500 KB; session-media
93 // posters follow the same budget so the two surfaces stay consistent.
94 expect(MEDIA_BUDGETS.poster).toEqual({ width: 1280, height: 720, maxBytes: 500_000 });
95 expect(MEDIA_BUDGETS.video.width).toBe(MEDIA_BUDGETS.poster.width);
96 expect(MEDIA_BUDGETS.video.height).toBe(MEDIA_BUDGETS.poster.height);
97 expect(MEDIA_BUDGETS.video.maxDurationSeconds).toBeLessThanOrEqual(120);
98 expect(MEDIA_BUDGETS.gifFallback.maxBytes).toBeGreaterThan(0);
99 for (const locale of shippedLocales) {
100 expect(MEDIA_BUDGETS.captionLocales).toContain(locale);
101 }
102 });
103 });
104
105 function assertPublishedAsset(asset: MediaAsset): void {
106 const { poster, video, captions, gifFallback, transcript } = asset;
107 expect(poster, `${asset.id}.poster`).toBeTruthy();
108 expect(video, `${asset.id}.video`).toBeTruthy();
109 expect(captions, `${asset.id}.captions`).toBeTruthy();
110 expect(gifFallback, `${asset.id}.gifFallback`).toBeTruthy();
111 expect(transcript, `${asset.id}.transcript`).toBeTruthy();
112
113 expect(pngDimensions(poster!.src)).toEqual([poster!.width, poster!.height]);
114 expect([poster!.width, poster!.height]).toEqual([
115 MEDIA_BUDGETS.poster.width,
116 MEDIA_BUDGETS.poster.height,
117 ]);
118 expect(publicFileBytes(poster!.src)).toBeLessThanOrEqual(MEDIA_BUDGETS.poster.maxBytes);
119
120 expect([video!.width, video!.height]).toEqual([
121 MEDIA_BUDGETS.video.width,
122 MEDIA_BUDGETS.video.height,
123 ]);
124 expect(video!.durationSeconds).toBeLessThanOrEqual(MEDIA_BUDGETS.video.maxDurationSeconds);
125 expect(publicFileBytes(video!.src)).toBeLessThanOrEqual(MEDIA_BUDGETS.video.maxBytes);
126
127 expect(publicFileBytes(gifFallback!.src)).toBeLessThanOrEqual(
128 MEDIA_BUDGETS.gifFallback.maxBytes,
129 );
130
131 const captionLangs = captions!.map((t) => t.srclang);
132 for (const locale of MEDIA_BUDGETS.captionLocales) {
133 expect(captionLangs, `${asset.id} missing ${locale} captions`).toContain(locale);
134 }
135 for (const track of captions!) {
136 expect(track.src.endsWith(".vtt"), track.src).toBe(true);
137 expect(publicFileBytes(track.src), track.src).toBeGreaterThan(0);
138 expect(track.label.trim().length).toBeGreaterThan(0);
139 }
140
141 expect(existsSync(new URL(transcript!, repoRoot)), `${asset.id}.transcript`).toBe(true);
142 }
143
144 describe("session media component contract", () => {
145 const component = readFileSync(
146 new URL("../components/session-media.tsx", import.meta.url),
147 "utf8",
148 );
149
150 it("renders the visible pending state instead of any imagery", () => {
151 expect(component).toContain('asset.status === "pending"');
152 expect(component).toContain("session-media-pending");
153 expect(component).toContain("asset.pendingLabel");
154 // Pending copy must explicitly refuse placeholder footage, both locales.
155 expect(component).toContain("No placeholder or staged footage");
156 expect(component).toContain("占位或摆拍影像");
157 });
158
159 it("carries the structural reduced-motion contract: no autoplay, ever", () => {
160 expect(component).toContain("REDUCED_MOTION_POLICY");
161 expect(REDUCED_MOTION_POLICY).toBe("static-poster-no-autoplay");
162 // No autoplay as a JSX/DOM attribute (the policy string and prose may
163 // mention the word; the attribute must never appear).
164 expect(component).not.toMatch(/autoPlay\s*[={]/);
165 expect(component).not.toMatch(/\sautoplay\s*[="]/);
166 expect(component).toContain('preload="none"');
167 expect(component).toContain("controls");
168 expect(component).toContain("poster={`/${poster.src}`}");
169 });
170
171 it("wires captions, transcript, and the GIF fallback for published assets", () => {
172 expect(component).toContain('kind="captions"');
173 expect(component).toContain("srcLang={track.srclang}");
174 expect(component).toContain("asset.gifFallback");
175 expect(component).toContain("asset.transcript");
176 });
177
178 it("exposes machine-checkable state hooks", () => {
179 expect(component).toContain("data-media-id={asset.id}");
180 expect(component).toContain("data-media-status={asset.status}");
181 expect(component).toContain("data-reduced-motion-policy={REDUCED_MOTION_POLICY}");
182 });
183
184 it("documents the recording plan location from the pending state", () => {
185 expect(component).toContain("docs/releases/v0.9.2-media-plan.md");
186 expect(existsSync(new URL("docs/releases/v0.9.2-media-plan.md", repoRoot))).toBe(true);
187 });
188
189 it("keeps the first-fleet-session entry addressable for the guide page", () => {
190 const asset = getMediaAsset("first-fleet-session");
191 expect(asset).toBeTruthy();
192 expect(["pending", "published"]).toContain(asset!.status);
193 const guide = readFileSync(
194 new URL("../app/[locale]/docs/guide/page.tsx", import.meta.url),
195 "utf8",
196 );
197 expect(guide).toContain('getMediaAsset("first-fleet-session")');
198 expect(guide).toContain("<SessionMedia");
199 });
200 });
201
201 lines TYPESCRIPT