diff --git a/.fallowrc.jsonc b/.fallowrc.jsonc index f13d626a34..085bffc510 100644 --- a/.fallowrc.jsonc +++ b/.fallowrc.jsonc @@ -28,6 +28,8 @@ "packages/producer/we-render.mjs", "packages/producer/scripts/validate-fast-video.ts", "packages/cli/scripts/generate-font-data.ts", + // Run by CI before the CLI tests (.github/actions/opencv-test-cache). + "packages/cli/scripts/prepare-test-opencv.mjs", "packages/engine/scripts/test-fitTextFontSize-browser.ts", "packages/aws-lambda/scripts/*.ts", // Run inside the arm64 video image by the lambda-arm64-image workflow. diff --git a/.github/actions/opencv-test-cache/action.yml b/.github/actions/opencv-test-cache/action.yml new file mode 100644 index 0000000000..9ebebc39e5 --- /dev/null +++ b/.github/actions/opencv-test-cache/action.yml @@ -0,0 +1,15 @@ +name: Cache opencv.js for the CLI tests +description: >- + Restore opencv.js and the face models into ~/.cache/hyperframes/opencv, where the CLI tests copy them from. + On a cache miss, fetch them once with opencv.ts's own pins, before any test runs. +runs: + using: composite + steps: + - id: cache + uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4 + with: + path: ~/.cache/hyperframes/opencv + key: opencv-${{ runner.os }}-${{ hashFiles('packages/cli/src/measure/opencv.ts') }} + - if: steps.cache.outputs.cache-hit != 'true' + shell: bash + run: node --import tsx packages/cli/scripts/prepare-test-opencv.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dffb95bd16..14f96cd373 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -612,6 +612,7 @@ jobs: with: lfs: true - uses: ./.github/actions/setup-test-workspace + - uses: ./.github/actions/opencv-test-cache - run: bun run --filter '@hyperframes/cli' test --shard=${{ matrix.shard }}/2 test-support: diff --git a/.github/workflows/windows-render.yml b/.github/workflows/windows-render.yml index af004adc29..1c12f01415 100644 --- a/.github/workflows/windows-render.yml +++ b/.github/workflows/windows-render.yml @@ -504,6 +504,9 @@ jobs: bun run --filter '@hyperframes/{parsers,lint,studio-server}' build bun run --cwd packages/core build + - if: contains(matrix.packages, '@hyperframes/cli') + uses: ./.github/actions/opencv-test-cache + - name: Prepare browser for process tests if: matrix.process_tests shell: pwsh diff --git a/packages/cli/scripts/prepare-test-opencv.mjs b/packages/cli/scripts/prepare-test-opencv.mjs new file mode 100644 index 0000000000..eaa83d4cb3 --- /dev/null +++ b/packages/cli/scripts/prepare-test-opencv.mjs @@ -0,0 +1,6 @@ +// Fetches opencv.js and the face models into this machine's cache, verified against opencv.ts's pins, so the +// tests copy them from there and none downloads anything. +import { loadOpenCv, OPENCV_DIR } from "../src/measure/opencv.ts"; + +await loadOpenCv((message) => console.log(message)); +console.log(`opencv.js ready in ${OPENCV_DIR}`); diff --git a/packages/cli/src/measure/diff.test-helpers.ts b/packages/cli/src/measure/diff.test-helpers.ts index 02b3f040aa..a9bbb7c020 100644 --- a/packages/cli/src/measure/diff.test-helpers.ts +++ b/packages/cli/src/measure/diff.test-helpers.ts @@ -1,7 +1,7 @@ import { execFileSync } from "node:child_process"; -import { cpSync, existsSync, mkdtempSync, renameSync, rmSync } from "node:fs"; +import { cpSync, existsSync, mkdirSync, mkdtempSync, renameSync, rmSync } from "node:fs"; import { tmpdir, userInfo } from "node:os"; -import { basename, join } from "node:path"; +import { basename, dirname, join } from "node:path"; import { afterAll, beforeAll } from "vitest"; import { findFFmpeg, findFFprobe } from "../browser/ffmpeg.js"; import { diffVideos, type DiffOptions } from "./diff.js"; @@ -35,22 +35,35 @@ export function clipFolder(prefix: string): { /** diffVideos for a test that reads verdicts only: no worst-moment pictures, each of which costs seven ffmpeg runs. */ export const diffVerdicts = (options: DiffOptions) => diffVideos({ worst: 0, ...options }); -/** opencv.js loaded before a block's tests, so none pays for its download inside the per-test limit. The files come - * from this machine's cache, else a temp copy kept between runs (each has its own home), else the network. */ +const machineOpenCv = join( + userInfo().homedir, + ".cache", + "hyperframes", + "opencv", + basename(OPENCV_DIR), +); +const sharedOpenCv = () => join(tmpdir(), `hf-test-opencv-${basename(OPENCV_DIR)}`); + +/** A cached opencv.js copied into this file's home, in private folders: opencv.ts refuses group-writable ones. */ +export function seedOpenCvFromCache(): boolean { + for (const from of [machineOpenCv, sharedOpenCv()]) + if (!existsSync(join(OPENCV_DIR, "opencv.js")) && existsSync(join(from, "opencv.js"))) { + mkdirSync(dirname(OPENCV_DIR), { recursive: true, mode: 0o700 }); + cpSync(from, OPENCV_DIR, { recursive: true }); + } + return existsSync(join(OPENCV_DIR, "opencv.js")); +} + +/** opencv.js loaded before a block's tests, so none pays for its download inside the per-test limit. Without a cached + * copy it downloads, except on CI, where the opencv-test-cache action fills the machine's cache before the tests. */ export function loadOpenCvFirst(): void { beforeAll(async () => { - const shared = join(tmpdir(), `hf-test-opencv-${basename(OPENCV_DIR)}`); - const machine = join( - userInfo().homedir, - ".cache", - "hyperframes", - "opencv", - basename(OPENCV_DIR), - ); - for (const from of [machine, shared]) - if (!existsSync(join(OPENCV_DIR, "opencv.js")) && existsSync(join(from, "opencv.js"))) - cpSync(from, OPENCV_DIR, { recursive: true }); + if (!seedOpenCvFromCache() && process.env.CI) + throw new Error( + `opencv.js is not in ${machineOpenCv}: on CI the opencv-test-cache action puts it there`, + ); await loadOpenCv(); + const shared = sharedOpenCv(); if (existsSync(join(shared, "opencv.js"))) return; const staged = `${shared}-${process.pid}`; cpSync(OPENCV_DIR, staged, { recursive: true }); diff --git a/packages/cli/src/measure/face.test.ts b/packages/cli/src/measure/face.test.ts index 630488493e..c506869d45 100644 --- a/packages/cli/src/measure/face.test.ts +++ b/packages/cli/src/measure/face.test.ts @@ -1,12 +1,12 @@ import { execFileSync } from "node:child_process"; -import { cpSync, existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; -import { tmpdir, userInfo } from "node:os"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterAll, describe, expect, it, vi } from "vitest"; import { findFFmpeg, findFFprobe } from "../browser/ffmpeg.js"; import { againstStart, faceBoxes, faceFit, minWindow, readSamples, sampleFrames } from "./face.js"; import { probeVideo, runFfmpeg } from "./media.js"; -import { OPENCV_DIR } from "./opencv.js"; +import { seedOpenCvFromCache } from "./diff.test-helpers.js"; // A run with the 30 s limit that names this file fails the way execFileSync reports a run its timeout stopped. const stalled = vi.hoisted(() => ({ file: "" })); @@ -23,12 +23,9 @@ vi.mock("node:child_process", async (importOriginal) => { return { ...actual, execFileSync }; }); -// As in opencv.test.ts: borrow this machine's verified opencv.js cache, skip without one. -const cached = join(userInfo().homedir, ".cache", "hyperframes", "opencv", "4.10.0"); -if (existsSync(join(cached, "opencv.js")) && !existsSync(join(OPENCV_DIR, "opencv.js"))) - cpSync(cached, OPENCV_DIR, { recursive: true }); const media = !!findFFmpeg() && !!findFFprobe(); -const opencv = media && existsSync(join(OPENCV_DIR, "opencv.js")); +// Borrows a cached opencv.js and skips without one. +const opencv = media && seedOpenCvFromCache(); const dir = mkdtempSync(join(tmpdir(), "hf-face-test-")); afterAll(() => rmSync(dir, { recursive: true, force: true })); diff --git a/scripts/test-reachability.json b/scripts/test-reachability.json index 788968a851..efee1a3663 100644 --- a/scripts/test-reachability.json +++ b/scripts/test-reachability.json @@ -65,7 +65,7 @@ "tests": ["packages/cli/src/**/*.test.ts"], "sources": { "packages/cli/vitest.config.ts": "135ef1c442b32ec5316b23e24d2f8cf0111d889fcb141c95dbd933cf2eaaf8f4", - ".github/workflows/ci.yml#job#test-cli": "66a1249d0d3f0e6f72adb4c22bf7f76b7a8dabcee3204b6bf8cbcc636a9d7cb4", + ".github/workflows/ci.yml#job#test-cli": "db01120324977a0f01c69ce35e3ab8efe7f0d5e7130522dd2fe34cb95a461777", "packages/cli/package.json#script#test": "ad6cd6b8acf39571390d7bc1772410249266fa406be150a22f0065827e400496" }, "reason": "The cli test script is split into two vitest shards by a matrix; both run in one job, so every file the config's three projects select (src/**/*.test.ts) runs in one of them. Config, job and script are pinned."