@softure-ai/marketing-kit 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/README.md +107 -15
- package/dist/cli/main.js +84 -29
- package/dist/cli/main.js.map +1 -1
- package/dist/cli/og.d.ts +4 -1
- package/dist/cli/og.d.ts.map +1 -1
- package/dist/cli/og.js +15 -7
- package/dist/cli/og.js.map +1 -1
- package/dist/cli/options.d.ts +26 -2
- package/dist/cli/options.d.ts.map +1 -1
- package/dist/cli/options.js +97 -9
- package/dist/cli/options.js.map +1 -1
- package/dist/cli/voice.d.ts +14 -0
- package/dist/cli/voice.d.ts.map +1 -1
- package/dist/cli/voice.js +13 -0
- package/dist/cli/voice.js.map +1 -1
- package/dist/config/actions-schema.d.ts +21 -15
- package/dist/config/actions-schema.d.ts.map +1 -1
- package/dist/config/actions-schema.js +18 -3
- package/dist/config/actions-schema.js.map +1 -1
- package/dist/config/config.d.ts +2 -0
- package/dist/config/config.d.ts.map +1 -1
- package/dist/config/config.js +2 -2
- package/dist/config/config.js.map +1 -1
- package/dist/config/og-image-names.d.ts +7 -0
- package/dist/config/og-image-names.d.ts.map +1 -0
- package/dist/config/og-image-names.js +9 -0
- package/dist/config/og-image-names.js.map +1 -0
- package/dist/config/schema.d.ts +111 -16
- package/dist/config/schema.d.ts.map +1 -1
- package/dist/config/schema.js +77 -11
- package/dist/config/schema.js.map +1 -1
- package/dist/film.d.ts +20 -5
- package/dist/film.d.ts.map +1 -1
- package/dist/film.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/og/index.d.ts +3 -3
- package/dist/og/index.d.ts.map +1 -1
- package/dist/og/index.js +3 -2
- package/dist/og/index.js.map +1 -1
- package/dist/og/render.d.ts +13 -0
- package/dist/og/render.d.ts.map +1 -1
- package/dist/og/render.js +31 -6
- package/dist/og/render.js.map +1 -1
- package/dist/og/templates/big-number.d.ts +7 -0
- package/dist/og/templates/big-number.d.ts.map +1 -0
- package/dist/og/templates/big-number.js +41 -0
- package/dist/og/templates/big-number.js.map +1 -0
- package/dist/og/templates/carousel.d.ts +5 -0
- package/dist/og/templates/carousel.d.ts.map +1 -0
- package/dist/og/templates/carousel.js +26 -0
- package/dist/og/templates/carousel.js.map +1 -0
- package/dist/og/templates/context.d.ts +11 -3
- package/dist/og/templates/context.d.ts.map +1 -1
- package/dist/og/templates/context.js +6 -0
- package/dist/og/templates/context.js.map +1 -1
- package/dist/og/templates/frame.d.ts +2 -2
- package/dist/og/templates/frame.d.ts.map +1 -1
- package/dist/og/templates/frame.js +2 -2
- package/dist/og/templates/frame.js.map +1 -1
- package/dist/og/templates/index.d.ts +8 -3
- package/dist/og/templates/index.d.ts.map +1 -1
- package/dist/og/templates/index.js +18 -5
- package/dist/og/templates/index.js.map +1 -1
- package/dist/og/templates/portrait.d.ts +22 -0
- package/dist/og/templates/portrait.d.ts.map +1 -0
- package/dist/og/templates/portrait.js +75 -0
- package/dist/og/templates/portrait.js.map +1 -0
- package/dist/og/templates/schemas.d.ts +28 -0
- package/dist/og/templates/schemas.d.ts.map +1 -1
- package/dist/og/templates/schemas.js +26 -0
- package/dist/og/templates/schemas.js.map +1 -1
- package/dist/record/actions.d.ts.map +1 -1
- package/dist/record/actions.js +6 -1
- package/dist/record/actions.js.map +1 -1
- package/dist/record/record.d.ts.map +1 -1
- package/dist/record/record.js +51 -17
- package/dist/record/record.js.map +1 -1
- package/dist/record/screen-guard.d.ts +13 -0
- package/dist/record/screen-guard.d.ts.map +1 -0
- package/dist/record/screen-guard.js +27 -0
- package/dist/record/screen-guard.js.map +1 -0
- package/dist/screenshot/gates.d.ts +6 -3
- package/dist/screenshot/gates.d.ts.map +1 -1
- package/dist/screenshot/gates.js +12 -3
- package/dist/screenshot/gates.js.map +1 -1
- package/dist/screenshot/screenshot.d.ts +17 -0
- package/dist/screenshot/screenshot.d.ts.map +1 -1
- package/dist/screenshot/screenshot.js +37 -6
- package/dist/screenshot/screenshot.js.map +1 -1
- package/dist/screenshot/storage-state.d.ts +9 -0
- package/dist/screenshot/storage-state.d.ts.map +1 -0
- package/dist/screenshot/storage-state.js +25 -0
- package/dist/screenshot/storage-state.js.map +1 -0
- package/dist/voice/placeholder.d.ts +35 -0
- package/dist/voice/placeholder.d.ts.map +1 -0
- package/dist/voice/placeholder.js +53 -0
- package/dist/voice/placeholder.js.map +1 -0
- package/package.json +2 -1
- package/schema/marketing.schema.json +392 -43
- package/src/cli/main.ts +80 -27
- package/src/cli/og.ts +14 -6
- package/src/cli/options.ts +109 -10
- package/src/cli/voice.ts +20 -0
- package/src/config/actions-schema.ts +18 -3
- package/src/config/config.ts +4 -3
- package/src/config/og-image-names.ts +7 -0
- package/src/config/schema.ts +91 -13
- package/src/film.ts +15 -5
- package/src/index.ts +7 -1
- package/src/og/index.ts +16 -2
- package/src/og/render.ts +36 -6
- package/src/og/templates/big-number.ts +44 -0
- package/src/og/templates/carousel.ts +27 -0
- package/src/og/templates/context.ts +16 -3
- package/src/og/templates/frame.ts +2 -2
- package/src/og/templates/index.ts +31 -9
- package/src/og/templates/portrait.ts +126 -0
- package/src/og/templates/schemas.ts +35 -0
- package/src/record/actions.ts +6 -1
- package/src/record/record.ts +53 -19
- package/src/record/screen-guard.ts +37 -0
- package/src/screenshot/gates.ts +13 -3
- package/src/screenshot/screenshot.ts +51 -11
- package/src/screenshot/storage-state.ts +24 -0
- package/src/voice/placeholder.ts +83 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { h, type OgChild, type OgNode } from "../element.js";
|
|
2
|
+
import type { OgTemplateContext } from "./context.js";
|
|
3
|
+
import { px, text, type Tile } from "./frame.js";
|
|
4
|
+
|
|
5
|
+
/** Sizes in this file are written for the 1080×1350 post (`PORTRAIT_BASE`). */
|
|
6
|
+
|
|
7
|
+
const PADDING = 80;
|
|
8
|
+
|
|
9
|
+
export interface PortraitFrame {
|
|
10
|
+
/** "2/6" in the top corner, or null. */
|
|
11
|
+
counter: string | null;
|
|
12
|
+
content: (OgChild | null)[];
|
|
13
|
+
cta: string | undefined;
|
|
14
|
+
source: string | undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The portrait post: the logo and name on top (the counter opposite), the content filling the middle, and the call
|
|
19
|
+
* to action and source line pinned to the bottom, so a tall frame spreads the post instead of adding empty bands.
|
|
20
|
+
*/
|
|
21
|
+
export function portraitFrame(context: OgTemplateContext, frame: PortraitFrame): OgNode {
|
|
22
|
+
const { palette } = context;
|
|
23
|
+
const logoSize = px(context, 64);
|
|
24
|
+
const hasFooter = frame.cta !== undefined || frame.source !== undefined;
|
|
25
|
+
return h(
|
|
26
|
+
"div",
|
|
27
|
+
{
|
|
28
|
+
style: {
|
|
29
|
+
display: "flex",
|
|
30
|
+
flexDirection: "column",
|
|
31
|
+
width: "100%",
|
|
32
|
+
height: "100%",
|
|
33
|
+
padding: px(context, PADDING),
|
|
34
|
+
backgroundColor: palette.background,
|
|
35
|
+
color: palette.foreground,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
h(
|
|
39
|
+
"div",
|
|
40
|
+
{ style: { display: "flex", alignItems: "center", justifyContent: "space-between" } },
|
|
41
|
+
h(
|
|
42
|
+
"div",
|
|
43
|
+
{ style: { display: "flex", alignItems: "center", gap: px(context, 20) } },
|
|
44
|
+
context.brand.logo === null ? null : h("img", { src: context.brand.logo, width: logoSize, height: logoSize, style: { width: logoSize, height: logoSize } }),
|
|
45
|
+
h("div", { style: { ...text(context, context.fonts.body, 32, 600), wordBreak: "break-word", color: palette.muted } }, context.brand.name),
|
|
46
|
+
),
|
|
47
|
+
frame.counter === null ? null : h("div", { style: { ...text(context, context.fonts.body, 32, 600), wordBreak: "break-word", color: palette.muted } }, frame.counter),
|
|
48
|
+
),
|
|
49
|
+
h(
|
|
50
|
+
"div",
|
|
51
|
+
{ style: { display: "flex", flexDirection: "column", flexGrow: 1, justifyContent: "center", marginTop: px(context, 32), marginBottom: px(context, 32) } },
|
|
52
|
+
...frame.content,
|
|
53
|
+
),
|
|
54
|
+
hasFooter
|
|
55
|
+
? h(
|
|
56
|
+
"div",
|
|
57
|
+
{ style: { display: "flex", flexDirection: "column", alignItems: "flex-start", gap: px(context, 28) } },
|
|
58
|
+
frame.cta === undefined
|
|
59
|
+
? null
|
|
60
|
+
: h(
|
|
61
|
+
"div",
|
|
62
|
+
{
|
|
63
|
+
style: {
|
|
64
|
+
...text(context, context.fonts.body, 34, 600),
|
|
65
|
+
wordBreak: "break-word",
|
|
66
|
+
maxWidth: "100%",
|
|
67
|
+
padding: `${px(context, 18)}px ${px(context, 36)}px`,
|
|
68
|
+
borderRadius: px(context, 999),
|
|
69
|
+
backgroundColor: palette.cta,
|
|
70
|
+
color: palette.onCta,
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
frame.cta,
|
|
74
|
+
),
|
|
75
|
+
frame.source === undefined ? null : h("div", { style: { ...text(context, context.fonts.body, 24, 400), wordBreak: "break-word", lineHeight: 1.3, color: palette.muted } }, frame.source),
|
|
76
|
+
)
|
|
77
|
+
: null,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function portraitEyebrow(context: OgTemplateContext, value: string | undefined): OgNode | null {
|
|
82
|
+
if (value === undefined) return null;
|
|
83
|
+
return h("div", { style: { ...text(context, context.fonts.body, 34, 600), wordBreak: "break-word", color: context.palette.accent, marginBottom: px(context, 24) } }, value);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The portrait headline size: shorter headlines get bigger type. */
|
|
87
|
+
export function portraitHeadlineSize(headline: string): number {
|
|
88
|
+
if (headline.length <= 30) return 84;
|
|
89
|
+
if (headline.length <= 60) return 70;
|
|
90
|
+
return 60;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function portraitHeadline(context: OgTemplateContext, value: string): OgNode {
|
|
94
|
+
return h("div", { style: { ...text(context, context.fonts.heading, portraitHeadlineSize(value), 700), wordBreak: "break-word", lineHeight: 1.12, color: context.palette.foreground } }, value);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Up to two figures in boxes side by side: the value large, the label under it. */
|
|
98
|
+
export function portraitTiles(context: OgTemplateContext, entries: readonly Tile[]): OgNode | null {
|
|
99
|
+
if (entries.length === 0) return null;
|
|
100
|
+
const { palette } = context;
|
|
101
|
+
const gap = px(context, 20);
|
|
102
|
+
// Two per row: half of the content width (the frame's padding off both sides) minus half the gap.
|
|
103
|
+
const tileWidth = Math.floor((context.width - 2 * px(context, PADDING) - gap) / 2);
|
|
104
|
+
return h(
|
|
105
|
+
"div",
|
|
106
|
+
{ style: { display: "flex", flexWrap: "wrap", gap, marginTop: px(context, 40) } },
|
|
107
|
+
...entries.map((tile) =>
|
|
108
|
+
h(
|
|
109
|
+
"div",
|
|
110
|
+
{
|
|
111
|
+
style: {
|
|
112
|
+
display: "flex",
|
|
113
|
+
flexDirection: "column",
|
|
114
|
+
width: tileWidth,
|
|
115
|
+
padding: `${px(context, 22)}px ${px(context, 28)}px`,
|
|
116
|
+
borderRadius: px(context, 24),
|
|
117
|
+
border: `${px(context, 2)}px solid ${palette.border}`,
|
|
118
|
+
backgroundColor: palette.surface,
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
h("div", { style: { ...text(context, context.fonts.heading, 44, 700), wordBreak: "break-word", color: palette.foreground } }, tile.value),
|
|
122
|
+
h("div", { style: { ...text(context, context.fonts.body, 28, 400), wordBreak: "break-word", color: palette.muted, marginTop: px(context, 6) } }, tile.label),
|
|
123
|
+
),
|
|
124
|
+
),
|
|
125
|
+
);
|
|
126
|
+
}
|
|
@@ -63,3 +63,38 @@ export const headlineChartDataSchema = z.strictObject({
|
|
|
63
63
|
|
|
64
64
|
export type HeadlineCtaData = z.output<typeof headlineCtaDataSchema>;
|
|
65
65
|
export type HeadlineChartData = z.output<typeof headlineChartDataSchema>;
|
|
66
|
+
|
|
67
|
+
/*
|
|
68
|
+
* The portrait templates are written for a 1080×1350 post and scale by the limiting side, so their limits hold at
|
|
69
|
+
* every size: copy at the limits stays inside the frame (tests/og/portrait-fit.test.ts renders it and checks).
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
const source = line(80).optional().describe('Where the figures come from, drawn small at the bottom, e.g. "Source: ZUS, 2026"; at most 80 characters.');
|
|
73
|
+
const portraitCta = line(32).optional().describe("A call to action, drawn as a pill in the cta colours at the bottom; at most 32 characters.");
|
|
74
|
+
|
|
75
|
+
export const bigNumberDataSchema = z.strictObject({
|
|
76
|
+
eyebrow,
|
|
77
|
+
number: line(12).describe('The figure that fills the post, unit included, e.g. "898 PLN"; at most 12 characters. Shorter numbers get bigger type.'),
|
|
78
|
+
caption: line(90).describe("The sentence that says what the number is, under it; at most 90 characters."),
|
|
79
|
+
tiles: z.array(tileSchema).max(2, "at most two tiles fit").default([]).describe("Up to two label and value tiles under the caption, e.g. a second figure."),
|
|
80
|
+
cta: portraitCta,
|
|
81
|
+
source,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const slideSchema = z.strictObject({
|
|
85
|
+
eyebrow,
|
|
86
|
+
headline: line(90).describe("The slide's headline, at most 90 characters."),
|
|
87
|
+
body: line(200).optional().describe("A paragraph under the headline, at most 200 characters."),
|
|
88
|
+
tiles: z.array(tileSchema).max(2, "at most two tiles fit").default([]).describe("Up to two label and value tiles side by side."),
|
|
89
|
+
cta: portraitCta,
|
|
90
|
+
source,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
export const carouselDataSchema = z.strictObject({
|
|
94
|
+
slides: z.array(slideSchema).min(2, "a carousel needs at least two slides").max(10, "at most ten slides").describe("The slides in order, two to ten; each renders to its own file <id>-<n>.png."),
|
|
95
|
+
counter: z.boolean().default(true).describe('Draws "n/N" in the top corner of every slide.'),
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
export type BigNumberData = z.output<typeof bigNumberDataSchema>;
|
|
99
|
+
export type CarouselData = z.output<typeof carouselDataSchema>;
|
|
100
|
+
export type CarouselSlide = CarouselData["slides"][number];
|
package/src/record/actions.ts
CHANGED
|
@@ -55,8 +55,13 @@ export async function runAction(director: Director, action: SceneAction): Promis
|
|
|
55
55
|
return director.tap(getLocator(page, action.target), action.after === undefined ? undefined : { after: action.after });
|
|
56
56
|
case "type":
|
|
57
57
|
return director.type(action.text, action.perChar === undefined ? undefined : { perChar: action.perChar });
|
|
58
|
+
case "press":
|
|
59
|
+
return director.press(action.key, {
|
|
60
|
+
...(action.times === undefined ? {} : { times: action.times }),
|
|
61
|
+
...(action.perKey === undefined ? {} : { perKey: action.perKey }),
|
|
62
|
+
});
|
|
58
63
|
case "fill":
|
|
59
|
-
return director.fill(action.input, action.value);
|
|
64
|
+
return director.fill(action.input, action.value, action.clear === undefined ? undefined : { clear: action.clear });
|
|
60
65
|
case "blur":
|
|
61
66
|
return director.blur();
|
|
62
67
|
case "focus":
|
package/src/record/record.ts
CHANGED
|
@@ -5,8 +5,9 @@ import { chromium, type Locator } from "playwright";
|
|
|
5
5
|
|
|
6
6
|
import { fitScale, getGeometry, unionRect, type Rect } from "../compose/timeline.js";
|
|
7
7
|
import type { ColorTheme } from "../config/colors.js";
|
|
8
|
-
import {
|
|
8
|
+
import { sceneBeats, type Beat, type CueName, type Director, type Film } from "../film.js";
|
|
9
9
|
import type { BeatVoice } from "../voice/voiceover.js";
|
|
10
|
+
import { describeCheckScreenFailure, describeSentenceFailure, findMissingPhrases, getCheckScreenPhrases } from "./screen-guard.js";
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Recording of the real app **frame by frame**, with the page clock frozen.
|
|
@@ -26,6 +27,8 @@ export const FPS = 30;
|
|
|
26
27
|
const DT = 1000 / FPS;
|
|
27
28
|
/** Limit of one scene action: longer means the app looks different from what the scene expects. */
|
|
28
29
|
const ACTION_TIMEOUT_MS = 5000;
|
|
30
|
+
/** How long one typed character or pressed key stays on screen before the next. */
|
|
31
|
+
const KEY_SECONDS = 0.13;
|
|
29
32
|
|
|
30
33
|
export interface CameraCue {
|
|
31
34
|
f: number;
|
|
@@ -96,7 +99,8 @@ export async function recordFilm(options: RecordOptions): Promise<RecordingLog>
|
|
|
96
99
|
mkdirSync(framesDir, { recursive: true });
|
|
97
100
|
|
|
98
101
|
const voiceById = new Map(voices.map((voice) => [voice.id, voice]));
|
|
99
|
-
const
|
|
102
|
+
const sentenceById = new Map(sceneBeats(film).map((beat) => [beat.id, beat]));
|
|
103
|
+
const expectedBeats = [...sentenceById.keys()];
|
|
100
104
|
const log: RecordingLog = {
|
|
101
105
|
voiceoverKey: options.voiceoverKey,
|
|
102
106
|
beatIds: expectedBeats,
|
|
@@ -157,7 +161,8 @@ export async function recordFilm(options: RecordOptions): Promise<RecordingLog>
|
|
|
157
161
|
|
|
158
162
|
let virtualMs = 0;
|
|
159
163
|
let frame = 0;
|
|
160
|
-
|
|
164
|
+
/** The sentence being recorded; `isGuardChecked` once a checkScreen inside it checked its own phrases. */
|
|
165
|
+
let current: { id: string; f0: number; sentence: Beat; isGuardChecked: boolean } | null = null;
|
|
161
166
|
let guardChecked = false;
|
|
162
167
|
|
|
163
168
|
const where = (): string => (current === null ? "before the first sentence" : `sentence "${current.id}"`);
|
|
@@ -196,6 +201,10 @@ export async function recordFilm(options: RecordOptions): Promise<RecordingLog>
|
|
|
196
201
|
for (let i = 0, count = Math.round(seconds * FPS); i < count; i += 1) await shoot();
|
|
197
202
|
}
|
|
198
203
|
|
|
204
|
+
async function readScreenText(): Promise<string> {
|
|
205
|
+
return (await page.locator(settings.screenGuardSelector).first().innerText()).replace(/\s+/g, " ");
|
|
206
|
+
}
|
|
207
|
+
|
|
199
208
|
async function rectOf(target: Locator): Promise<Rect> {
|
|
200
209
|
try {
|
|
201
210
|
// "visible", not "attached": a hidden element has a 0×0 rectangle and a tap would miss.
|
|
@@ -249,13 +258,21 @@ export async function recordFilm(options: RecordOptions): Promise<RecordingLog>
|
|
|
249
258
|
);
|
|
250
259
|
}
|
|
251
260
|
const voice = voiceById.get(id);
|
|
252
|
-
|
|
253
|
-
|
|
261
|
+
const sentence = sentenceById.get(id);
|
|
262
|
+
if (voice === undefined || sentence === undefined) throw new Error(`No voiceover for sentence "${id}".`);
|
|
263
|
+
const state = { id, f0: frame, sentence, isGuardChecked: false };
|
|
264
|
+
current = state;
|
|
254
265
|
await actions();
|
|
255
266
|
const minFrames = Math.round((voice.end - voice.start + (beatOptions?.pad ?? 0.35)) * FPS);
|
|
256
|
-
const missing = minFrames - (frame -
|
|
267
|
+
const missing = minFrames - (frame - state.f0);
|
|
257
268
|
if (missing > 0) await hold(missing / FPS);
|
|
258
|
-
|
|
269
|
+
// The sentence's own phrases, on its last frame, unless a checkScreen inside it already checked them.
|
|
270
|
+
const phrases = sentence.screenGuard ?? [];
|
|
271
|
+
if (!state.isGuardChecked && phrases.length > 0) {
|
|
272
|
+
const absent = findMissingPhrases(await readScreenText(), phrases);
|
|
273
|
+
if (absent.length > 0) throw new ScreenGuardError(describeSentenceFailure(id, absent, filmPath));
|
|
274
|
+
}
|
|
275
|
+
log.beats.push({ id, f0: state.f0, f1: frame });
|
|
259
276
|
},
|
|
260
277
|
|
|
261
278
|
async until(word) {
|
|
@@ -290,11 +307,19 @@ export async function recordFilm(options: RecordOptions): Promise<RecordingLog>
|
|
|
290
307
|
for (const character of text) {
|
|
291
308
|
log.keys.push(frame);
|
|
292
309
|
await page.keyboard.type(character);
|
|
293
|
-
await hold(typeOptions?.perChar ??
|
|
310
|
+
await hold(typeOptions?.perChar ?? KEY_SECONDS);
|
|
294
311
|
}
|
|
295
312
|
},
|
|
296
313
|
|
|
297
|
-
async
|
|
314
|
+
async press(key, pressOptions) {
|
|
315
|
+
for (let i = 0, times = pressOptions?.times ?? 1; i < times; i += 1) {
|
|
316
|
+
log.keys.push(frame);
|
|
317
|
+
await page.keyboard.press(key);
|
|
318
|
+
await hold(pressOptions?.perKey ?? KEY_SECONDS);
|
|
319
|
+
}
|
|
320
|
+
},
|
|
321
|
+
|
|
322
|
+
async fill(name, value, fillOptions) {
|
|
298
323
|
const input = page.locator(`input[name=${name}]`);
|
|
299
324
|
await ensureVisible(input);
|
|
300
325
|
const rect = await rectOf(input);
|
|
@@ -302,6 +327,19 @@ export async function recordFilm(options: RecordOptions): Promise<RecordingLog>
|
|
|
302
327
|
const scale = isDesktop ? Math.min(1.55, fitScale(geometry, rect)) : 1.55;
|
|
303
328
|
log.camera.push({ f: frame, kind: "focus", rect, scale, whoosh: false });
|
|
304
329
|
await director.tap(input, { after: 0.2 });
|
|
330
|
+
// A tap leaves the caret where the finger landed and selects nothing: typed keys would join the old value.
|
|
331
|
+
// An empty field presses nothing, so it records frame for frame as before.
|
|
332
|
+
if (fillOptions?.clear !== false && (await input.inputValue()) !== "") {
|
|
333
|
+
await director.press("ControlOrMeta+A");
|
|
334
|
+
await director.press("Backspace");
|
|
335
|
+
const left = await input.inputValue();
|
|
336
|
+
if (left !== "") {
|
|
337
|
+
throw new Error(
|
|
338
|
+
`${where()}: ${String(input)} still holds "${left}" after select all and Backspace; ` +
|
|
339
|
+
`the app puts a value back. Set "clear": false or fix the scene: ${filmPath}`,
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
305
343
|
await director.type(value);
|
|
306
344
|
},
|
|
307
345
|
|
|
@@ -341,16 +379,10 @@ export async function recordFilm(options: RecordOptions): Promise<RecordingLog>
|
|
|
341
379
|
},
|
|
342
380
|
|
|
343
381
|
async checkScreen() {
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
if (missing.length > 0) {
|
|
347
|
-
throw new ScreenGuardError(
|
|
348
|
-
`The screen does not say what the voiceover says: missing ${missing.map((p) => `"${p}"`).join(", ")}. ` +
|
|
349
|
-
`The app counts from the recording day: if the voiceover was paid for on another day, pin that day in the video's ` +
|
|
350
|
-
`"today" (or pass --today=YYYY-MM-DD); otherwise fix the sentences and phrases in ${filmPath}.`,
|
|
351
|
-
);
|
|
352
|
-
}
|
|
382
|
+
const missing = findMissingPhrases(await readScreenText(), getCheckScreenPhrases(film.screenGuard, current?.sentence ?? null));
|
|
383
|
+
if (missing.length > 0) throw new ScreenGuardError(describeCheckScreenFailure(missing, filmPath));
|
|
353
384
|
guardChecked = true;
|
|
385
|
+
if (current !== null) current.isGuardChecked = true;
|
|
354
386
|
},
|
|
355
387
|
};
|
|
356
388
|
|
|
@@ -362,7 +394,9 @@ export async function recordFilm(options: RecordOptions): Promise<RecordingLog>
|
|
|
362
394
|
`The scene recorded ${log.beats.length} of ${expectedBeats.length} sentences; "${expectedBeats[log.beats.length]}" is missing.`,
|
|
363
395
|
);
|
|
364
396
|
}
|
|
365
|
-
if (!guardChecked
|
|
397
|
+
if (!guardChecked && film.screenGuard.length > 0) {
|
|
398
|
+
throw new Error("The scene never called checkScreen(): the video's screenGuard phrases were never checked, so the film could say what the screen does not show.");
|
|
399
|
+
}
|
|
366
400
|
if (log.stills[film.hook.still] === undefined) {
|
|
367
401
|
throw new Error(`The scene did not save the opening frame "${film.hook.still}" (still).`);
|
|
368
402
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { containsPhrase, type Beat } from "../film.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The screen guard's rules, apart from the browser: which phrases a check covers and what a failure says. The
|
|
5
|
+
* video's `screenGuard` is checked at every `checkScreen`; a sentence's own `screenGuard` at a `checkScreen` inside
|
|
6
|
+
* that sentence, or, without one, when the sentence ends, so each number is proven on screen while it is spoken.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** The phrases a `checkScreen` covers: the video's, plus the current sentence's own (none before the first sentence). */
|
|
10
|
+
export function getCheckScreenPhrases(videoPhrases: readonly string[], sentence: Beat | null): string[] {
|
|
11
|
+
return [...videoPhrases, ...(sentence?.screenGuard ?? [])];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** The phrases the screen's text does not contain as whole words, in their order. */
|
|
15
|
+
export function findMissingPhrases(text: string, phrases: readonly string[]): string[] {
|
|
16
|
+
return phrases.filter((phrase) => !containsPhrase(text, phrase));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const quote = (phrases: readonly string[]): string => phrases.map((phrase) => `"${phrase}"`).join(", ");
|
|
20
|
+
|
|
21
|
+
function describeFix(filmPath: string): string {
|
|
22
|
+
return (
|
|
23
|
+
`The app counts from the recording day: if the voiceover was paid for on another day, pin that day in the video's ` +
|
|
24
|
+
`"today" (or pass --today=YYYY-MM-DD); otherwise fix the sentences and phrases in ${filmPath}.`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function describeCheckScreenFailure(missing: readonly string[], filmPath: string): string {
|
|
29
|
+
return `The screen does not say what the voiceover says: missing ${quote(missing)}. ${describeFix(filmPath)}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function describeSentenceFailure(sentenceId: string, missing: readonly string[], filmPath: string): string {
|
|
33
|
+
return (
|
|
34
|
+
`Sentence "${sentenceId}" ended without its screenGuard phrases on screen: missing ${quote(missing)}. ` +
|
|
35
|
+
`Reveal them before the sentence ends, or check them where they show with a checkScreen inside the sentence. ${describeFix(filmPath)}`
|
|
36
|
+
);
|
|
37
|
+
}
|
package/src/screenshot/gates.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The quality gates a screenshot passes before it is kept (FIRE's screenshot script): the page
|
|
3
|
-
* answered below HTTP 400, it
|
|
4
|
-
* blank page, an error page
|
|
3
|
+
* answered below HTTP 400, it reached the requested scroll offset, it shows the expected phrase, and
|
|
4
|
+
* the file is not suspiciously small. A blank page, an error page, a half-loaded one or a frame of
|
|
5
|
+
* the wrong place fails at least one of them.
|
|
5
6
|
*/
|
|
6
7
|
|
|
7
|
-
export const SCREENSHOT_GATES = ["load", "status", "phrase", "size"] as const;
|
|
8
|
+
export const SCREENSHOT_GATES = ["load", "status", "scroll", "phrase", "size"] as const;
|
|
8
9
|
|
|
9
10
|
export type ScreenshotGate = (typeof SCREENSHOT_GATES)[number];
|
|
10
11
|
|
|
@@ -18,6 +19,15 @@ export function findStatusFailure(status: number | null, url: string): string |
|
|
|
18
19
|
return null;
|
|
19
20
|
}
|
|
20
21
|
|
|
22
|
+
/** Browsers report fractional offsets on scaled screens; half a pixel short still counts as there. */
|
|
23
|
+
const SCROLL_TOLERANCE_PX = 1;
|
|
24
|
+
|
|
25
|
+
/** Null when the page scrolled to the offset; otherwise why the frame would show another place. */
|
|
26
|
+
export function findScrollFailure(reached: number, wanted: number): string | null {
|
|
27
|
+
if (reached >= wanted - SCROLL_TOLERANCE_PX) return null;
|
|
28
|
+
return `the page scrolls only to ${Math.round(reached)} px, short of scrollTo ${wanted}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
21
31
|
/** Null when the file is large enough; otherwise why it is refused. */
|
|
22
32
|
export function findSizeFailure(bytes: number, minBytes: number): string | null {
|
|
23
33
|
if (bytes >= minBytes) return null;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { mkdirSync, rmSync, statSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
2
|
+
import { basename, dirname, extname, join } from "node:path";
|
|
3
3
|
|
|
4
4
|
import { chromium, errors, type Browser, type Page } from "playwright";
|
|
5
5
|
|
|
6
6
|
import type { ColorTheme } from "../config/colors.js";
|
|
7
7
|
import type { MarketingJson } from "../config/schema.js";
|
|
8
|
-
import { getScreenshotNames, getScreenshotShots
|
|
8
|
+
import { getScreenshotNames, getScreenshotShots } from "../config/screenshot-names.js";
|
|
9
9
|
import { containsPhrase } from "../film.js";
|
|
10
|
-
import { findSizeFailure, findStatusFailure, type ScreenshotGate } from "./gates.js";
|
|
10
|
+
import { findScrollFailure, findSizeFailure, findStatusFailure, type ScreenshotGate } from "./gates.js";
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* `softure-marketing shots`: the `screenshots` entries of `marketing.json`. Each shot of an entry
|
|
@@ -121,6 +121,15 @@ async function captureLoadedPage(page: Page, target: ShotTarget): Promise<Screen
|
|
|
121
121
|
const { entry, name, url, file } = target;
|
|
122
122
|
await page.evaluate(() => document.fonts.ready);
|
|
123
123
|
if (entry.full) await scrollThroughPage(page);
|
|
124
|
+
if (entry.scrollTo !== undefined) {
|
|
125
|
+
const reached = await page.evaluate((top) => {
|
|
126
|
+
window.scrollTo(0, top);
|
|
127
|
+
return window.scrollY;
|
|
128
|
+
}, entry.scrollTo);
|
|
129
|
+
const scrollFailure = findScrollFailure(reached, entry.scrollTo);
|
|
130
|
+
if (scrollFailure !== null) return { ok: false, id: entry.id, name, gate: "scroll", message: scrollFailure };
|
|
131
|
+
}
|
|
132
|
+
if (entry.waitMs > 0) await page.waitForTimeout(entry.waitMs);
|
|
124
133
|
if (!(await waitForPhrase(page, entry.expect))) {
|
|
125
134
|
return { ok: false, id: entry.id, name, gate: "phrase", message: `${url} does not show "${entry.expect}"` };
|
|
126
135
|
}
|
|
@@ -142,21 +151,22 @@ function removeEntryFiles(outDir: string, entry: ScreenshotEntry): void {
|
|
|
142
151
|
interface TakeShotOptions {
|
|
143
152
|
browser: Browser;
|
|
144
153
|
settings: ScreenshotBrowser;
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
baseUrl: string;
|
|
148
|
-
outDir: string;
|
|
154
|
+
target: ShotTarget;
|
|
155
|
+
scheme: ColorTheme;
|
|
149
156
|
}
|
|
150
157
|
|
|
151
158
|
async function takeShot(options: TakeShotOptions): Promise<ScreenshotResult> {
|
|
152
|
-
const { browser, settings,
|
|
159
|
+
const { browser, settings, target, scheme } = options;
|
|
160
|
+
const { entry } = target;
|
|
153
161
|
const context = await browser.newContext({
|
|
154
162
|
viewport: { width: entry.width, height: entry.height },
|
|
155
163
|
deviceScaleFactor: entry.scale,
|
|
156
|
-
colorScheme:
|
|
164
|
+
colorScheme: scheme,
|
|
157
165
|
locale: settings.locale,
|
|
158
166
|
timezoneId: settings.timezone,
|
|
159
167
|
reducedMotion: entry.motion,
|
|
168
|
+
// Resolved to an absolute path by the caller (the CLI resolves it against the folder of marketing.json).
|
|
169
|
+
...(entry.storageState === undefined ? {} : { storageState: entry.storageState }),
|
|
160
170
|
});
|
|
161
171
|
try {
|
|
162
172
|
const page = await context.newPage();
|
|
@@ -173,7 +183,6 @@ async function takeShot(options: TakeShotOptions): Promise<ScreenshotResult> {
|
|
|
173
183
|
}
|
|
174
184
|
});
|
|
175
185
|
}, css);
|
|
176
|
-
const target = { entry, name: shot.name, url: new URL(entry.path, options.baseUrl).href, file: getScreenshotFile(options.outDir, shot.name) };
|
|
177
186
|
return await takeOne(page, target);
|
|
178
187
|
} finally {
|
|
179
188
|
await context.close();
|
|
@@ -190,7 +199,8 @@ export async function takeScreenshots(options: TakeScreenshotsOptions): Promise<
|
|
|
190
199
|
for (const entry of entries) {
|
|
191
200
|
removeEntryFiles(outDir, entry);
|
|
192
201
|
for (const shot of getScreenshotShots(entry, settings.colorScheme)) {
|
|
193
|
-
|
|
202
|
+
const target = { entry, name: shot.name, url: new URL(entry.path, baseUrl).href, file: getScreenshotFile(outDir, shot.name) };
|
|
203
|
+
results.push(await takeShot({ browser, settings, target, scheme: shot.scheme }));
|
|
194
204
|
}
|
|
195
205
|
}
|
|
196
206
|
} finally {
|
|
@@ -198,3 +208,33 @@ export async function takeScreenshots(options: TakeScreenshotsOptions): Promise<
|
|
|
198
208
|
}
|
|
199
209
|
return results;
|
|
200
210
|
}
|
|
211
|
+
|
|
212
|
+
export interface TakePageScreenshotOptions {
|
|
213
|
+
/** The shot's settings; its `path` is ignored, `url` is the page. */
|
|
214
|
+
entry: ScreenshotEntry;
|
|
215
|
+
/** The page to capture, any http(s) address. */
|
|
216
|
+
url: string;
|
|
217
|
+
/** The PNG to write; removed first, so a refused shot leaves no older file behind. */
|
|
218
|
+
file: string;
|
|
219
|
+
/** The scheme the browser prefers; `browser.colorScheme` when absent. */
|
|
220
|
+
scheme?: ColorTheme;
|
|
221
|
+
browser: ScreenshotBrowser;
|
|
222
|
+
executablePath?: string;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* One page anywhere (`shots --page`): a competitor's page, production, a page of another app. The same browser
|
|
227
|
+
* settings and gates as an entry, one file at the given path, and no app is started.
|
|
228
|
+
*/
|
|
229
|
+
export async function takePageScreenshot(options: TakePageScreenshotOptions): Promise<ScreenshotResult> {
|
|
230
|
+
const { entry, url, file, browser: settings } = options;
|
|
231
|
+
rmSync(file, { force: true });
|
|
232
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
233
|
+
const browser = await chromium.launch(options.executablePath === undefined ? {} : { executablePath: options.executablePath });
|
|
234
|
+
try {
|
|
235
|
+
const target = { entry, name: basename(file, extname(file)), url, file };
|
|
236
|
+
return await takeShot({ browser, settings, target, scheme: options.scheme ?? settings.colorScheme });
|
|
237
|
+
} finally {
|
|
238
|
+
await browser.close();
|
|
239
|
+
}
|
|
240
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A Playwright storage state (`{ cookies, origins }`) restores a signed-in session for a screenshot. Checked before
|
|
5
|
+
* the browser starts, so a missing or broken file is reported by name instead of as a browser error halfway through
|
|
6
|
+
* the run. The file holds session cookies: its contents are never printed.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const STORAGE_STATE_HINT = "create it with your app's login script (context.storageState({ path })) or npx playwright codegen --save-storage=<file> <url>";
|
|
10
|
+
|
|
11
|
+
/** Null when the file looks like a storage state; otherwise what is wrong with it, without its contents. */
|
|
12
|
+
export function findStorageStateProblem(path: string): string | null {
|
|
13
|
+
if (!existsSync(path)) return `the storage state ${path} does not exist; ${STORAGE_STATE_HINT}`;
|
|
14
|
+
let data: unknown;
|
|
15
|
+
try {
|
|
16
|
+
data = JSON.parse(readFileSync(path, "utf8"));
|
|
17
|
+
} catch {
|
|
18
|
+
// The parse error would quote the file, which holds a session; the path is enough to find it.
|
|
19
|
+
return `the storage state ${path} is not JSON; ${STORAGE_STATE_HINT}`;
|
|
20
|
+
}
|
|
21
|
+
const isStorageState = typeof data === "object" && data !== null && Array.isArray((data as { cookies?: unknown }).cookies);
|
|
22
|
+
if (!isStorageState) return `the storage state ${path} has no "cookies" list, so it is not a Playwright storage state; ${STORAGE_STATE_HINT}`;
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
import type { Voiceover } from "./cache.js";
|
|
6
|
+
import type { TimedWord } from "./voiceover.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A free stand-in for the paid voiceover, so a film can be recorded and rendered end to end before anyone pays
|
|
10
|
+
* (`all`/`record`/`render --placeholder`): every word at a fixed pace, a pause between sentences, and a quiet tone as
|
|
11
|
+
* the audio. It lives in the film's build folder under its own key, never in `voice.cacheDir`: a placeholder in the
|
|
12
|
+
* cache would be a cache hit for the real key, so the paid recording would never be made.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export interface PlaceholderPace {
|
|
16
|
+
/** Words spoken per second (`voice.placeholder.wordsPerSecond`). */
|
|
17
|
+
wordsPerSecond: number;
|
|
18
|
+
/** Silence after every sentence but the last (`voice.placeholder.sentencePauseSeconds`). */
|
|
19
|
+
sentencePauseSeconds: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The key a recording made on the placeholder carries in its log, so it never renders with the real voiceover. */
|
|
23
|
+
export const PLACEHOLDER_KEY_PREFIX = "placeholder-";
|
|
24
|
+
|
|
25
|
+
/** Silence before the first word and after the last, so the tone does not start or end on a word. */
|
|
26
|
+
const LEAD_SECONDS = 0.1;
|
|
27
|
+
const TAIL_SECONDS = 0.5;
|
|
28
|
+
/** A word ends a little before the next starts, as in a real recording's timings. */
|
|
29
|
+
const WORD_GAP_SECONDS = 0.05;
|
|
30
|
+
const TONE_HZ = 220;
|
|
31
|
+
const TONE_VOLUME = 0.05;
|
|
32
|
+
|
|
33
|
+
const round = (value: number): number => Math.round(value * 1000) / 1000;
|
|
34
|
+
|
|
35
|
+
export function getPlaceholderKey(voiceoverKey: string): string {
|
|
36
|
+
return `${PLACEHOLDER_KEY_PREFIX}${voiceoverKey}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function isPlaceholderKey(key: string | undefined): boolean {
|
|
40
|
+
return key?.startsWith(PLACEHOLDER_KEY_PREFIX) ?? false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The words of the script, evenly paced, with a pause after every sentence; the shape the provider returns. */
|
|
44
|
+
export function makePlaceholderWords(sentences: { text: string }[], pace: PlaceholderPace): TimedWord[] {
|
|
45
|
+
const wordSeconds = 1 / pace.wordsPerSecond;
|
|
46
|
+
const words: TimedWord[] = [];
|
|
47
|
+
let time = LEAD_SECONDS;
|
|
48
|
+
sentences.forEach((sentence, index) => {
|
|
49
|
+
for (const text of sentence.text.trim().split(/\s+/).filter((word) => word.length > 0)) {
|
|
50
|
+
words.push({ text, start: round(time), end: round(time + wordSeconds - WORD_GAP_SECONDS) });
|
|
51
|
+
time += wordSeconds;
|
|
52
|
+
}
|
|
53
|
+
if (index < sentences.length - 1) time += pace.sentencePauseSeconds;
|
|
54
|
+
});
|
|
55
|
+
return words;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** How long the placeholder's audio lasts: past the last word by the tail. */
|
|
59
|
+
export function getPlaceholderSeconds(words: TimedWord[]): number {
|
|
60
|
+
return round((words.at(-1)?.end ?? 0) + TAIL_SECONDS);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface WritePlaceholderOptions {
|
|
64
|
+
/** The folder of the placeholder's files: `<output.buildDir>/<video>/placeholder/`. */
|
|
65
|
+
dir: string;
|
|
66
|
+
sentences: { text: string }[];
|
|
67
|
+
pace: PlaceholderPace;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Writes `voiceover.json` (the words) and `voiceover.mp3` (a quiet tone as long as them); needs ffmpeg in PATH. */
|
|
71
|
+
export function writePlaceholderVoiceover(options: WritePlaceholderOptions): Voiceover {
|
|
72
|
+
const words = makePlaceholderWords(options.sentences, options.pace);
|
|
73
|
+
mkdirSync(options.dir, { recursive: true });
|
|
74
|
+
const audio = join(options.dir, "voiceover.mp3");
|
|
75
|
+
const seconds = getPlaceholderSeconds(words);
|
|
76
|
+
const tone = ["-v", "error", "-y", "-f", "lavfi", "-i", `sine=frequency=${TONE_HZ}:duration=${seconds}`, "-filter:a", `volume=${TONE_VOLUME}`];
|
|
77
|
+
const result = spawnSync("ffmpeg", [...tone, "-c:a", "libmp3lame", "-q:a", "6", audio], { encoding: "utf8" });
|
|
78
|
+
if (result.status !== 0) {
|
|
79
|
+
throw new Error(`ffmpeg could not write the placeholder voiceover ${audio}: ${result.error?.message ?? result.stderr.trim()}`);
|
|
80
|
+
}
|
|
81
|
+
writeFileSync(join(options.dir, "voiceover.json"), `${JSON.stringify(words, null, 1)}\n`);
|
|
82
|
+
return { audio, words };
|
|
83
|
+
}
|