Initial implementation
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { createApp } from "../src/app.js";
|
||||
import { baseConfig, listen } from "./helpers.js";
|
||||
|
||||
const feed = {
|
||||
body: "<rss></rss>",
|
||||
etag: '"feed-tag"',
|
||||
lastModified: new Date("2025-01-01T00:00:00Z"),
|
||||
};
|
||||
|
||||
test("serves health and RSS endpoints with conditional GET", async (t) => {
|
||||
const app = createApp(baseConfig, {
|
||||
feedService: { async get() { return feed; } },
|
||||
silent: true,
|
||||
});
|
||||
const server = await listen(app);
|
||||
t.after(server.close);
|
||||
|
||||
const health = await fetch(`${server.url}/healthz`);
|
||||
assert.equal(health.status, 200);
|
||||
assert.deepEqual(await health.json(), { status: "ok" });
|
||||
|
||||
const response = await fetch(`${server.url}/rss.xml`);
|
||||
assert.equal(response.status, 200);
|
||||
assert.match(response.headers.get("content-type"), /application\/rss\+xml/);
|
||||
assert.equal(response.headers.get("etag"), feed.etag);
|
||||
assert.equal(await response.text(), feed.body);
|
||||
|
||||
const fresh = await fetch(`${server.url}/rss.xml`, {
|
||||
headers: { "if-none-match": feed.etag },
|
||||
});
|
||||
assert.equal(fresh.status, 304);
|
||||
});
|
||||
|
||||
test("optionally protects the feed with HTTP Basic auth", async (t) => {
|
||||
const config = structuredClone(baseConfig);
|
||||
config.feed.username = "reader";
|
||||
config.feed.password = "secret:with-colon";
|
||||
const app = createApp(config, {
|
||||
feedService: { async get() { return feed; } },
|
||||
silent: true,
|
||||
});
|
||||
const server = await listen(app);
|
||||
t.after(server.close);
|
||||
|
||||
const denied = await fetch(`${server.url}/rss.xml`);
|
||||
assert.equal(denied.status, 401);
|
||||
assert.match(denied.headers.get("www-authenticate"), /Basic/);
|
||||
|
||||
const accepted = await fetch(`${server.url}/rss.xml`, {
|
||||
headers: {
|
||||
authorization: `Basic ${Buffer.from("reader:secret:with-colon").toString("base64")}`,
|
||||
},
|
||||
});
|
||||
assert.equal(accepted.status, 200);
|
||||
});
|
||||
|
||||
test("returns 502 without leaking upstream error details", async (t) => {
|
||||
const app = createApp(baseConfig, {
|
||||
feedService: { async get() { throw new Error("secret upstream detail"); } },
|
||||
silent: true,
|
||||
});
|
||||
const server = await listen(app);
|
||||
t.after(server.close);
|
||||
|
||||
const response = await fetch(`${server.url}/rss.xml`);
|
||||
assert.equal(response.status, 502);
|
||||
assert.deepEqual(await response.json(), {
|
||||
error: "Could not generate feed from Suwayomi",
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { loadConfig } from "../src/config.js";
|
||||
|
||||
test("loads defaults", () => {
|
||||
const config = loadConfig({});
|
||||
assert.equal(config.port, 3000);
|
||||
assert.equal(config.suwayomi.graphqlUrl, "http://suwayomi:4567/api/graphql");
|
||||
assert.equal(config.feed.itemLimit, 50);
|
||||
});
|
||||
|
||||
test("requires credentials for authenticated modes", () => {
|
||||
assert.throws(
|
||||
() => loadConfig({ SUWAYOMI_AUTH_MODE: "ui_login" }),
|
||||
/USERNAME and SUWAYOMI_PASSWORD/,
|
||||
);
|
||||
assert.throws(
|
||||
() => loadConfig({ RSS_USERNAME: "reader" }),
|
||||
/must be set together/,
|
||||
);
|
||||
});
|
||||
|
||||
test("validates bounded integer settings", () => {
|
||||
assert.throws(() => loadConfig({ FEED_ITEM_LIMIT: "201" }), /between 1 and 200/);
|
||||
assert.throws(() => loadConfig({ PORT: "wat" }), /PORT must be an integer/);
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
export const baseConfig = {
|
||||
suwayomi: {
|
||||
url: "http://suwayomi:4567",
|
||||
graphqlUrl: "http://suwayomi:4567/api/graphql",
|
||||
authMode: "none",
|
||||
timeoutMs: 1000,
|
||||
},
|
||||
feed: {
|
||||
title: "Manga releases",
|
||||
description: "Recently fetched chapters",
|
||||
link: "https://manga.example.test",
|
||||
publicUrl: "https://rss.example.test/rss.xml",
|
||||
itemLimit: 25,
|
||||
cacheSeconds: 300,
|
||||
},
|
||||
};
|
||||
|
||||
export const chapter = {
|
||||
id: 42,
|
||||
name: "Chapter 12 & a half",
|
||||
chapterNumber: 12.5,
|
||||
scanlator: "A <Team>",
|
||||
uploadDate: 1700000000000,
|
||||
fetchedAt: 1700000100000,
|
||||
realUrl: "https://source.example.test/chapter?x=1&y=2",
|
||||
manga: {
|
||||
id: 7,
|
||||
title: "Example & Manga",
|
||||
realUrl: "https://source.example.test/manga",
|
||||
},
|
||||
};
|
||||
|
||||
export async function listen(app) {
|
||||
const server = app.listen(0, "127.0.0.1");
|
||||
await new Promise((resolve) => server.once("listening", resolve));
|
||||
const { port } = server.address();
|
||||
return {
|
||||
url: `http://127.0.0.1:${port}`,
|
||||
close: () => new Promise((resolve) => server.close(resolve)),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { buildRss, FeedService } from "../src/rss.js";
|
||||
import { baseConfig, chapter } from "./helpers.js";
|
||||
|
||||
test("buildRss emits valid escaped RSS fields and a stable GUID", () => {
|
||||
const xml = buildRss([chapter], baseConfig.feed, 1700000200000);
|
||||
|
||||
assert.match(xml, /<rss version="2.0"/);
|
||||
assert.match(xml, /Example & Manga — Chapter 12 & a half/);
|
||||
assert.match(xml, /A <Team>/);
|
||||
assert.match(xml, /suwayomi:chapter:42/);
|
||||
assert.match(xml, /x=1&y=2/);
|
||||
assert.match(xml, /atom:link/);
|
||||
});
|
||||
|
||||
test("FeedService caches responses and coalesces concurrent refreshes", async () => {
|
||||
let calls = 0;
|
||||
let now = 1000;
|
||||
const client = {
|
||||
async recentLibraryChapters(limit) {
|
||||
calls += 1;
|
||||
assert.equal(limit, 25);
|
||||
await Promise.resolve();
|
||||
return [chapter];
|
||||
},
|
||||
};
|
||||
const service = new FeedService(client, baseConfig.feed, () => now);
|
||||
|
||||
const [first, second] = await Promise.all([service.get(), service.get()]);
|
||||
assert.strictEqual(first, second);
|
||||
assert.equal(calls, 1);
|
||||
|
||||
now += 301000;
|
||||
await service.get();
|
||||
assert.equal(calls, 2);
|
||||
});
|
||||
@@ -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