## Summary - Add Siri and Shortcuts actions for music playback, queue management, playlists, favorites, and transport controls. - Bridge native App Intents to the mobile app’s playback and library data. ## Testing - Not run Reviewed-on: #40
148 lines
4.9 KiB
JavaScript
148 lines
4.9 KiB
JavaScript
const { test } = require("node:test")
|
|
const assert = require("node:assert/strict")
|
|
const fs = require("node:fs")
|
|
const vm = require("node:vm")
|
|
const ts = require("typescript")
|
|
|
|
// Exercise the app's catalog lifecycle without requiring a React Native runtime.
|
|
function runtime() {
|
|
const disk = new Map()
|
|
const catalogs = new Map()
|
|
const deleted = []
|
|
const donated = []
|
|
const native = {
|
|
catalogJSON: async (kind) => JSON.stringify(disk.get(kind) ?? []),
|
|
replaceCatalog: async (kind, json) => disk.set(kind, JSON.parse(json)),
|
|
completeInvocation: async () => {},
|
|
}
|
|
const expo = {
|
|
isAvailable: () => true,
|
|
getEntityCatalogAsync: async (kind) => catalogs.get(kind) ?? [],
|
|
setEntityCatalogAsync: async (kind, entities) =>
|
|
catalogs.set(kind, entities),
|
|
deleteDonationsAsync: async (filter) => deleted.push(filter),
|
|
donateIntentAsync: async (name, params) => donated.push({ name, params }),
|
|
}
|
|
const module = { exports: {} }
|
|
const source = ts.transpileModule(
|
|
fs.readFileSync(`${__dirname}/app-intents.ts`, "utf8"),
|
|
{
|
|
compilerOptions: {
|
|
module: ts.ModuleKind.CommonJS,
|
|
target: ts.ScriptTarget.ES2022,
|
|
},
|
|
}
|
|
).outputText
|
|
vm.runInNewContext(source, {
|
|
module,
|
|
exports: module.exports,
|
|
console: { warn() {} },
|
|
require: (name) => {
|
|
if (name === "expo") return { requireOptionalNativeModule: () => native }
|
|
if (name === "expo-app-intents") return expo
|
|
throw new Error(`Unexpected import ${name}`)
|
|
},
|
|
})
|
|
return { api: module.exports, native, expo, disk, catalogs, deleted, donated }
|
|
}
|
|
|
|
function song(id, favorite = false) {
|
|
return {
|
|
id: String(id),
|
|
title: `Song ${id}`,
|
|
metadata: { favorite: favorite ? "1" : "0" },
|
|
}
|
|
}
|
|
|
|
function deferred() {
|
|
let resolve
|
|
const promise = new Promise((done) => {
|
|
resolve = done
|
|
})
|
|
return { promise, resolve }
|
|
}
|
|
|
|
test("large libraries remain fully searchable on disk while suggestions stay bounded", async () => {
|
|
const { api, disk, catalogs } = runtime()
|
|
const songs = Array.from({ length: 10_000 }, (_, id) => song(id, id === 9999))
|
|
await api.publishMusicCatalog("song", songs)
|
|
assert.equal(disk.get("song").length, 10_000)
|
|
assert.equal(catalogs.get("song").length, 100)
|
|
assert.equal(catalogs.get("song")[0].id, "9999")
|
|
})
|
|
|
|
test("deleting a song outside the suggestion subset removes its donations", async () => {
|
|
const { api, deleted } = runtime()
|
|
const songs = Array.from({ length: 200 }, (_, id) => song(id))
|
|
await api.publishMusicCatalog("song", songs)
|
|
await api.publishMusicCatalog("song", songs.slice(0, -1))
|
|
assert.equal(deleted.length, 1)
|
|
assert.equal(deleted[0].entity, "song")
|
|
assert.equal(deleted[0].id, "199")
|
|
})
|
|
|
|
test("a queued clear finishes after an in-flight publication and leaves no stale index", async () => {
|
|
const { api, native, disk, catalogs } = runtime()
|
|
const gate = deferred()
|
|
const original = native.replaceCatalog
|
|
native.replaceCatalog = async (...args) => {
|
|
await gate.promise
|
|
return original(...args)
|
|
}
|
|
const publish = api.publishMusicCatalog("song", [song(1)])
|
|
const clear = api.publishMusicCatalog("song", [])
|
|
gate.resolve()
|
|
await Promise.all([publish, clear])
|
|
assert.equal(disk.get("song").length, 0)
|
|
assert.equal(catalogs.get("song").length, 0)
|
|
})
|
|
|
|
test("a storage failure does not poison subsequent catalog cleanup", async () => {
|
|
const { api, native, catalogs } = runtime()
|
|
const original = native.replaceCatalog
|
|
native.replaceCatalog = async () => {
|
|
throw new Error("Disk unavailable")
|
|
}
|
|
await assert.rejects(
|
|
api.publishMusicCatalog("song", [song(1)]),
|
|
/Disk unavailable/
|
|
)
|
|
native.replaceCatalog = original
|
|
await api.publishMusicCatalog("song", [])
|
|
assert.equal(catalogs.get("song").length, 0)
|
|
})
|
|
|
|
test("donations wait for catalog synchronization and failures do not reject playback", async () => {
|
|
const { api, donated, expo } = runtime()
|
|
const sync = deferred()
|
|
api.trackMusicCatalogSync(sync.promise)
|
|
const donation = api.donateMusicPlay("album", "album-1")
|
|
await new Promise((resolve) => setImmediate(resolve))
|
|
assert.equal(donated.length, 0)
|
|
sync.resolve()
|
|
await donation
|
|
assert.equal(donated[0].params.id, "album-1")
|
|
expo.donateIntentAsync = async () => {
|
|
throw new Error("Suggestions unavailable")
|
|
}
|
|
await api.donateMusicPlay("album", "album-1")
|
|
})
|
|
|
|
test("failed donation cleanup retains deleted records so the next sync can retry", async () => {
|
|
const { api, disk, expo, deleted } = runtime()
|
|
await api.publishMusicCatalog("song", [song(1), song(2)])
|
|
const cleanup = expo.deleteDonationsAsync
|
|
expo.deleteDonationsAsync = async () => {
|
|
throw new Error("Cleanup failed")
|
|
}
|
|
await assert.rejects(
|
|
api.publishMusicCatalog("song", [song(1)]),
|
|
/Cleanup failed/
|
|
)
|
|
assert.equal(disk.get("song").length, 2)
|
|
expo.deleteDonationsAsync = cleanup
|
|
await api.publishMusicCatalog("song", [song(1)])
|
|
assert.equal(deleted[0].id, "2")
|
|
assert.equal(disk.get("song").length, 1)
|
|
})
|