All files / lib screenshots.ts

98.18% Statements 108/110
88.88% Branches 80/90
100% Functions 24/24
98.96% Lines 96/97

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286                                                                                                      1x           1x   1x 1x   1x       11x 11x 17x 17x 57x 57x 1x 57x 57x 57x 1x 57x 1x 57x 4x     57x 1x 57x 1x 57x       2x     11x         2x             3x 2x 2x 1x           3x 2x               2x         3x                 2x 4x 1x         2x                             1x                                 16x 16x 16x 16x 16x 16x   16x   11x   16x         17x                     6x                                     1x 1x 1x   1x 1x 3x     1x 1x 1x 7x 7x 1x 1x   6x     1x 2x 2x 2x 3x 1x 2x 2x 2x       2x 2x 2x 6x   2x 2x 3x 3x 3x 3x 3x 3x 3x     1x         6x 2x 1x 1x 1x 2x 2x      
/**
 * The screenshot shot list (screenshots/shots.json) and the pure logic around it.
 * Used by scripts/screenshots/capture.ts and import.ts, and unit tested.
 * Kept free of path aliases so Node can run the scripts directly.
 */
 
export type Device = "desktop" | "phone";
 
export interface Shot {
  file: string;
  path?: string;
  device: Device;
  alt: string;
  caption?: string;
  /** Capture the whole scrolling page instead of one screen. */
  fullPage?: boolean;
  /**
   * Capture just one panel: the region (a <section> with a heading or aria-label) with this
   * accessible name, e.g. "Fleet map". Scrolled into view first.
   */
  section?: string;
  /** Click this selector before capturing, e.g. to open the first item in a list. */
  click?: string;
  /** Window width in CSS pixels, for panels whose tables need more room than the default. */
  width?: number;
  /**
   * The image in screenshots/inbox/ to import, for shots taken by hand,
   * e.g. "steps/mobile/03-contest-leaderboard.png". Without it, the import looks for
   * an image named after `file` in the app's folders.
   */
  source?: string;
}
 
export interface AppShots {
  baseUrl: string;
  signIn: boolean;
  /** Captured by hand (e.g. Unity); the capture script skips these. */
  manual?: boolean;
  vars?: Record<string, string>;
  note?: string;
  /** Other inbox folder names for this app, e.g. ["steps"] for step-contest. */
  folders?: string[];
  /** Inbox images (paths inside screenshots/inbox/) never to import, e.g. private screens. */
  skip?: string[];
  shots: Shot[];
}
 
export interface ShotList {
  apps: Record<string, AppShots>;
}
 
export const VIEWPORTS: Record<Device, { width: number; height: number }> = {
  desktop: { width: 1440, height: 900 },
  phone: { width: 390, height: 844 },
};
 
/** Captures are taken at 2x; anything wider than this is scaled down on import. */
export const MAX_WIDTH: Record<Device, number> = { desktop: 2400, phone: 780 };
 
const FILE_NAME = /^[a-z0-9]+(-[a-z0-9]+)*$/;
const IMAGE = /\.(png|jpe?g|webp)$/i;
/** A relative image path with no "..", no leading slash and no backslashes. */
const INBOX_PATH = /^(?!.*(^|\/)\.\.(\/|$))[^/\\][^\\]*\.(png|jpe?g|webp)$/i;
 
/** Every problem in a shot list, as readable sentences. Empty means it's valid. */
export function shotListProblems(list: ShotList): string[] {
  const problems: string[] = [];
  for (const [slug, app] of Object.entries(list.apps)) {
    const seen = new Set<string>();
    for (const shot of app.shots) {
      const where = `${slug}/${shot.file}`;
      if (!FILE_NAME.test(shot.file))
        problems.push(`${where}: file names use lowercase letters, numbers and hyphens.`);
      if (seen.has(shot.file)) problems.push(`${where}: this file name is used twice.`);
      seen.add(shot.file);
      if (!(shot.device in VIEWPORTS))
        problems.push(`${where}: device must be "desktop" or "phone".`);
      if (!app.manual && !shot.source && !shot.path?.startsWith("/"))
        problems.push(`${where}: path must start with "/".`);
      if (shot.source !== undefined && !INBOX_PATH.test(shot.source))
        problems.push(
          `${where}: source must be an image path inside screenshots/inbox/, like "steps/mobile/01-home.png".`,
        );
      if (shot.section !== undefined && !shot.section.trim())
        problems.push(`${where}: section can't be empty.`);
      if (shot.section && shot.fullPage)
        problems.push(`${where}: use section or fullPage, not both.`);
      if (
        shot.width !== undefined &&
        !(Number.isInteger(shot.width) && shot.width >= 320 && shot.width <= 3840)
      )
        problems.push(`${where}: width must be a whole number of pixels from 320 to 3840.`);
    }
  }
  return problems;
}
 
