Initial implementation
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { SuwayomiClient } from "../src/suwayomi.js";
|
||||
|
||||
const config = {
|
||||
graphqlUrl: "https://suwayomi.example.test/api/graphql",
|
||||
authMode: "none",
|
||||
timeoutMs: 1000,
|
||||
};
|
||||
|
||||
function response(data, init = {}) {
|
||||
return new Response(JSON.stringify(data), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
...init,
|
||||
});
|
||||
}
|
||||
|
||||
test("queries recent chapters from library in fetched order", async () => {
|
||||
const calls = [];
|
||||
const client = new SuwayomiClient(config, async (url, init) => {
|
||||
calls.push({ url, init });
|
||||
return response({ data: { chapters: { nodes: [{ id: 1 }] } } });
|
||||
});
|
||||
|
||||
assert.deepEqual(await client.recentLibraryChapters(12), [{ id: 1 }]);
|
||||
const body = JSON.parse(calls[0].init.body);
|
||||
assert.equal(calls[0].url, config.graphqlUrl);
|
||||
assert.equal(body.variables.first, 12);
|
||||
assert.match(body.query, /inLibrary/);
|
||||
assert.match(body.query, /FETCHED_AT/);
|
||||
});
|
||||
|
||||
test("sends Basic credentials", async () => {
|
||||
const headers = [];
|
||||
const client = new SuwayomiClient(
|
||||
{ ...config, authMode: "basic", username: "me", password: "pass" },
|
||||
async (_url, init) => {
|
||||
headers.push(init.headers.authorization);
|
||||
return response({ data: { chapters: { nodes: [] } } });
|
||||
},
|
||||
);
|
||||
|
||||
await client.recentLibraryChapters(5);
|
||||
assert.equal(headers[0], `Basic ${Buffer.from("me:pass").toString("base64")}`);
|
||||
});
|
||||
|
||||
test("logs in once for UI login and sends the JWT", async () => {
|
||||
const token = [
|
||||
"header",
|
||||
Buffer.from(JSON.stringify({ exp: Math.floor(Date.now() / 1000) + 300 })).toString("base64url"),
|
||||
"signature",
|
||||
].join(".");
|
||||
const operations = [];
|
||||
const client = new SuwayomiClient(
|
||||
{
|
||||
...config,
|
||||
authMode: "ui_login",
|
||||
username: "reader",
|
||||
password: "secret",
|
||||
},
|
||||
async (_url, init) => {
|
||||
const request = JSON.parse(init.body);
|
||||
operations.push({ request, authorization: init.headers.authorization });
|
||||
if (request.query.includes("mutation Login")) {
|
||||
return response({
|
||||
data: { login: { accessToken: token, refreshToken: "refresh" } },
|
||||
});
|
||||
}
|
||||
return response({ data: { chapters: { nodes: [] } } });
|
||||
},
|
||||
);
|
||||
|
||||
await client.recentLibraryChapters(5);
|
||||
await client.recentLibraryChapters(5);
|
||||
assert.equal(operations.filter(({ request }) => request.query.includes("mutation Login")).length, 1);
|
||||
assert.equal(operations[1].authorization, `Bearer ${token}`);
|
||||
assert.equal(operations[2].authorization, `Bearer ${token}`);
|
||||
});
|
||||
|
||||
test("turns GraphQL errors into useful upstream errors", async () => {
|
||||
const client = new SuwayomiClient(config, async () =>
|
||||
response({ errors: [{ message: "Unauthorized" }] }),
|
||||
);
|
||||
await assert.rejects(
|
||||
client.recentLibraryChapters(5),
|
||||
/Suwayomi GraphQL error: Unauthorized/,
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user