File size: 7,640 Bytes
146ca6d
 
5bbf908
 
 
 
146ca6d
 
5bbf908
 
 
 
 
 
 
 
 
 
 
 
146ca6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import type { CompositionDocumentV1 } from "@oloka/contracts";
import { describe, expect, it } from "vitest";
import {
  materializeCompositionPreview,
  readMaterializationStage,
} from "./composition-materializer.js";

describe("composition preview materializer", () => {
  it("exposes only the fixed materialization stage attached to a failure", () => {
    const error = new Error("sensitive materializer detail");
    Object.defineProperty(error, "materializationStage", {
      value: "load-font",
    });

    expect(readMaterializationStage(error)).toBe("load-font");
    expect(
      readMaterializationStage({ materializationStage: "private/path" }),
    ).toBeUndefined();
  });

  it("produces identical self-contained bytes for the full structured fixture", async () => {
    const assets = [
      {
        id: AUDIO_ID,
        mime: "audio/wav",
        bytes: Buffer.from("deterministic-audio"),
      },
      {
        id: IMAGE_ID,
        mime: "image/png",
        bytes: Buffer.from("deterministic-image"),
      },
    ];
    const originalOrder = assets.map((asset) => asset.id);

    const first = await materializeCompositionPreview(fixture(), assets);
    const second = await materializeCompositionPreview(
      fixture(),
      [...assets].reverse(),
    );
    const html = Buffer.from(first.bytes).toString("utf8");

    expect(first.checksumSha256).toBe(second.checksumSha256);
    expect(
      Buffer.compare(Buffer.from(first.bytes), Buffer.from(second.bytes)),
    ).toBe(0);
    expect(first.bytes.byteLength).toBeGreaterThan(2_000_000);
    expect(assets.map((asset) => asset.id)).toEqual(originalOrder);
    expect(html).toContain("data:font/ttf;base64,");
    expect(html).toContain("data:image/png;base64,");
    expect(html).toContain("data:audio/wav;base64,");
    expect(html).toContain('data-kind="shape"');
    expect(html).toContain('data-kind="text"');
    expect(html).toContain("ducking");
    expect(html).not.toContain("nonce=");
    expect(html).not.toMatch(/channel=["'][a-f0-9]{32}/);
  });

  it("materializes all four exact frame presets deterministically", async () => {
    const presets = [
      ["16:9", 1920, 1080],
      ["9:16", 1080, 1920],
      ["1:1", 1080, 1080],
      ["4:5", 1080, 1350],
    ] as const;
    for (const [aspectRatio, width, height] of presets) {
      const first = await materializeCompositionPreview(
        fixture(aspectRatio),
        [],
      );
      const second = await materializeCompositionPreview(
        fixture(aspectRatio),
        [],
      );
      const html = Buffer.from(first.bytes).toString("utf8");
      expect(first.checksumSha256).toBe(second.checksumSha256);
      expect(html).toContain(`data-width="${width}" data-height="${height}"`);
    }
  });
});

const IMAGE_ID = "30000000-0000-4000-8000-000000000001";
const AUDIO_ID = "30000000-0000-4000-8000-000000000002";

function fixture(
  aspectRatio: CompositionDocumentV1["aspectRatio"] = "16:9",
): CompositionDocumentV1 {
  const dimensions = (
    {
      "16:9": [1920, 1080],
      "9:16": [1080, 1920],
      "1:1": [1080, 1080],
      "4:5": [1080, 1350],
    } satisfies Record<
      CompositionDocumentV1["aspectRatio"],
      readonly [number, number]
    >
  )[aspectRatio];
  return {
    schemaVersion: 1,
    compositionId: "10000000-0000-4000-8000-000000000001",
    name: "Bản dựng tiếng Việt",
    aspectRatio,
    width: dimensions[0],
    height: dimensions[1],
    fps: 30,
    durationMs: 4_000,
    background: "#102030",
    scenes: [
      {
        id: "20000000-0000-4000-8000-000000000001",
        order: 0,
        startMs: 0,
        durationMs: 4_000,
        text: "Cộng hòa Xã hội Chủ nghĩa Việt Nam\nDấu kết hợp: Việt Nam!",
        narration: {
          itemKey: "scene-1",
          text: "Xin chào Việt Nam",
          silence: false,
          locale: "vi-VN",
          rate: 1,
          gainDb: 0,
        },
        assetReferences: [
          { assetId: IMAGE_ID, usage: "visual" },
          { assetId: AUDIO_ID, usage: "audio" },
        ],
        style: {
          layout: "center",
          foreground: "#ffffff",
          accent: "#ffd23f",
          paddingPercent: 8,
          gapPercent: 4,
        },
        tracks: [
          {
            id: "40000000-0000-4000-8000-000000000001",
            type: "overlay",
            order: 0,
            clips: [
              {
                id: "50000000-0000-4000-8000-000000000001",
                kind: "shape",
                startMs: 0,
                durationMs: 4_000,
                layerOrder: 0,
                transitionIn: {
                  type: "wipe",
                  durationMs: 400,
                  easing: "ease-out",
                },
                transitionOut: {
                  type: "fade",
                  durationMs: 300,
                  easing: "ease-in",
                },
                animations: [
                  {
                    property: "scale",
                    easing: "ease-in-out",
                    keyframes: [
                      { offset: 0, value: 0.9 },
                      { offset: 1, value: 1 },
                    ],
                  },
                ],
                primitive: "rectangle",
                fill: "#102030cc",
                stroke: "#ffd23f",
                strokeWidth: 4,
                radius: 32,
                bounds: {
                  x: 0.1,
                  y: 0.2,
                  width: 0.8,
                  height: 0.6,
                  anchor: "center",
                },
              },
              {
                id: "50000000-0000-4000-8000-000000000002",
                kind: "text",
                startMs: 0,
                durationMs: 4_000,
                layerOrder: 1,
                animations: [],
                text: "Nội dung nhiều dòng, có dấu tiếng Việt.",
                typography: {
                  fontToken: "oloka-sans-v1",
                  size: 64,
                  weight: 700,
                  lineHeight: 1.2,
                  align: "center",
                  color: "#ffffff",
                  maxLines: 3,
                  overflow: "wrap",
                },
                bounds: {
                  x: 0.15,
                  y: 0.3,
                  width: 0.7,
                  height: 0.4,
                  anchor: "center",
                },
              },
            ],
          },
        ],
      },
    ],
    voiceConfig: {
      voiceToken: "neutral-vi-v1",
      locale: "vi-VN",
      rate: 1,
      gainDb: 0,
    },
    captionConfig: {
      enabled: true,
      preset: "Bold",
      fontToken: "oloka-sans-v1",
      safeMarginPercent: 8,
      maxLines: 3,
    },
    bgmConfig: {
      assetId: AUDIO_ID,
      volume: 0.5,
      ducking: {
        enabled: true,
        targetVolume: 0.2,
        attackMs: 150,
        releaseMs: 300,
      },
    },
    manifests: {
      dependency: { schemaVersion: 1, hashSha256: "a".repeat(64) },
      asset: { schemaVersion: 1, hashSha256: "b".repeat(64) },
      font: { schemaVersion: 1, hashSha256: "c".repeat(64) },
      caption: { schemaVersion: 1, hashSha256: "d".repeat(64) },
    },
    runtimeVersions: {
      materializer: "oloka-composition-v1.0.0",
      renderer: "oloka-browser-v1",
      renderProtocol: 1,
      hyperframes: "0.7.104",
      hyperframesRuntimeSha256: "e".repeat(64),
      templateRegistry: "oloka-registry-v1",
    },
    metadata: { locale: "vi-VN", title: "Bản dựng", safeAreaMode: "action" },
  };
}