Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

5.0 KB
Raw Blame
import { mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import sharp from 'sharp';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { prepareReactorStartingFrame, readImageSize } from './reactorStartingFrame.js';

let dir;
const writeImage = async (name, width, height) => {
  const path = join(dir, name);
  await sharp({ create: { width, height, channels: 3, background: '#204080' } }).png().toFile(path);
  return path;
};
// A phone camera writes its sensor buffer UNROTATED and records the display
// rotation in EXIF, so a portrait photo lands on disk as landscape pixels plus
// `orientation: 6`.
const writePhonePhoto = async (name, rawWidth, rawHeight, orientation) => {
  const path = join(dir, name);
  await sharp({ create: { width: rawWidth, height: rawHeight, channels: 3, background: '#204080' } })
    .withMetadata({ orientation }).jpeg().toFile(path);
  return path;
};

beforeAll(async () => { dir = await mkdtemp(join(tmpdir(), 'reactor-frame-')); });
afterAll(async () => { await rm(dir, { recursive: true, force: true }); });

describe('prepareReactorStartingFrame', () => {
  // The reported failure: a tall phone photo opened a 1344x768 session, so
  // reactor's own fit left almost nothing of it to animate.
  it('opens a portrait session for a portrait frame and crops it to that canvas', async () => {
    const source = await writeImage('portrait.png', 900, 1600);
    const out = join(dir, 'portrait-render.mp4');
    const frame = await prepareReactorStartingFrame(source, undefined, out);
    expect(frame.aspect).toBe('9:16');
    expect(frame.framePath).toBe(`${out}.start.png`);
    expect(frame.fittedPath).toBe(frame.framePath);
    await expect(readImageSize(frame.framePath)).resolves.toEqual({ width: 768, height: 1344, oriented: false });
  });

  // `sharp.metadata()` reports RAW dimensions regardless of how it is
  // constructed, so measuring without reading the tag derives a landscape
  // canvas for a portrait photo — the exact failure this module prevents.
  it('reads a phone photo at its displayed size, not its sensor size', async () => {
    const source = await writePhonePhoto('iphone.jpg', 4032, 3024, 6);
    await expect(readImageSize(source)).resolves.toEqual({ width: 3024, height: 4032, oriented: true });
    const out = join(dir, 'iphone-render.mp4');
    const frame = await prepareReactorStartingFrame(source, undefined, out);
    expect(frame.aspect).toBe('9:16');
    await expect(readImageSize(frame.framePath)).resolves.toMatchObject({ width: 768, height: 1344 });
  });

  // Matching the canvas on DISPLAYED size is not enough — uploading bytes that
  // still owe an EXIF transform hands reactor a sideways frame.
  it('re-encodes a frame whose pixels still owe an EXIF transform', async () => {
    const source = await writePhonePhoto('sideways.jpg', 1344, 768, 3);
    const frame = await prepareReactorStartingFrame(source, undefined, join(dir, 'sideways-render.mp4'));
    expect(frame.aspect).toBe('16:9');
    expect(frame.fittedPath).toBe(frame.framePath);
    await expect(readImageSize(frame.framePath)).resolves.toEqual({ width: 1344, height: 768, oriented: false });
  });

  it('honours an explicitly requested canvas over the frame it was given', async () => {
    const source = await writeImage('explicit.png', 900, 1600);
    const out = join(dir, 'explicit-render.mp4');
    const frame = await prepareReactorStartingFrame(source, '1:1', out);
    expect(frame.aspect).toBe('1:1');
    await expect(readImageSize(frame.framePath)).resolves.toEqual({ width: 768, height: 768, oriented: false });
  });

  // A re-encode of an already-correct frame is a lossy no-op plus a temp file
  // the caller then has to delete.
  it('uploads a frame that already matches the canvas untouched', async () => {
    const source = await writeImage('exact.png', 1344, 768);
    const frame = await prepareReactorStartingFrame(source, undefined, join(dir, 'exact-render.mp4'));
    expect(frame).toMatchObject({ aspect: '16:9', framePath: source, fittedPath: null });
  });

  // reactor decodes more container formats than sharp does, so failing to
  // MEASURE a frame must not fail a render reactor could have fitted itself.
  it('passes an unreadable frame through on the default canvas', async () => {
    const frame = await prepareReactorStartingFrame(join(dir, 'missing.heic'), undefined, join(dir, 'missing-render.mp4'));
    expect(frame).toMatchObject({ aspect: '16:9', framePath: join(dir, 'missing.heic'), fittedPath: null });
    expect(frame.canvas).toMatchObject({ width: 1344, height: 768 });
  });

  it('resolves a text render to the requested canvas with no frame at all', async () => {
    expect(await prepareReactorStartingFrame(null, '9:16', join(dir, 't.mp4')))
      .toMatchObject({ aspect: '9:16', framePath: null, fittedPath: null });
    expect(await prepareReactorStartingFrame(null, undefined, join(dir, 't.mp4')))
      .toMatchObject({ aspect: '16:9', framePath: null, fittedPath: null });
  });
});