Skip to content

Commit

Permalink
chore: basic scanner unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Dec 6, 2023
1 parent 43453f1 commit f695837
Show file tree
Hide file tree
Showing 11 changed files with 2,402 additions and 39 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ jobs:
env:
CI: true

test:
name: Tests
runs-on: ubuntu-latest
continue-on-error: true
timeout-minutes: 15

steps:
- uses: actions/checkout@v4
- name: Use Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: 18.x
- name: Prepare Environment
run: |
corepack enable
yarn install
env:
CI: true
- name: Run tests
run: |
yarn unit
env:
CI: true

build:
name: Build binaries
runs-on: ${{ matrix.os }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ casparcg.config
!.yarn/releases
!.yarn/sdks
!.yarn/versions
.ffmpeg/
/coverage
33 changes: 33 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module.exports = {
transform: {
'^.+\\.(ts|tsx)$': [
'ts-jest',
{
tsconfig: 'tsconfig.json',
},
],
},
testMatch: ['**/__tests__/**/*.spec.(ts|js)'],
testPathIgnorePatterns: ['integrationTests'],
testEnvironment: 'node',
coverageThreshold: {
global: {
branches: 0,
functions: 0,
lines: 0,
statements: 0,
},
},
collectCoverageFrom: [
'**/src/**/*.{ts,js}',
'!**/src/__tests__/**',
'!**/node_modules/**',
'!**/dist/**',
'!packages/webhid-demo/**',
],
collectCoverage: true,
projects: ['<rootDir>'],
coverageDirectory: '<rootDir>/coverage/',

preset: 'ts-jest',
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"lint:raw": "run eslint --ext .ts --ext .js --ext .tsx --ext .jsx --ext .mts --ext .mjs --ignore-pattern dist",
"lint": "yarn lint:raw .",
"lint-fix": "yarn lint --fix",
"unit": "node tools/fetch_ffmpeg.mjs && yarn jest",
"license-validate": "yarn sofie-licensecheck",
"eslint": "./node_modules/.bin/eslint"
},
Expand All @@ -47,6 +48,7 @@
"@sofie-automation/code-standard-preset": "^2.5.1",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.11",
"@types/mkdirp": "^2.0.0",
"@types/nconf": "^0.10.6",
"@types/node": "^18.19.2",
Expand All @@ -55,10 +57,12 @@
"@types/recursive-readdir": "^2.2.4",
"@types/xml2js": "^0.4.14",
"husky": "^8.0.3",
"jest": "^29.7.0",
"lint-staged": "^14.0.1",
"nodemon": "^2.0.22",
"pkg": "5.8.1",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tslib": "^2.6.2",
"typescript": "^5.3.2",
Expand Down
48 changes: 48 additions & 0 deletions src/__tests__/ffmpeg.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { generateInfo } from '../ffmpeg'
import { PouchDBMediaDocument } from '../db'
import path from 'path'
import moment from 'moment'

// eslint-disable-next-line @typescript-eslint/no-var-requires
const targetVersions = require('./ffmpegReleases.json')

const testMediaPath = path.join(__dirname, 'samples')

function runForFFmpegRelease(ffprobePath: string, ffmpegPath: string) {
function createBareDoc(filename: string): PouchDBMediaDocument {
return {
_id: 'test',
_rev: '0',
mediaPath: path.join(testMediaPath, filename),
mediaSize: 45678,
mediaTime: moment('2023-12-06 12:34:56').unix() * 1000,
}
}
const defaultConfig = {
metadata: null,
paths: {
media: testMediaPath,
ffprobe: ffprobePath,
ffmpeg: ffmpegPath,
},
}

it('png', async () => {
const doc = createBareDoc('grey.png')
await generateInfo(defaultConfig, doc)
expect(doc.cinf).toBe('"GREY" STILL 45678 20231206123456 0 0/1\r\n')
})
}

const ffprobeFilename = process.platform === 'win32' ? 'bin/ffprobe.exe' : 'ffprobe'
const ffmpegFilename = process.platform === 'win32' ? 'bin/ffmpeg.exe' : 'ffmpeg'

const ffmpegRootPath = path.join(__dirname, '../../.ffmpeg')
for (const version of targetVersions[`${process.platform}-${process.arch}`]) {
describe(`FFmpeg ${version.id}`, () => {
runForFFmpegRelease(
path.join(ffmpegRootPath, version.id, ffprobeFilename),
path.join(ffmpegRootPath, version.id, ffmpegFilename)
)
})
}
46 changes: 46 additions & 0 deletions src/__tests__/ffmpegReleases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"linux-x64": [
{
"id": "4.1.4",
"url": "https://github.com/CasparCG/dependencies/releases/download/ffmpeg-binaries/ffmpeg-4.1.4-amd64-static.tar.xz"
},
{
"id": "4.2.2",
"url": "https://github.com/CasparCG/dependencies/releases/download/ffmpeg-binaries/ffmpeg-4.2.2-amd64-static.tar.xz"
},
{
"id": "4.4.1",
"url": "https://github.com/CasparCG/dependencies/releases/download/ffmpeg-binaries/ffmpeg-4.4.1-amd64-static.tar.xz"
},
{
"id": "5.1.1",
"url": "https://github.com/CasparCG/dependencies/releases/download/ffmpeg-binaries/ffmpeg-5.1.1-amd64-static.tar.xz"
},
{
"id": "6.1",
"url": "https://github.com/CasparCG/dependencies/releases/download/ffmpeg-binaries/ffmpeg-6.1-amd64-static.tar.xz"
}
],
"win32-x64": [
{
"id": "4.1.4",
"url": "https://github.com/CasparCG/dependencies/releases/download/ffmpeg-binaries/ffmpeg-4.1.4-win64-static.zip"
},
{
"id": "4.2.2",
"url": "https://github.com/CasparCG/dependencies/releases/download/ffmpeg-binaries/ffmpeg-4.2.2-win64-static.zip"
},
{
"id": "4.3.1",
"url": "https://github.com/CasparCG/dependencies/releases/download/ffmpeg-binaries/ffmpeg-4.3.1-win64-static.zip"
},
{
"id": "5.1.2",
"url": "https://github.com/CasparCG/dependencies/releases/download/ffmpeg-binaries/ffmpeg-5.1.2-essentials_build.zip"
},
{
"id": "6.1",
"url": "https://github.com/CasparCG/dependencies/releases/download/ffmpeg-binaries/ffmpeg-6.1-essentials_build.zip"
}
]
}
Binary file added src/__tests__/samples/grey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/ffmpeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const statAsync = util.promisify(fs.stat)
const unlinkAsync = util.promisify(fs.unlink)
const readFileAsync = util.promisify(fs.readFile)

