import assert from 'node:assert/strict' import { Buffer } from 'node:buffer' import fs from 'node:fs' import process from 'node:process' import { ensureArchive, hash, readMember } from '../../refactor/scripts/releases.mjs' import { browserCandidate } from '../helpers/browser-candidate.js' import { expect, test } from './fixtures.js' export function useAudioFixture() { let publishedCode let sourceCode let evidence const samples = 16000 * 16 const tone = fs.readFileSync(new URL('./media/audio-tone.m4a', import.meta.url)) const toneManifest = JSON.parse(fs.readFileSync(new URL('./media/audio-tone.json', import.meta.url))) const wav = Buffer.alloc(44 + samples * 2) wav.write('RIFF', 0) wav.writeUInt32LE(wav.length - 8, 4) wav.write('WAVEfmt ', 8) wav.writeUInt32LE(16, 16) wav.writeUInt16LE(1, 20) wav.writeUInt16LE(1, 22) wav.writeUInt32LE(16000, 24) wav.writeUInt32LE(32000, 28) wav.writeUInt16LE(2, 32) wav.writeUInt16LE(16, 34) wav.write('data', 36) wav.writeUInt32LE(samples * 2, 40) for (let index = 0; index < samples; index++) wav.writeInt16LE(index % 80 < 40 ? 2000 : -2000, 44 + index * 2) test.beforeAll(async () => { assert.equal(hash(tone), toneManifest.sha256) assert.equal(tone.length, toneManifest.bytes) const { release } = JSON.parse(fs.readFileSync(new URL('../../refactor/baselines/audio-track-release.json', import.meta.url))) const member = 'package/dist/artplayer-plugin-audio-track.js' const bytes = readMember(await ensureArchive(release), member) assert.equal(hash(bytes), release.files[member]) publishedCode = bytes.toString() const candidate = await browserCandidate(release.name, process.env.ARTPLAYER_AUDIO_ARTIFACT) sourceCode = candidate.code evidence = { release, sourceSHA256: hash(sourceCode), candidate: candidate.provenance, audio: { sha256: hash(tone), bytes: tone.length, format: 'AAC mono 48000 Hz, 16 seconds, 440 Hz tone in MP4' }, wav: { sha256: hash(wav), bytes: wav.length, format: 'PCM16LE mono 16000 Hz, 16 seconds, integer square wave 200 Hz' }, scope: 'Desktop browser media checks; candidate provenance identifies installed, source or explicit artifact. No physical device acceptance.' } }) test.afterEach(async ({ page }, testInfo) => { const state = await page.evaluate(() => { const audio = window.audioPlugin?.audio const video = window.art?.video const media = element => element && ({ src: element.currentSrc, attribute: element.getAttribute('src'), time: element.currentTime, paused: element.paused, readyState: element.readyState, networkState: element.networkState, error: element.error?.code, rate: element.playbackRate, seekable: Array.from({ length: element.seekable.length }, (_, index) => [element.seekable.start(index), element.seekable.end(index)]) }) return { audio: media(audio), video: media(video), events: window.audioEvents, hostEvents: window.audioHostEvents, warnings: window.audioWarnings } }).catch(error => ({ error: error.message })) await testInfo.attach('audio-state', { contentType: 'application/json', body: JSON.stringify(state) }) }) async function openAudio(page, core, plugin, testInfo, url = '/test/audio-tone.m4a?source=first') { await testInfo.attach('audio-inputs', { contentType: 'application/json', body: JSON.stringify({ ...evidence, core, plugin, selected: plugin === 'published' ? { kind: 'published', sha256: hash(publishedCode) } : evidence.candidate }) }) await page.goto(`/test/player.html?core=${core}`) await page.addScriptTag({ content: plugin === 'published' ? publishedCode : sourceCode }) await page.evaluate((url) => { window.audioEvents = [] window.audioHostEvents = [] window.audioWarnings = [] const warn = console.warn.bind(console) console.warn = (...args) => { window.audioWarnings.push(args.map(String)) warn(...args) } window.art = new window.Artplayer({ container: '.player', url: '/test/pattern.mp4', muted: true, plugins: [window.artplayerPluginAudioTrack({ url, sync: 0.1 })], }) window.audioPlugin = window.art.plugins.artplayerPluginAudioTrack for (const event of ['play', 'pause', 'seek', 'video:waiting', 'video:playing', 'video:canplay', 'video:seeking', 'video:seeked', 'video:pause', 'video:ended']) window.art.on(event, () => window.audioHostEvents.push({ event, videoTime: window.art.currentTime, videoPaused: window.art.video.paused, videoReadyState: window.art.video.readyState, playing: window.art.playing, audioTime: window.audioPlugin.audio.currentTime, audioPaused: window.audioPlugin.audio.paused })) for (const event of ['loadedmetadata', 'playing', 'pause', 'seeked', 'error', 'emptied']) window.audioPlugin.audio.addEventListener(event, () => window.audioEvents.push({ event, time: window.audioPlugin.audio.currentTime, src: window.audioPlugin.audio.currentSrc })) document.querySelector('#play').onclick = () => window.art.play() }, url) await expect.poll(() => page.evaluate(() => window.art.isReady)).toBe(true) if (url) await expect.poll(() => page.evaluate(() => window.audioPlugin.audio.readyState)).toBeGreaterThanOrEqual(2) } return { openAudio, wav } }