179 lines
5.3 KiB
JavaScript
179 lines
5.3 KiB
JavaScript
"use strict";
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
|
|
function loadPluginFresh() {
|
|
const modulePath = require.resolve("./music_server_lan");
|
|
delete require.cache[modulePath];
|
|
return require("./music_server_lan");
|
|
}
|
|
|
|
function clearRuntimeEnv() {
|
|
delete globalThis.env;
|
|
}
|
|
|
|
test.afterEach(() => {
|
|
clearRuntimeEnv();
|
|
});
|
|
|
|
test("plugin.userVariables exposes public and lan base urls", () => {
|
|
const plugin = loadPluginFresh();
|
|
const keys = plugin.userVariables.map((item) => item.key);
|
|
assert.deepEqual(keys, ["baseUrl", "lanBaseUrl", "accessToken"]);
|
|
});
|
|
|
|
test("recommend requests use lan base url when lan health probe succeeds", async () => {
|
|
const plugin = loadPluginFresh();
|
|
plugin.__clearTestState();
|
|
plugin.__setConfigForTests({
|
|
baseUrl: "http://64.83.43.123:18081",
|
|
lanBaseUrl: "http://192.168.5.43:18081",
|
|
accessToken: "",
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
const fetchCalls = [];
|
|
globalThis.fetch = async (url, options) => {
|
|
fetchCalls.push({ url, options });
|
|
if (url === "http://192.168.5.43:18081/healthz") {
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
text: async () => JSON.stringify({ status: "ok" }),
|
|
};
|
|
}
|
|
if (url === "http://192.168.5.43:18081/mf/v1/recommend/tags") {
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
text: async () =>
|
|
JSON.stringify({
|
|
pinned: [{ id: "all", title: "all" }],
|
|
data: [],
|
|
}),
|
|
};
|
|
}
|
|
throw new Error(`Unexpected request: ${url}`);
|
|
};
|
|
|
|
try {
|
|
const result = await plugin.getRecommendSheetTags();
|
|
assert.equal(result.pinned[0].id, "all");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
|
|
assert.deepEqual(
|
|
fetchCalls.map((entry) => entry.url),
|
|
[
|
|
"http://192.168.5.43:18081/healthz",
|
|
"http://192.168.5.43:18081/mf/v1/recommend/tags",
|
|
],
|
|
);
|
|
});
|
|
|
|
test("recommend requests fall back to public base url when lan health probe fails", async () => {
|
|
const plugin = loadPluginFresh();
|
|
plugin.__clearTestState();
|
|
plugin.__setConfigForTests({
|
|
baseUrl: "http://64.83.43.123:18081",
|
|
lanBaseUrl: "http://192.168.5.43:18081",
|
|
accessToken: "",
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
const fetchCalls = [];
|
|
globalThis.fetch = async (url, options) => {
|
|
fetchCalls.push({ url, options });
|
|
if (url === "http://192.168.5.43:18081/healthz") {
|
|
throw new Error("connect failed");
|
|
}
|
|
if (url === "http://64.83.43.123:18081/mf/v1/recommend/tags") {
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
text: async () =>
|
|
JSON.stringify({
|
|
pinned: [{ id: "all", title: "all" }],
|
|
data: [],
|
|
}),
|
|
};
|
|
}
|
|
throw new Error(`Unexpected request: ${url}`);
|
|
};
|
|
|
|
try {
|
|
const result = await plugin.getRecommendSheetTags();
|
|
assert.equal(result.pinned[0].id, "all");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
|
|
assert.deepEqual(
|
|
fetchCalls.map((entry) => entry.url),
|
|
[
|
|
"http://192.168.5.43:18081/healthz",
|
|
"http://64.83.43.123:18081/mf/v1/recommend/tags",
|
|
],
|
|
);
|
|
});
|
|
|
|
test("getMediaSource joins relative stream url against lan base url after successful probe", async () => {
|
|
const plugin = loadPluginFresh();
|
|
plugin.__clearTestState();
|
|
plugin.__setConfigForTests({
|
|
baseUrl: "http://64.83.43.123:18081",
|
|
lanBaseUrl: "http://192.168.5.43:18081",
|
|
accessToken: "",
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
const fetchCalls = [];
|
|
globalThis.fetch = async (url, options) => {
|
|
fetchCalls.push({ url, options });
|
|
if (url === "http://192.168.5.43:18081/healthz") {
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
text: async () => JSON.stringify({ status: "ok" }),
|
|
};
|
|
}
|
|
if (url === "http://192.168.5.43:18081/mf/v1/media/resolve") {
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
text: async () =>
|
|
JSON.stringify({
|
|
stream: {
|
|
url: "/mf/v1/media/stream/token-1",
|
|
},
|
|
selected_source: {
|
|
quality: "super",
|
|
},
|
|
}),
|
|
};
|
|
}
|
|
throw new Error(`Unexpected request: ${url}`);
|
|
};
|
|
|
|
try {
|
|
const result = await plugin.getMediaSource({ id: "song-1" }, "high");
|
|
assert.deepEqual(result, {
|
|
url: "http://192.168.5.43:18081/mf/v1/media/stream/token-1",
|
|
headers: {},
|
|
quality: "super",
|
|
});
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
|
|
assert.deepEqual(
|
|
fetchCalls.map((entry) => entry.url),
|
|
[
|
|
"http://192.168.5.43:18081/healthz",
|
|
"http://192.168.5.43:18081/mf/v1/media/resolve",
|
|
],
|
|
);
|
|
});
|