quak backup: print the date and time a run starts and finishes (closes #245)
check / check (push) Waiting to run

Adds a `Started at` line after `Starting backup...` and a `Finished at`
line after `--- Backup complete ---`, in local time with the UTC offset.
No existing line changes. The clock comes in through `CliContext.now`.

The README's crontab now pipes the output to `logger`, which adds its own
time to each line; appending to a file remains as the alternative.

Model: opus-5-5
This commit was merged in pull request #256.
This commit is contained in:
clawbot committed 2026-11-19 08:47:06 +01:00
1 parent 2492946e9d
commit ecdd90b0a8
6 files changed
+124 -19

No files matched your search

+62 -3
View File
@@ -33,6 +33,7 @@ import {
vi,
beforeAll,
beforeEach,
afterAll,
afterEach,
} from "vitest";
@@ -194,6 +195,12 @@ const FETCHED_BOTH_ALBUMS =
"Fetched the files of 1 of 2 collections\n" +
"Fetched the files of 2 of 2 collections\n";
// The time every context's clock reads. `backup` prints it in local time, and
// the tests pin the zone to Europe/Berlin, where it is 03:30.
const NOW = new Date(Date.UTC(2026, 10, 11, 2, 30));
const STARTED = "Started at 2026-11-11 03:30:00 +0100\n";
const FINISHED = "Finished at 2026-11-11 03:30:00 +0100\n";
// Collects everything written to it.
class Output {
text = "";
@@ -223,11 +230,18 @@ const context = (client: Client | null = fakeClient()): CliContext => ({
promptSecret: async () => {
throw new Error("prompt not expected");
},
now: () => NOW,
});
const savedTZ = process.env.TZ;
beforeAll(async () => {
process.env.TZ = "Europe/Berlin";
await init();
});
afterAll(() => {
if (savedTZ === undefined) delete process.env.TZ;
else process.env.TZ = savedTZ;
});
beforeEach(() => {
root = mkdtempSync(join(tmpdir(), "quak-cli-test-"));
@@ -959,6 +973,7 @@ describe("backup", () => {
expect(await backupCommand(context(), dir, {})).toBe(0);
expect(stderr.text).toContain(
"\n--- Backup complete ---\n" +
FINISHED +
" Total files: 3\n" +
" Downloaded: 3\n" +
" Skipped: 0\n" +
@@ -968,6 +983,43 @@ describe("backup", () => {
expect(existsSync(join(dir, "backup.lock"))).toBe(false);
});
// A log of many nights' runs shows when each started and finished, in
// local time with the offset from UTC.
it("prints the local date and time the run started and finished", async () => {
const start = new Date(Date.UTC(2026, 10, 11, 2, 30, 5));
const end = new Date(Date.UTC(2026, 10, 11, 3, 4, 9));
const zones = [
{
zone: "Europe/Berlin",
started: "2026-11-11 03:30:05 +0100",
finished: "2026-11-11 04:04:09 +0100",
},
{
zone: "Asia/Kolkata",
started: "2026-11-11 08:00:05 +0530",
finished: "2026-11-11 08:34:09 +0530",
},
{
zone: "America/New_York",
started: "2026-11-10 21:30:05 -0500",
finished: "2026-11-10 22:04:09 -0500",
},
];
for (const { zone, started, finished } of zones) {
process.env.TZ = zone;
stderr.text = "";
const times = [start, end];
const ctx = { ...context(), now: () => times.shift()! };
expect(await backupCommand(ctx, join(root, zone), {})).toBe(0);
const begin = `Starting backup...\nStarted at ${started}\n`;
expect(stderr.text.slice(0, begin.length)).toBe(begin);
expect(stderr.text).toContain(
`--- Backup complete ---\nFinished at ${finished}\n`,
);
}
process.env.TZ = "Europe/Berlin";
});
// The backup opens its library with the precache off: it fetches the
// originals it needs into the backup, and must not also fetch every
// thumbnail in the account, or keep originals, in the per-user cache.
@@ -1049,6 +1101,7 @@ describe("backup", () => {
it("counts on stderr each album whose files its first refresh fetched, with --json too", async () => {
const start =
"Starting backup...\n" +
STARTED +
FETCHED_BOTH_ALBUMS +
"Refreshing library...\n";
for (const json of [false, true]) {
@@ -1168,7 +1221,9 @@ describe("backup", () => {
const { code, runText } = await backupThroughRun(context(client), dir);
expect(code).toBe(1);
expect(runText).toBe("quak: HTTP 503 from server\n");
expect(stderr.text).toBe("Starting backup...\nRefreshing library...\n");
expect(stderr.text).toBe(
"Starting backup...\n" + STARTED + "Refreshing library...\n",
);
expect(readdirSync(dir)).toEqual([]);
});
@@ -1199,7 +1254,9 @@ describe("backup", () => {
expect(runText).toBe(
`quak: the saved session is no longer valid; run "quak login"\n`,
);
expect(stderr.text).toBe("Starting backup...\nRefreshing library...\n");
expect(stderr.text).toBe(
"Starting backup...\n" + STARTED + "Refreshing library...\n",
);
expect(readdirSync(dir)).toEqual([]);
});
@@ -1227,7 +1284,9 @@ describe("backup", () => {
expect(await backupCommand(context(client), dir, {})).toBe(2);
expect(requests).toBe(0);
expect(stderr.text).toBe(
`Starting backup...\nquak: another backup of ${dir} is running\n`,
"Starting backup...\n" +
STARTED +
`quak: another backup of ${dir} is running\n`,
);
expect(readdirSync(dir)).toEqual(["backup.lock"]);
// Opening the library would have made its cache directory.