/** Alt text is required before a screenshot can be published. */
export function missingAlt(slug: string, shot: Shot): string | null {
  return shot.alt.trim()
    ? null
    : `${slug}/${shot.file}: write alt text for this shot in screenshots/shots.json before importing it.`;
}
 
/** Fills {placeholders} in a path from the app's vars. Throws if one isn't set. */
export function resolvePath(path: string, vars: Record<string, string> = {}): string {
  return path.replace(/\{(\w+)\}/g, (_, name: string) => {
    const value = vars[name]?.trim();
    if (!value) throw new Error(`Set vars.${name} in screenshots/shots.json to capture ${path}.`);
    return encodeURIComponent(value);
  });
}
 
/** Joins a base URL and a path without doubling or dropping the slash. */
export function shotUrl(baseUrl: string, path: string): string {
  if (!baseUrl.trim()) throw new Error("Set baseUrl in screenshots/shots.json first.");
  return `${baseUrl.replace(/\/+$/, "")}${path}`;
}
 
/** The window size for a shot: the device's, with an optional wider (or narrower) width. */
export function viewportFor(shot: Pick<Shot, "device" | "width">): {
  width: number;
  height: number;
} {
  return { ...VIEWPORTS[shot.device], ...(shot.width ? { width: shot.width } : {}) };
}
 
/** Width to store an image at: never enlarged, capped per device. */
export function outputWidth(device: Device, sourceWidth: number): number {
  return Math.min(sourceWidth, MAX_WIDTH[device]);
}
 
/**
 * Name swaps as [real, demo] pairs, longest names first so "Ann Lee" is replaced before "Ann".
 * Blank names are dropped. The capture script applies these in the page, so real names
 * never reach an image.
 */
export function redactionPairs(swaps: Record<string, string>): Array<[string, string]> {
  return Object.entries(swaps)
    .filter(([from]) => from.trim())
    .sort(([a], [b]) => b.length - a.length);
}
 
/** Applies redaction pairs to text, the same way the page-side swap does. */
export function applyRedactions(text: string, pairs: Array<[string, string]>): string {
  return pairs.reduce((t, [from, to]) => t.split(from).join(to), text);
}
 
/* ---------------------------------------------------------------------------------------
 * The inbox: images saved by hand into screenshots/inbox/, in any folder layout.
 * ------------------------------------------------------------------------------------- */
 
export interface InboxImage {
  /** Path inside screenshots/inbox/, with forward slashes. */
  path: string;
  width: number;
  height: number;
}
 
/** Folder names that say which device a screenshot came from. */
const DEVICE_FOLDERS: Record<string, Device> = {
  desktop: "desktop",
  laptop: "desktop",
  mobile: "phone",
  phone: "phone",
};
 
/**
 * Reads what an inbox path says about an image: the app folder it's in, its order
 * ("03-leaderboard" sorts third), a clean file name, and a device if a folder names one.
 */
export function parseInboxPath(path: string): {
  folder: string;
  order: number | null;
  name: string;
  device: Device | null;
} {
  const parts = path.split("/");
  const folder = parts[0]!.toLowerCase();
  const base = parts[parts.length - 1]!.replace(IMAGE, "");
  const match = /^(\d+)[a-z]?[-_ ]+(.*)$/i.exec(base);
  const order = match ? Number(match[1]) : null;
  const name = slugify(match ? match[2]! : base) || "shot";
  const device =
    parts
      .slice(1, -1)
      .map((p) => DEVICE_FOLDERS[p.toLowerCase()])
      .find(Boolean) ?? null;
  return { folder, order, name, device };
}
 
