Skip to content

The open-source full-stack studio for Opus 5.5 motion video. Reverse engineering for any video. Code video with HTML: recreate, edit and render. BYOA + BYOK · Motion.md · Images, Video, Music & Voice.

6.5 KB
Raw Blame
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, mkdir, copyFile, writeFile, readFile, rm, access } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { download, testVersion, checkReleaseVersion, lock, targetInfo } from '../scripts/test-package.mjs';
import { nativeComponents, runFile } from '../scripts/runtime-assets.mjs';

test('complete bundle starts on macOS 15 while retaining CPU, executable and Windows version checks', async t => {
  const directory = await mkdtemp(path.join(tmpdir(), 'motion-bundle-os-'));
  t.after(() => rm(directory, { recursive: true, force: true }));
  const bootstrap = path.join(directory, 'bootstrap.mjs');
  await copyFile(new URL('../scripts/bundle/bootstrap.mjs', import.meta.url), bootstrap);
  await writeFile(path.join(directory, 'release.json'), JSON.stringify({ target: 'mac-arm64' }));
  for (const file of ['runtime/media/bin/ffmpeg', 'runtime/media/bin/ffprobe', 'runtime/browser/chrome-headless-shell-mac-arm64/chrome-headless-shell']) {
    await mkdir(path.dirname(path.join(directory, file)), { recursive: true });
    await writeFile(path.join(directory, file), 'fixture', { mode: 0o755 });
  }
  const script = `import os from 'node:os';
    import { pathToFileURL } from 'node:url';
    Object.defineProperty(process, 'platform', { value: process.argv[2] });
    Object.defineProperty(process, 'arch', { value: process.argv[3] });
    os.release = () => process.argv[4];
    await import(pathToFileURL(process.argv[1]).href);
    console.log(process.env.HYPERFRAMES_FFMPEG_PATH);`;
  const run = (platform: string, arch: string, kernel: string) => runFile(process.execPath, ['--input-type=module', '-e', script, bootstrap, platform, arch, kernel]);
  assert((await run('darwin', 'arm64', '24.6.0')).includes(path.join(directory, 'runtime/media/bin/ffmpeg')));
  await assert.rejects(run('darwin', 'x64', '24.6.0'), /CPU/);
  await rm(path.join(directory, 'runtime/media/bin/ffmpeg'));
  await assert.rejects(run('darwin', 'arm64', '24.6.0'), /ENOENT/);
  await writeFile(path.join(directory, 'release.json'), JSON.stringify({ target: 'win-x64' }));
  await assert.rejects(run('win32', 'x64', '10.0.19045'), /Windows 11/);
});

test('test versions sort above an already distributed source version and distinguish attempts', () => {
  assert.equal(testVersion('0.6.1', '42', '1'), '0.6.2-test.42.1');
  assert.equal(testVersion('0.6.1', '42', '2'), '0.6.2-test.42.2');
  assert.throws(() => testVersion('0.6.1', '../x'), /positive/);
  assert.throws(() => testVersion('0.6.1-beta.1', '1'), /MAJOR/);
  assert.equal(targetInfo('mac-arm64').platform, 'darwin');
  assert.equal(targetInfo('win-x64').platform, 'win32');
  assert.throws(() => targetInfo('unknown'), /Unsupported/);
});
test('release versions accept test and stable versions and reject ambiguous or non-test candidates', () => {
  for (const version of ['0.6.4-test.1', '0.6.4-test.42.2', '0.6.4']) checkReleaseVersion(version);
  for (const version of ['latest', '0.6.4-beta.1', '0.6.4-rc.1', '0.6.4-test.1.0', '0.6.4-test.0', '0.6.4-test.01', '01.6.4', '0.6.4+local', '../x']) {
    assert.throws(() => checkReleaseVersion(version), /MAJOR.MINOR.PATCH/);
  }
});
test('public runtime downloads never attach repository credentials and reject HTTPS downgrades', async t => {
  const dir = await mkdtemp(path.join(tmpdir(), 'motion-redirect-')), filename = path.join(dir, 'runtime.zip');
  const previous = process.env.FORGEJO_TOKEN;
  process.env.FORGEJO_TOKEN = 'test-only-token';
  try {
    const bytes = 'pinned runtime', sum = createHash('sha256').update(bytes).digest('hex');
    let calls = 0;
    t.mock.method(globalThis, 'fetch', async (url, options) => {
      if (++calls === 1) {
        assert.equal(options.headers?.Authorization, undefined);
        return new Response(null, { status: 302, headers: { location: 'https://cdn.example.invalid/runtime.zip' } });
      }
      assert.equal(options.headers?.Authorization, undefined);
      return new Response(bytes);
    });
    await download({ url: 'https://origin.example.invalid/runtime.zip', sha256: sum }, filename);
    assert.equal(await readFile(filename, 'utf8'), bytes);
    t.mock.method(globalThis, 'fetch', async () => new Response(null, { status: 302, headers: { location: 'http://example.invalid/runtime.zip' } }));
    await assert.rejects(download({ url: 'https://example.invalid/runtime.zip', sha256: 'a'.repeat(64) }, filename), /HTTPS/);
  } finally {
    if (previous === undefined) delete process.env.FORGEJO_TOKEN; else process.env.FORGEJO_TOKEN = previous;
    await rm(dir, { recursive: true, force: true });
  }
});
test('runtime sources require a pinned checksum and cannot replace earlier cache contents on invalid input', async t => {
  const dir = await mkdtemp(path.join(tmpdir(), 'motion-pin-')), filename = path.join(dir, 'runtime.zip');
  try {
    await writeFile(filename, 'keep previous cache');
    await assert.rejects(download({ url: 'http://example.invalid/a.zip', sha256: 'a'.repeat(64) }, filename), /HTTPS/);
    t.mock.method(globalThis, 'fetch', async () => new Response('invalid runtime archive'));
    await assert.rejects(download({ url: 'https://example.invalid/a.zip', sha256: 'a'.repeat(64) }, filename), /SHA256 mismatch/);
    await assert.rejects(access(filename + '.part'), { code: 'ENOENT' });
    assert.equal(await readFile(filename, 'utf8'), 'keep previous cache');
    for (const target of Object.values(lock.targets)) for (const key of ['node', ...nativeComponents(target)]) {
      assert.match(target[key].sha256, /^[a-f0-9]{64}$/); assert.equal(new URL(target[key].url).protocol, 'https:');
      assert(['nodejs.org', 'storage.googleapis.com', 'ffmpeg.martin-riedl.de', 'github.com'].includes(new URL(target[key].url).hostname), 'Runtime sources must be public upstream hosts');
      assert(!target[key].url.includes('/latest/'), 'Use an immutable public version URL');
    }
  } finally { await rm(dir, { recursive: true, force: true }); }
});

test('public source failures report the HTTP status without requiring repository credentials', async t => {
  const dir = await mkdtemp(path.join(tmpdir(), 'motion-public-'));
  try {
    t.mock.method(globalThis, 'fetch', async () => new Response(null, { status: 404 }));
    await assert.rejects(download({ url: 'https://example.invalid/runtime.zip', sha256: 'a'.repeat(64) }, path.join(dir, 'media.zip')), /HTTP 404$/);
  } finally { await rm(dir, { recursive: true, force: true }); }
});