import test from 'node:test' import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import ts from 'typescript' const page = readFileSync(new URL('../pages/music.vue', import.meta.url), 'utf8') const source = page.slice(page.indexOf('async function tryRecover()'), page.indexOf('async function attachLiveMusic(')) const code = ts.transpileModule(source, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText function recovery(fetch, apply, id = { value: 'music-1' }) { return new Function('$fetch', 'applyEvent', 'jobId', 'busy', 'queued', 'settled', `${code}; return tryRecover`)( fetch, apply, id, { value: true }, { value: false }, false ) } test('music status recovers without a working event stream, including completion and errors', async () => { for (const status of ['running', 'complete', 'error']) { const snapshot = { status, message: 'YuE Stage A', progress: 20 } const events = [] await recovery(async url => { assert.equal(url, '/api/generate/music-1') return snapshot }, event => events.push(event))() assert.deepEqual(events, [snapshot]) } }) test('late status response cannot overwrite a different music job', async () => { const id = { value: 'music-1' } const events = [] await recovery(async () => { id.value = 'music-2' return { status: 'complete' } }, event => events.push(event), id)() assert.deepEqual(events, []) }) test('missing live job still falls back to saved audio recovery', async () => { const events = [] await recovery(async url => { if (url === '/api/generate/music-1') throw new Error('Job unavailable') assert.equal(url, '/api/generate/recover') return { trackId: 'saved-track' } }, event => events.push(event))() assert.deepEqual(events, [{ trackId: 'saved-track' }]) }) test('inspecting a queued YuE job uses music controls', () => { const inspect = readFileSync(new URL('../server/utils/queuedInspect.ts', import.meta.url), 'utf8') const fn = inspect.slice(inspect.indexOf('export function inspectStudioJob'), inspect.indexOf('export function inspectLiveMusicJob')).replace('export function', 'function') const js = ts.transpileModule(fn, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText const result = new Function(`${js}; return inspectStudioJob({kind:'music',payload:{prompt:'rock',musicEngine:'yue'}})`)() assert.equal(result.kind, 'music') assert.equal(result.payload.musicEngine, 'yue') assert.equal(result.payload.tags, 'rock') })