improve album tests

This commit is contained in:
austinried
2025-11-02 18:35:13 +09:00
parent c900c9750a
commit 2df86f4faa
9 changed files with 399 additions and 37 deletions

View File

@@ -13,7 +13,7 @@ const client = new SubsonicClient(
);
for (const id of ["197", "199", "321"]) {
const res = await client.get("download", { id });
const { res } = await client.get("download", { id });
let filename = res.headers.get("Content-Disposition")
?.split(";")[1];

View File

@@ -1,18 +1,65 @@
#!/usr/bin/env -S deno --allow-all
import { SubsonicClient } from "./util/subsonic.ts";
import { sleep } from "./util/util.ts";
async function scrobbleTrack(
client: SubsonicClient,
album: string,
track: number,
) {
const { xml: albumsXml } = await client.get("getAlbumList2", {
type: "newest",
});
const albumId = albumsXml.querySelector(`album[name='${album}']`)?.id;
const { xml: songsXml } = await client.get("getAlbum", { id: albumId! });
const songId = songsXml.querySelector(`song[track='${track}']`)?.id;
await client.get("scrobble", {
id: songId!,
submission: "true",
});
}
async function setupTestData(client: SubsonicClient) {
await scrobbleTrack(client, "Retroconnaissance EP", 1);
await sleep(1_000);
await scrobbleTrack(client, "Retroconnaissance EP", 2);
await sleep(1_000);
await scrobbleTrack(client, "Kosmonaut", 1);
}
async function setupNavidrome() {
console.log("setting up navidrome...");
const baseUrl = "http://navidrome:4533";
const username = "admin";
const password = "password";
await fetch("http://navidrome:4533/auth/createAdmin", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
username: "admin",
password: "password",
}),
body: JSON.stringify({ username, password }),
});
const client = new SubsonicClient(baseUrl, username, password);
await setupTestData(client);
}
await setupNavidrome();
async function setupGonic() {
console.log("setting up gonic...");
const baseUrl = "http://gonic";
const username = "admin";
const password = "admin";
const client = new SubsonicClient(baseUrl, username, password);
await setupTestData(client);
}
await Promise.all([
setupNavidrome(),
setupGonic(),
]);
console.log("setup-servers complete");

View File

@@ -1,3 +1,6 @@
// @deno-types="npm:@types/jsdom@27.0.0"
import { JSDOM } from "npm:jsdom@27.1.0";
export class SubsonicClient {
constructor(
readonly baseUrl: string,
@@ -5,7 +8,18 @@ export class SubsonicClient {
readonly password: string,
) {}
get(method: string, params?: Record<string, string>) {
async get(
method: "download",
params?: Record<string, string>,
): Promise<{ res: Response; xml: undefined }>;
async get(
method: string,
params?: Record<string, string>,
): Promise<{ res: Response; xml: Document }>;
async get(
method: string,
params?: Record<string, string>,
): Promise<{ res: Response; xml: Document | undefined }> {
const url = new URL(`rest/${method}.view`, this.baseUrl);
url.searchParams.set("u", this.username);
@@ -19,6 +33,26 @@ export class SubsonicClient {
);
}
return fetch(url);
const res = await fetch(url);
let xml: Document | undefined;
if (res.headers.get("content-type")?.includes("xml")) {
xml = new JSDOM(await res.text(), {
contentType: "text/xml",
}).window.document;
}
if (!res.ok) {
let message = `HTTP error ${res.status}`;
if (xml) {
const error = xml.querySelector("error");
const errorCode = error?.getAttribute("code");
const errorMessage = error?.getAttribute("message");
message += `\nSubsonic error${errorCode}: ${errorMessage}`;
}
throw new Error(message);
}
return { res, xml };
}
}

View File

@@ -0,0 +1,3 @@
export function sleep(milliseconds: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}