/** Lowercase letters, numbers and hyphens, as shot file names require. */
export function slugify(text: string): string {
  return text
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, "-")
    .replace(/^-+|-+$/g, "");
}
 
/**
 * The device for an image: the folder's word for it if there is one, otherwise its width.
 * Phone captures (at 2x or 3x) are under ~1300 px wide; desktop captures are wider.
 */
export function inferDevice(hint: Device | null, width: number): Device {
  return hint ?? (width > 0 && width < 1300 ? "phone" : "desktop");
}
 
export interface InboxPlan {
  /** "<slug>/<file>" → the inbox image to import for that shot. */
  sources: Map<string, string>;
  /** New shots for images that aren't in the shot list yet, per app, already in order. */
  added: Record<string, Shot[]>;
  /** Images in folders that don't belong to any app. */
  unknown: string[];
}
 
/**
 * Matches inbox images to the shot list. A shot uses its `source` if given, otherwise an
 * image in one of its app's folders named after it ("leaderboard.png" or "03-leaderboard.png").
 * Every other image in an app's folders becomes a new shot, sorted by folder and number,
 * with empty alt text so nothing is published until someone describes it.
 */
export function planInbox(list: ShotList, images: InboxImage[]): InboxPlan {
  const sources = new Map<string, string>();
  const added: Record<string, Shot[]> = {};
  const unknown: string[] = [];
 
  const appFor = new Map<string, string>();
  for (const [slug, app] of Object.entries(list.apps)) {
    for (const folder of [slug, ...(app.folders ?? [])]) appFor.set(folder.toLowerCase(), slug);
  }
 
  const used = new Set<string>();
  const byApp = new Map<string, InboxImage[]>();
  for (const image of images) {
    const slug = appFor.get(parseInboxPath(image.path).folder);
    if (!slug) {
      unknown.push(image.path);
      continue;
    }
    byApp.set(slug, [...(byApp.get(slug) ?? []), image]);
  }
 
  for (const [slug, app] of Object.entries(list.apps)) {
    const pool = byApp.get(slug) ?? [];
    for (const shot of app.shots) {
      const image = shot.source
        ? pool.find((i) => i.path === shot.source)
        : pool.find((i) => !used.has(i.path) && parseInboxPath(i.path).name === shot.file);
      Eif (image) {
        sources.set(`${slug}/${shot.file}`, image.path);
        used.add(image.path);
      }
    }
 
    const skip = new Set(app.skip ?? []);
    const listedSources = new Set(app.shots.map((s) => s.source).filter(Boolean));
    const fresh = pool
      .filter((i) => !used.has(i.path) && !skip.has(i.path) && !listedSources.has(i.path))
      .sort(compareInbox);
    const taken = new Set(app.shots.map((s) => s.file));
    for (const image of fresh) {
      const { name, device: hint } = parseInboxPath(image.path);
      const device = inferDevice(hint, image.width);
      let file = name;
      if (taken.has(file) && device === "phone") file = `${name}-phone`;
      for (let n = 2; taken.has(file); n++) file = `${name}-${n}`;
      taken.add(file);
      (added[slug] ??= []).push({ file, source: image.path, device, alt: "", caption: "" });
    }
  }
  return { sources, added, unknown: unknown.sort() };
}
 
/** Folder first, then the number at the front of the name, then the name. */
function compareInbox(a: InboxImage, b: InboxImage): number {
  const dir = (p: string) => p.slice(0, p.lastIndexOf("/") + 1);
  if (dir(a.path) !== dir(b.path)) return dir(a.path) < dir(b.path) ? -1 : 1;
  const pa = parseInboxPath(a.path);
  const pb = parseInboxPath(b.path);
  const oa = pa.order ?? Number.POSITIVE_INFINITY;
  const ob = pb.order ?? Number.POSITIVE_INFINITY;
  if (oa !== ob) return oa - ob;
  return a.path < b.path ? -1 : a.path > b.path ? 1 : 0;
}