export async function generateThumb(config: Record<string, any>, doc: PouchDBMediaDocument) {
export async function generateThumb(config: Record<string, any>, doc: PouchDBMediaDocument): Promise<void> {
const tmpPath = path.join(os.tmpdir(), Math.random().toString(16)) + '.png'

const args = [
Expand Down Expand Up @@ -53,7 +53,7 @@ export async function generateThumb(config: Record<string, any>, doc: PouchDBMed
await unlinkAsync(tmpPath)
}

export async function generateInfo(config: Record<string, any>, doc: PouchDBMediaDocument) {
export async function generateInfo(config: Record<string, any>, doc: PouchDBMediaDocument): Promise<void> {
const json = await new Promise((resolve, reject) => {
const args = [
// TODO (perf) Low priority process?
Expand Down Expand Up @@ -133,7 +133,7 @@ function generateCinf(config: Record<string, any>, doc: MediaDocument, json: any
`"${getId(config.paths.media, doc.mediaPath)}"`,
type,
doc.mediaSize,
moment(doc.thumbTime).format('YYYYMMDDHHmmss'),
moment(doc.mediaTime).format('YYYYMMDDHHmmss'),
tb[0] === 0 ? 0 : Math.floor((dur * tb[1]) / tb[0]),
`${tb[0]}/${tb[1]}`,
].join(' ') + '\r\n'
Expand Down
61 changes: 61 additions & 0 deletions tools/fetch_ffmpeg.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import fs from 'fs/promises'
import { pipeline } from 'node:stream'
import { promisify } from 'node:util'
import { createWriteStream } from 'node:fs'
import path from 'path'
import cp from 'child_process'

const targetVersions = JSON.parse(await fs.readFile('./src/__tests__/ffmpegReleases.json'))

const toPosix = (str) => str.split(path.sep).join(path.posix.sep)

const streamPipeline = promisify(pipeline)

const ffmpegRootDir = './.ffmpeg'
await fs.mkdir(ffmpegRootDir).catch(() => null)

async function pathExists(path) {
try {
await fs.stat(path)
return true
} catch (e) {
return false
}
}

const platformInfo = `${process.platform}-${process.arch}`
const platformVersions = targetVersions[platformInfo]

if (platformVersions) {
const tmpPath = path.join(ffmpegRootDir, 'tmp')

for (const version of platformVersions) {
const versionPath = path.join(ffmpegRootDir, version.id)
const dirStat = await pathExists(versionPath)
if (!dirStat) {
console.log(`Fetching ${version.url}`)
// Download it

// eslint-disable-next-line no-undef
const response = await fetch(version.url)
if (!response.ok) throw new Error(`unexpected response ${response.statusText}`)
await streamPipeline(response.body, createWriteStream(tmpPath))

// Extract it
if (version.url.endsWith('.tar.xz')) {
await fs.mkdir(versionPath).catch(() => null)
cp.execSync(`tar -xJf ${toPosix(tmpPath)} --strip-components=1 -C ${toPosix(versionPath)}`)
} else if (version.url.endsWith('.zip')) {
cp.execSync(`unzip ${toPosix(tmpPath)} -d ${toPosix(ffmpegRootDir)}`)

const dirname = path.parse(version.url).name
await fs.rename(path.join(ffmpegRootDir, dirname), versionPath)
} else {
throw new Error(`Unhandled file extension: ${version.url}`)
}
await fs.rm(tmpPath)
}
}
} else {
throw new Error(`FFmpeg downloading for "${platformInfo}" not supported yet`)
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"extends": "./tsconfig.build.json",
"exclude": ["node_modules/**"],
"compilerOptions": {
"types": ["node"]
"types": ["node", "jest"]
}
}
Loading

0 comments on commit f695837

Please sign in to comment.