Read Duplicati's form-encoded reports

Duplicati posts its message as a form body by default (message=<URL-encoded
text>). Unwrap it (any field name, other fields beside it), take the job and
operation from the 'Duplicati Backup report for <job>' line, read JSON under
that line, and read the Warnings and Errors lists of the text form.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-09 23:44:16 +01:00
1 parent e517de35af
commit 4d653f5a80
3 files changed
+134 -16

No files matched your search

+75
View File
@@ -198,6 +198,73 @@ describe("where a backup stands, and what is an alert", () => {
});
});
describe("the ways Duplicati wraps its message", () => {
const TEXT = ["DeletedFiles: 0", "ModifiedFiles: 3", "ExaminedFiles: 1,520", "SizeOfExaminedFiles: 3221225472 (3.00 GB)", "AddedFiles: 2", "MainOperation: Backup", "ParsedResult: Success", "Duration: 00:05:12.7", "WarningsActualLength: 0", "ErrorsActualLength: 0"].join("\n");
const message = (job = "Documents", result = TEXT, operation = "Backup") => `Duplicati ${operation} report for ${job}\n\n${result}`;
/** As Duplicati posts it by default: one form field, the text URL-encoded. */
const form = (text: string, field = "message") => `${field}=${encodeURIComponent(text)}`;
test("the default: a form body whose message is Duplicati's text, with the job named on the first line", () => {
const r = d.parseReport(form(message()))!;
assert.deepEqual([r.operation, r.result, r.backupName, r.durationSeconds, r.examinedFiles, r.sizeBytes, r.addedFiles, r.modifiedFiles, r.warnings], ["Backup", "Success", "Documents", 312, 1520, 3221225472, 2, 3, 0]);
});
test("a form body as a browser writes it, with + for a space, and with other fields beside the message", () => {
const plus = "message=Duplicati+Backup+report+for+My+Photos%0A%0AMainOperation%3A+Backup%0AParsedResult%3A+Warning%0AWarningsActualLength%3A+2&source=laptop&level=all";
const r = d.parseReport(plus)!;
assert.deepEqual([r.operation, r.result, r.backupName, r.warnings], ["Backup", "Warning", "My Photos", 2]);
});
test("a message parameter with another name is found too: the longest value is the message", () => {
const r = d.parseReport(`${form(message("Docs"), "msg")}&x=1`)!;
assert.deepEqual([r.result, r.backupName], ["Success", "Docs"]);
});
test("the result in JSON, under the header line, inside a form body or posted as it is", () => {
const json = report({}, "", {});
const wrapped = d.parseReport(form(message("Photos", JSON.stringify(JSON.parse(json)))))!;
assert.deepEqual([wrapped.result, wrapped.backupName, wrapped.sizeBytes], ["Success", "Photos", 3221225472]);
const direct = d.parseReport(message("Photos", json))!;
assert.deepEqual([direct.result, direct.backupName], ["Success", "Photos"]);
assert.equal(d.parseReport(form(json))?.backupName, "Backup DB-1", "JSON alone in a form: the job is known by its id");
});
test("the operation can come from the header line when the result does not say it", () => {
const r = d.parseReport(form(message("Docs", "ParsedResult: Success\nDuration: 00:00:05", "Compact")))!;
assert.deepEqual([r.operation, r.backupName], ["Compact", "Docs"]);
});
test("a name in the result itself wins over the header's", () => {
const r = d.parseReport(form(message("From header", `${TEXT}\nbackup-name: From result`)))!;
assert.equal(r.backupName, "From result");
});
test("the warnings and errors of the text form are read as lines", () => {
const text = [TEXT.replace("WarningsActualLength: 0", "WarningsActualLength: 2").replace("ParsedResult: Success", "ParsedResult: Warning"), "Warnings: [", " Failed to process path: /a/b.lock,", " Could not read /c/d", "]", "Errors: []"].join("\n");
const r = d.parseReport(form(message("Docs", text)))!;
assert.deepEqual([r.result, r.warnings, r.warningMessages, r.message], ["Warning", 2, ["Failed to process path: /a/b.lock", "Could not read /c/d"], "Failed to process path: /a/b.lock"]);
const failed = d.parseReport(form(message("Docs", ["ParsedResult: Error", "ErrorsActualLength: 1", "Errors: [", " Unable to connect to the backend", "]"].join("\n"))))!;
assert.deepEqual([failed.errors, failed.message], [1, "Unable to connect to the backend"]);
});
test("what is a form but has no report in it is not read, and says what is missing", () => {
for (const body of ["message=hello", "a=1&b=2", form("Duplicati Backup report for Docs\n\nnothing useful")]) assert.equal(d.parseReport(body), null, body);
assert.match(d.explainUnreadable(form("Duplicati Backup report for Docs\n\nnothing useful")).reason, /no "ParsedResult:" line/);
assert.equal(d.explainUnreadable("message=hello").sample, "message=hello", "the sample is what was sent, not what was made of it");
});
test("a text result or JSON that is not a form is never taken for one", () => {
assert.equal(d.parseReport(TEXT)?.result, "Success");
assert.equal(d.parseReport(report())?.result, "Success");
assert.deepEqual(d.unwrapMessage("DeletedFiles: 0\nParsedResult: Success"), { text: "DeletedFiles: 0\nParsedResult: Success", header: null });
});
test("the header line is read for the operation and the job, however it is wrapped", () => {
assert.deepEqual(d.unwrapMessage(form(message("Jackie's Docs", "x", "Repair"))).header, { operation: "Repair", name: "Jackie's Docs" });
assert.equal(d.unwrapMessage("no header here").header, null);
});
});
describe("what the last report was", () => {
test("why a body is not a report, in a sentence, with the start of it", () => {
assert.deepEqual(d.explainUnreadable(""), { reason: "the body was empty (or not text) — Duplicati should post its result as text or JSON", sample: null });
@@ -473,6 +540,14 @@ describe("the address Duplicati posts to, the pages, the alerts and /metrics", (
who = "admin";
});
test("Duplicati's own way of posting — a form body — is read, and makes the job", async () => {
const text = `Duplicati Backup report for Documents\n\nMainOperation: Backup\nParsedResult: Success\nExaminedFiles: 1520\nSizeOfExaminedFiles: 3221225472\nDuration: 00:05:12.7`;
const res = await call("POST", `/duplicati-report/${token}`, `message=${encodeURIComponent(text)}`, { "content-type": "application/x-www-form-urlencoded" });
assert.deepEqual([res.status, await res.text()], [200, "OK"]);
const documents = (await db.select().from(duplicatiBackups).where(eq(duplicatiBackups.name, "Documents")))[0];
assert.deepEqual([documents.lastResult, documents.sizeBytes], ["Success", 3221225472]);
});
test("warnings: the page can read what they said, per job and per run; an operator can switch the alert on, and a viewer cannot", async () => {
const docs = (await db.select().from(duplicatiBackups).where(eq(duplicatiBackups.name, "Documents")))[0];
await d.recordReport(token, report({ ParsedResult: "Warning", WarningsActualLength: 2, Warnings: ["file one locked", "file two locked"], EndTime: new Date().toISOString() }, "Documents"));